Convert parsing to method-based

This commit is contained in:
greg 2016-01-12 01:58:12 -08:00
parent b1b6672399
commit 674f70a428

View File

@ -58,23 +58,48 @@ impl ParseError {
op := '+', '-', etc. op := '+', '-', etc.
*/ */
pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult<AST> { struct Parser {
tokens: Vec<Token>,
}
impl Parser {
fn initialize(tokens: &[Token]) -> Parser {
let mut tokens = tokens.to_vec();
tokens.reverse();
Parser { tokens: tokens }
}
fn peek(&mut self) -> Option<Token> {
self.tokens.last().map(|x| x.clone())
}
fn next(&mut self) {
self.tokens.pop();
}
}
macro_rules! expect {
($self_:expr, $token:pat, $error:expr) => {
match $self_.peek() {
Some($token) => {$self_.next();},
_ => return ParseError::result_from_str($error)
}
}
}
impl Parser {
fn program(&mut self) -> ParseResult<AST> {
use tokenizer::Token::*; use tokenizer::Token::*;
let mut ast = Vec::new(); //TODO have this come from previously-parsed tree
let mut rest: Vec<Token> = tokens.to_vec();
rest.reverse();
let mut ast = parsed_tree.to_vec();
loop { loop {
let cur_tok = match rest.last() { let cur_tok = match self.peek() {
Some(t) => t.clone(), Some(t) => t.clone(),
None => break None => break
}; };
let result: ParseResult<ASTNode> = match cur_tok { let result: ParseResult<ASTNode> = match cur_tok {
Newline | Semicolon => { rest.pop(); continue}, Newline | Semicolon => { self.next(); continue},
_ => parse_statement(&mut rest) _ => self.statement()
}; };
match result { match result {
@ -86,56 +111,47 @@ pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult<AST> {
Ok(ast) Ok(ast)
} }
macro_rules! expect { fn statement(&mut self) -> ParseResult<ASTNode> {
($token:pat, $tokens:expr, $error:expr) => {
match $tokens.pop() {
Some($token) => (),
_ => return ParseError::result_from_str($error)
};
}
}
fn parse_statement(tokens: &mut Vec<Token>) -> ParseResult<ASTNode> {
use tokenizer::Token::*; use tokenizer::Token::*;
let cur_tok: Token = tokens.last().unwrap().clone(); let cur_tok: Token = self.peek().unwrap().clone();
let node: ASTNode = match cur_tok { let node: ASTNode = match cur_tok {
Keyword(Kw::Fn) => try!(parse_declaration(tokens)), Keyword(Kw::Fn) => try!(self.declaration()),
_ => try!(parse_expression(tokens)) _ => try!(self.expression())
}; };
Ok(node) Ok(node)
} }
fn parse_declaration(tokens: &mut Vec<Token>) -> ParseResult<ASTNode> { fn declaration(&mut self) -> ParseResult<ASTNode> {
use tokenizer::Token::*; use tokenizer::Token::*;
expect!(Fn, tokens, "Expected 'fn'"); expect!(self, Fn, "Expected 'fn'");
let prototype = try!(parse_prototype(tokens)); let prototype = try!(self.prototype());
let body = try!(parse_body(tokens)); let body = try!(self.body());
Ok(ASTNode::FuncNode(Function { prototype: prototype, body: body})) Ok(ASTNode::FuncNode(Function { prototype: prototype, body: body}))
} }
fn parse_prototype(tokens: &mut Vec<Token>) -> ParseResult<Prototype> { fn prototype(&mut self) -> ParseResult<Prototype> {
use tokenizer::Token::*; use tokenizer::Token::*;
let name: String = match tokens.pop() { let name: String = match self.peek() {
Some(Identifier(name)) => name, Some(Identifier(name)) => {self.next(); name},
_ => return ParseError::result_from_str("Expected identifier") _ => return ParseError::result_from_str("Expected identifier")
}; };
expect!(LParen, tokens, "Expected '('"); expect!(self, LParen, "Expected '('");
let mut args: Vec<String> = try!(parse_identlist(tokens)); let mut args: Vec<String> = try!(self.identlist());
expect!(RParen, tokens, "Expected ')'"); expect!(self, RParen, "Expected ')'");
Ok(Prototype {name: name, args: args}) Ok(Prototype {name: name, args: args})
} }
fn parse_identlist(tokens: &mut Vec<Token>) -> ParseResult<Vec<String>> { fn identlist(&mut self) -> ParseResult<Vec<String>> {
use tokenizer::Token::*; use tokenizer::Token::*;
let mut args: Vec<String> = Vec::new(); let mut args: Vec<String> = Vec::new();
loop { loop {
match tokens.pop() { match self.peek() {
Some(Identifier(name)) => { Some(Identifier(name)) => {
args.push(name); args.push(name);
if let Some(&Comma) = tokens.last() { self.next();
tokens.pop(); if let Some(Comma) = self.peek() {
self.next();
} else { } else {
break; break;
} }
@ -148,15 +164,21 @@ fn parse_identlist(tokens: &mut Vec<Token>) -> ParseResult<Vec<String>> {
Ok(args) Ok(args)
} }
fn parse_body(tokens: &mut Vec<Token>) -> ParseResult<Expression> { fn body(&mut self) -> ParseResult<Expression> {
use tokenizer::Token::*; use tokenizer::Token::*;
tokens.pop(); self.next();
Ok(Expression::Number(101.01)) Ok(Expression::Number(101.01))
} }
fn parse_expression(tokens: &mut Vec<Token>) -> ParseResult<ASTNode> { fn expression(&mut self) -> ParseResult<ASTNode> {
use tokenizer::Token::*; use tokenizer::Token::*;
tokens.pop(); self.next();
Ok(ASTNode::ExprNode(Expression::StringLiteral("Expr".to_string()))) Ok(ASTNode::ExprNode(Expression::StringLiteral("Expr".to_string())))
} }
}
pub fn parse(tokens: &[Token], _parsed_tree: &[ASTNode]) -> ParseResult<AST> {
let mut parser = Parser::initialize(tokens);
parser.program()
}