[parser] Handle None from input

This commit is contained in:
Eryn Wells 2018-08-24 08:26:08 -07:00
parent 96e92230ed
commit 976675027f
5 changed files with 29 additions and 3 deletions

View file

@ -44,6 +44,11 @@ impl<T> Parser<T> where T: Iterator<Item=LexerResult> {
let parser = self.parsers.last_mut().expect("couldn't get a parser -- this is unexpected");
parser.parse(lex)
}
fn parse_none(&mut self) -> NodeParseResult {
let parser = self.parsers.last_mut().expect("couldn't get a parser -- this is unexpected");
parser.none()
}
}
impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
@ -59,7 +64,11 @@ impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
Some(NodeParseResult::Continue) => self.input.next(),
Some(NodeParseResult::Complete{ obj }) => {
self.parsers.pop();
// TODO: Handle obj
if self.parsers.len() == 0 && input_lex.is_none() {
// We are done.
out = Some(Ok(obj));
break;
}
self.input.next()
},
Some(NodeParseResult::Push{ next }) => {
@ -87,7 +96,7 @@ impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
// TODO: We didn't get a Lex from the input, which means the
// input is done. If there's any parse result waiting
// around, clean it up and return it or return an error.
break;
result = Some(self.parse_none());
}
}
}

View file

@ -46,4 +46,9 @@ impl NodeParser for ListParser {
}
}
}
fn none(&mut self) -> NodeParseResult {
let msg = format!("Unmatched paren, found EOF");
NodeParseResult::Error { msg }
}
}

View file

@ -33,4 +33,6 @@ impl NodeParseResult {
/// nodes it encounters.
pub trait NodeParser: Debug {
fn parse(&mut self, lex: &Lex) -> NodeParseResult;
/// Called on a NodeParser when None is encountered in the input.
fn none(&mut self) -> NodeParseResult;
}

View file

@ -3,6 +3,7 @@
*/
use sibillexer::{Lex, Token};
use sibiltypes::Obj;
use list_parser::ListParser;
use node_parser::{NodeParser, NodeParseResult};
use sym_parser::SymParser;
@ -35,5 +36,9 @@ impl NodeParser for ProgramParser {
}
}
}
fn none(&mut self) -> NodeParseResult {
NodeParseResult::Complete { obj: Obj::Null }
}
}

View file

@ -19,9 +19,14 @@ impl NodeParser for SymParser {
NodeParseResult::Complete { obj: obj }
}
_ => {
let msg = format!("Expected symbol found {:?}", lex);
let msg = format!("Expected symbol, found {:?}", lex);
NodeParseResult::error(msg)
}
}
}
fn none(&mut self) -> NodeParseResult {
let msg = format!("Expected symbol, found EOF");
NodeParseResult::Error { msg }
}
}