From ea08f8cab83de70c71ae9cbeb59ddfee19435518 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 9 Sep 2017 01:25:11 -0700 Subject: [PATCH] More parse infra --- src/schala_lang/parsing.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index e19ecda..1ca16c4 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -4,9 +4,10 @@ use std::collections::HashMap; use std::rc::Rc; use std::iter::{Enumerate, Peekable}; use self::itertools::Itertools; +use std::vec::IntoIter; use std::str::Chars; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum TokenType { Newline, Semicolon, @@ -24,6 +25,7 @@ pub enum TokenType { Identifier(Rc), Keyword(Kw), + EOF, Error(String), } @@ -274,8 +276,26 @@ postop := ε | LParen exprlist RParen | LBracket expression RBracket op := '+', '-', etc. */ +type TokenIter = Peekable>; + struct Parser { - tokens: Vec, + tokens: TokenIter, +} + +impl Parser { + fn new(input: Vec) -> Parser { + Parser { tokens: input.into_iter().peekable() } + } + + fn peek(&mut self) -> TokenType { + self.tokens.peek().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) + } + + fn program(&mut self) -> ParseResult { + use self::Statement::*; use self::Declaration::*; use self::Expression::*; + let statements = vec![Expression(UnsignedIntLiteral(1))]; + Ok(AST(statements)) + } } pub struct ParseError { @@ -306,17 +326,6 @@ pub enum Expression { FloatLiteral(f64), } -impl Parser { - fn new(input: Vec) -> Parser { - Parser { tokens: input } - } - - fn program(&mut self) -> ParseResult { - use self::Statement::*; use self::Declaration::*; use self::Expression::*; - let statements = vec![Expression(UnsignedIntLiteral(1))]; - Ok(AST(statements)) - } -} pub fn parse(input: Vec) -> Result { let mut parser = Parser::new(input);