[parser] Fix crash
- Create a ProgramParser on each call of next() - Fix up ending condition check
This commit is contained in:
parent
fea93f44eb
commit
7b90b5d229
1 changed files with 19 additions and 3 deletions
|
@ -33,13 +33,18 @@ pub struct Parser<T> where T: Iterator<Item=LexerResult> {
|
||||||
|
|
||||||
impl<T> Parser<T> where T: Iterator<Item=LexerResult> {
|
impl<T> Parser<T> where T: Iterator<Item=LexerResult> {
|
||||||
pub fn new(input: T) -> Parser<T> {
|
pub fn new(input: T) -> Parser<T> {
|
||||||
let program_parser = Box::new(ProgramParser::new());
|
|
||||||
Parser {
|
Parser {
|
||||||
input: input.peekable(),
|
input: input.peekable(),
|
||||||
parsers: vec!(program_parser)
|
parsers: vec![]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prepare(&mut self) {
|
||||||
|
assert_eq!(self.parsers.len(), 0);
|
||||||
|
let program_parser = Box::new(ProgramParser::new());
|
||||||
|
self.parsers.push(program_parser);
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_lex(&mut self, lex: &Lex) -> NodeParseResult {
|
fn parse_lex(&mut self, lex: &Lex) -> NodeParseResult {
|
||||||
let parser = self.parsers.last_mut().expect("couldn't get a parser -- this is unexpected");
|
let parser = self.parsers.last_mut().expect("couldn't get a parser -- this is unexpected");
|
||||||
parser.parse(lex)
|
parser.parse(lex)
|
||||||
|
@ -81,7 +86,18 @@ impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
|
||||||
let mut input_lex: Option<T::Item> = None;
|
let mut input_lex: Option<T::Item> = None;
|
||||||
loop {
|
loop {
|
||||||
input_lex = match result {
|
input_lex = match result {
|
||||||
None => self.next_lex(), // Starting condition
|
None => {
|
||||||
|
let next_lex = self.next_lex();
|
||||||
|
if next_lex.is_none() {
|
||||||
|
// First run through the loop and our input has nothing to give.
|
||||||
|
out = None;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// Prepare for parsing!
|
||||||
|
self.prepare();
|
||||||
|
}
|
||||||
|
next_lex
|
||||||
|
}
|
||||||
Some(NodeParseResult::Continue) => self.next_lex(),
|
Some(NodeParseResult::Continue) => self.next_lex(),
|
||||||
Some(NodeParseResult::Complete{ obj }) => {
|
Some(NodeParseResult::Complete{ obj }) => {
|
||||||
self.pop_parser();
|
self.pop_parser();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue