From 8755723e7acecf0ff243477ccb251e879f0fd19b Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 18 Jun 2015 05:57:11 -0400 Subject: [PATCH] kill some dead code, write a lower test (that fails) --- src/grammar/parse_tree.rs | 26 -------------------------- src/grammar/repr.rs | 2 +- src/normalize/lower/mod.rs | 6 ++++-- src/normalize/lower/test.rs | 20 ++++++++++++++++++++ src/normalize/mod.rs | 7 +++++++ src/normalize/test_util.rs | 17 +++++++++++++++-- src/normalize/tyinfer/test.rs | 2 +- 7 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 src/normalize/lower/test.rs diff --git a/src/grammar/parse_tree.rs b/src/grammar/parse_tree.rs index ff047c3..ccfdce1 100644 --- a/src/grammar/parse_tree.rs +++ b/src/grammar/parse_tree.rs @@ -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 diff --git a/src/grammar/repr.rs b/src/grammar/repr.rs index 536494e..a380c35 100644 --- a/src/grammar/repr.rs +++ b/src/grammar/repr.rs @@ -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), } } diff --git a/src/normalize/lower/mod.rs b/src/normalize/lower/mod.rs index 013d6ea..59e85b4 100644 --- a/src/normalize/lower/mod.rs +++ b/src/normalize/lower/mod.rs @@ -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 { - let mut state = LowerState::new(types); + let state = LowerState::new(types); state.lower(grammar) } diff --git a/src/normalize/lower/test.rs b/src/normalize/lower/test.rs new file mode 100644 index 0000000..de17f60 --- /dev/null +++ b/src/normalize/lower/test.rs @@ -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: Vec = + ~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"); +} diff --git a/src/normalize/mod.rs b/src/normalize/mod.rs index 727d922..5bde4c7 100644 --- a/src/normalize/mod.rs +++ b/src/normalize/mod.rs @@ -5,6 +5,7 @@ */ use grammar::parse_tree as pt; +use grammar::repr as r; pub type NormResult = Result; @@ -23,6 +24,12 @@ macro_rules! return_err { } } +pub fn normalize(grammar: pt::Grammar) -> NormResult { + 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 diff --git a/src/normalize/test_util.rs b/src/normalize/test_util.rs index 0b1de1a..e54ad78 100644 --- a/src/normalize/test_util.rs +++ b/src/normalize/test_util.rs @@ -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(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(actual: D, expected: &str) { + compare(ExpectedDebug(&format!("{:#?}", actual)), + ExpectedDebug(expected)) +} + +pub fn compare(actual: D, expected: E) { let actual_s = format!("{:?}", actual); let expected_s = format!("{:?}", expected); diff --git a/src/normalize/tyinfer/test.rs b/src/normalize/tyinfer/test.rs index 15dc5b3..8f84613 100644 --- a/src/normalize/tyinfer/test.rs +++ b/src/normalize/tyinfer/test.rs @@ -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\";