move the action code back into the parent module so that super::

resolves correctly (as I originally intended, but forgot) and
add a test
This commit is contained in:
Niko Matsakis 2015-07-20 05:21:12 -04:00
parent 0ec6824eeb
commit ae37f374cc
8 changed files with 4766 additions and 4769 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -22,11 +22,11 @@ mod __parse__Items {
use util::tok::Tok;
pub enum __Nonterminal<> {
____Items(Vec<(Option<usize>, Option<usize>)>),
Items(Vec<(Option<usize>, Option<usize>)>),
_40_3e(::std::option::Option<usize>),
Spanned_3c_22_2b_22_3e((Option<usize>, Option<usize>)),
_40_3c(::std::option::Option<usize>),
____Items(Vec<(Option<usize>, Option<usize>)>),
Spanned_3c_22_2b_22_3e((Option<usize>, Option<usize>)),
}
// State 0
@ -41,9 +41,9 @@ mod __parse__Items {
// Items = (*) Items "-" ["-"]
// __Items = (*) Items [EOF]
//
// "-" -> Reduce(Items = => Call(ActionFn(1));)
// "+" -> Reduce(Items = => Call(ActionFn(1));)
// EOF -> Reduce(Items = => Call(ActionFn(1));)
// "-" -> Reduce(Items = => Call(ActionFn(1));)
//
// Items -> S1
pub fn __state0<
@ -56,16 +56,16 @@ mod __parse__Items {
{
let mut __result: (Option<usize>, Option<(usize,Tok,usize)>, __Nonterminal<>);
match __lookahead {
Some((_, Tok::Minus(..), _)) => {
let __nt = super::__action1();
__result = (__lookbehind, __lookahead, __Nonterminal::Items(__nt));
}
Some((_, Tok::Plus(..), _)) => {
let __nt = super::__actions::__action1();
let __nt = super::__action1();
__result = (__lookbehind, __lookahead, __Nonterminal::Items(__nt));
}
None => {
let __nt = super::__actions::__action1();
__result = (__lookbehind, __lookahead, __Nonterminal::Items(__nt));
}
Some((_, Tok::Minus(..), _)) => {
let __nt = super::__actions::__action1();
let __nt = super::__action1();
__result = (__lookbehind, __lookahead, __Nonterminal::Items(__nt));
}
_ => {
@ -99,12 +99,12 @@ mod __parse__Items {
// Spanned<"+"> = (*) @< "+" @> ["-"]
// __Items = Items (*) [EOF]
//
// "+" -> Reduce(@< = => Lookahead;)
// EOF -> Reduce(__Items = Items => Call(ActionFn(0));)
// "-" -> Shift(S2)
// "+" -> Reduce(@< = => Lookahead;)
//
// @< -> S3
// Spanned<"+"> -> S4
// @< -> S3
pub fn __state1<
__TOKENS: Iterator<Item=(usize,Tok,usize)>,
>(
@ -122,15 +122,15 @@ mod __parse__Items {
let __lookahead = __tokens.next();
__result = try!(__state2(__lookbehind, __lookahead, __tokens, __sym0, __sym1));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action0(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::____Items(__nt)));
}
Some((_, Tok::Plus(..), _)) => {
let __nt = __lookahead.as_ref().map(|o| ::std::clone::Clone::clone(&o.0));
__result = (__lookbehind, __lookahead, __Nonterminal::_40_3c(__nt));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action0(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::____Items(__nt)));
}
_ => {
return Err(__lookahead);
}
@ -138,14 +138,14 @@ mod __parse__Items {
while __sym0.is_some() {
let (__lookbehind, __lookahead, __nt) = __result;
match __nt {
__Nonterminal::_40_3c(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state3(__lookbehind, __lookahead, __tokens, __sym1));
}
__Nonterminal::Spanned_3c_22_2b_22_3e(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state4(__lookbehind, __lookahead, __tokens, __sym0, __sym1));
}
__Nonterminal::_40_3c(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state3(__lookbehind, __lookahead, __tokens, __sym1));
}
_ => {
return Ok((__lookbehind, __lookahead, __nt));
}
@ -178,19 +178,19 @@ mod __parse__Items {
Some((_, Tok::Minus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__actions::__action3(__sym0, __sym1);
let __nt = super::__action3(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
Some((_, Tok::Plus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__actions::__action3(__sym0, __sym1);
let __nt = super::__action3(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__actions::__action3(__sym0, __sym1);
let __nt = super::__action3(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
_ => {
@ -236,8 +236,8 @@ mod __parse__Items {
// Items = Items Spanned<"+"> (*) ["-"]
//
// EOF -> Reduce(Items = Items, Spanned<"+"> => Call(ActionFn(2));)
// "+" -> Reduce(Items = Items, Spanned<"+"> => Call(ActionFn(2));)
// "-" -> Reduce(Items = Items, Spanned<"+"> => Call(ActionFn(2));)
// "+" -> Reduce(Items = Items, Spanned<"+"> => Call(ActionFn(2));)
//
pub fn __state4<
__TOKENS: Iterator<Item=(usize,Tok,usize)>,
@ -254,19 +254,19 @@ mod __parse__Items {
None => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
Some((_, Tok::Plus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1);
let __nt = super::__action2(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
Some((_, Tok::Minus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1);
let __nt = super::__action2(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
Some((_, Tok::Plus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __nt = super::__action2(__sym0, __sym1);
return Ok((__lookbehind, __lookahead, __Nonterminal::Items(__nt)));
}
_ => {
@ -283,9 +283,9 @@ mod __parse__Items {
// Spanned<"+"> = @< "+" (*) @> ["+"]
// Spanned<"+"> = @< "+" (*) @> ["-"]
//
// "-" -> Reduce(@> = => Lookbehind;)
// "+" -> Reduce(@> = => Lookbehind;)
// EOF -> Reduce(@> = => Lookbehind;)
// "+" -> Reduce(@> = => Lookbehind;)
// "-" -> Reduce(@> = => Lookbehind;)
//
// @> -> S6
pub fn __state5<
@ -300,7 +300,7 @@ mod __parse__Items {
{
let mut __result: (Option<usize>, Option<(usize,Tok,usize)>, __Nonterminal<>);
match __lookahead {
Some((_, Tok::Minus(..), _)) => {
None => {
let __nt = ::std::clone::Clone::clone(&__lookbehind);
__result = (__lookbehind, __lookahead, __Nonterminal::_40_3e(__nt));
}
@ -308,7 +308,7 @@ mod __parse__Items {
let __nt = ::std::clone::Clone::clone(&__lookbehind);
__result = (__lookbehind, __lookahead, __Nonterminal::_40_3e(__nt));
}
None => {
Some((_, Tok::Minus(..), _)) => {
let __nt = ::std::clone::Clone::clone(&__lookbehind);
__result = (__lookbehind, __lookahead, __Nonterminal::_40_3e(__nt));
}
@ -336,9 +336,9 @@ mod __parse__Items {
// Spanned<"+"> = @< "+" @> (*) ["+"]
// Spanned<"+"> = @< "+" @> (*) ["-"]
//
// "-" -> Reduce(Spanned<"+"> = @<, "+", @> => Call(ActionFn(4));)
// "+" -> Reduce(Spanned<"+"> = @<, "+", @> => Call(ActionFn(4));)
// EOF -> Reduce(Spanned<"+"> = @<, "+", @> => Call(ActionFn(4));)
// "-" -> Reduce(Spanned<"+"> = @<, "+", @> => Call(ActionFn(4));)
//
pub fn __state6<
__TOKENS: Iterator<Item=(usize,Tok,usize)>,
@ -353,25 +353,25 @@ mod __parse__Items {
{
let mut __result: (Option<usize>, Option<(usize,Tok,usize)>, __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((__lookbehind, __lookahead, __Nonterminal::Spanned_3c_22_2b_22_3e(__nt)));
}
Some((_, Tok::Plus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action4(__sym0, __sym1, __sym2);
let __nt = super::__action4(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::Spanned_3c_22_2b_22_3e(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action4(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::Spanned_3c_22_2b_22_3e(__nt)));
}
Some((_, Tok::Minus(..), _)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action4(__sym0, __sym1, __sym2);
let __nt = super::__action4(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::Spanned_3c_22_2b_22_3e(__nt)));
}
_ => {
@ -381,54 +381,49 @@ mod __parse__Items {
}
}
mod __actions {
use util::tok::Tok;
pub fn __action0<
>(
__0: Vec<(Option<usize>, Option<usize>)>,
) -> Vec<(Option<usize>, Option<usize>)>
{
(__0)
}
pub fn __action1<
>(
) -> Vec<(Option<usize>, Option<usize>)>
{
vec![]
}
pub fn __action0<
>(
__0: Vec<(Option<usize>, Option<usize>)>,
) -> Vec<(Option<usize>, Option<usize>)>
pub fn __action2<
>(
v: Vec<(Option<usize>, Option<usize>)>,
e: (Option<usize>, Option<usize>),
) -> Vec<(Option<usize>, Option<usize>)>
{
{
(__0)
}
pub fn __action1<
>(
) -> Vec<(Option<usize>, Option<usize>)>
{
vec![]
}
pub fn __action2<
>(
v: Vec<(Option<usize>, Option<usize>)>,
e: (Option<usize>, Option<usize>),
) -> Vec<(Option<usize>, Option<usize>)>
{
{
let mut v = v;
v.push(e);
v
}
}
pub fn __action3<
>(
v: Vec<(Option<usize>, Option<usize>)>,
_: Tok,
) -> Vec<(Option<usize>, Option<usize>)>
{
v
}
pub fn __action4<
>(
__0: ::std::option::Option<usize>,
_: Tok,
__1: ::std::option::Option<usize>,
) -> (Option<usize>, Option<usize>)
{
(__0, __1)
}
}
pub fn __action3<
>(
v: Vec<(Option<usize>, Option<usize>)>,
_: Tok,
) -> Vec<(Option<usize>, Option<usize>)>
{
v
}
pub fn __action4<
>(
__0: ::std::option::Option<usize>,
_: Tok,
__1: ::std::option::Option<usize>,
) -> (Option<usize>, Option<usize>)
{
(__0, __1)
}

View File

@ -5,6 +5,10 @@ mod loc;
mod sub;
mod util;
/// This constant is here so that some of the generator parsers can
/// refer to it in order to test `super::` handling in action code.
const ZERO: i32 = 0;
#[test]
fn expr_test1() {
util::test(|v| expr::parse_Expr(1, v), "22 - 3", 22 - 3);

View File

@ -13,10 +13,10 @@ extern token {
pub S = E;
E = {
<l:E> "-" <r:T> => l-r;
E: i32 = {
<l:E> "-" <r:T> => l - r;
T;
<t:T> => t - super::ZERO;
};
T = {

View File

@ -22,10 +22,10 @@ mod __parse__S {
use util::tok::Tok;
pub enum __Nonterminal<> {
E(i32),
____S(i32),
S(i32),
T(i32),
____S(i32),
E(i32),
}
// State 0
@ -40,12 +40,12 @@ mod __parse__S {
// T = (*) "Num" ["-"]
// __S = (*) S [EOF]
//
// "Num" -> Shift(S2)
// "Num" -> Shift(S5)
// "(" -> Shift(S4)
//
// S -> S3
// E -> S1
// T -> S5
// S -> S2
// T -> S1
// E -> S3
pub fn __state0<
__TOKENS: Iterator<Item=Tok>,
>(
@ -60,7 +60,7 @@ mod __parse__S {
let mut __lookbehind = None;
let mut __sym0 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state2(__lookbehind, __lookahead, __tokens, __sym0));
__result = try!(__state5(__lookbehind, __lookahead, __tokens, __sym0));
}
Some(__tok @ Tok::LParen(..)) => {
let mut __lookbehind = None;
@ -77,15 +77,15 @@ mod __parse__S {
match __nt {
__Nonterminal::S(__nt) => {
let __sym0 = &mut Some(__nt);
__result = try!(__state3(__lookbehind, __lookahead, __tokens, __sym0));
}
__Nonterminal::E(__nt) => {
let __sym0 = &mut Some(__nt);
__result = try!(__state1(__lookbehind, __lookahead, __tokens, __sym0));
__result = try!(__state2(__lookbehind, __lookahead, __tokens, __sym0));
}
__Nonterminal::T(__nt) => {
let __sym0 = &mut Some(__nt);
__result = try!(__state5(__lookbehind, __lookahead, __tokens, __sym0));
__result = try!(__state1(__lookbehind, __lookahead, __tokens, __sym0));
}
__Nonterminal::E(__nt) => {
let __sym0 = &mut Some(__nt);
__result = try!(__state3(__lookbehind, __lookahead, __tokens, __sym0));
}
_ => {
return Ok((__lookbehind, __lookahead, __nt));
@ -95,14 +95,75 @@ mod __parse__S {
}
// State 1
// E = T (*) [EOF]
// E = T (*) ["-"]
//
// "-" -> Reduce(E = T => Call(ActionFn(3));)
// EOF -> Reduce(E = T => Call(ActionFn(3));)
//
pub fn __state1<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 2
// __S = S (*) [EOF]
//
// EOF -> Reduce(__S = S => Call(ActionFn(0));)
//
pub fn __state2<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action0(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::____S(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 3
// E = E (*) "-" T [EOF]
// E = E (*) "-" T ["-"]
// S = E (*) [EOF]
//
// "-" -> Shift(S6)
// EOF -> Reduce(S = E => Call(ActionFn(1));)
// "-" -> Shift(S6)
//
pub fn __state1<
pub fn __state3<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
@ -121,7 +182,7 @@ mod __parse__S {
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action1(__sym0);
let __nt = super::__action1(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::S(__nt)));
}
_ => {
@ -131,67 +192,6 @@ mod __parse__S {
return Ok(__result);
}
// State 2
// T = "Num" (*) [EOF]
// T = "Num" (*) ["-"]
//
// "-" -> Reduce(T = "Num" => Call(ActionFn(4));)
// EOF -> Reduce(T = "Num" => Call(ActionFn(4));)
//
pub fn __state2<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 3
// __S = S (*) [EOF]
//
// EOF -> Reduce(__S = S => Call(ActionFn(0));)
//
pub fn __state3<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action0(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::____S(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 4
// E = (*) E "-" T [")"]
// E = (*) E "-" T ["-"]
@ -204,11 +204,11 @@ mod __parse__S {
// T = (*) "Num" [")"]
// T = (*) "Num" ["-"]
//
// "Num" -> Shift(S9)
// "(" -> Shift(S10)
// "Num" -> Shift(S10)
// "(" -> Shift(S9)
//
// E -> S7
// T -> S8
// E -> S8
// T -> S7
pub fn __state4<
__TOKENS: Iterator<Item=Tok>,
>(
@ -224,13 +224,13 @@ mod __parse__S {
let mut __lookbehind = None;
let mut __sym1 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state9(__lookbehind, __lookahead, __tokens, __sym1));
__result = try!(__state10(__lookbehind, __lookahead, __tokens, __sym1));
}
Some(__tok @ Tok::LParen(..)) => {
let mut __lookbehind = None;
let mut __sym1 = &mut Some((__tok));
let __lookahead = __tokens.next();
__result = try!(__state10(__lookbehind, __lookahead, __tokens, __sym1));
__result = try!(__state9(__lookbehind, __lookahead, __tokens, __sym1));
}
_ => {
return Err(__lookahead);
@ -241,11 +241,11 @@ mod __parse__S {
match __nt {
__Nonterminal::E(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state7(__lookbehind, __lookahead, __tokens, __sym0, __sym1));
__result = try!(__state8(__lookbehind, __lookahead, __tokens, __sym0, __sym1));
}
__Nonterminal::T(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state8(__lookbehind, __lookahead, __tokens, __sym1));
__result = try!(__state7(__lookbehind, __lookahead, __tokens, __sym1));
}
_ => {
return Ok((__lookbehind, __lookahead, __nt));
@ -256,11 +256,11 @@ mod __parse__S {
}
// State 5
// E = T (*) [EOF]
// E = T (*) ["-"]
// T = "Num" (*) [EOF]
// T = "Num" (*) ["-"]
//
// "-" -> Reduce(E = T => Call(ActionFn(3));)
// EOF -> Reduce(E = T => Call(ActionFn(3));)
// EOF -> Reduce(T = "Num" => Call(ActionFn(4));)
// "-" -> Reduce(T = "Num" => Call(ActionFn(4));)
//
pub fn __state5<
__TOKENS: Iterator<Item=Tok>,
@ -273,15 +273,15 @@ mod __parse__S {
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
let __nt = super::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
_ => {
return Err(__lookahead);
@ -298,7 +298,7 @@ mod __parse__S {
// T = (*) "Num" ["-"]
//
// "(" -> Shift(S4)
// "Num" -> Shift(S2)
// "Num" -> Shift(S5)
//
// T -> S11
pub fn __state6<
@ -323,7 +323,7 @@ mod __parse__S {
let mut __lookbehind = None;
let mut __sym2 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state2(__lookbehind, __lookahead, __tokens, __sym2));
__result = try!(__state5(__lookbehind, __lookahead, __tokens, __sym2));
}
_ => {
return Err(__lookahead);
@ -345,6 +345,40 @@ mod __parse__S {
}
// State 7
// E = T (*) [")"]
// E = T (*) ["-"]
//
// "-" -> Reduce(E = T => Call(ActionFn(3));)
// ")" -> Reduce(E = T => Call(ActionFn(3));)
//
pub fn __state7<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 8
// E = E (*) "-" T [")"]
// E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [EOF]
@ -353,7 +387,7 @@ mod __parse__S {
// "-" -> Shift(S12)
// ")" -> Shift(S13)
//
pub fn __state7<
pub fn __state8<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
@ -384,75 +418,7 @@ mod __parse__S {
return Ok(__result);
}
// State 8
// E = T (*) [")"]
// E = T (*) ["-"]
//
// "-" -> Reduce(E = T => Call(ActionFn(3));)
// ")" -> Reduce(E = T => Call(ActionFn(3));)
//
pub fn __state8<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action3(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 9
// T = "Num" (*) [")"]
// T = "Num" (*) ["-"]
//
// ")" -> Reduce(T = "Num" => Call(ActionFn(4));)
// "-" -> Reduce(T = "Num" => Call(ActionFn(4));)
//
pub fn __state9<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__actions::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 10
// E = (*) E "-" T [")"]
// E = (*) E "-" T ["-"]
// E = (*) T [")"]
@ -464,12 +430,12 @@ mod __parse__S {
// T = (*) "Num" [")"]
// T = (*) "Num" ["-"]
//
// "(" -> Shift(S10)
// "Num" -> Shift(S9)
// "(" -> Shift(S9)
// "Num" -> Shift(S10)
//
// T -> S8
// E -> S14
pub fn __state10<
// T -> S7
pub fn __state9<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
@ -484,13 +450,13 @@ mod __parse__S {
let mut __lookbehind = None;
let mut __sym1 = &mut Some((__tok));
let __lookahead = __tokens.next();
__result = try!(__state10(__lookbehind, __lookahead, __tokens, __sym1));
__result = try!(__state9(__lookbehind, __lookahead, __tokens, __sym1));
}
Some(Tok::Num(__tok0)) => {
let mut __lookbehind = None;
let mut __sym1 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state9(__lookbehind, __lookahead, __tokens, __sym1));
__result = try!(__state10(__lookbehind, __lookahead, __tokens, __sym1));
}
_ => {
return Err(__lookahead);
@ -499,14 +465,14 @@ mod __parse__S {
while __sym0.is_some() {
let (__lookbehind, __lookahead, __nt) = __result;
match __nt {
__Nonterminal::T(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state8(__lookbehind, __lookahead, __tokens, __sym1));
}
__Nonterminal::E(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state14(__lookbehind, __lookahead, __tokens, __sym0, __sym1));
}
__Nonterminal::T(__nt) => {
let __sym1 = &mut Some(__nt);
__result = try!(__state7(__lookbehind, __lookahead, __tokens, __sym1));
}
_ => {
return Ok((__lookbehind, __lookahead, __nt));
}
@ -515,6 +481,40 @@ mod __parse__S {
return Ok(__result);
}
// State 10
// T = "Num" (*) [")"]
// T = "Num" (*) ["-"]
//
// "-" -> Reduce(T = "Num" => Call(ActionFn(4));)
// ")" -> Reduce(T = "Num" => Call(ActionFn(4));)
//
pub fn __state10<
__TOKENS: Iterator<Item=Tok>,
>(
__lookbehind: Option<()>,
__lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<()>, Option<Tok>, __Nonterminal<>), Option<Tok>>
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action4(__sym0);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 11
// E = E "-" T (*) [EOF]
// E = E "-" T (*) ["-"]
@ -539,14 +539,14 @@ mod __parse__S {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1, __sym2);
let __nt = super::__action2(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1, __sym2);
let __nt = super::__action2(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
_ => {
@ -563,8 +563,8 @@ mod __parse__S {
// T = (*) "Num" [")"]
// T = (*) "Num" ["-"]
//
// "Num" -> Shift(S9)
// "(" -> Shift(S10)
// "Num" -> Shift(S10)
// "(" -> Shift(S9)
//
// T -> S15
pub fn __state12<
@ -583,13 +583,13 @@ mod __parse__S {
let mut __lookbehind = None;
let mut __sym2 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state9(__lookbehind, __lookahead, __tokens, __sym2));
__result = try!(__state10(__lookbehind, __lookahead, __tokens, __sym2));
}
Some(__tok @ Tok::LParen(..)) => {
let mut __lookbehind = None;
let mut __sym2 = &mut Some((__tok));
let __lookahead = __tokens.next();
__result = try!(__state10(__lookbehind, __lookahead, __tokens, __sym2));
__result = try!(__state9(__lookbehind, __lookahead, __tokens, __sym2));
}
_ => {
return Err(__lookahead);
@ -634,14 +634,14 @@ mod __parse__S {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action5(__sym0, __sym1, __sym2);
let __nt = super::__action5(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action5(__sym0, __sym1, __sym2);
let __nt = super::__action5(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
_ => {
@ -694,8 +694,8 @@ mod __parse__S {
// E = E "-" T (*) [")"]
// E = E "-" T (*) ["-"]
//
// "-" -> Reduce(E = E, "-", T => Call(ActionFn(2));)
// ")" -> Reduce(E = E, "-", T => Call(ActionFn(2));)
// "-" -> Reduce(E = E, "-", T => Call(ActionFn(2));)
//
pub fn __state15<
__TOKENS: Iterator<Item=Tok>,
@ -710,18 +710,18 @@ mod __parse__S {
{
let mut __result: (Option<()>, Option<Tok>, __Nonterminal<>);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action2(__sym0, __sym1, __sym2);
let __nt = super::__action2(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __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);
return Ok((__lookbehind, __lookahead, __Nonterminal::E(__nt)));
}
_ => {
@ -754,14 +754,14 @@ mod __parse__S {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action5(__sym0, __sym1, __sym2);
let __nt = super::__action5(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap();
let __nt = super::__actions::__action5(__sym0, __sym1, __sym2);
let __nt = super::__action5(__sym0, __sym1, __sym2);
return Ok((__lookbehind, __lookahead, __Nonterminal::T(__nt)));
}
_ => {
@ -771,59 +771,54 @@ mod __parse__S {
}
}
mod __actions {
use util::tok::Tok;
pub fn __action0<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action1<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action2<
>(
l: i32,
_: Tok,
r: i32,
) -> i32
{
l-r
}
pub fn __action3<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action4<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action5<
>(
_: Tok,
__0: i32,
_: Tok,
) -> i32
{
(__0)
}
pub fn __action0<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action1<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action2<
>(
l: i32,
_: Tok,
r: i32,
) -> i32
{
l - r
}
pub fn __action3<
>(
t: i32,
) -> i32
{
t - super::ZERO
}
pub fn __action4<
>(
__0: i32,
) -> i32
{
(__0)
}
pub fn __action5<
>(
_: Tok,
__0: i32,
_: Tok,
) -> i32
{
(__0)
}

View File

@ -95,12 +95,32 @@ fn emit_uses<W:Write>(grammar: &r::Grammar,
Ok(())
}
fn emit_action_code<W:Write>(grammar: &r::Grammar,
rust: &mut RustWrite<W>)
-> io::Result<()>
fn emit_recursive_ascent(output_path: &Path, grammar: &r::Grammar) -> io::Result<()>
{
rust!(rust, "");
rust!(rust, "mod {}actions {{", grammar.prefix);
let output_file = try!(fs::File::create(output_path));
let mut rust = RustWrite::new(output_file);
// We generate a module structure like this:
//
// ```
// mod <output-file> {
// // For each public symbol:
// pub fn parse_XYZ();
// mod __XYZ { ... }
//
// // For each bit of action code:
// <action-code>
// }
// ```
//
// Note that the action code goes in the outer module. This is
// intentional because it means that the foo.lalrpop file serves
// as a module in the rust hierarchy, so if the action code
// includes things like `super::` it will resolve in the natural
// way.
// often some of the uses are not used here
rust!(rust, "#![allow(unused_imports)]");
// we always thread the parameters through to the action code,
// even if they are not used, and hence we need to disable the
@ -109,35 +129,6 @@ fn emit_action_code<W:Write>(grammar: &r::Grammar,
rust!(rust, "#![allow(unused_variables)]");
}
try!(emit_uses(grammar, rust));
for (i, defn) in grammar.action_fn_defns.iter().enumerate() {
rust!(rust, "");
try!(rust.write_pub_fn_header(
grammar,
format!("{}action{}", grammar.prefix, i),
vec![],
defn.arg_patterns.iter()
.zip(defn.arg_types.iter())
.map(|(p, t)| format!("{}: {}", p, t))
.collect(),
format!("{}", defn.ret_type),
vec![]));
rust!(rust, "{{");
rust!(rust, "{}", defn.code);
rust!(rust, "}}");
}
rust!(rust, "}}");
Ok(())
}
fn emit_recursive_ascent(output_path: &Path, grammar: &r::Grammar) -> io::Result<()>
{
let output_file = try!(fs::File::create(output_path));
let mut rust = RustWrite::new(output_file);
// often some of the uses are not used here
rust!(rust, "#![allow(unused_imports)]");
try!(emit_uses(grammar, &mut rust));
if grammar.start_nonterminals.is_empty() {
@ -165,3 +156,27 @@ fn emit_recursive_ascent(output_path: &Path, grammar: &r::Grammar) -> io::Result
try!(emit_action_code(grammar, &mut rust));
Ok(())
}
fn emit_action_code<W:Write>(grammar: &r::Grammar,
rust: &mut RustWrite<W>)
-> io::Result<()>
{
for (i, defn) in grammar.action_fn_defns.iter().enumerate() {
rust!(rust, "");
try!(rust.write_pub_fn_header(
grammar,
format!("{}action{}", grammar.prefix, i),
vec![],
defn.arg_patterns.iter()
.zip(defn.arg_types.iter())
.map(|(p, t)| format!("{}: {}", p, t))
.collect(),
format!("{}", defn.ret_type),
vec![]));
rust!(rust, "{{");
rust!(rust, "{}", defn.code);
rust!(rust, "}}");
}
Ok(())
}

View File

@ -239,8 +239,7 @@ impl<'ascent,'grammar,W:Write> RecursiveAscent<'ascent,'grammar,W> {
// invoke the action code
match production.action {
ActionKind::Call(action_fn) => {
rust!(self.out, "let {}nt = super::{}actions::{}action{}({}{});",
self.prefix,
rust!(self.out, "let {}nt = super::{}action{}({}{});",
self.prefix,
self.prefix,
action_fn.index(),