From dbd6329dd641f63a7f8e119b704bca1aad510257 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 24 Aug 2018 08:05:39 -0700 Subject: [PATCH] [parser] Hand off lex to current parser and collect the result --- parser/src/lib.rs | 14 +++++++++++--- parser/src/list_parser.rs | 2 +- parser/src/node_parser.rs | 2 +- parser/src/program_parser.rs | 2 +- parser/src/sym_parser.rs | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/parser/src/lib.rs b/parser/src/lib.rs index ad8d394..f0c754a 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -12,6 +12,7 @@ mod sym_parser; use std::iter::Peekable; use sibillexer::Result as LexerResult; +use sibillexer::Lex; use sibiltypes::Obj; use node_parser::{NodeParser, NodeParseResult}; use program_parser::ProgramParser; @@ -38,6 +39,11 @@ impl Parser where T: Iterator { 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 Iterator for Parser where T: Iterator { @@ -51,15 +57,17 @@ impl Iterator for Parser where T: Iterator { Some(Ok(ref lex)) => { // TODO: Valid Lex from our input. Hand it off to the // current parser and process the result. + result = Some(self.parse_lex(lex)); }, Some(Err(ref error)) => { // TODO: Lexer error. Throw it up and out. out = Some(Err(ParseError::LexerError { msg: error.msg().to_string() })); + break; }, None => { - // TODO: We didn't get a Lex from the input. If there's any - // parse result waiting around, clean it up and return it or - // return an error. + // 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; } } diff --git a/parser/src/list_parser.rs b/parser/src/list_parser.rs index a90db80..3e993fb 100644 --- a/parser/src/list_parser.rs +++ b/parser/src/list_parser.rs @@ -21,7 +21,7 @@ impl ListParser { } impl NodeParser for ListParser { - fn parse(&mut self, lex: Lex) -> NodeParseResult { + fn parse(&mut self, lex: &Lex) -> NodeParseResult { match lex.token() { Token::LeftParen => { match self.list { diff --git a/parser/src/node_parser.rs b/parser/src/node_parser.rs index 062216e..7effe29 100644 --- a/parser/src/node_parser.rs +++ b/parser/src/node_parser.rs @@ -32,5 +32,5 @@ impl NodeParseResult { /// through the stream of tokens, new NodeParsers are created to handle the /// nodes it encounters. pub trait NodeParser: Debug { - fn parse(&mut self, lex: Lex) -> NodeParseResult; + fn parse(&mut self, lex: &Lex) -> NodeParseResult; } diff --git a/parser/src/program_parser.rs b/parser/src/program_parser.rs index 15096a7..d40226f 100644 --- a/parser/src/program_parser.rs +++ b/parser/src/program_parser.rs @@ -17,7 +17,7 @@ impl ProgramParser { } impl NodeParser for ProgramParser { - fn parse(&mut self, lex: Lex) -> NodeParseResult { + fn parse(&mut self, lex: &Lex) -> NodeParseResult { match lex.token() { Token::LeftParen => { let parser = ListParser::new(); diff --git a/parser/src/sym_parser.rs b/parser/src/sym_parser.rs index 6988a29..def12fe 100644 --- a/parser/src/sym_parser.rs +++ b/parser/src/sym_parser.rs @@ -10,7 +10,7 @@ use node_parser::{NodeParser, NodeParseResult}; pub struct SymParser; impl NodeParser for SymParser { - fn parse(&mut self, lex: Lex) -> NodeParseResult { + fn parse(&mut self, lex: &Lex) -> NodeParseResult { match lex.token() { Token::Id => { let value = String::from(lex.value());