[parser] Hand off lex to current parser and collect the result
This commit is contained in:
		
							parent
							
								
									d13396bb5d
								
							
						
					
					
						commit
						dbd6329dd6
					
				
					 5 changed files with 15 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -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<T> Parser<T> where T: Iterator<Item=LexerResult> {
 | 
			
		|||
            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> {
 | 
			
		||||
| 
						 | 
				
			
			@ -51,15 +57,17 @@ impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
 | 
			
		|||
                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;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue