diff --git a/lalrpop-test/src/expr.rs b/lalrpop-test/src/expr.rs index 9d3c3fa..1174d4b 100644 --- a/lalrpop-test/src/expr.rs +++ b/lalrpop-test/src/expr.rs @@ -19,10 +19,10 @@ mod __parse__Expr { use util::tok::Tok; pub enum __Nonterminal { - Expr(i32), - Term(i32), - Factor(i32), __Expr(i32), + Factor(i32), + Term(i32), + Expr(i32), } // State 0 @@ -62,12 +62,12 @@ mod __parse__Expr { // Term = (*) "Num" ["/"] // __Expr = (*) Expr [EOF] // - // "(" -> Shift(S2) + // "(" -> Shift(S3) // "Num" -> Shift(S4) // - // Factor -> S3 + // Factor -> S5 // Term -> S1 - // Expr -> S5 + // Expr -> S2 pub fn __state0>( mut lookahead: Option, tokens: &mut TOKENS, @@ -77,7 +77,7 @@ mod __parse__Expr { Some(tok @ Tok::LParen(..)) => { let sym0 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state2(lookahead, tokens, sym0)); + result = try!(__state3(lookahead, tokens, sym0)); } Some(tok @ Tok::Num(..)) => { let sym0 = &mut Some(tok); @@ -93,7 +93,7 @@ mod __parse__Expr { match nt { __Nonterminal::Factor(nt) => { let sym0 = &mut Some(nt); - result = try!(__state3(lookahead, tokens, sym0)); + result = try!(__state5(lookahead, tokens, sym0)); } __Nonterminal::Term(nt) => { let sym0 = &mut Some(nt); @@ -101,7 +101,7 @@ mod __parse__Expr { } __Nonterminal::Expr(nt) => { let sym0 = &mut Some(nt); - result = try!(__state5(lookahead, tokens, sym0)); + result = try!(__state2(lookahead, tokens, sym0)); } _ => { return Ok((lookahead, nt)); @@ -117,11 +117,11 @@ mod __parse__Expr { // Factor = Term (*) ["-"] // Factor = Term (*) ["/"] // - // EOF -> Reduce(Factor = Term => ActionFn(6);) // "/" -> Reduce(Factor = Term => ActionFn(6);) - // "-" -> Reduce(Factor = Term => ActionFn(6);) // "*" -> Reduce(Factor = Term => ActionFn(6);) + // "-" -> Reduce(Factor = Term => ActionFn(6);) // "+" -> Reduce(Factor = Term => ActionFn(6);) + // EOF -> Reduce(Factor = Term => ActionFn(6);) // pub fn __state1>( mut lookahead: Option, @@ -130,31 +130,31 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - None => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action6(sym0); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } Some(Tok::Div(..)) => { let sym0 = sym0.take().unwrap(); let nt = super::__action6(sym0); return Ok((lookahead, __Nonterminal::Factor(nt))); } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action6(sym0); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } Some(Tok::Times(..)) => { let sym0 = sym0.take().unwrap(); let nt = super::__action6(sym0); return Ok((lookahead, __Nonterminal::Factor(nt))); } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action6(sym0); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } Some(Tok::Plus(..)) => { let sym0 = sym0.take().unwrap(); let nt = super::__action6(sym0); return Ok((lookahead, __Nonterminal::Factor(nt))); } + None => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action6(sym0); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } _ => { return Err(lookahead); } @@ -162,6 +162,48 @@ mod __parse__Expr { } // State 2 + // Expr = Expr (*) "+" Factor [EOF] + // Expr = Expr (*) "+" Factor ["+"] + // Expr = Expr (*) "+" Factor ["-"] + // Expr = Expr (*) "-" Factor [EOF] + // Expr = Expr (*) "-" Factor ["+"] + // Expr = Expr (*) "-" Factor ["-"] + // __Expr = Expr (*) [EOF] + // + // "-" -> Shift(S6) + // "+" -> Shift(S7) + // EOF -> Reduce(__Expr = Expr => ActionFn(0);) + // + pub fn __state2>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Minus(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state6(lookahead, tokens, sym0, sym1)); + } + Some(tok @ Tok::Plus(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state7(lookahead, tokens, sym0, sym1)); + } + None => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action0(sym0); + return Ok((lookahead, __Nonterminal::__Expr(nt))); + } + _ => { + return Err(lookahead); + } + } + return Ok(result); + } + + // State 3 // Expr = (*) Expr "+" Factor [")"] // Expr = (*) Expr "+" Factor ["+"] // Expr = (*) Expr "+" Factor ["-"] @@ -202,29 +244,29 @@ mod __parse__Expr { // Term = (*) "Num" ["-"] // Term = (*) "Num" ["/"] // + // "Num" -> Shift(S10) // "(" -> Shift(S9) - // "Num" -> Shift(S7) // - // Factor -> S8 - // Term -> S10 - // Expr -> S6 - pub fn __state2>( + // Term -> S12 + // Expr -> S8 + // Factor -> S11 + pub fn __state3>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { + Some(tok @ Tok::Num(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state10(lookahead, tokens, sym1)); + } Some(tok @ Tok::LParen(..)) => { let sym1 = &mut Some(tok); let lookahead = tokens.next(); result = try!(__state9(lookahead, tokens, sym1)); } - Some(tok @ Tok::Num(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state7(lookahead, tokens, sym1)); - } _ => { return Err(lookahead); } @@ -232,17 +274,17 @@ mod __parse__Expr { while sym0.is_some() { let (lookahead, nt) = result; match nt { - __Nonterminal::Factor(nt) => { - let sym1 = &mut Some(nt); - result = try!(__state8(lookahead, tokens, sym1)); - } __Nonterminal::Term(nt) => { let sym1 = &mut Some(nt); - result = try!(__state10(lookahead, tokens, sym1)); + result = try!(__state12(lookahead, tokens, sym1)); } __Nonterminal::Expr(nt) => { let sym1 = &mut Some(nt); - result = try!(__state6(lookahead, tokens, sym0, sym1)); + result = try!(__state8(lookahead, tokens, sym0, sym1)); + } + __Nonterminal::Factor(nt) => { + let sym1 = &mut Some(nt); + result = try!(__state11(lookahead, tokens, sym1)); } _ => { return Ok((lookahead, nt)); @@ -252,7 +294,58 @@ mod __parse__Expr { return Ok(result); } - // State 3 + // State 4 + // Term = "Num" (*) [EOF] + // Term = "Num" (*) ["*"] + // Term = "Num" (*) ["+"] + // Term = "Num" (*) ["-"] + // Term = "Num" (*) ["/"] + // + // "+" -> Reduce(Term = "Num" => ActionFn(7);) + // "/" -> Reduce(Term = "Num" => ActionFn(7);) + // EOF -> Reduce(Term = "Num" => ActionFn(7);) + // "-" -> Reduce(Term = "Num" => ActionFn(7);) + // "*" -> Reduce(Term = "Num" => ActionFn(7);) + // + pub fn __state4>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Div(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + None => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Times(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + _ => { + return Err(lookahead); + } + } + } + + // State 5 // Expr = Factor (*) [EOF] // Expr = Factor (*) ["+"] // Expr = Factor (*) ["-"] @@ -267,114 +360,11 @@ mod __parse__Expr { // Factor = Factor (*) "/" Term ["-"] // Factor = Factor (*) "/" Term ["/"] // - // "/" -> Shift(S12) // "+" -> Reduce(Expr = Factor => ActionFn(3);) - // "*" -> Shift(S11) - // "-" -> Reduce(Expr = Factor => ActionFn(3);) // EOF -> Reduce(Expr = Factor => ActionFn(3);) - // - pub fn __state3>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::Div(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state12(lookahead, tokens, sym0, sym1)); - } - Some(tok @ Tok::Times(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state11(lookahead, tokens, sym0, sym1)); - } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::Expr(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::Expr(nt))); - } - None => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::Expr(nt))); - } - _ => { - return Err(lookahead); - } - } - return Ok(result); - } - - // State 4 - // Term = "Num" (*) [EOF] - // Term = "Num" (*) ["*"] - // Term = "Num" (*) ["+"] - // Term = "Num" (*) ["-"] - // Term = "Num" (*) ["/"] - // - // "/" -> Reduce(Term = "Num" => ActionFn(7);) - // "*" -> Reduce(Term = "Num" => ActionFn(7);) - // "+" -> Reduce(Term = "Num" => ActionFn(7);) - // "-" -> Reduce(Term = "Num" => ActionFn(7);) - // EOF -> Reduce(Term = "Num" => ActionFn(7);) - // - pub fn __state4>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(Tok::Div(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::Times(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - None => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 5 - // Expr = Expr (*) "+" Factor [EOF] - // Expr = Expr (*) "+" Factor ["+"] - // Expr = Expr (*) "+" Factor ["-"] - // Expr = Expr (*) "-" Factor [EOF] - // Expr = Expr (*) "-" Factor ["+"] - // Expr = Expr (*) "-" Factor ["-"] - // __Expr = Expr (*) [EOF] - // - // "+" -> Shift(S13) - // "-" -> Shift(S14) - // EOF -> Reduce(__Expr = Expr => ActionFn(0);) + // "-" -> Reduce(Expr = Factor => ActionFn(3);) + // "*" -> Shift(S13) + // "/" -> Shift(S14) // pub fn __state5>( mut lookahead: Option, @@ -383,20 +373,30 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::Plus(..)) => { + Some(tok @ Tok::Times(..)) => { let sym1 = &mut Some(tok); let lookahead = tokens.next(); result = try!(__state13(lookahead, tokens, sym0, sym1)); } - Some(tok @ Tok::Minus(..)) => { + Some(tok @ Tok::Div(..)) => { let sym1 = &mut Some(tok); let lookahead = tokens.next(); result = try!(__state14(lookahead, tokens, sym0, sym1)); } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } None => { let sym0 = sym0.take().unwrap(); - let nt = super::__action0(sym0); - return Ok((lookahead, __Nonterminal::__Expr(nt))); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::Expr(nt))); } _ => { return Err(lookahead); @@ -406,6 +406,158 @@ mod __parse__Expr { } // State 6 + // Expr = Expr "-" (*) Factor [EOF] + // Expr = Expr "-" (*) Factor ["+"] + // Expr = Expr "-" (*) Factor ["-"] + // Factor = (*) Factor "*" Term [EOF] + // Factor = (*) Factor "*" Term ["*"] + // Factor = (*) Factor "*" Term ["+"] + // Factor = (*) Factor "*" Term ["-"] + // Factor = (*) Factor "*" Term ["/"] + // Factor = (*) Factor "/" Term [EOF] + // Factor = (*) Factor "/" Term ["*"] + // Factor = (*) Factor "/" Term ["+"] + // Factor = (*) Factor "/" Term ["-"] + // Factor = (*) Factor "/" Term ["/"] + // Factor = (*) Term [EOF] + // Factor = (*) Term ["*"] + // Factor = (*) Term ["+"] + // Factor = (*) Term ["-"] + // Factor = (*) Term ["/"] + // Term = (*) "(" Expr ")" [EOF] + // Term = (*) "(" Expr ")" ["*"] + // Term = (*) "(" Expr ")" ["+"] + // Term = (*) "(" Expr ")" ["-"] + // Term = (*) "(" Expr ")" ["/"] + // Term = (*) "Num" [EOF] + // Term = (*) "Num" ["*"] + // Term = (*) "Num" ["+"] + // Term = (*) "Num" ["-"] + // Term = (*) "Num" ["/"] + // + // "Num" -> Shift(S4) + // "(" -> Shift(S3) + // + // Term -> S1 + // Factor -> S15 + pub fn __state6>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Num(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state4(lookahead, tokens, sym2)); + } + Some(tok @ Tok::LParen(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state3(lookahead, tokens, sym2)); + } + _ => { + return Err(lookahead); + } + } + while sym1.is_some() { + let (lookahead, nt) = result; + match nt { + __Nonterminal::Term(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state1(lookahead, tokens, sym2)); + } + __Nonterminal::Factor(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state15(lookahead, tokens, sym0, sym1, sym2)); + } + _ => { + return Ok((lookahead, nt)); + } + } + } + return Ok(result); + } + + // State 7 + // Expr = Expr "+" (*) Factor [EOF] + // Expr = Expr "+" (*) Factor ["+"] + // Expr = Expr "+" (*) Factor ["-"] + // Factor = (*) Factor "*" Term [EOF] + // Factor = (*) Factor "*" Term ["*"] + // Factor = (*) Factor "*" Term ["+"] + // Factor = (*) Factor "*" Term ["-"] + // Factor = (*) Factor "*" Term ["/"] + // Factor = (*) Factor "/" Term [EOF] + // Factor = (*) Factor "/" Term ["*"] + // Factor = (*) Factor "/" Term ["+"] + // Factor = (*) Factor "/" Term ["-"] + // Factor = (*) Factor "/" Term ["/"] + // Factor = (*) Term [EOF] + // Factor = (*) Term ["*"] + // Factor = (*) Term ["+"] + // Factor = (*) Term ["-"] + // Factor = (*) Term ["/"] + // Term = (*) "(" Expr ")" [EOF] + // Term = (*) "(" Expr ")" ["*"] + // Term = (*) "(" Expr ")" ["+"] + // Term = (*) "(" Expr ")" ["-"] + // Term = (*) "(" Expr ")" ["/"] + // Term = (*) "Num" [EOF] + // Term = (*) "Num" ["*"] + // Term = (*) "Num" ["+"] + // Term = (*) "Num" ["-"] + // Term = (*) "Num" ["/"] + // + // "(" -> Shift(S3) + // "Num" -> Shift(S4) + // + // Term -> S1 + // Factor -> S16 + pub fn __state7>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::LParen(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state3(lookahead, tokens, sym2)); + } + Some(tok @ Tok::Num(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state4(lookahead, tokens, sym2)); + } + _ => { + return Err(lookahead); + } + } + while sym1.is_some() { + let (lookahead, nt) = result; + match nt { + __Nonterminal::Term(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state1(lookahead, tokens, sym2)); + } + __Nonterminal::Factor(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state16(lookahead, tokens, sym0, sym1, sym2)); + } + _ => { + return Ok((lookahead, nt)); + } + } + } + return Ok(result); + } + + // State 8 // Expr = Expr (*) "+" Factor [")"] // Expr = Expr (*) "+" Factor ["+"] // Expr = Expr (*) "+" Factor ["-"] @@ -418,11 +570,11 @@ mod __parse__Expr { // Term = "(" Expr (*) ")" ["-"] // Term = "(" Expr (*) ")" ["/"] // - // "-" -> Shift(S16) - // "+" -> Shift(S15) - // ")" -> Shift(S17) + // "+" -> Shift(S17) + // ")" -> Shift(S19) + // "-" -> Shift(S18) // - pub fn __state6>( + pub fn __state8>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -430,131 +582,20 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::Minus(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state16(lookahead, tokens, sym1, sym2)); - } Some(tok @ Tok::Plus(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state15(lookahead, tokens, sym1, sym2)); + result = try!(__state17(lookahead, tokens, sym1, sym2)); } Some(tok @ Tok::RParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state17(lookahead, tokens, sym0, sym1, sym2)); + result = try!(__state19(lookahead, tokens, sym0, sym1, sym2)); } - _ => { - return Err(lookahead); - } - } - return Ok(result); - } - - // State 7 - // Term = "Num" (*) [")"] - // Term = "Num" (*) ["*"] - // Term = "Num" (*) ["+"] - // Term = "Num" (*) ["-"] - // Term = "Num" (*) ["/"] - // - // "+" -> Reduce(Term = "Num" => ActionFn(7);) - // "*" -> Reduce(Term = "Num" => ActionFn(7);) - // ")" -> Reduce(Term = "Num" => ActionFn(7);) - // "-" -> Reduce(Term = "Num" => ActionFn(7);) - // "/" -> Reduce(Term = "Num" => ActionFn(7);) - // - pub fn __state7>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::Times(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::Div(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action7(sym0); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 8 - // Expr = Factor (*) [")"] - // Expr = Factor (*) ["+"] - // Expr = Factor (*) ["-"] - // Factor = Factor (*) "*" Term [")"] - // Factor = Factor (*) "*" Term ["*"] - // Factor = Factor (*) "*" Term ["+"] - // Factor = Factor (*) "*" Term ["-"] - // Factor = Factor (*) "*" Term ["/"] - // Factor = Factor (*) "/" Term [")"] - // Factor = Factor (*) "/" Term ["*"] - // Factor = Factor (*) "/" Term ["+"] - // Factor = Factor (*) "/" Term ["-"] - // Factor = Factor (*) "/" Term ["/"] - // - // "+" -> Reduce(Expr = Factor => ActionFn(3);) - // ")" -> Reduce(Expr = Factor => ActionFn(3);) - // "/" -> Shift(S19) - // "-" -> Reduce(Expr = Factor => ActionFn(3);) - // "*" -> Shift(S18) - // - pub fn __state8>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::Div(..)) => { - let sym1 = &mut Some(tok); + Some(tok @ Tok::Minus(..)) => { + let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state19(lookahead, tokens, sym0, sym1)); - } - Some(tok @ Tok::Times(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state18(lookahead, tokens, sym0, sym1)); - } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::Expr(nt))); - } - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::Expr(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::Expr(nt))); + result = try!(__state18(lookahead, tokens, sym1, sym2)); } _ => { return Err(lookahead); @@ -604,12 +645,12 @@ mod __parse__Expr { // Term = (*) "Num" ["-"] // Term = (*) "Num" ["/"] // - // "Num" -> Shift(S7) // "(" -> Shift(S9) + // "Num" -> Shift(S10) // + // Factor -> S11 // Expr -> S20 - // Factor -> S8 - // Term -> S10 + // Term -> S12 pub fn __state9>( mut lookahead: Option, tokens: &mut TOKENS, @@ -617,16 +658,16 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::Num(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state7(lookahead, tokens, sym1)); - } Some(tok @ Tok::LParen(..)) => { let sym1 = &mut Some(tok); let lookahead = tokens.next(); result = try!(__state9(lookahead, tokens, sym1)); } + Some(tok @ Tok::Num(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state10(lookahead, tokens, sym1)); + } _ => { return Err(lookahead); } @@ -634,17 +675,17 @@ mod __parse__Expr { while sym0.is_some() { let (lookahead, nt) = result; match nt { + __Nonterminal::Factor(nt) => { + let sym1 = &mut Some(nt); + result = try!(__state11(lookahead, tokens, sym1)); + } __Nonterminal::Expr(nt) => { let sym1 = &mut Some(nt); result = try!(__state20(lookahead, tokens, sym0, sym1)); } - __Nonterminal::Factor(nt) => { - let sym1 = &mut Some(nt); - result = try!(__state8(lookahead, tokens, sym1)); - } __Nonterminal::Term(nt) => { let sym1 = &mut Some(nt); - result = try!(__state10(lookahead, tokens, sym1)); + result = try!(__state12(lookahead, tokens, sym1)); } _ => { return Ok((lookahead, nt)); @@ -655,6 +696,117 @@ mod __parse__Expr { } // State 10 + // Term = "Num" (*) [")"] + // Term = "Num" (*) ["*"] + // Term = "Num" (*) ["+"] + // Term = "Num" (*) ["-"] + // Term = "Num" (*) ["/"] + // + // "-" -> Reduce(Term = "Num" => ActionFn(7);) + // "/" -> Reduce(Term = "Num" => ActionFn(7);) + // ")" -> Reduce(Term = "Num" => ActionFn(7);) + // "*" -> Reduce(Term = "Num" => ActionFn(7);) + // "+" -> Reduce(Term = "Num" => ActionFn(7);) + // + pub fn __state10>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Div(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Times(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action7(sym0); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + _ => { + return Err(lookahead); + } + } + } + + // State 11 + // Expr = Factor (*) [")"] + // Expr = Factor (*) ["+"] + // Expr = Factor (*) ["-"] + // Factor = Factor (*) "*" Term [")"] + // Factor = Factor (*) "*" Term ["*"] + // Factor = Factor (*) "*" Term ["+"] + // Factor = Factor (*) "*" Term ["-"] + // Factor = Factor (*) "*" Term ["/"] + // Factor = Factor (*) "/" Term [")"] + // Factor = Factor (*) "/" Term ["*"] + // Factor = Factor (*) "/" Term ["+"] + // Factor = Factor (*) "/" Term ["-"] + // Factor = Factor (*) "/" Term ["/"] + // + // "/" -> Shift(S22) + // "*" -> Shift(S21) + // ")" -> Reduce(Expr = Factor => ActionFn(3);) + // "-" -> Reduce(Expr = Factor => ActionFn(3);) + // "+" -> Reduce(Expr = Factor => ActionFn(3);) + // + pub fn __state11>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Div(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state22(lookahead, tokens, sym0, sym1)); + } + Some(tok @ Tok::Times(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state21(lookahead, tokens, sym0, sym1)); + } + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + _ => { + return Err(lookahead); + } + } + return Ok(result); + } + + // State 12 // Factor = Term (*) [")"] // Factor = Term (*) ["*"] // Factor = Term (*) ["+"] @@ -663,11 +815,11 @@ mod __parse__Expr { // // ")" -> Reduce(Factor = Term => ActionFn(6);) // "*" -> Reduce(Factor = Term => ActionFn(6);) - // "-" -> Reduce(Factor = Term => ActionFn(6);) // "+" -> Reduce(Factor = Term => ActionFn(6);) + // "-" -> Reduce(Factor = Term => ActionFn(6);) // "/" -> Reduce(Factor = Term => ActionFn(6);) // - pub fn __state10>( + pub fn __state12>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -684,12 +836,12 @@ mod __parse__Expr { let nt = super::__action6(sym0); return Ok((lookahead, __Nonterminal::Factor(nt))); } - Some(Tok::Minus(..)) => { + Some(Tok::Plus(..)) => { let sym0 = sym0.take().unwrap(); let nt = super::__action6(sym0); return Ok((lookahead, __Nonterminal::Factor(nt))); } - Some(Tok::Plus(..)) => { + Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); let nt = super::__action6(sym0); return Ok((lookahead, __Nonterminal::Factor(nt))); @@ -705,7 +857,7 @@ mod __parse__Expr { } } - // State 11 + // State 13 // Factor = Factor "*" (*) Term [EOF] // Factor = Factor "*" (*) Term ["*"] // Factor = Factor "*" (*) Term ["+"] @@ -722,11 +874,11 @@ mod __parse__Expr { // Term = (*) "Num" ["-"] // Term = (*) "Num" ["/"] // - // "(" -> Shift(S2) + // "(" -> Shift(S3) // "Num" -> Shift(S4) // - // Term -> S21 - pub fn __state11>( + // Term -> S23 + pub fn __state13>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -737,7 +889,7 @@ mod __parse__Expr { Some(tok @ Tok::LParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state2(lookahead, tokens, sym2)); + result = try!(__state3(lookahead, tokens, sym2)); } Some(tok @ Tok::Num(..)) => { let sym2 = &mut Some(tok); @@ -753,7 +905,7 @@ mod __parse__Expr { match nt { __Nonterminal::Term(nt) => { let sym2 = &mut Some(nt); - result = try!(__state21(lookahead, tokens, sym0, sym1, sym2)); + result = try!(__state23(lookahead, tokens, sym0, sym1, sym2)); } _ => { return Ok((lookahead, nt)); @@ -763,7 +915,7 @@ mod __parse__Expr { return Ok(result); } - // State 12 + // State 14 // Factor = Factor "/" (*) Term [EOF] // Factor = Factor "/" (*) Term ["*"] // Factor = Factor "/" (*) Term ["+"] @@ -780,158 +932,10 @@ mod __parse__Expr { // Term = (*) "Num" ["-"] // Term = (*) "Num" ["/"] // - // "(" -> Shift(S2) + // "(" -> Shift(S3) // "Num" -> Shift(S4) // - // Term -> S22 - pub fn __state12>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::LParen(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state2(lookahead, tokens, sym2)); - } - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state4(lookahead, tokens, sym2)); - } - _ => { - return Err(lookahead); - } - } - while sym1.is_some() { - let (lookahead, nt) = result; - match nt { - __Nonterminal::Term(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state22(lookahead, tokens, sym0, sym1, sym2)); - } - _ => { - return Ok((lookahead, nt)); - } - } - } - return Ok(result); - } - - // State 13 - // Expr = Expr "+" (*) Factor [EOF] - // Expr = Expr "+" (*) Factor ["+"] - // Expr = Expr "+" (*) Factor ["-"] - // Factor = (*) Factor "*" Term [EOF] - // Factor = (*) Factor "*" Term ["*"] - // Factor = (*) Factor "*" Term ["+"] - // Factor = (*) Factor "*" Term ["-"] - // Factor = (*) Factor "*" Term ["/"] - // Factor = (*) Factor "/" Term [EOF] - // Factor = (*) Factor "/" Term ["*"] - // Factor = (*) Factor "/" Term ["+"] - // Factor = (*) Factor "/" Term ["-"] - // Factor = (*) Factor "/" Term ["/"] - // Factor = (*) Term [EOF] - // Factor = (*) Term ["*"] - // Factor = (*) Term ["+"] - // Factor = (*) Term ["-"] - // Factor = (*) Term ["/"] - // Term = (*) "(" Expr ")" [EOF] - // Term = (*) "(" Expr ")" ["*"] - // Term = (*) "(" Expr ")" ["+"] - // Term = (*) "(" Expr ")" ["-"] - // Term = (*) "(" Expr ")" ["/"] - // Term = (*) "Num" [EOF] - // Term = (*) "Num" ["*"] - // Term = (*) "Num" ["+"] - // Term = (*) "Num" ["-"] - // Term = (*) "Num" ["/"] - // - // "(" -> Shift(S2) - // "Num" -> Shift(S4) - // - // Factor -> S23 - // Term -> S1 - pub fn __state13>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::LParen(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state2(lookahead, tokens, sym2)); - } - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state4(lookahead, tokens, sym2)); - } - _ => { - return Err(lookahead); - } - } - while sym1.is_some() { - let (lookahead, nt) = result; - match nt { - __Nonterminal::Factor(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state23(lookahead, tokens, sym0, sym1, sym2)); - } - __Nonterminal::Term(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state1(lookahead, tokens, sym2)); - } - _ => { - return Ok((lookahead, nt)); - } - } - } - return Ok(result); - } - - // State 14 - // Expr = Expr "-" (*) Factor [EOF] - // Expr = Expr "-" (*) Factor ["+"] - // Expr = Expr "-" (*) Factor ["-"] - // Factor = (*) Factor "*" Term [EOF] - // Factor = (*) Factor "*" Term ["*"] - // Factor = (*) Factor "*" Term ["+"] - // Factor = (*) Factor "*" Term ["-"] - // Factor = (*) Factor "*" Term ["/"] - // Factor = (*) Factor "/" Term [EOF] - // Factor = (*) Factor "/" Term ["*"] - // Factor = (*) Factor "/" Term ["+"] - // Factor = (*) Factor "/" Term ["-"] - // Factor = (*) Factor "/" Term ["/"] - // Factor = (*) Term [EOF] - // Factor = (*) Term ["*"] - // Factor = (*) Term ["+"] - // Factor = (*) Term ["-"] - // Factor = (*) Term ["/"] - // Term = (*) "(" Expr ")" [EOF] - // Term = (*) "(" Expr ")" ["*"] - // Term = (*) "(" Expr ")" ["+"] - // Term = (*) "(" Expr ")" ["-"] - // Term = (*) "(" Expr ")" ["/"] - // Term = (*) "Num" [EOF] - // Term = (*) "Num" ["*"] - // Term = (*) "Num" ["+"] - // Term = (*) "Num" ["-"] - // Term = (*) "Num" ["/"] - // - // "(" -> Shift(S2) - // "Num" -> Shift(S4) - // - // Factor -> S24 - // Term -> S1 + // Term -> S24 pub fn __state14>( mut lookahead: Option, tokens: &mut TOKENS, @@ -943,7 +947,7 @@ mod __parse__Expr { Some(tok @ Tok::LParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state2(lookahead, tokens, sym2)); + result = try!(__state3(lookahead, tokens, sym2)); } Some(tok @ Tok::Num(..)) => { let sym2 = &mut Some(tok); @@ -957,13 +961,9 @@ mod __parse__Expr { while sym1.is_some() { let (lookahead, nt) = result; match nt { - __Nonterminal::Factor(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state24(lookahead, tokens, sym0, sym1, sym2)); - } __Nonterminal::Term(nt) => { let sym2 = &mut Some(nt); - result = try!(__state1(lookahead, tokens, sym2)); + result = try!(__state24(lookahead, tokens, sym0, sym1, sym2)); } _ => { return Ok((lookahead, nt)); @@ -974,6 +974,142 @@ mod __parse__Expr { } // State 15 + // Expr = Expr "-" Factor (*) [EOF] + // Expr = Expr "-" Factor (*) ["+"] + // Expr = Expr "-" Factor (*) ["-"] + // Factor = Factor (*) "*" Term [EOF] + // Factor = Factor (*) "*" Term ["*"] + // Factor = Factor (*) "*" Term ["+"] + // Factor = Factor (*) "*" Term ["-"] + // Factor = Factor (*) "*" Term ["/"] + // Factor = Factor (*) "/" Term [EOF] + // Factor = Factor (*) "/" Term ["*"] + // Factor = Factor (*) "/" Term ["+"] + // Factor = Factor (*) "/" Term ["-"] + // Factor = Factor (*) "/" Term ["/"] + // + // "/" -> Shift(S14) + // EOF -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) + // "+" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) + // "-" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) + // "*" -> Shift(S13) + // + pub fn __state15>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + sym2: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Div(..)) => { + let sym3 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state14(lookahead, tokens, sym2, sym3)); + } + Some(tok @ Tok::Times(..)) => { + let sym3 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state13(lookahead, tokens, sym2, sym3)); + } + None => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action1(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action1(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action1(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + _ => { + return Err(lookahead); + } + } + return Ok(result); + } + + // State 16 + // Expr = Expr "+" Factor (*) [EOF] + // Expr = Expr "+" Factor (*) ["+"] + // Expr = Expr "+" Factor (*) ["-"] + // Factor = Factor (*) "*" Term [EOF] + // Factor = Factor (*) "*" Term ["*"] + // Factor = Factor (*) "*" Term ["+"] + // Factor = Factor (*) "*" Term ["-"] + // Factor = Factor (*) "*" Term ["/"] + // Factor = Factor (*) "/" Term [EOF] + // Factor = Factor (*) "/" Term ["*"] + // Factor = Factor (*) "/" Term ["+"] + // Factor = Factor (*) "/" Term ["-"] + // Factor = Factor (*) "/" Term ["/"] + // + // EOF -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) + // "-" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) + // "*" -> Shift(S13) + // "/" -> Shift(S14) + // "+" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) + // + pub fn __state16>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + sym2: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Times(..)) => { + let sym3 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state13(lookahead, tokens, sym2, sym3)); + } + Some(tok @ Tok::Div(..)) => { + let sym3 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state14(lookahead, tokens, sym2, sym3)); + } + None => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action2(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action2(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action2(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Expr(nt))); + } + _ => { + return Err(lookahead); + } + } + return Ok(result); + } + + // State 17 // Expr = Expr "+" (*) Factor [")"] // Expr = Expr "+" (*) Factor ["+"] // Expr = Expr "+" (*) Factor ["-"] @@ -1003,12 +1139,12 @@ mod __parse__Expr { // Term = (*) "Num" ["-"] // Term = (*) "Num" ["/"] // - // "Num" -> Shift(S7) // "(" -> Shift(S9) + // "Num" -> Shift(S10) // - // Term -> S10 + // Term -> S12 // Factor -> S25 - pub fn __state15>( + pub fn __state17>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -1016,16 +1152,16 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state7(lookahead, tokens, sym2)); - } Some(tok @ Tok::LParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); result = try!(__state9(lookahead, tokens, sym2)); } + Some(tok @ Tok::Num(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state10(lookahead, tokens, sym2)); + } _ => { return Err(lookahead); } @@ -1035,7 +1171,7 @@ mod __parse__Expr { match nt { __Nonterminal::Term(nt) => { let sym2 = &mut Some(nt); - result = try!(__state10(lookahead, tokens, sym2)); + result = try!(__state12(lookahead, tokens, sym2)); } __Nonterminal::Factor(nt) => { let sym2 = &mut Some(nt); @@ -1049,7 +1185,7 @@ mod __parse__Expr { return Ok(result); } - // State 16 + // State 18 // Expr = Expr "-" (*) Factor [")"] // Expr = Expr "-" (*) Factor ["+"] // Expr = Expr "-" (*) Factor ["-"] @@ -1079,12 +1215,12 @@ mod __parse__Expr { // Term = (*) "Num" ["-"] // Term = (*) "Num" ["/"] // + // "Num" -> Shift(S10) // "(" -> Shift(S9) - // "Num" -> Shift(S7) // - // Term -> S10 + // Term -> S12 // Factor -> S26 - pub fn __state16>( + pub fn __state18>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -1092,16 +1228,16 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { + Some(tok @ Tok::Num(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state10(lookahead, tokens, sym2)); + } Some(tok @ Tok::LParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); result = try!(__state9(lookahead, tokens, sym2)); } - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state7(lookahead, tokens, sym2)); - } _ => { return Err(lookahead); } @@ -1111,7 +1247,7 @@ mod __parse__Expr { match nt { __Nonterminal::Term(nt) => { let sym2 = &mut Some(nt); - result = try!(__state10(lookahead, tokens, sym2)); + result = try!(__state12(lookahead, tokens, sym2)); } __Nonterminal::Factor(nt) => { let sym2 = &mut Some(nt); @@ -1125,20 +1261,20 @@ mod __parse__Expr { return Ok(result); } - // State 17 + // State 19 // Term = "(" Expr ")" (*) [EOF] // Term = "(" Expr ")" (*) ["*"] // Term = "(" Expr ")" (*) ["+"] // Term = "(" Expr ")" (*) ["-"] // Term = "(" Expr ")" (*) ["/"] // + // "-" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) // "/" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) // EOF -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) - // "-" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) - // "*" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) // "+" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) + // "*" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) // - pub fn __state17>( + pub fn __state19>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -1147,6 +1283,13 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action8(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Term(nt))); + } Some(Tok::Div(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); @@ -1161,7 +1304,7 @@ mod __parse__Expr { let nt = super::__action8(sym0, sym1, sym2); return Ok((lookahead, __Nonterminal::Term(nt))); } - Some(Tok::Minus(..)) => { + Some(Tok::Plus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); @@ -1175,20 +1318,60 @@ mod __parse__Expr { let nt = super::__action8(sym0, sym1, sym2); return Ok((lookahead, __Nonterminal::Term(nt))); } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action8(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Term(nt))); - } _ => { return Err(lookahead); } } } - // State 18 + // State 20 + // Expr = Expr (*) "+" Factor [")"] + // Expr = Expr (*) "+" Factor ["+"] + // Expr = Expr (*) "+" Factor ["-"] + // Expr = Expr (*) "-" Factor [")"] + // Expr = Expr (*) "-" Factor ["+"] + // Expr = Expr (*) "-" Factor ["-"] + // Term = "(" Expr (*) ")" [")"] + // Term = "(" Expr (*) ")" ["*"] + // Term = "(" Expr (*) ")" ["+"] + // Term = "(" Expr (*) ")" ["-"] + // Term = "(" Expr (*) ")" ["/"] + // + // "+" -> Shift(S17) + // ")" -> Shift(S27) + // "-" -> Shift(S18) + // + pub fn __state20>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Plus(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state17(lookahead, tokens, sym1, sym2)); + } + Some(tok @ Tok::RParen(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state27(lookahead, tokens, sym0, sym1, sym2)); + } + Some(tok @ Tok::Minus(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state18(lookahead, tokens, sym1, sym2)); + } + _ => { + return Err(lookahead); + } + } + return Ok(result); + } + + // State 21 // Factor = Factor "*" (*) Term [")"] // Factor = Factor "*" (*) Term ["*"] // Factor = Factor "*" (*) Term ["+"] @@ -1206,68 +1389,10 @@ mod __parse__Expr { // Term = (*) "Num" ["/"] // // "(" -> Shift(S9) - // "Num" -> Shift(S7) - // - // Term -> S27 - pub fn __state18>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::LParen(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state9(lookahead, tokens, sym2)); - } - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state7(lookahead, tokens, sym2)); - } - _ => { - return Err(lookahead); - } - } - while sym1.is_some() { - let (lookahead, nt) = result; - match nt { - __Nonterminal::Term(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state27(lookahead, tokens, sym0, sym1, sym2)); - } - _ => { - return Ok((lookahead, nt)); - } - } - } - return Ok(result); - } - - // State 19 - // Factor = Factor "/" (*) Term [")"] - // Factor = Factor "/" (*) Term ["*"] - // Factor = Factor "/" (*) Term ["+"] - // Factor = Factor "/" (*) Term ["-"] - // Factor = Factor "/" (*) Term ["/"] - // Term = (*) "(" Expr ")" [")"] - // Term = (*) "(" Expr ")" ["*"] - // Term = (*) "(" Expr ")" ["+"] - // Term = (*) "(" Expr ")" ["-"] - // Term = (*) "(" Expr ")" ["/"] - // Term = (*) "Num" [")"] - // Term = (*) "Num" ["*"] - // Term = (*) "Num" ["+"] - // Term = (*) "Num" ["-"] - // Term = (*) "Num" ["/"] - // - // "(" -> Shift(S9) - // "Num" -> Shift(S7) + // "Num" -> Shift(S10) // // Term -> S28 - pub fn __state19>( + pub fn __state21>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -1283,7 +1408,7 @@ mod __parse__Expr { Some(tok @ Tok::Num(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state7(lookahead, tokens, sym2)); + result = try!(__state10(lookahead, tokens, sym2)); } _ => { return Err(lookahead); @@ -1304,199 +1429,76 @@ mod __parse__Expr { return Ok(result); } - // State 20 - // Expr = Expr (*) "+" Factor [")"] - // Expr = Expr (*) "+" Factor ["+"] - // Expr = Expr (*) "+" Factor ["-"] - // Expr = Expr (*) "-" Factor [")"] - // Expr = Expr (*) "-" Factor ["+"] - // Expr = Expr (*) "-" Factor ["-"] - // Term = "(" Expr (*) ")" [")"] - // Term = "(" Expr (*) ")" ["*"] - // Term = "(" Expr (*) ")" ["+"] - // Term = "(" Expr (*) ")" ["-"] - // Term = "(" Expr (*) ")" ["/"] + // State 22 + // Factor = Factor "/" (*) Term [")"] + // Factor = Factor "/" (*) Term ["*"] + // Factor = Factor "/" (*) Term ["+"] + // Factor = Factor "/" (*) Term ["-"] + // Factor = Factor "/" (*) Term ["/"] + // Term = (*) "(" Expr ")" [")"] + // Term = (*) "(" Expr ")" ["*"] + // Term = (*) "(" Expr ")" ["+"] + // Term = (*) "(" Expr ")" ["-"] + // Term = (*) "(" Expr ")" ["/"] + // Term = (*) "Num" [")"] + // Term = (*) "Num" ["*"] + // Term = (*) "Num" ["+"] + // Term = (*) "Num" ["-"] + // Term = (*) "Num" ["/"] // - // ")" -> Shift(S29) - // "+" -> Shift(S15) - // "-" -> Shift(S16) + // "Num" -> Shift(S10) + // "(" -> Shift(S9) // - pub fn __state20>( + // Term -> S29 + pub fn __state22>( mut lookahead: Option, tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, + sym0: &mut Option, + sym1: &mut Option, ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::RParen(..)) => { + Some(tok @ Tok::Num(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state29(lookahead, tokens, sym0, sym1, sym2)); + result = try!(__state10(lookahead, tokens, sym2)); } - Some(tok @ Tok::Plus(..)) => { + Some(tok @ Tok::LParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state15(lookahead, tokens, sym1, sym2)); - } - Some(tok @ Tok::Minus(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state16(lookahead, tokens, sym1, sym2)); + result = try!(__state9(lookahead, tokens, sym2)); } _ => { return Err(lookahead); } } + while sym1.is_some() { + let (lookahead, nt) = result; + match nt { + __Nonterminal::Term(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state29(lookahead, tokens, sym0, sym1, sym2)); + } + _ => { + return Ok((lookahead, nt)); + } + } + } return Ok(result); } - // State 21 + // State 23 // Factor = Factor "*" Term (*) [EOF] // Factor = Factor "*" Term (*) ["*"] // Factor = Factor "*" Term (*) ["+"] // Factor = Factor "*" Term (*) ["-"] // Factor = Factor "*" Term (*) ["/"] // - // "+" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // "*" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // "-" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) // "/" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // "-" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) // EOF -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // - pub fn __state21>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - sym2: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Times(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Div(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - None => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 22 - // Factor = Factor "/" Term (*) [EOF] - // Factor = Factor "/" Term (*) ["*"] - // Factor = Factor "/" Term (*) ["+"] - // Factor = Factor "/" Term (*) ["-"] - // Factor = Factor "/" Term (*) ["/"] - // - // "-" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // EOF -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "+" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "/" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "*" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // - pub fn __state22>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - sym2: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - None => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Div(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Times(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 23 - // Expr = Expr "+" Factor (*) [EOF] - // Expr = Expr "+" Factor (*) ["+"] - // Expr = Expr "+" Factor (*) ["-"] - // Factor = Factor (*) "*" Term [EOF] - // Factor = Factor (*) "*" Term ["*"] - // Factor = Factor (*) "*" Term ["+"] - // Factor = Factor (*) "*" Term ["-"] - // Factor = Factor (*) "*" Term ["/"] - // Factor = Factor (*) "/" Term [EOF] - // Factor = Factor (*) "/" Term ["*"] - // Factor = Factor (*) "/" Term ["+"] - // Factor = Factor (*) "/" Term ["-"] - // Factor = Factor (*) "/" Term ["/"] - // - // "-" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) - // "/" -> Shift(S12) - // EOF -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) - // "+" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) - // "*" -> Shift(S11) + // "*" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // "+" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) // pub fn __state23>( mut lookahead: Option, @@ -1507,64 +1509,59 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::Div(..)) => { - let sym3 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state12(lookahead, tokens, sym2, sym3)); - } - Some(tok @ Tok::Times(..)) => { - let sym3 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state11(lookahead, tokens, sym2, sym3)); + Some(Tok::Div(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action2(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Expr(nt))); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } None => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action2(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Expr(nt))); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::Times(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } Some(Tok::Plus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action2(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Expr(nt))); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } _ => { return Err(lookahead); } } - return Ok(result); } // State 24 - // Expr = Expr "-" Factor (*) [EOF] - // Expr = Expr "-" Factor (*) ["+"] - // Expr = Expr "-" Factor (*) ["-"] - // Factor = Factor (*) "*" Term [EOF] - // Factor = Factor (*) "*" Term ["*"] - // Factor = Factor (*) "*" Term ["+"] - // Factor = Factor (*) "*" Term ["-"] - // Factor = Factor (*) "*" Term ["/"] - // Factor = Factor (*) "/" Term [EOF] - // Factor = Factor (*) "/" Term ["*"] - // Factor = Factor (*) "/" Term ["+"] - // Factor = Factor (*) "/" Term ["-"] - // Factor = Factor (*) "/" Term ["/"] + // Factor = Factor "/" Term (*) [EOF] + // Factor = Factor "/" Term (*) ["*"] + // Factor = Factor "/" Term (*) ["+"] + // Factor = Factor "/" Term (*) ["-"] + // Factor = Factor "/" Term (*) ["/"] // - // EOF -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) - // "*" -> Shift(S11) - // "-" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) - // "/" -> Shift(S12) - // "+" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) + // "*" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "+" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "-" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "/" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // EOF -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) // pub fn __state24>( mut lookahead: Option, @@ -1575,42 +1572,45 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(tok @ Tok::Times(..)) => { - let sym3 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state11(lookahead, tokens, sym2, sym3)); - } - Some(tok @ Tok::Div(..)) => { - let sym3 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state12(lookahead, tokens, sym2, sym3)); - } - None => { + Some(Tok::Times(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action1(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Expr(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action1(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Expr(nt))); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } Some(Tok::Plus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action1(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Expr(nt))); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::Div(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + None => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } _ => { return Err(lookahead); } } - return Ok(result); } // State 25 @@ -1628,11 +1628,11 @@ mod __parse__Expr { // Factor = Factor (*) "/" Term ["-"] // Factor = Factor (*) "/" Term ["/"] // + // "*" -> Shift(S21) // "-" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) - // "*" -> Shift(S18) // ")" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) - // "/" -> Shift(S19) // "+" -> Reduce(Expr = Expr, "+", Factor => ActionFn(2);) + // "/" -> Shift(S22) // pub fn __state25>( mut lookahead: Option, @@ -1646,12 +1646,12 @@ mod __parse__Expr { Some(tok @ Tok::Times(..)) => { let sym3 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state18(lookahead, tokens, sym2, sym3)); + result = try!(__state21(lookahead, tokens, sym2, sym3)); } Some(tok @ Tok::Div(..)) => { let sym3 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state19(lookahead, tokens, sym2, sym3)); + result = try!(__state22(lookahead, tokens, sym2, sym3)); } Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); @@ -1696,10 +1696,10 @@ mod __parse__Expr { // Factor = Factor (*) "/" Term ["-"] // Factor = Factor (*) "/" Term ["/"] // - // "*" -> Shift(S18) - // "+" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) // ")" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) - // "/" -> Shift(S19) + // "+" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) + // "*" -> Shift(S21) + // "/" -> Shift(S22) // "-" -> Reduce(Expr = Expr, "-", Factor => ActionFn(1);) // pub fn __state26>( @@ -1714,21 +1714,21 @@ mod __parse__Expr { Some(tok @ Tok::Times(..)) => { let sym3 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state18(lookahead, tokens, sym2, sym3)); + result = try!(__state21(lookahead, tokens, sym2, sym3)); } Some(tok @ Tok::Div(..)) => { let sym3 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state19(lookahead, tokens, sym2, sym3)); + result = try!(__state22(lookahead, tokens, sym2, sym3)); } - Some(Tok::Plus(..)) => { + Some(Tok::RParen(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); let nt = super::__action1(sym0, sym1, sym2); return Ok((lookahead, __Nonterminal::Expr(nt))); } - Some(Tok::RParen(..)) => { + Some(Tok::Plus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); @@ -1750,61 +1750,61 @@ mod __parse__Expr { } // State 27 - // Factor = Factor "*" Term (*) [")"] - // Factor = Factor "*" Term (*) ["*"] - // Factor = Factor "*" Term (*) ["+"] - // Factor = Factor "*" Term (*) ["-"] - // Factor = Factor "*" Term (*) ["/"] + // Term = "(" Expr ")" (*) [")"] + // Term = "(" Expr ")" (*) ["*"] + // Term = "(" Expr ")" (*) ["+"] + // Term = "(" Expr ")" (*) ["-"] + // Term = "(" Expr ")" (*) ["/"] // - // "-" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // ")" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // "+" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // "*" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) - // "/" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // "*" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) + // "/" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) + // "+" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) + // ")" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) + // "-" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) // pub fn __state27>( mut lookahead: Option, tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - sym2: &mut Option, + sym0: &mut Option, + sym1: &mut Option, + sym2: &mut Option, ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } Some(Tok::Times(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); + let nt = super::__action8(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Term(nt))); } Some(Tok::Div(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action4(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); + let nt = super::__action8(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action8(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action8(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Term(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action8(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Term(nt))); } _ => { return Err(lookahead); @@ -1813,17 +1813,17 @@ mod __parse__Expr { } // State 28 - // Factor = Factor "/" Term (*) [")"] - // Factor = Factor "/" Term (*) ["*"] - // Factor = Factor "/" Term (*) ["+"] - // Factor = Factor "/" Term (*) ["-"] - // Factor = Factor "/" Term (*) ["/"] + // Factor = Factor "*" Term (*) [")"] + // Factor = Factor "*" Term (*) ["*"] + // Factor = Factor "*" Term (*) ["+"] + // Factor = Factor "*" Term (*) ["-"] + // Factor = Factor "*" Term (*) ["/"] // - // ")" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "+" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "/" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "-" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) - // "*" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "-" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // "*" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // ")" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // "+" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) + // "/" -> Reduce(Factor = Factor, "*", Term => ActionFn(4);) // pub fn __state28>( mut lookahead: Option, @@ -1834,39 +1834,39 @@ mod __parse__Expr { ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } - Some(Tok::Div(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Factor(nt))); - } Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); + let nt = super::__action4(sym0, sym1, sym2); return Ok((lookahead, __Nonterminal::Factor(nt))); } Some(Tok::Times(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action5(sym0, sym1, sym2); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action4(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::Div(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action4(sym0, sym1, sym2); return Ok((lookahead, __Nonterminal::Factor(nt))); } _ => { @@ -1876,61 +1876,61 @@ mod __parse__Expr { } // State 29 - // Term = "(" Expr ")" (*) [")"] - // Term = "(" Expr ")" (*) ["*"] - // Term = "(" Expr ")" (*) ["+"] - // Term = "(" Expr ")" (*) ["-"] - // Term = "(" Expr ")" (*) ["/"] + // Factor = Factor "/" Term (*) [")"] + // Factor = Factor "/" Term (*) ["*"] + // Factor = Factor "/" Term (*) ["+"] + // Factor = Factor "/" Term (*) ["-"] + // Factor = Factor "/" Term (*) ["/"] // - // "+" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) - // "-" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) - // ")" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) - // "/" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) - // "*" -> Reduce(Term = "(", Expr, ")" => ActionFn(8);) + // "-" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "/" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "*" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // ")" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) + // "+" -> Reduce(Factor = Factor, "/", Term => ActionFn(5);) // pub fn __state29>( mut lookahead: Option, tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - sym2: &mut Option, + sym0: &mut Option, + sym1: &mut Option, + sym2: &mut Option, ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - Some(Tok::Plus(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action8(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Term(nt))); - } Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action8(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Term(nt))); - } - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action8(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Term(nt))); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } Some(Tok::Div(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action8(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Term(nt))); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } Some(Tok::Times(..)) => { let sym0 = sym0.take().unwrap(); let sym1 = sym1.take().unwrap(); let sym2 = sym2.take().unwrap(); - let nt = super::__action8(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::Term(nt))); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); + } + Some(Tok::Plus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action5(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::Factor(nt))); } _ => { return Err(lookahead); diff --git a/lalrpop-test/src/sub.rs b/lalrpop-test/src/sub.rs index bf3cddd..4c63570 100644 --- a/lalrpop-test/src/sub.rs +++ b/lalrpop-test/src/sub.rs @@ -20,9 +20,9 @@ mod __parse__S { pub enum __Nonterminal { E(i32), + T(i32), S(i32), __S(i32), - T(i32), } // State 0 @@ -37,12 +37,12 @@ mod __parse__S { // T = (*) "Num" ["-"] // __S = (*) S [EOF] // - // "Num" -> Shift(S1) - // "(" -> Shift(S5) + // "Num" -> Shift(S4) + // "(" -> Shift(S2) // - // T -> S3 - // S -> S2 - // E -> S4 + // T -> S1 + // S -> S5 + // E -> S3 pub fn __state0>( mut lookahead: Option, tokens: &mut TOKENS, @@ -52,12 +52,12 @@ mod __parse__S { Some(tok @ Tok::Num(..)) => { let sym0 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state1(lookahead, tokens, sym0)); + result = try!(__state4(lookahead, tokens, sym0)); } Some(tok @ Tok::LParen(..)) => { let sym0 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state5(lookahead, tokens, sym0)); + result = try!(__state2(lookahead, tokens, sym0)); } _ => { return Err(lookahead); @@ -68,15 +68,15 @@ mod __parse__S { match nt { __Nonterminal::T(nt) => { let sym0 = &mut Some(nt); - result = try!(__state3(lookahead, tokens, sym0)); + result = try!(__state1(lookahead, tokens, sym0)); } __Nonterminal::S(nt) => { let sym0 = &mut Some(nt); - result = try!(__state2(lookahead, tokens, sym0)); + result = try!(__state5(lookahead, tokens, sym0)); } __Nonterminal::E(nt) => { let sym0 = &mut Some(nt); - result = try!(__state4(lookahead, tokens, sym0)); + result = try!(__state3(lookahead, tokens, sym0)); } _ => { return Ok((lookahead, nt)); @@ -86,25 +86,144 @@ mod __parse__S { } // State 1 - // T = "Num" (*) [EOF] - // T = "Num" (*) ["-"] + // E = T (*) [EOF] + // E = T (*) ["-"] // - // EOF -> Reduce(T = "Num" => ActionFn(4);) - // "-" -> Reduce(T = "Num" => ActionFn(4);) + // EOF -> Reduce(E = T => ActionFn(3);) + // "-" -> Reduce(E = T => ActionFn(3);) // pub fn __state1>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + None => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::E(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::E(nt))); + } + _ => { + return Err(lookahead); + } + } + } + + // State 2 + // E = (*) E "-" T [")"] + // E = (*) E "-" T ["-"] + // E = (*) T [")"] + // E = (*) T ["-"] + // T = (*) "(" E ")" [")"] + // T = (*) "(" E ")" ["-"] + // T = "(" (*) E ")" [EOF] + // T = "(" (*) E ")" ["-"] + // T = (*) "Num" [")"] + // T = (*) "Num" ["-"] + // + // "Num" -> Shift(S7) + // "(" -> Shift(S6) + // + // T -> S9 + // E -> S8 + pub fn __state2>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { + Some(tok @ Tok::Num(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state7(lookahead, tokens, sym1)); + } + Some(tok @ Tok::LParen(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state6(lookahead, tokens, sym1)); + } + _ => { + return Err(lookahead); + } + } + while sym0.is_some() { + let (lookahead, nt) = result; + match nt { + __Nonterminal::T(nt) => { + let sym1 = &mut Some(nt); + result = try!(__state9(lookahead, tokens, sym1)); + } + __Nonterminal::E(nt) => { + let sym1 = &mut Some(nt); + result = try!(__state8(lookahead, tokens, sym0, sym1)); + } + _ => { + return Ok((lookahead, nt)); + } + } + } + return Ok(result); + } + + // State 3 + // E = E (*) "-" T [EOF] + // E = E (*) "-" T ["-"] + // S = E (*) [EOF] + // + // EOF -> Reduce(S = E => ActionFn(1);) + // "-" -> Shift(S10) + // + pub fn __state3>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Minus(..)) => { + let sym1 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state10(lookahead, tokens, sym0, sym1)); + } None => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action1(sym0); + return Ok((lookahead, __Nonterminal::S(nt))); + } + _ => { + return Err(lookahead); + } + } + return Ok(result); + } + + // State 4 + // T = "Num" (*) [EOF] + // T = "Num" (*) ["-"] + // + // "-" -> Reduce(T = "Num" => ActionFn(4);) + // EOF -> Reduce(T = "Num" => ActionFn(4);) + // + pub fn __state4>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); let nt = super::__action4(sym0); return Ok((lookahead, __Nonterminal::T(nt))); } - Some(Tok::Minus(..)) => { + None => { let sym0 = sym0.take().unwrap(); let nt = super::__action4(sym0); return Ok((lookahead, __Nonterminal::T(nt))); @@ -115,12 +234,12 @@ mod __parse__S { } } - // State 2 + // State 5 // __S = S (*) [EOF] // // EOF -> Reduce(__S = S => ActionFn(0);) // - pub fn __state2>( + pub fn __state5>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -138,235 +257,7 @@ mod __parse__S { } } - // State 3 - // E = T (*) [EOF] - // E = T (*) ["-"] - // - // EOF -> Reduce(E = T => ActionFn(3);) - // "-" -> Reduce(E = T => ActionFn(3);) - // - pub fn __state3>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - None => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::E(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::E(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 4 - // E = E (*) "-" T [EOF] - // E = E (*) "-" T ["-"] - // S = E (*) [EOF] - // - // "-" -> Shift(S6) - // EOF -> Reduce(S = E => ActionFn(1);) - // - pub fn __state4>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::Minus(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state6(lookahead, tokens, sym0, sym1)); - } - None => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action1(sym0); - return Ok((lookahead, __Nonterminal::S(nt))); - } - _ => { - return Err(lookahead); - } - } - return Ok(result); - } - - // State 5 - // E = (*) E "-" T [")"] - // E = (*) E "-" T ["-"] - // E = (*) T [")"] - // E = (*) T ["-"] - // T = (*) "(" E ")" [")"] - // T = (*) "(" E ")" ["-"] - // T = "(" (*) E ")" [EOF] - // T = "(" (*) E ")" ["-"] - // T = (*) "Num" [")"] - // T = (*) "Num" ["-"] - // - // "(" -> Shift(S9) - // "Num" -> Shift(S8) - // - // T -> S7 - // E -> S10 - pub fn __state5>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::LParen(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state9(lookahead, tokens, sym1)); - } - Some(tok @ Tok::Num(..)) => { - let sym1 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state8(lookahead, tokens, sym1)); - } - _ => { - return Err(lookahead); - } - } - while sym0.is_some() { - let (lookahead, nt) = result; - match nt { - __Nonterminal::T(nt) => { - let sym1 = &mut Some(nt); - result = try!(__state7(lookahead, tokens, sym1)); - } - __Nonterminal::E(nt) => { - let sym1 = &mut Some(nt); - result = try!(__state10(lookahead, tokens, sym0, sym1)); - } - _ => { - return Ok((lookahead, nt)); - } - } - } - return Ok(result); - } - // State 6 - // E = E "-" (*) T [EOF] - // E = E "-" (*) T ["-"] - // T = (*) "(" E ")" [EOF] - // T = (*) "(" E ")" ["-"] - // T = (*) "Num" [EOF] - // T = (*) "Num" ["-"] - // - // "Num" -> Shift(S1) - // "(" -> Shift(S5) - // - // T -> S11 - pub fn __state6>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state1(lookahead, tokens, sym2)); - } - Some(tok @ Tok::LParen(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state5(lookahead, tokens, sym2)); - } - _ => { - return Err(lookahead); - } - } - while sym1.is_some() { - let (lookahead, nt) = result; - match nt { - __Nonterminal::T(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state11(lookahead, tokens, sym0, sym1, sym2)); - } - _ => { - return Ok((lookahead, nt)); - } - } - } - return Ok(result); - } - - // State 7 - // E = T (*) [")"] - // E = T (*) ["-"] - // - // ")" -> Reduce(E = T => ActionFn(3);) - // "-" -> Reduce(E = T => ActionFn(3);) - // - pub fn __state7>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::E(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action3(sym0); - return Ok((lookahead, __Nonterminal::E(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 8 - // T = "Num" (*) [")"] - // T = "Num" (*) ["-"] - // - // ")" -> Reduce(T = "Num" => ActionFn(4);) - // "-" -> Reduce(T = "Num" => ActionFn(4);) - // - pub fn __state8>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(Tok::RParen(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action4(sym0); - return Ok((lookahead, __Nonterminal::T(nt))); - } - Some(Tok::Minus(..)) => { - let sym0 = sym0.take().unwrap(); - let nt = super::__action4(sym0); - return Ok((lookahead, __Nonterminal::T(nt))); - } - _ => { - return Err(lookahead); - } - } - } - - // State 9 // E = (*) E "-" T [")"] // E = (*) E "-" T ["-"] // E = (*) T [")"] @@ -378,12 +269,12 @@ mod __parse__S { // T = (*) "Num" [")"] // T = (*) "Num" ["-"] // - // "Num" -> Shift(S8) - // "(" -> Shift(S9) + // "Num" -> Shift(S7) + // "(" -> Shift(S6) // - // E -> S12 - // T -> S7 - pub fn __state9>( + // E -> S11 + // T -> S9 + pub fn __state6>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -393,12 +284,12 @@ mod __parse__S { Some(tok @ Tok::Num(..)) => { let sym1 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state8(lookahead, tokens, sym1)); + result = try!(__state7(lookahead, tokens, sym1)); } Some(tok @ Tok::LParen(..)) => { let sym1 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state9(lookahead, tokens, sym1)); + result = try!(__state6(lookahead, tokens, sym1)); } _ => { return Err(lookahead); @@ -409,11 +300,11 @@ mod __parse__S { match nt { __Nonterminal::E(nt) => { let sym1 = &mut Some(nt); - result = try!(__state12(lookahead, tokens, sym0, sym1)); + result = try!(__state11(lookahead, tokens, sym0, sym1)); } __Nonterminal::T(nt) => { let sym1 = &mut Some(nt); - result = try!(__state7(lookahead, tokens, sym1)); + result = try!(__state9(lookahead, tokens, sym1)); } _ => { return Ok((lookahead, nt)); @@ -423,16 +314,46 @@ mod __parse__S { return Ok(result); } - // State 10 + // State 7 + // T = "Num" (*) [")"] + // T = "Num" (*) ["-"] + // + // ")" -> Reduce(T = "Num" => ActionFn(4);) + // "-" -> Reduce(T = "Num" => ActionFn(4);) + // + pub fn __state7>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action4(sym0); + return Ok((lookahead, __Nonterminal::T(nt))); + } + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action4(sym0); + return Ok((lookahead, __Nonterminal::T(nt))); + } + _ => { + return Err(lookahead); + } + } + } + + // State 8 // E = E (*) "-" T [")"] // E = E (*) "-" T ["-"] // T = "(" E (*) ")" [EOF] // T = "(" E (*) ")" ["-"] // // "-" -> Shift(S13) - // ")" -> Shift(S14) + // ")" -> Shift(S12) // - pub fn __state10>( + pub fn __state8>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -448,7 +369,7 @@ mod __parse__S { Some(tok @ Tok::RParen(..)) => { let sym2 = &mut Some(tok); let lookahead = tokens.next(); - result = try!(__state14(lookahead, tokens, sym0, sym1, sym2)); + result = try!(__state12(lookahead, tokens, sym0, sym1, sym2)); } _ => { return Err(lookahead); @@ -457,34 +378,28 @@ mod __parse__S { return Ok(result); } - // State 11 - // E = E "-" T (*) [EOF] - // E = E "-" T (*) ["-"] + // State 9 + // E = T (*) [")"] + // E = T (*) ["-"] // - // EOF -> Reduce(E = E, "-", T => ActionFn(2);) - // "-" -> Reduce(E = E, "-", T => ActionFn(2);) + // "-" -> Reduce(E = T => ActionFn(3);) + // ")" -> Reduce(E = T => ActionFn(3);) // - pub fn __state11>( + pub fn __state9>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, - sym1: &mut Option, - sym2: &mut Option, ) -> Result<(Option, __Nonterminal), Option> { let mut result: (Option, __Nonterminal); match lookahead { - None => { - let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action2(sym0, sym1, sym2); - return Ok((lookahead, __Nonterminal::E(nt))); - } Some(Tok::Minus(..)) => { let sym0 = sym0.take().unwrap(); - let sym1 = sym1.take().unwrap(); - let sym2 = sym2.take().unwrap(); - let nt = super::__action2(sym0, sym1, sym2); + let nt = super::__action3(sym0); + return Ok((lookahead, __Nonterminal::E(nt))); + } + Some(Tok::RParen(..)) => { + let sym0 = sym0.take().unwrap(); + let nt = super::__action3(sym0); return Ok((lookahead, __Nonterminal::E(nt))); } _ => { @@ -493,7 +408,56 @@ mod __parse__S { } } - // State 12 + // State 10 + // E = E "-" (*) T [EOF] + // E = E "-" (*) T ["-"] + // T = (*) "(" E ")" [EOF] + // T = (*) "(" E ")" ["-"] + // T = (*) "Num" [EOF] + // T = (*) "Num" ["-"] + // + // "Num" -> Shift(S4) + // "(" -> Shift(S2) + // + // T -> S14 + pub fn __state10>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::Num(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state4(lookahead, tokens, sym2)); + } + Some(tok @ Tok::LParen(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state2(lookahead, tokens, sym2)); + } + _ => { + return Err(lookahead); + } + } + while sym1.is_some() { + let (lookahead, nt) = result; + match nt { + __Nonterminal::T(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state14(lookahead, tokens, sym0, sym1, sym2)); + } + _ => { + return Ok((lookahead, nt)); + } + } + } + return Ok(result); + } + + // State 11 // E = E (*) "-" T [")"] // E = E (*) "-" T ["-"] // T = "(" E (*) ")" [")"] @@ -502,7 +466,7 @@ mod __parse__S { // "-" -> Shift(S13) // ")" -> Shift(S15) // - pub fn __state12>( + pub fn __state11>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -527,63 +491,14 @@ mod __parse__S { return Ok(result); } - // State 13 - // E = E "-" (*) T [")"] - // E = E "-" (*) T ["-"] - // T = (*) "(" E ")" [")"] - // T = (*) "(" E ")" ["-"] - // T = (*) "Num" [")"] - // T = (*) "Num" ["-"] - // - // "(" -> Shift(S9) - // "Num" -> Shift(S8) - // - // T -> S16 - pub fn __state13>( - mut lookahead: Option, - tokens: &mut TOKENS, - sym0: &mut Option, - sym1: &mut Option, - ) -> Result<(Option, __Nonterminal), Option> { - let mut result: (Option, __Nonterminal); - match lookahead { - Some(tok @ Tok::LParen(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state9(lookahead, tokens, sym2)); - } - Some(tok @ Tok::Num(..)) => { - let sym2 = &mut Some(tok); - let lookahead = tokens.next(); - result = try!(__state8(lookahead, tokens, sym2)); - } - _ => { - return Err(lookahead); - } - } - while sym1.is_some() { - let (lookahead, nt) = result; - match nt { - __Nonterminal::T(nt) => { - let sym2 = &mut Some(nt); - result = try!(__state16(lookahead, tokens, sym0, sym1, sym2)); - } - _ => { - return Ok((lookahead, nt)); - } - } - } - return Ok(result); - } - - // State 14 + // State 12 // T = "(" E ")" (*) [EOF] // T = "(" E ")" (*) ["-"] // // "-" -> Reduce(T = "(", E, ")" => ActionFn(5);) // EOF -> Reduce(T = "(", E, ")" => ActionFn(5);) // - pub fn __state14>( + pub fn __state12>( mut lookahead: Option, tokens: &mut TOKENS, sym0: &mut Option, @@ -612,6 +527,91 @@ mod __parse__S { } } + // State 13 + // E = E "-" (*) T [")"] + // E = E "-" (*) T ["-"] + // T = (*) "(" E ")" [")"] + // T = (*) "(" E ")" ["-"] + // T = (*) "Num" [")"] + // T = (*) "Num" ["-"] + // + // "(" -> Shift(S6) + // "Num" -> Shift(S7) + // + // T -> S16 + pub fn __state13>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(tok @ Tok::LParen(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state6(lookahead, tokens, sym2)); + } + Some(tok @ Tok::Num(..)) => { + let sym2 = &mut Some(tok); + let lookahead = tokens.next(); + result = try!(__state7(lookahead, tokens, sym2)); + } + _ => { + return Err(lookahead); + } + } + while sym1.is_some() { + let (lookahead, nt) = result; + match nt { + __Nonterminal::T(nt) => { + let sym2 = &mut Some(nt); + result = try!(__state16(lookahead, tokens, sym0, sym1, sym2)); + } + _ => { + return Ok((lookahead, nt)); + } + } + } + return Ok(result); + } + + // State 14 + // E = E "-" T (*) [EOF] + // E = E "-" T (*) ["-"] + // + // "-" -> Reduce(E = E, "-", T => ActionFn(2);) + // EOF -> Reduce(E = E, "-", T => ActionFn(2);) + // + pub fn __state14>( + mut lookahead: Option, + tokens: &mut TOKENS, + sym0: &mut Option, + sym1: &mut Option, + sym2: &mut Option, + ) -> Result<(Option, __Nonterminal), Option> { + let mut result: (Option, __Nonterminal); + match lookahead { + Some(Tok::Minus(..)) => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action2(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::E(nt))); + } + None => { + let sym0 = sym0.take().unwrap(); + let sym1 = sym1.take().unwrap(); + let sym2 = sym2.take().unwrap(); + let nt = super::__action2(sym0, sym1, sym2); + return Ok((lookahead, __Nonterminal::E(nt))); + } + _ => { + return Err(lookahead); + } + } + } + // State 15 // T = "(" E ")" (*) [")"] // T = "(" E ")" (*) ["-"] diff --git a/lalrpop/src/build/filetext.rs b/lalrpop/src/build/filetext.rs index da6d4b3..849ed80 100644 --- a/lalrpop/src/build/filetext.rs +++ b/lalrpop/src/build/filetext.rs @@ -1,4 +1,5 @@ use grammar::parse_tree as pt; +use std::fmt::{Display, Formatter, Error}; use std::fs::File; use std::path::PathBuf; use std::io::{self, Read, Write}; @@ -18,12 +19,16 @@ impl FileText { } fn new(path: PathBuf, input_str: String) -> FileText { - let newline_indices: Vec = - input_str.as_bytes().iter() - .enumerate() - .filter(|&(_, &b)| b == ('\n' as u8)) - .map(|(i, _)| i + 1) // store the first character in the line - .collect(); + let newline_indices: Vec = { + let input_indices = + input_str.as_bytes().iter() + .enumerate() + .filter(|&(_, &b)| b == ('\n' as u8)) + .map(|(i, _)| i + 1); // index of first char in the line + Some(0).into_iter() + .chain(input_indices) + .collect() + }; FileText { path: path, input_str: input_str, newlines: newline_indices } } @@ -65,7 +70,7 @@ impl FileText { &self.input_str[start_offset..] } else { let end_offset = self.newlines[line_num + 1]; - &self.input_str[start_offset..end_offset] + &self.input_str[start_offset..end_offset-1] } } @@ -82,24 +87,38 @@ impl FileText { try!(writeln!(out, " {}", text)); if end_col - start_col <= 1 { - try!(writeln!(out, " {0:1$}^", "", start_col)); + try!(writeln!(out, " {}^", Repeat(' ', start_col))); } else { let width = end_col - start_col; - try!(writeln!(out, " {0:1$}|{0:-2$}|", - "", start_col, width.saturating_sub(2))); + try!(writeln!(out, " {}~{}~", + Repeat(' ', start_col), + Repeat('~', width.saturating_sub(2)))); } } else { // span is across many lines, find the maximal width of any of those let line_strs: Vec<_> = (start_line..end_line+1).map(|i| self.line_text(i)).collect(); let max_len = line_strs.iter().map(|l| l.len()).max().unwrap(); - try!(writeln!(out, " {0:1$}|{0:-2$}-+", "", start_col, max_len - start_col)); + try!(writeln!(out, " {}{}~+", + Repeat(' ', start_col), + Repeat('~', max_len - start_col))); for line in &line_strs[..line_strs.len()-1] { try!(writeln!(out, "| {0:<1$} |", line, max_len)); } try!(writeln!(out, "| {}", line_strs[line_strs.len()-1])); - try!(writeln!(out, "+-{0:-1$}", "", end_col)); + try!(writeln!(out, "+~{}", Repeat('~', end_col))); } Ok(()) } } + +struct Repeat(char, usize); + +impl Display for Repeat { + fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { + for _ in 0..self.1 { + try!(write!(fmt, "{}", self.0)); + } + Ok(()) + } +} diff --git a/lalrpop/src/lr1/test.rs b/lalrpop/src/lr1/test.rs index 408fa1c..9ec2454 100644 --- a/lalrpop/src/lr1/test.rs +++ b/lalrpop/src/lr1/test.rs @@ -97,7 +97,7 @@ grammar { fn expr_grammar1() { let grammar = normalized_grammar(r#" grammar { - token Tok where { }; + token Tok where { "-" => "Sub" }; S: () = E => (); diff --git a/lalrpop/src/normalize/mod.rs b/lalrpop/src/normalize/mod.rs index 7c7d356..a7dd45b 100644 --- a/lalrpop/src/normalize/mod.rs +++ b/lalrpop/src/normalize/mod.rs @@ -25,6 +25,11 @@ macro_rules! return_err { } pub fn normalize(grammar: pt::Grammar) -> NormResult { + try!(validate::validate(&grammar)); + normalize_without_validating(grammar) +} + +pub fn normalize_without_validating(grammar: pt::Grammar) -> NormResult { let grammar = try!(macro_expand::expand_macros(grammar)); let types = try!(tyinfer::infer_types(&grammar)); lower::lower(grammar, types) diff --git a/lalrpop/src/normalize/tyinfer/mod.rs b/lalrpop/src/normalize/tyinfer/mod.rs index 3dba638..96a34dc 100644 --- a/lalrpop/src/normalize/tyinfer/mod.rs +++ b/lalrpop/src/normalize/tyinfer/mod.rs @@ -4,7 +4,7 @@ use super::norm_util::{self, AlternativeAction, Symbols}; use std::collections::{HashMap}; use grammar::parse_tree::{Alternative, Grammar, GrammarItem, NonterminalData, NonterminalString, - Span, Symbol, SymbolKind, TypeRef}; + Span, SymbolKind, TypeRef}; use grammar::repr::{Types, TypeRepr}; #[cfg(test)] diff --git a/lalrpop/src/normalize/validate/mod.rs b/lalrpop/src/normalize/validate/mod.rs index 256d149..4dd61fb 100644 --- a/lalrpop/src/normalize/validate/mod.rs +++ b/lalrpop/src/normalize/validate/mod.rs @@ -45,12 +45,13 @@ struct Validator<'grammar> { conversions: Set, } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] enum Def { TopLevel(usize), // argument is the number of macro arguments MacroArg } +#[derive(Debug)] struct ScopeChain<'scope> { previous: Option<&'scope ScopeChain<'scope>>, nonterminals: Map, @@ -194,10 +195,10 @@ impl<'grammar> Validator<'grammar> { } } SymbolKind::Repeat(ref repeat) => { - self.validate_symbol(scope, &repeat.symbol); + try!(self.validate_symbol(scope, &repeat.symbol)); } SymbolKind::Choose(ref sym) | SymbolKind::Name(_, ref sym) => { - self.validate_symbol(scope, sym); + try!(self.validate_symbol(scope, sym)); } } @@ -235,7 +236,7 @@ impl<'grammar> Validator<'grammar> { nt: NonterminalString) -> NormResult { match scope.def(nt) { - Some(arity) => Ok(arity), + Some(def) => Ok(def), None => return_err!(span, "no definition found for nonterminal `{}`", nt) } } diff --git a/lalrpop/src/normalize/validate/test.rs b/lalrpop/src/normalize/validate/test.rs index d561f01..24ac512 100644 --- a/lalrpop/src/normalize/validate/test.rs +++ b/lalrpop/src/normalize/validate/test.rs @@ -45,3 +45,10 @@ fn repeated_macro_arg() { "multiple macro arguments declared with the name `Y`", r#"grammar { >>>X <<<= "foo"; }"#); } + +#[test] +fn unknown_nonterminal_two() { + check_err( + "no definition found for nonterminal `Expr`", + r#"grammar { Term = { n:"Num" => n.as_num(); "A" ~>>>Expr <<<"B"; }; }"#); +} diff --git a/lalrpop/src/test_util.rs b/lalrpop/src/test_util.rs index b3d440a..4cb1001 100644 --- a/lalrpop/src/test_util.rs +++ b/lalrpop/src/test_util.rs @@ -46,5 +46,5 @@ pub fn compare(actual: D, expected: E) { } pub fn normalized_grammar(s: &str) -> r::Grammar { - ::normalize::normalize(::parser::parse_grammar(s).unwrap()).unwrap() + ::normalize::normalize_without_validating(::parser::parse_grammar(s).unwrap()).unwrap() }