introduce a write_fn_header fn and use it in a few places,

with the intention of making it possible to thread grammar parameters
through
This commit is contained in:
Niko Matsakis 2015-07-14 22:28:20 -04:00
parent 48bbe83c5e
commit ccf9320b10
6 changed files with 1732 additions and 1597 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,11 @@ use util::tok::Tok;
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn parse_S< pub fn parse_S<
__TOKENS: IntoIterator<Item=Tok>, __TOKENS: IntoIterator<Item=Tok>,
>( >(
__tokens: __TOKENS, __tokens: __TOKENS,
) -> Result<(Option<Tok>, i32), Option<Tok>> { ) -> Result<(Option<Tok>, i32), Option<Tok>>
{
let mut __tokens = __tokens.into_iter(); let mut __tokens = __tokens.into_iter();
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
match try!(__parse__S::__state0(__lookahead, &mut __tokens)) { match try!(__parse__S::__state0(__lookahead, &mut __tokens)) {
@ -20,9 +21,9 @@ mod __parse__S {
use util::tok::Tok; use util::tok::Tok;
pub enum __Nonterminal { pub enum __Nonterminal {
E(i32),
T(i32), T(i32),
__S(i32), __S(i32),
E(i32),
S(i32), S(i32),
} }
@ -38,14 +39,14 @@ mod __parse__S {
// T = (*) "Num" ["-"] // T = (*) "Num" ["-"]
// __S = (*) S [EOF] // __S = (*) S [EOF]
// //
// "(" -> Shift(S5) // "(" -> Shift(S1)
// "Num" -> Shift(S3) // "Num" -> Shift(S4)
// //
// S -> S2 // S -> S2
// E -> S4 // E -> S3
// T -> S1 // T -> S5
pub fn __state0< pub fn __state0<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -55,12 +56,12 @@ mod __parse__S {
Some(__tok @ Tok::LParen(..)) => { Some(__tok @ Tok::LParen(..)) => {
let mut __sym0 = &mut Some(__tok); let mut __sym0 = &mut Some(__tok);
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
__result = try!(__state5(__lookahead, __tokens, __sym0)); __result = try!(__state1(__lookahead, __tokens, __sym0));
} }
Some(Tok::Num(__tok0)) => { Some(Tok::Num(__tok0)) => {
let mut __sym0 = &mut Some((__tok0)); let mut __sym0 = &mut Some((__tok0));
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
__result = try!(__state3(__lookahead, __tokens, __sym0)); __result = try!(__state4(__lookahead, __tokens, __sym0));
} }
_ => { _ => {
return Err(__lookahead); return Err(__lookahead);
@ -75,11 +76,11 @@ mod __parse__S {
} }
__Nonterminal::E(__nt) => { __Nonterminal::E(__nt) => {
let __sym0 = &mut Some(__nt); let __sym0 = &mut Some(__nt);
__result = try!(__state4(__lookahead, __tokens, __sym0)); __result = try!(__state3(__lookahead, __tokens, __sym0));
} }
__Nonterminal::T(__nt) => { __Nonterminal::T(__nt) => {
let __sym0 = &mut Some(__nt); let __sym0 = &mut Some(__nt);
__result = try!(__state1(__lookahead, __tokens, __sym0)); __result = try!(__state5(__lookahead, __tokens, __sym0));
} }
_ => { _ => {
return Ok((__lookahead, __nt)); return Ok((__lookahead, __nt));
@ -89,35 +90,62 @@ mod __parse__S {
} }
// State 1 // State 1
// E = T (*) [EOF] // E = (*) E "-" T [")"]
// E = T (*) ["-"] // E = (*) E "-" T ["-"]
// E = (*) T [")"]
// E = (*) T ["-"]
// T = (*) "(" E ")" [")"]
// T = (*) "(" E ")" ["-"]
// T = "(" (*) E ")" [EOF]
// T = "(" (*) E ")" ["-"]
// T = (*) "Num" [")"]
// T = (*) "Num" ["-"]
// //
// "-" -> Reduce(E = T => ActionFn(3);) // "(" -> Shift(S7)
// EOF -> Reduce(E = T => ActionFn(3);) // "Num" -> Shift(S6)
// //
// T -> S9
// E -> S8
pub fn __state1< pub fn __state1<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
__sym0: &mut Option<i32>, __sym0: &mut Option<Tok>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::Minus(..)) => { Some(__tok @ Tok::LParen(..)) => {
let __sym0 = __sym0.take().unwrap(); let mut __sym1 = &mut Some(__tok);
let __nt = super::__action3(__sym0); let __lookahead = __tokens.next();
return Ok((__lookahead, __Nonterminal::E(__nt))); __result = try!(__state7(__lookahead, __tokens, __sym1));
} }
None => { Some(Tok::Num(__tok0)) => {
let __sym0 = __sym0.take().unwrap(); let mut __sym1 = &mut Some((__tok0));
let __nt = super::__action3(__sym0); let __lookahead = __tokens.next();
return Ok((__lookahead, __Nonterminal::E(__nt))); __result = try!(__state6(__lookahead, __tokens, __sym1));
} }
_ => { _ => {
return Err(__lookahead); 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 2 // State 2
@ -126,7 +154,7 @@ mod __parse__S {
// EOF -> Reduce(__S = S => ActionFn(0);) // EOF -> Reduce(__S = S => ActionFn(0);)
// //
pub fn __state2< pub fn __state2<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -146,47 +174,15 @@ mod __parse__S {
} }
// State 3 // State 3
// T = "Num" (*) [EOF]
// T = "Num" (*) ["-"]
//
// "-" -> Reduce(T = "Num" => ActionFn(4);)
// EOF -> Reduce(T = "Num" => ActionFn(4);)
//
pub fn __state3<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action4(__sym0);
return Ok((__lookahead, __Nonterminal::T(__nt)));
}
None => {
let __sym0 = __sym0.take().unwrap();
let __nt = super::__action4(__sym0);
return Ok((__lookahead, __Nonterminal::T(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
// State 4
// E = E (*) "-" T [EOF] // E = E (*) "-" T [EOF]
// E = E (*) "-" T ["-"] // E = E (*) "-" T ["-"]
// S = E (*) [EOF] // S = E (*) [EOF]
// //
// "-" -> Shift(S6) // "-" -> Shift(S10)
// EOF -> Reduce(S = E => ActionFn(1);) // EOF -> Reduce(S = E => ActionFn(1);)
// //
pub fn __state4< pub fn __state3<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -197,7 +193,7 @@ mod __parse__S {
Some(__tok @ Tok::Minus(..)) => { Some(__tok @ Tok::Minus(..)) => {
let mut __sym1 = &mut Some(__tok); let mut __sym1 = &mut Some(__tok);
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
__result = try!(__state6(__lookahead, __tokens, __sym0, __sym1)); __result = try!(__state10(__lookahead, __tokens, __sym0, __sym1));
} }
None => { None => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
@ -211,125 +207,47 @@ mod __parse__S {
return Ok(__result); return Ok(__result);
} }
// State 5 // State 4
// E = (*) E "-" T [")"] // T = "Num" (*) [EOF]
// E = (*) E "-" T ["-"] // T = "Num" (*) ["-"]
// E = (*) T [")"]
// E = (*) T ["-"]
// T = (*) "(" E ")" [")"]
// T = (*) "(" E ")" ["-"]
// T = "(" (*) E ")" [EOF]
// T = "(" (*) E ")" ["-"]
// T = (*) "Num" [")"]
// T = (*) "Num" ["-"]
// //
// "(" -> Shift(S10) // "-" -> Reduce(T = "Num" => ActionFn(4);)
// "Num" -> Shift(S9) // EOF -> Reduce(T = "Num" => ActionFn(4);)
// //
// T -> S7 pub fn __state4<
// E -> S8 __TOKENS: Iterator<Item=Tok>,
pub fn __state5<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<Tok>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(__tok @ Tok::LParen(..)) => {
let mut __sym1 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state10(__lookahead, __tokens, __sym1));
}
Some(Tok::Num(__tok0)) => {
let mut __sym1 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state9(__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!(__state8(__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(S3)
// "(" -> Shift(S5)
//
// T -> S11
pub fn __state6<
__TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
__sym0: &mut Option<i32>, __sym0: &mut Option<i32>,
__sym1: &mut Option<Tok>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::Num(__tok0)) => { Some(Tok::Minus(..)) => {
let mut __sym2 = &mut Some((__tok0)); let __sym0 = __sym0.take().unwrap();
let __lookahead = __tokens.next(); let __nt = super::__action4(__sym0);
__result = try!(__state3(__lookahead, __tokens, __sym2)); return Ok((__lookahead, __Nonterminal::T(__nt)));
} }
Some(__tok @ Tok::LParen(..)) => { None => {
let mut __sym2 = &mut Some(__tok); let __sym0 = __sym0.take().unwrap();
let __lookahead = __tokens.next(); let __nt = super::__action4(__sym0);
__result = try!(__state5(__lookahead, __tokens, __sym2)); return Ok((__lookahead, __Nonterminal::T(__nt)));
} }
_ => { _ => {
return Err(__lookahead); 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 // State 5
// E = T (*) [")"] // E = T (*) [EOF]
// E = T (*) ["-"] // E = T (*) ["-"]
// //
// ")" -> Reduce(E = T => ActionFn(3);) // EOF -> Reduce(E = T => ActionFn(3);)
// "-" -> Reduce(E = T => ActionFn(3);) // "-" -> Reduce(E = T => ActionFn(3);)
// //
pub fn __state7< pub fn __state5<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -337,7 +255,7 @@ mod __parse__S {
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::RParen(..)) => { None => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __nt = super::__action3(__sym0); let __nt = super::__action3(__sym0);
return Ok((__lookahead, __Nonterminal::E(__nt))); return Ok((__lookahead, __Nonterminal::E(__nt)));
@ -353,51 +271,15 @@ mod __parse__S {
} }
} }
// State 8 // State 6
// E = E (*) "-" T [")"]
// E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [EOF]
// T = "(" E (*) ")" ["-"]
//
// ")" -> Shift(S12)
// "-" -> Shift(S13)
//
pub fn __state8<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<Tok>,
__sym1: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(__tok @ Tok::RParen(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state12(__lookahead, __tokens, __sym0, __sym1, __sym2));
}
Some(__tok @ Tok::Minus(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state13(__lookahead, __tokens, __sym1, __sym2));
}
_ => {
return Err(__lookahead);
}
}
return Ok(__result);
}
// State 9
// T = "Num" (*) [")"] // T = "Num" (*) [")"]
// T = "Num" (*) ["-"] // T = "Num" (*) ["-"]
// //
// ")" -> Reduce(T = "Num" => ActionFn(4);) // ")" -> Reduce(T = "Num" => ActionFn(4);)
// "-" -> Reduce(T = "Num" => ActionFn(4);) // "-" -> Reduce(T = "Num" => ActionFn(4);)
// //
pub fn __state9< pub fn __state6<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -421,7 +303,7 @@ mod __parse__S {
} }
} }
// State 10 // State 7
// E = (*) E "-" T [")"] // E = (*) E "-" T [")"]
// E = (*) E "-" T ["-"] // E = (*) E "-" T ["-"]
// E = (*) T [")"] // E = (*) T [")"]
@ -433,13 +315,13 @@ mod __parse__S {
// T = (*) "Num" [")"] // T = (*) "Num" [")"]
// T = (*) "Num" ["-"] // T = (*) "Num" ["-"]
// //
// "(" -> Shift(S10) // "(" -> Shift(S7)
// "Num" -> Shift(S9) // "Num" -> Shift(S6)
// //
// E -> S14 // E -> S11
// T -> S7 // T -> S9
pub fn __state10< pub fn __state7<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -450,12 +332,12 @@ mod __parse__S {
Some(__tok @ Tok::LParen(..)) => { Some(__tok @ Tok::LParen(..)) => {
let mut __sym1 = &mut Some(__tok); let mut __sym1 = &mut Some(__tok);
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
__result = try!(__state10(__lookahead, __tokens, __sym1)); __result = try!(__state7(__lookahead, __tokens, __sym1));
} }
Some(Tok::Num(__tok0)) => { Some(Tok::Num(__tok0)) => {
let mut __sym1 = &mut Some((__tok0)); let mut __sym1 = &mut Some((__tok0));
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
__result = try!(__state9(__lookahead, __tokens, __sym1)); __result = try!(__state6(__lookahead, __tokens, __sym1));
} }
_ => { _ => {
return Err(__lookahead); return Err(__lookahead);
@ -466,11 +348,130 @@ mod __parse__S {
match __nt { match __nt {
__Nonterminal::E(__nt) => { __Nonterminal::E(__nt) => {
let __sym1 = &mut Some(__nt); let __sym1 = &mut Some(__nt);
__result = try!(__state14(__lookahead, __tokens, __sym0, __sym1)); __result = try!(__state11(__lookahead, __tokens, __sym0, __sym1));
} }
__Nonterminal::T(__nt) => { __Nonterminal::T(__nt) => {
let __sym1 = &mut Some(__nt); let __sym1 = &mut Some(__nt);
__result = try!(__state7(__lookahead, __tokens, __sym1)); __result = try!(__state9(__lookahead, __tokens, __sym1));
}
_ => {
return Ok((__lookahead, __nt));
}
}
}
return Ok(__result);
}
// State 8
// E = E (*) "-" T [")"]
// E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [EOF]
// T = "(" E (*) ")" ["-"]
//
// "-" -> Shift(S13)
// ")" -> Shift(S12)
//
pub fn __state8<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<Tok>,
__sym1: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(__tok @ Tok::Minus(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state13(__lookahead, __tokens, __sym1, __sym2));
}
Some(__tok @ Tok::RParen(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state12(__lookahead, __tokens, __sym0, __sym1, __sym2));
}
_ => {
return Err(__lookahead);
}
}
return Ok(__result);
}
// State 9
// E = T (*) [")"]
// E = T (*) ["-"]
//
// ")" -> Reduce(E = T => ActionFn(3);)
// "-" -> Reduce(E = T => ActionFn(3);)
//
pub fn __state9<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __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 10
// E = E "-" (*) T [EOF]
// E = E "-" (*) T ["-"]
// T = (*) "(" E ")" [EOF]
// T = (*) "(" E ")" ["-"]
// T = (*) "Num" [EOF]
// T = (*) "Num" ["-"]
//
// "(" -> Shift(S1)
// "Num" -> Shift(S4)
//
// T -> S14
pub fn __state10<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
__sym1: &mut Option<Tok>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(__tok @ Tok::LParen(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state1(__lookahead, __tokens, __sym2));
}
Some(Tok::Num(__tok0)) => {
let mut __sym2 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state4(__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((__lookahead, __nt));
@ -481,52 +482,50 @@ mod __parse__S {
} }
// State 11 // State 11
// E = E "-" T (*) [EOF] // E = E (*) "-" T [")"]
// E = E "-" T (*) ["-"] // E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [")"]
// T = "(" E (*) ")" ["-"]
// //
// "-" -> Reduce(E = E, "-", T => ActionFn(2);) // "-" -> Shift(S13)
// EOF -> Reduce(E = E, "-", T => ActionFn(2);) // ")" -> Shift(S15)
// //
pub fn __state11< pub fn __state11<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
__sym0: &mut Option<i32>, __sym0: &mut Option<Tok>,
__sym1: &mut Option<Tok>, __sym1: &mut Option<i32>,
__sym2: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::Minus(..)) => { Some(__tok @ Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap(); let mut __sym2 = &mut Some(__tok);
let __sym1 = __sym1.take().unwrap(); let __lookahead = __tokens.next();
let __sym2 = __sym2.take().unwrap(); __result = try!(__state13(__lookahead, __tokens, __sym1, __sym2));
let __nt = super::__action2(__sym0, __sym1, __sym2);
return Ok((__lookahead, __Nonterminal::E(__nt)));
} }
None => { Some(__tok @ Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap(); let mut __sym2 = &mut Some(__tok);
let __sym1 = __sym1.take().unwrap(); let __lookahead = __tokens.next();
let __sym2 = __sym2.take().unwrap(); __result = try!(__state15(__lookahead, __tokens, __sym0, __sym1, __sym2));
let __nt = super::__action2(__sym0, __sym1, __sym2);
return Ok((__lookahead, __Nonterminal::E(__nt)));
} }
_ => { _ => {
return Err(__lookahead); return Err(__lookahead);
} }
} }
return Ok(__result);
} }
// State 12 // State 12
// T = "(" E ")" (*) [EOF] // T = "(" E ")" (*) [EOF]
// T = "(" E ")" (*) ["-"] // T = "(" E ")" (*) ["-"]
// //
// EOF -> Reduce(T = "(", E, ")" => ActionFn(5);)
// "-" -> Reduce(T = "(", E, ")" => ActionFn(5);) // "-" -> Reduce(T = "(", E, ")" => ActionFn(5);)
// EOF -> Reduce(T = "(", E, ")" => ActionFn(5);)
// //
pub fn __state12< pub fn __state12<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -536,14 +535,14 @@ mod __parse__S {
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
None => { Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap(); let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap(); let __sym2 = __sym2.take().unwrap();
let __nt = super::__action5(__sym0, __sym1, __sym2); let __nt = super::__action5(__sym0, __sym1, __sym2);
return Ok((__lookahead, __Nonterminal::T(__nt))); return Ok((__lookahead, __Nonterminal::T(__nt)));
} }
Some(Tok::Minus(..)) => { None => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap(); let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap(); let __sym2 = __sym2.take().unwrap();
@ -564,12 +563,12 @@ mod __parse__S {
// T = (*) "Num" [")"] // T = (*) "Num" [")"]
// T = (*) "Num" ["-"] // T = (*) "Num" ["-"]
// //
// "Num" -> Shift(S9) // "(" -> Shift(S7)
// "(" -> Shift(S10) // "Num" -> Shift(S6)
// //
// T -> S15 // T -> S16
pub fn __state13< pub fn __state13<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -578,15 +577,15 @@ mod __parse__S {
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::Num(__tok0)) => {
let mut __sym2 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state9(__lookahead, __tokens, __sym2));
}
Some(__tok @ Tok::LParen(..)) => { Some(__tok @ Tok::LParen(..)) => {
let mut __sym2 = &mut Some(__tok); let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next(); let __lookahead = __tokens.next();
__result = try!(__state10(__lookahead, __tokens, __sym2)); __result = try!(__state7(__lookahead, __tokens, __sym2));
}
Some(Tok::Num(__tok0)) => {
let mut __sym2 = &mut Some((__tok0));
let __lookahead = __tokens.next();
__result = try!(__state6(__lookahead, __tokens, __sym2));
} }
_ => { _ => {
return Err(__lookahead); return Err(__lookahead);
@ -597,7 +596,7 @@ mod __parse__S {
match __nt { match __nt {
__Nonterminal::T(__nt) => { __Nonterminal::T(__nt) => {
let __sym2 = &mut Some(__nt); let __sym2 = &mut Some(__nt);
__result = try!(__state15(__lookahead, __tokens, __sym0, __sym1, __sym2)); __result = try!(__state16(__lookahead, __tokens, __sym0, __sym1, __sym2));
} }
_ => { _ => {
return Ok((__lookahead, __nt)); return Ok((__lookahead, __nt));
@ -608,50 +607,14 @@ mod __parse__S {
} }
// State 14 // State 14
// E = E (*) "-" T [")"] // E = E "-" T (*) [EOF]
// E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [")"]
// T = "(" E (*) ")" ["-"]
//
// "-" -> Shift(S13)
// ")" -> Shift(S16)
//
pub fn __state14<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<Tok>,
__sym1: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(__tok @ Tok::Minus(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state13(__lookahead, __tokens, __sym1, __sym2));
}
Some(__tok @ Tok::RParen(..)) => {
let mut __sym2 = &mut Some(__tok);
let __lookahead = __tokens.next();
__result = try!(__state16(__lookahead, __tokens, __sym0, __sym1, __sym2));
}
_ => {
return Err(__lookahead);
}
}
return Ok(__result);
}
// State 15
// E = E "-" T (*) [")"]
// E = E "-" T (*) ["-"] // E = E "-" T (*) ["-"]
// //
// EOF -> Reduce(E = E, "-", T => ActionFn(2);)
// "-" -> Reduce(E = E, "-", T => ActionFn(2);) // "-" -> Reduce(E = E, "-", T => ActionFn(2);)
// ")" -> Reduce(E = E, "-", T => ActionFn(2);)
// //
pub fn __state15< pub fn __state14<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -661,14 +624,14 @@ mod __parse__S {
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::Minus(..)) => { None => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap(); let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap(); let __sym2 = __sym2.take().unwrap();
let __nt = super::__action2(__sym0, __sym1, __sym2); let __nt = super::__action2(__sym0, __sym1, __sym2);
return Ok((__lookahead, __Nonterminal::E(__nt))); return Ok((__lookahead, __Nonterminal::E(__nt)));
} }
Some(Tok::RParen(..)) => { Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap(); let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap(); let __sym2 = __sym2.take().unwrap();
@ -681,15 +644,15 @@ mod __parse__S {
} }
} }
// State 16 // State 15
// T = "(" E ")" (*) [")"] // T = "(" E ")" (*) [")"]
// T = "(" E ")" (*) ["-"] // T = "(" E ")" (*) ["-"]
// //
// ")" -> Reduce(T = "(", E, ")" => ActionFn(5);)
// "-" -> Reduce(T = "(", E, ")" => ActionFn(5);) // "-" -> Reduce(T = "(", E, ")" => ActionFn(5);)
// ")" -> Reduce(T = "(", E, ")" => ActionFn(5);)
// //
pub fn __state16< pub fn __state15<
__TOKENS: Iterator<Item=Tok>, __TOKENS: Iterator<Item=Tok>,
>( >(
mut __lookahead: Option<Tok>, mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS, __tokens: &mut __TOKENS,
@ -699,14 +662,14 @@ mod __parse__S {
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> { ) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal); let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead { match __lookahead {
Some(Tok::RParen(..)) => { Some(Tok::Minus(..)) => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap(); let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap(); let __sym2 = __sym2.take().unwrap();
let __nt = super::__action5(__sym0, __sym1, __sym2); let __nt = super::__action5(__sym0, __sym1, __sym2);
return Ok((__lookahead, __Nonterminal::T(__nt))); return Ok((__lookahead, __Nonterminal::T(__nt)));
} }
Some(Tok::Minus(..)) => { Some(Tok::RParen(..)) => {
let __sym0 = __sym0.take().unwrap(); let __sym0 = __sym0.take().unwrap();
let __sym1 = __sym1.take().unwrap(); let __sym1 = __sym1.take().unwrap();
let __sym2 = __sym2.take().unwrap(); let __sym2 = __sym2.take().unwrap();
@ -718,38 +681,94 @@ mod __parse__S {
} }
} }
} }
// State 16
// E = E "-" T (*) [")"]
// E = E "-" T (*) ["-"]
//
// ")" -> Reduce(E = E, "-", T => ActionFn(2);)
// "-" -> Reduce(E = E, "-", T => ActionFn(2);)
//
pub fn __state16<
__TOKENS: Iterator<Item=Tok>,
>(
mut __lookahead: Option<Tok>,
__tokens: &mut __TOKENS,
__sym0: &mut Option<i32>,
__sym1: &mut Option<Tok>,
__sym2: &mut Option<i32>,
) -> Result<(Option<Tok>, __Nonterminal), Option<Tok>> {
let mut __result: (Option<Tok>, __Nonterminal);
match __lookahead {
Some(Tok::RParen(..)) => {
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);
return Ok((__lookahead, __Nonterminal::E(__nt)));
}
_ => {
return Err(__lookahead);
}
}
}
} }
fn __action0(
fn __action0<
>(
__0: i32, __0: i32,
) -> i32 { ) -> i32
{
(__0) (__0)
} }
fn __action1(
fn __action1<
>(
__0: i32, __0: i32,
) -> i32 { ) -> i32
{
(__0) (__0)
} }
fn __action2(
fn __action2<
>(
l: i32, l: i32,
_: Tok, _: Tok,
r: i32, r: i32,
) -> i32 { ) -> i32
{
l-r l-r
} }
fn __action3(
fn __action3<
>(
__0: i32, __0: i32,
) -> i32 { ) -> i32
{
(__0) (__0)
} }
fn __action4(
fn __action4<
>(
__0: i32, __0: i32,
) -> i32 { ) -> i32
{
(__0) (__0)
} }
fn __action5(
fn __action5<
>(
_: Tok, _: Tok,
__0: i32, __0: i32,
_: Tok, _: Tok,
) -> i32 { ) -> i32
{
(__0) (__0)
} }

View File

@ -100,11 +100,18 @@ fn emit_action_code<W:Write>(grammar: &r::Grammar,
-> io::Result<()> -> io::Result<()>
{ {
for (i, defn) in grammar.action_fn_defns.iter().enumerate() { for (i, defn) in grammar.action_fn_defns.iter().enumerate() {
rust!(rust, "fn {}action{}(", grammar.prefix, i); rust!(rust, "");
for (p, t) in defn.arg_patterns.iter().zip(defn.arg_types.iter()) { try!(rust.write_fn_header(
rust!(rust, "{}: {},", p, t); grammar,
} format!("{}action{}", grammar.prefix, i),
rust!(rust, ") -> {} {{", defn.ret_type); 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, "{}", defn.code);
rust!(rust, "}}"); rust!(rust, "}}");
} }

View File

@ -325,6 +325,15 @@ impl Display for MacroSymbol {
} }
} }
impl Display for TypeParameter {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self {
TypeParameter::Lifetime(s) => write!(fmt, "'{}", s),
TypeParameter::Id(s) => write!(fmt, "{}", s),
}
}
}
impl Display for TypeRef { impl Display for TypeRef {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self { match *self {

View File

@ -104,12 +104,15 @@ impl<'ascent,'grammar,W:Write> RecursiveAscent<'ascent,'grammar,W> {
fn write_start_fn(&mut self) -> io::Result<()> { fn write_start_fn(&mut self) -> io::Result<()> {
let terminal_type = self.types.terminal_enum_type(); let terminal_type = self.types.terminal_enum_type();
rust!(self.out, "#[allow(non_snake_case)]"); rust!(self.out, "#[allow(non_snake_case)]");
rust!(self.out, "pub fn parse_{}<", self.user_start_symbol); try!(self.out.write_pub_fn_header(
rust!(self.out, " {}TOKENS: IntoIterator<Item={}>,", self.prefix, terminal_type); self.grammar,
rust!(self.out, ">("); format!("parse_{}", self.user_start_symbol),
rust!(self.out, "{}tokens: {}TOKENS,", self.prefix, self.prefix); vec![format!("{}TOKENS: IntoIterator<Item={}>", self.prefix, terminal_type)],
rust!(self.out, ") -> Result<(Option<{}>, {}), Option<{}>> {{", vec![format!("{}tokens: {}TOKENS", self.prefix, self.prefix)],
terminal_type, self.types.nonterminal_type(self.start_symbol), terminal_type); format!("Result<(Option<{}>, {}), Option<{}>>",
terminal_type, self.types.nonterminal_type(self.start_symbol), terminal_type),
vec![]));
rust!(self.out, "{{");
rust!(self.out, "let mut {}tokens = {}tokens.into_iter();", self.prefix, self.prefix); rust!(self.out, "let mut {}tokens = {}tokens.into_iter();", self.prefix, self.prefix);
rust!(self.out, "let {}lookahead = {}tokens.next();", self.prefix, self.prefix); rust!(self.out, "let {}lookahead = {}tokens.next();", self.prefix, self.prefix);
rust!(self.out, "match try!({}parse{}::{}state0({}lookahead, &mut {}tokens)) {{", rust!(self.out, "match try!({}parse{}::{}state0({}lookahead, &mut {}tokens)) {{",

View File

@ -1,6 +1,7 @@
//! Simple Rust AST. This is what the various code generators create, //! Simple Rust AST. This is what the various code generators create,
//! which then gets serialized. //! which then gets serialized.
use grammar::repr::Grammar;
use std::io::{self, Write}; use std::io::{self, Write};
macro_rules! rust { macro_rules! rust {
@ -48,6 +49,8 @@ impl<W:Write> RustWrite<W> {
pub fn writeln(&mut self, out: &str) -> io::Result<()> { pub fn writeln(&mut self, out: &str) -> io::Result<()> {
let buf = out.as_bytes(); let buf = out.as_bytes();
println!("indent={:?} out={:?}", self.indent, out);
// pass empty lines through with no indentation // pass empty lines through with no indentation
if buf.is_empty() { if buf.is_empty() {
return self.write.write_all("\n".as_bytes()); return self.write.write_all("\n".as_bytes());
@ -55,13 +58,6 @@ impl<W:Write> RustWrite<W> {
let n = buf.len() - 1; let n = buf.len() - 1;
// Check for an opening brace all on its own. We only expect this to occur
// as part of a fn header. As a special exception, print it at one TAB less
// than normal but leave the indent otherwise unchanged.
if buf[0] == ('{' as u8) && n == 0 {
self.indent -= TAB;
}
// If the line begins with a `}`, `]`, or `)`, first decrement the indentation. // If the line begins with a `}`, `]`, or `)`, first decrement the indentation.
if buf[0] == ('}' as u8) || buf[0] == (']' as u8) || buf[0] == (')' as u8) { if buf[0] == ('}' as u8) || buf[0] == (']' as u8) || buf[0] == (')' as u8) {
self.indent -= TAB; self.indent -= TAB;
@ -76,4 +72,77 @@ impl<W:Write> RustWrite<W> {
Ok(()) Ok(())
} }
pub fn write_pub_fn_header(&mut self,
grammar: &Grammar,
name: String,
type_parameters: Vec<String>,
parameters: Vec<String>,
return_type: String,
where_clauses: Vec<String>)
-> io::Result<()>
{
self.write_fn_header_helper(grammar, "pub ", name, type_parameters,
parameters, return_type, where_clauses)
}
pub fn write_fn_header(&mut self,
grammar: &Grammar,
name: String,
type_parameters: Vec<String>,
parameters: Vec<String>,
return_type: String,
where_clauses: Vec<String>)
-> io::Result<()>
{
self.write_fn_header_helper(grammar, "", name, type_parameters,
parameters, return_type, where_clauses)
}
fn write_fn_header_helper(&mut self,
grammar: &Grammar,
qualifiers: &str,
name: String,
type_parameters: Vec<String>,
parameters: Vec<String>,
return_type: String,
where_clauses: Vec<String>)
-> io::Result<()>
{
rust!(self, "{}fn {}<", qualifiers, name);
for type_parameter in &grammar.type_parameters {
rust!(self, "{0:1$}{2},", "", TAB, type_parameter);
}
for type_parameter in type_parameters {
rust!(self, "{0:1$}{2},", "", TAB, type_parameter);
}
rust!(self, ">(");
for parameter in &grammar.parameters {
rust!(self, "{}: {},", parameter.name, parameter.ty);
}
for parameter in &parameters {
rust!(self, "{},", parameter);
}
if !grammar.where_clauses.is_empty() || !where_clauses.is_empty() {
rust!(self, ") -> {} where", return_type);
for where_clause in &grammar.where_clauses {
rust!(self, " {},", where_clause);
}
for where_clause in &where_clauses {
rust!(self, " {},", where_clause);
}
} else {
rust!(self,") -> {}", return_type);
}
Ok(())
}
} }