[parser] Hand off lex to current parser and collect the result

This commit is contained in:
Eryn Wells 2018-08-24 08:05:39 -07:00
parent d13396bb5d
commit dbd6329dd6
5 changed files with 15 additions and 7 deletions

View file

@ -12,6 +12,7 @@ mod sym_parser;
use std::iter::Peekable; use std::iter::Peekable;
use sibillexer::Result as LexerResult; use sibillexer::Result as LexerResult;
use sibillexer::Lex;
use sibiltypes::Obj; use sibiltypes::Obj;
use node_parser::{NodeParser, NodeParseResult}; use node_parser::{NodeParser, NodeParseResult};
use program_parser::ProgramParser; use program_parser::ProgramParser;
@ -38,6 +39,11 @@ impl<T> Parser<T> where T: Iterator<Item=LexerResult> {
parsers: vec!(program_parser) parsers: vec!(program_parser)
} }
} }
fn parse_lex(&mut self, lex: &Lex) -> NodeParseResult {
let parser = self.parsers.last_mut().expect("couldn't get a parser -- this is unexpected");
parser.parse(lex)
}
} }
impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> { impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
@ -51,15 +57,17 @@ impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
Some(Ok(ref lex)) => { Some(Ok(ref lex)) => {
// TODO: Valid Lex from our input. Hand it off to the // TODO: Valid Lex from our input. Hand it off to the
// current parser and process the result. // current parser and process the result.
result = Some(self.parse_lex(lex));
}, },
Some(Err(ref error)) => { Some(Err(ref error)) => {
// TODO: Lexer error. Throw it up and out. // TODO: Lexer error. Throw it up and out.
out = Some(Err(ParseError::LexerError { msg: error.msg().to_string() })); out = Some(Err(ParseError::LexerError { msg: error.msg().to_string() }));
break;
}, },
None => { None => {
// TODO: We didn't get a Lex from the input. If there's any // TODO: We didn't get a Lex from the input, which means the
// parse result waiting around, clean it up and return it or // input is done. If there's any parse result waiting
// return an error. // around, clean it up and return it or return an error.
break; break;
} }
} }

View file

@ -21,7 +21,7 @@ impl ListParser {
} }
impl NodeParser for ListParser { impl NodeParser for ListParser {
fn parse(&mut self, lex: Lex) -> NodeParseResult { fn parse(&mut self, lex: &Lex) -> NodeParseResult {
match lex.token() { match lex.token() {
Token::LeftParen => { Token::LeftParen => {
match self.list { match self.list {

View file

@ -32,5 +32,5 @@ impl NodeParseResult {
/// through the stream of tokens, new NodeParsers are created to handle the /// through the stream of tokens, new NodeParsers are created to handle the
/// nodes it encounters. /// nodes it encounters.
pub trait NodeParser: Debug { pub trait NodeParser: Debug {
fn parse(&mut self, lex: Lex) -> NodeParseResult; fn parse(&mut self, lex: &Lex) -> NodeParseResult;
} }

View file

@ -17,7 +17,7 @@ impl ProgramParser {
} }
impl NodeParser for ProgramParser { impl NodeParser for ProgramParser {
fn parse(&mut self, lex: Lex) -> NodeParseResult { fn parse(&mut self, lex: &Lex) -> NodeParseResult {
match lex.token() { match lex.token() {
Token::LeftParen => { Token::LeftParen => {
let parser = ListParser::new(); let parser = ListParser::new();

View file

@ -10,7 +10,7 @@ use node_parser::{NodeParser, NodeParseResult};
pub struct SymParser; pub struct SymParser;
impl NodeParser for SymParser { impl NodeParser for SymParser {
fn parse(&mut self, lex: Lex) -> NodeParseResult { fn parse(&mut self, lex: &Lex) -> NodeParseResult {
match lex.token() { match lex.token() {
Token::Id => { Token::Id => {
let value = String::from(lex.value()); let value = String::from(lex.value());