fix error reporting in terms of spans etc (no automated tests for this, boo)

This commit is contained in:
Niko Matsakis 2015-07-03 13:14:38 -04:00
parent 1b46191fdc
commit 8fc9b609a4
9 changed files with 1259 additions and 1227 deletions

File diff suppressed because it is too large Load Diff

View File

@ -20,9 +20,9 @@ mod __parse__S {
pub enum __Nonterminal { pub enum __Nonterminal {
E(i32), E(i32),
T(i32),
S(i32), S(i32),
__S(i32), __S(i32),
T(i32),
} }
// State 0 // State 0
@ -37,12 +37,12 @@ mod __parse__S {
// T = (*) "Num" ["-"] // T = (*) "Num" ["-"]
// __S = (*) S [EOF] // __S = (*) S [EOF]
// //
// "Num" -> Shift(S1) // "Num" -> Shift(S4)
// "(" -> Shift(S5) // "(" -> Shift(S2)
// //
// T -> S3 // T -> S1
// S -> S2 // S -> S5
// E -> S4 // E -> S3
pub fn __state0<TOKENS: Iterator<Item=Tok>>( pub fn __state0<TOKENS: Iterator<Item=Tok>>(
mut lookahead: Option<Tok>, mut lookahead: Option<Tok>,
tokens: &mut TOKENS, tokens: &mut TOKENS,
@ -52,12 +52,12 @@ mod __parse__S {
Some(tok @ Tok::Num(..)) => { Some(tok @ Tok::Num(..)) => {
let sym0 = &mut Some(tok); let sym0 = &mut Some(tok);
let lookahead = tokens.next(); let lookahead = tokens.next();
result = try!(__state1(lookahead, tokens, sym0)); result = try!(__state4(lookahead, tokens, sym0));
} }
Some(tok @ Tok::LParen(..)) => { Some(tok @ Tok::LParen(..)) => {
let sym0 = &mut Some(tok); let sym0 = &mut Some(tok);
let lookahead = tokens.next(); let lookahead = tokens.next();
result = try!(__state5(lookahead, tokens, sym0)); result = try!(__state2(lookahead, tokens, sym0));
} }
_ => { _ => {
return Err(lookahead); return Err(lookahead);
@ -68,15 +68,15 @@ mod __parse__S {
match nt { match nt {
__Nonterminal::T(nt) => { __Nonterminal::T(nt) => {
let sym0 = &mut Some(nt); let sym0 = &mut Some(nt);
result = try!(__state3(lookahead, tokens, sym0)); result = try!(__state1(lookahead, tokens, sym0));
} }
__Nonterminal::S(nt) => { __Nonterminal::S(nt) => {
let sym0 = &mut Some(nt); let sym0 = &mut Some(nt);
result = try!(__state2(lookahead, tokens, sym0)); result = try!(__state5(lookahead, tokens, sym0));
} }
__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));
} }
_ => { _ => {
return Ok((lookahead, nt)); return Ok((lookahead, nt));
@ -86,25 +86,144 @@ mod __parse__S {
} }
// State 1 // State 1
// T = "Num" (*) [EOF] // E = T (*) [EOF]
// T = "Num" (*) ["-"] // E = T (*) ["-"]
// //
// EOF -> Reduce(T = "Num" => ActionFn(4);) // EOF -> Reduce(E = T => ActionFn(3);)
// "-" -> Reduce(T = "Num" => ActionFn(4);) // "-" -> Reduce(E = T => ActionFn(3);)
// //
pub fn __state1<TOKENS: Iterator<Item=Tok>>( pub fn __state1<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 {
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<TOKENS: Iterator<Item=Tok>>(
mut lookahead: Option<Tok>, mut lookahead: Option<Tok>,
tokens: &mut TOKENS, tokens: &mut TOKENS,
sym0: &mut Option<Tok>, 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 @ 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<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 @ Tok::Minus(..)) => {
let sym1 = &mut Some(tok);
let lookahead = tokens.next();
result = try!(__state10(lookahead, tokens, sym0, sym1));
}
None => { 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<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::Minus(..)) => {
let sym0 = sym0.take().unwrap(); let sym0 = sym0.take().unwrap();
let nt = super::__action4(sym0); let nt = super::__action4(sym0);
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 nt = super::__action4(sym0); let nt = super::__action4(sym0);
return Ok((lookahead, __Nonterminal::T(nt))); return Ok((lookahead, __Nonterminal::T(nt)));
@ -115,12 +234,12 @@ mod __parse__S {
} }
} }
// State 2 // State 5
// __S = S (*) [EOF] // __S = S (*) [EOF]
// //
// EOF -> Reduce(__S = S => ActionFn(0);) // EOF -> Reduce(__S = S => ActionFn(0);)
// //
pub fn __state2<TOKENS: Iterator<Item=Tok>>( pub fn __state5<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>,
@ -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<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 {
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<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 @ 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<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 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 // 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<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::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<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 8
// T = "Num" (*) [")"]
// T = "Num" (*) ["-"]
//
// ")" -> Reduce(T = "Num" => ActionFn(4);)
// "-" -> Reduce(T = "Num" => ActionFn(4);)
//
pub fn __state8<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::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 = (*) E "-" T ["-"] // E = (*) E "-" T ["-"]
// E = (*) T [")"] // E = (*) T [")"]
@ -378,12 +269,12 @@ mod __parse__S {
// T = (*) "Num" [")"] // T = (*) "Num" [")"]
// T = (*) "Num" ["-"] // T = (*) "Num" ["-"]
// //
// "Num" -> Shift(S8) // "Num" -> Shift(S7)
// "(" -> Shift(S9) // "(" -> Shift(S6)
// //
// E -> S12 // E -> S11
// T -> S7 // T -> S9
pub fn __state9<TOKENS: Iterator<Item=Tok>>( pub fn __state6<TOKENS: Iterator<Item=Tok>>(
mut lookahead: Option<Tok>, mut lookahead: Option<Tok>,
tokens: &mut TOKENS, tokens: &mut TOKENS,
sym0: &mut Option<Tok>, sym0: &mut Option<Tok>,
@ -393,12 +284,12 @@ mod __parse__S {
Some(tok @ Tok::Num(..)) => { Some(tok @ Tok::Num(..)) => {
let sym1 = &mut Some(tok); let sym1 = &mut Some(tok);
let lookahead = tokens.next(); let lookahead = tokens.next();
result = try!(__state8(lookahead, tokens, sym1)); result = try!(__state7(lookahead, tokens, sym1));
} }
Some(tok @ Tok::LParen(..)) => { Some(tok @ Tok::LParen(..)) => {
let sym1 = &mut Some(tok); let sym1 = &mut Some(tok);
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);
@ -409,11 +300,11 @@ 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!(__state12(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((lookahead, nt));
@ -423,16 +314,46 @@ mod __parse__S {
return Ok(result); return Ok(result);
} }
// State 10 // State 7
// T = "Num" (*) [")"]
// T = "Num" (*) ["-"]
//
// ")" -> Reduce(T = "Num" => ActionFn(4);)
// "-" -> Reduce(T = "Num" => ActionFn(4);)
//
pub fn __state7<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::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 [")"]
// E = E (*) "-" T ["-"] // E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [EOF] // T = "(" E (*) ")" [EOF]
// T = "(" E (*) ")" ["-"] // T = "(" E (*) ")" ["-"]
// //
// "-" -> Shift(S13) // "-" -> Shift(S13)
// ")" -> Shift(S14) // ")" -> Shift(S12)
// //
pub fn __state10<TOKENS: Iterator<Item=Tok>>( pub fn __state8<TOKENS: Iterator<Item=Tok>>(
mut lookahead: Option<Tok>, mut lookahead: Option<Tok>,
tokens: &mut TOKENS, tokens: &mut TOKENS,
sym0: &mut Option<Tok>, sym0: &mut Option<Tok>,
@ -448,7 +369,7 @@ mod __parse__S {
Some(tok @ Tok::RParen(..)) => { Some(tok @ Tok::RParen(..)) => {
let sym2 = &mut Some(tok); let sym2 = &mut Some(tok);
let lookahead = tokens.next(); let lookahead = tokens.next();
result = try!(__state14(lookahead, tokens, sym0, sym1, sym2)); result = try!(__state12(lookahead, tokens, sym0, sym1, sym2));
} }
_ => { _ => {
return Err(lookahead); return Err(lookahead);
@ -457,34 +378,28 @@ mod __parse__S {
return Ok(result); return Ok(result);
} }
// State 11 // State 9
// E = E "-" T (*) [EOF] // E = T (*) [")"]
// E = E "-" T (*) ["-"] // E = T (*) ["-"]
// //
// EOF -> Reduce(E = E, "-", T => ActionFn(2);) // "-" -> Reduce(E = T => ActionFn(3);)
// "-" -> Reduce(E = E, "-", T => ActionFn(2);) // ")" -> Reduce(E = T => ActionFn(3);)
// //
pub fn __state11<TOKENS: Iterator<Item=Tok>>( pub fn __state9<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>,
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 {
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(..)) => { Some(Tok::Minus(..)) => {
let sym0 = sym0.take().unwrap(); let sym0 = sym0.take().unwrap();
let sym1 = sym1.take().unwrap(); let nt = super::__action3(sym0);
let sym2 = sym2.take().unwrap(); return Ok((lookahead, __Nonterminal::E(nt)));
let nt = super::__action2(sym0, sym1, sym2); }
Some(Tok::RParen(..)) => {
let sym0 = sym0.take().unwrap();
let nt = super::__action3(sym0);
return Ok((lookahead, __Nonterminal::E(nt))); 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<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::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 [")"]
// E = E (*) "-" T ["-"] // E = E (*) "-" T ["-"]
// T = "(" E (*) ")" [")"] // T = "(" E (*) ")" [")"]
@ -502,7 +466,7 @@ mod __parse__S {
// "-" -> Shift(S13) // "-" -> Shift(S13)
// ")" -> Shift(S15) // ")" -> Shift(S15)
// //
pub fn __state12<TOKENS: Iterator<Item=Tok>>( pub fn __state11<TOKENS: Iterator<Item=Tok>>(
mut lookahead: Option<Tok>, mut lookahead: Option<Tok>,
tokens: &mut TOKENS, tokens: &mut TOKENS,
sym0: &mut Option<Tok>, sym0: &mut Option<Tok>,
@ -527,63 +491,14 @@ mod __parse__S {
return Ok(result); return Ok(result);
} }
// State 13 // State 12
// E = E "-" (*) T [")"]
// E = E "-" (*) T ["-"]
// T = (*) "(" E ")" [")"]
// T = (*) "(" E ")" ["-"]
// T = (*) "Num" [")"]
// T = (*) "Num" ["-"]
//
// "(" -> Shift(S9)
// "Num" -> Shift(S8)
//
// T -> S16
pub fn __state13<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 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
// T = "(" E ")" (*) [EOF] // T = "(" E ")" (*) [EOF]
// T = "(" E ")" (*) ["-"] // T = "(" E ")" (*) ["-"]
// //
// "-" -> Reduce(T = "(", E, ")" => ActionFn(5);) // "-" -> Reduce(T = "(", E, ")" => ActionFn(5);)
// EOF -> Reduce(T = "(", E, ")" => ActionFn(5);) // EOF -> Reduce(T = "(", E, ")" => ActionFn(5);)
// //
pub fn __state14<TOKENS: Iterator<Item=Tok>>( pub fn __state12<TOKENS: Iterator<Item=Tok>>(
mut lookahead: Option<Tok>, mut lookahead: Option<Tok>,
tokens: &mut TOKENS, tokens: &mut TOKENS,
sym0: &mut Option<Tok>, sym0: &mut Option<Tok>,
@ -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<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 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<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::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 // State 15
// T = "(" E ")" (*) [")"] // T = "(" E ")" (*) [")"]
// T = "(" E ")" (*) ["-"] // T = "(" E ")" (*) ["-"]

View File

@ -1,4 +1,5 @@
use grammar::parse_tree as pt; use grammar::parse_tree as pt;
use std::fmt::{Display, Formatter, Error};
use std::fs::File; use std::fs::File;
use std::path::PathBuf; use std::path::PathBuf;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
@ -18,12 +19,16 @@ impl FileText {
} }
fn new(path: PathBuf, input_str: String) -> FileText { fn new(path: PathBuf, input_str: String) -> FileText {
let newline_indices: Vec<usize> = let newline_indices: Vec<usize> = {
input_str.as_bytes().iter() let input_indices =
.enumerate() input_str.as_bytes().iter()
.filter(|&(_, &b)| b == ('\n' as u8)) .enumerate()
.map(|(i, _)| i + 1) // store the first character in the line .filter(|&(_, &b)| b == ('\n' as u8))
.collect(); .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 } FileText { path: path, input_str: input_str, newlines: newline_indices }
} }
@ -65,7 +70,7 @@ impl FileText {
&self.input_str[start_offset..] &self.input_str[start_offset..]
} else { } else {
let end_offset = self.newlines[line_num + 1]; 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)); try!(writeln!(out, " {}", text));
if end_col - start_col <= 1 { if end_col - start_col <= 1 {
try!(writeln!(out, " {0:1$}^", "", start_col)); try!(writeln!(out, " {}^", Repeat(' ', start_col)));
} else { } else {
let width = end_col - start_col; let width = end_col - start_col;
try!(writeln!(out, " {0:1$}|{0:-2$}|", try!(writeln!(out, " {}~{}~",
"", start_col, width.saturating_sub(2))); Repeat(' ', start_col),
Repeat('~', width.saturating_sub(2))));
} }
} else { } else {
// span is across many lines, find the maximal width of any of those // 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 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(); 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] { for line in &line_strs[..line_strs.len()-1] {
try!(writeln!(out, "| {0:<1$} |", line, max_len)); try!(writeln!(out, "| {0:<1$} |", line, max_len));
} }
try!(writeln!(out, "| {}", line_strs[line_strs.len()-1])); try!(writeln!(out, "| {}", line_strs[line_strs.len()-1]));
try!(writeln!(out, "+-{0:-1$}", "", end_col)); try!(writeln!(out, "+~{}", Repeat('~', end_col)));
} }
Ok(()) 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(())
}
}

View File

@ -97,7 +97,7 @@ grammar {
fn expr_grammar1() { fn expr_grammar1() {
let grammar = normalized_grammar(r#" let grammar = normalized_grammar(r#"
grammar { grammar {
token Tok where { }; token Tok where { "-" => "Sub" };
S: () = S: () =
E => (); E => ();

View File

@ -25,6 +25,11 @@ macro_rules! return_err {
} }
pub fn normalize(grammar: pt::Grammar) -> NormResult<r::Grammar> { pub fn normalize(grammar: pt::Grammar) -> NormResult<r::Grammar> {
try!(validate::validate(&grammar));
normalize_without_validating(grammar)
}
pub fn normalize_without_validating(grammar: pt::Grammar) -> NormResult<r::Grammar> {
let grammar = try!(macro_expand::expand_macros(grammar)); let grammar = try!(macro_expand::expand_macros(grammar));
let types = try!(tyinfer::infer_types(&grammar)); let types = try!(tyinfer::infer_types(&grammar));
lower::lower(grammar, types) lower::lower(grammar, types)

View File

@ -4,7 +4,7 @@ use super::norm_util::{self, AlternativeAction, Symbols};
use std::collections::{HashMap}; use std::collections::{HashMap};
use grammar::parse_tree::{Alternative, Grammar, GrammarItem, use grammar::parse_tree::{Alternative, Grammar, GrammarItem,
NonterminalData, NonterminalString, NonterminalData, NonterminalString,
Span, Symbol, SymbolKind, TypeRef}; Span, SymbolKind, TypeRef};
use grammar::repr::{Types, TypeRepr}; use grammar::repr::{Types, TypeRepr};
#[cfg(test)] #[cfg(test)]

View File

@ -45,12 +45,13 @@ struct Validator<'grammar> {
conversions: Set<TerminalString>, conversions: Set<TerminalString>,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone, Debug)]
enum Def { enum Def {
TopLevel(usize), // argument is the number of macro arguments TopLevel(usize), // argument is the number of macro arguments
MacroArg MacroArg
} }
#[derive(Debug)]
struct ScopeChain<'scope> { struct ScopeChain<'scope> {
previous: Option<&'scope ScopeChain<'scope>>, previous: Option<&'scope ScopeChain<'scope>>,
nonterminals: Map<NonterminalString, Def>, nonterminals: Map<NonterminalString, Def>,
@ -194,10 +195,10 @@ impl<'grammar> Validator<'grammar> {
} }
} }
SymbolKind::Repeat(ref repeat) => { 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) => { 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) nt: NonterminalString)
-> NormResult<Def> { -> NormResult<Def> {
match scope.def(nt) { match scope.def(nt) {
Some(arity) => Ok(arity), Some(def) => Ok(def),
None => return_err!(span, "no definition found for nonterminal `{}`", nt) None => return_err!(span, "no definition found for nonterminal `{}`", nt)
} }
} }

View File

@ -45,3 +45,10 @@ fn repeated_macro_arg() {
"multiple macro arguments declared with the name `Y`", "multiple macro arguments declared with the name `Y`",
r#"grammar { >>>X<Y,Y> <<<= "foo"; }"#); r#"grammar { >>>X<Y,Y> <<<= "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"; }; }"#);
}

View File

@ -46,5 +46,5 @@ pub fn compare<D:Debug,E:Debug>(actual: D, expected: E) {
} }
pub fn normalized_grammar(s: &str) -> r::Grammar { 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()
} }