From 0e19272c01aeb1087e7e10176483d6fe250e5518 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 24 Jul 2015 22:48:41 -0400 Subject: [PATCH] get unit tests to pass --- lalrpop/src/normalize/macro_expand/test.rs | 15 +++++++-------- lalrpop/src/normalize/prevalidate/test.rs | 8 ++++---- lalrpop/src/parser/lrgrammar.lalrpop | 9 +++++---- lalrpop/src/parser/mod.rs | 6 +++--- lalrpop/src/tok/mod.rs | 5 +++++ lalrpop/src/tok/test.rs | 4 ++-- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lalrpop/src/normalize/macro_expand/test.rs b/lalrpop/src/normalize/macro_expand/test.rs index 6750bb0..0a43625 100644 --- a/lalrpop/src/normalize/macro_expand/test.rs +++ b/lalrpop/src/normalize/macro_expand/test.rs @@ -16,26 +16,25 @@ grammar; let actual = expand_macros(grammar).unwrap(); - let expected = parser::parse_grammar(r#" + let expected = parser::parse_grammar(r##" grammar; Ids = `Comma<"Id">`; - `Comma<"Id">`: Vec<`"Id"`> = - ",")*`> => - v.into_iter().chain(e.into_iter()).collect(); + `Comma<"Id">`: Vec<#"Id"#> = + ",")*`> => v.into_iter().chain(e.into_iter()).collect(); - `"Id"?`: ::std::option::Option<`"Id"`> = { + `"Id"?`: ::std::option::Option<#"Id"#> = { "Id" => Some(<>); => None; }; - `(<"Id"> ",")*`: ::std::vec::Vec<``(<"Id"> ",")``> = { + `(<"Id"> ",")*`: ::std::vec::Vec<#`(<"Id"> ",")`#> = { => vec![]; ",")*`> ",")`> => { let mut v = v; v.push(e); v }; }; - `(<"Id"> ",")`: `"Id"` = <"Id"> "," => (<>); -"#).unwrap(); + `(<"Id"> ",")`: #"Id"# = <"Id"> "," => (<>); +"##).unwrap(); compare(actual, expected); } diff --git a/lalrpop/src/normalize/prevalidate/test.rs b/lalrpop/src/normalize/prevalidate/test.rs index eb38333..09ffbc2 100644 --- a/lalrpop/src/normalize/prevalidate/test.rs +++ b/lalrpop/src/normalize/prevalidate/test.rs @@ -53,7 +53,7 @@ fn unknown_nonterminal_in_repeat_question() { fn repeated_macro_arg() { check_err( "multiple macro arguments declared with the name `Y`", - r#"grammar; >>>X <<<= "foo";"#); + r#"grammar; >>>X<<< = "foo";"#); } #[test] @@ -74,7 +74,7 @@ fn named_symbols() { fn bad_assoc_type() { check_err( r#"associated type `Foo` not recognized"#, - r#"grammar; extern token { type >>>Foo <<<= i32; enum Tok { } }"#); + r#"grammar; extern token { type >>>Foo<<< = i32; enum Tok { } }"#); } #[test] @@ -82,7 +82,7 @@ fn dup_assoc_type() { check_err( r#"associated type `Location` already specified"#, r#"grammar; extern token { type Location = i32; - type >>>Location <<<= u32; + type >>>Location<<< = u32; enum Tok { } }"#); } @@ -97,5 +97,5 @@ fn lookahead_without_loc_type() { fn multiple_extern_token() { check_err( r#"multiple extern token definitions are not permitted"#, - r#"grammar; extern token { enum Tok { } } >>>extern token <<<{ enum Tok { } }"#); + r#"grammar; extern token { enum Tok { } } >>>extern token<<< { enum Tok { } }"#); } diff --git a/lalrpop/src/parser/lrgrammar.lalrpop b/lalrpop/src/parser/lrgrammar.lalrpop index c5e8812..ad2b733 100644 --- a/lalrpop/src/parser/lrgrammar.lalrpop +++ b/lalrpop/src/parser/lrgrammar.lalrpop @@ -86,8 +86,8 @@ Alternative: Alternative = Action: ActionKind = { "=>@L" => ActionKind::Lookahead; "=>@R" => ActionKind::Lookbehind; - "> => ActionKind::User(c.to_string()); - ?"> => ActionKind::Fallible(c.to_string()); + "> => ActionKind::User(strip(c).to_string()); + ?"> => ActionKind::Fallible(strip(c).to_string()); }; Cond: Condition = @@ -159,8 +159,8 @@ pub TypeRef: TypeRef = { "(" > ")" => TypeRef::Tuple(<>); - =>? { - panic!("parse escape symbol") + "#" "#" => { + TypeRef::OfSymbol(<>.kind) }; "&" => @@ -321,6 +321,7 @@ extern token { "=>@L" => Tok::EqualsGreaterThanLookahead(..), "=>@R" => Tok::EqualsGreaterThanLookbehind(..), ">" => Tok::GreaterThan(..), + "#" => Tok::Hash(..), "{" => Tok::LeftBrace(..), "[" => Tok::LeftBracket(..), "(" => Tok::LeftParen(..), diff --git a/lalrpop/src/parser/mod.rs b/lalrpop/src/parser/mod.rs index ee9564a..4b743f9 100644 --- a/lalrpop/src/parser/mod.rs +++ b/lalrpop/src/parser/mod.rs @@ -15,8 +15,8 @@ pub fn parse_grammar<'input>(input: &'input str) lrgrammar::parse_Grammar(input, tokenizer) } -pub fn parse_pattern<'input>(input: &'input str, offset: usize) - -> Result, ParseError<'input>> +fn parse_pattern<'input>(input: &'input str, offset: usize) + -> Result, ParseError<'input>> { let tokenizer = tok::Tokenizer::new(input, offset); lrgrammar::parse_Pattern(input, tokenizer) @@ -26,6 +26,6 @@ pub fn parse_pattern<'input>(input: &'input str, offset: usize) pub fn parse_type_ref<'input>(input: &'input str) -> Result> { - let tokenizer = tok::Tokenizer::new(input); + let tokenizer = tok::Tokenizer::new(input, 0); lrgrammar::parse_TypeRef(input, tokenizer) } diff --git a/lalrpop/src/tok/mod.rs b/lalrpop/src/tok/mod.rs index 96f8eff..9c4e39e 100644 --- a/lalrpop/src/tok/mod.rs +++ b/lalrpop/src/tok/mod.rs @@ -65,6 +65,7 @@ pub enum Tok<'input> { EqualsGreaterThanQuestionCode(&'input str), EqualsGreaterThanLookahead, EqualsGreaterThanLookbehind, + Hash, GreaterThan, LeftBrace, LeftBracket, @@ -184,6 +185,10 @@ impl<'input> Tokenizer<'input> { } } } + Some((idx0, '#')) => { + self.bump(); + Some(Ok((idx0, Hash, idx0+1))) + } Some((idx0, '>')) => { self.bump(); Some(Ok((idx0, GreaterThan, idx0+1))) diff --git a/lalrpop/src/tok/test.rs b/lalrpop/src/tok/test.rs index 4b4cb8d..414b118 100644 --- a/lalrpop/src/tok/test.rs +++ b/lalrpop/src/tok/test.rs @@ -8,7 +8,7 @@ fn test(input: &str, // for spans, and because it applies also to r#XXX# style strings: let input = input.replace("$", "\n"); - let tokenizer = Tokenizer::new(&input); + let tokenizer = Tokenizer::new(&input, 0); let len = expected.len(); for (token, (expected_span, expected_tok)) in tokenizer.zip(expected.into_iter()) { println!("token: {:?}", token); @@ -17,7 +17,7 @@ fn test(input: &str, assert_eq!(Ok((expected_start, expected_tok, expected_end)), token); } - let tokenizer = Tokenizer::new(&input); + let tokenizer = Tokenizer::new(&input, 0); assert_eq!(None, tokenizer.skip(len).next()); }