kill some dead code, write a lower test (that fails)

This commit is contained in:
Niko Matsakis 2015-06-18 05:57:11 -04:00
parent 9d3f7db25a
commit 8755723e7a
7 changed files with 48 additions and 32 deletions

View File

@ -220,19 +220,6 @@ impl Symbol {
pub fn canonical_form(&self) -> String {
format!("{}", self)
}
pub fn type_repr(&self, types: &Types) -> TypeRepr {
match *self {
Symbol::Terminal(_) => types.terminal_type().clone(),
Symbol::Nonterminal(id) => types.nonterminal_type(id).clone(),
Symbol::Choose(ref s) => s.type_repr(types),
Symbol::Name(_, ref s) => s.type_repr(types),
Symbol::Repeat(..) | Symbol::Expr(..) | Symbol::Macro(..) => {
unreachable!("symbol {} should have been expanded away", self)
}
}
}
}
impl Display for Symbol {
@ -321,19 +308,6 @@ impl Display for TypeRef {
}
}
impl RepeatOp {
pub fn type_repr(&self, symbol_type: TypeRepr) -> TypeRepr {
let path = match *self {
RepeatOp::Plus |
RepeatOp::Star =>
vec![intern("std"), intern("vec"), intern("Vec")],
RepeatOp::Question =>
vec![intern("std"), intern("option"), intern("Option")],
};
TypeRepr::Nominal { path: path, types: vec![symbol_type] }
}
}
impl TypeRef {
// Converts a TypeRef to a TypeRepr, assuming no inference is
// required etc. This is safe for all types a user can directly

View File

@ -113,7 +113,7 @@ impl ActionFn {
impl Symbol {
pub fn ty<'ty>(&self, t: &'ty Types) -> &'ty TypeRepr {
match *self {
Symbol::Nonterminal(id) => t.terminal_type(),
Symbol::Nonterminal(_) => t.terminal_type(),
Symbol::Terminal(id) => t.nonterminal_type(id),
}
}

View File

@ -8,10 +8,12 @@ use normalize::norm_util::{self, Symbols};
use grammar::parse_tree as pt;
use grammar::repr as r;
use std::collections::HashMap;
use util::Sep;
#[cfg(test)]
mod test;
pub fn lower(grammar: pt::Grammar, types: r::Types) -> NormResult<r::Grammar> {
let mut state = LowerState::new(types);
let state = LowerState::new(types);
state.lower(grammar)
}

View File

@ -0,0 +1,20 @@
use parser;
use normalize::normalize;
use normalize::test_util::expect_debug;
#[test]
fn test_comma() {
let grammar = parser::parse_grammar("
grammar Foo {
token Tok where { };
Comma<E>: Vec<E> =
~v:(~E \",\")* ~e:E? =>
v.into_iter().chain(e.into_iter()).collect();
Ids = Comma<\"Id\">;
}
").unwrap();
let actual = normalize(grammar).unwrap();
expect_debug(actual, "foo");
}

View File

@ -5,6 +5,7 @@
*/
use grammar::parse_tree as pt;
use grammar::repr as r;
pub type NormResult<T> = Result<T, NormError>;
@ -23,6 +24,12 @@ macro_rules! return_err {
}
}
pub fn normalize(grammar: pt::Grammar) -> NormResult<r::Grammar> {
let grammar = try!(macro_expand::expand_macros(grammar));
let types = try!(tyinfer::infer_types(&grammar));
lower::lower(grammar, types)
}
// These are executed *IN ORDER*:
// Expands macros and expressions

View File

@ -1,13 +1,26 @@
use diff;
use regex::Regex;
use std::fmt::Debug;
use std::fmt::{Debug, Formatter, Error};
thread_local! {
static SPAN: Regex =
Regex::new(r"Span\([0-9 ,]*\)").unwrap()
}
pub fn compare<D:Debug>(actual: D, expected: D) {
struct ExpectedDebug<'a>(&'a str);
impl<'a> Debug for ExpectedDebug<'a> {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
write!(fmt, "{}", self.0)
}
}
pub fn expect_debug<D:Debug>(actual: D, expected: &str) {
compare(ExpectedDebug(&format!("{:#?}", actual)),
ExpectedDebug(expected))
}
pub fn compare<D:Debug,E:Debug>(actual: D, expected: E) {
let actual_s = format!("{:?}", actual);
let expected_s = format!("{:?}", expected);

View File

@ -28,7 +28,7 @@ fn compare(g1: &str, expected: Vec<(&'static str, &'static str)>) {
fn test_pairs_and_tokens() {
compare("
grammar Foo {
token Tok where { };
token Tok where { };
X = Y Z;
Y: Foo = \"Hi\";
Z = \"Ho\";