Changes to make the code more concise
This commit is contained in:
parent
fdaf4c302c
commit
4f96abd7d9
@ -124,14 +124,10 @@ impl Parser {
|
|||||||
fn program(&mut self) -> ParseResult<AST> {
|
fn program(&mut self) -> ParseResult<AST> {
|
||||||
let mut ast = Vec::new(); //TODO have this come from previously-parsed tree
|
let mut ast = Vec::new(); //TODO have this come from previously-parsed tree
|
||||||
loop {
|
loop {
|
||||||
let cur_tok = match self.peek() {
|
let result: ParseResult<ASTNode> = match self.peek() {
|
||||||
Some(t) => t.clone(),
|
Some(ref t) if is_delimiter(&t) => { self.next(); continue},
|
||||||
None => break
|
Some(_) => self.statement(),
|
||||||
};
|
None => break,
|
||||||
|
|
||||||
let result: ParseResult<ASTNode> = match cur_tok {
|
|
||||||
ref t if is_delimiter(&t) => { self.next(); continue},
|
|
||||||
_ => self.statement()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
@ -145,10 +141,10 @@ impl Parser {
|
|||||||
|
|
||||||
fn statement(&mut self) -> ParseResult<ASTNode> {
|
fn statement(&mut self) -> ParseResult<ASTNode> {
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
let cur_tok: Token = self.peek().unwrap().clone();
|
let node: ASTNode = match self.peek() {
|
||||||
let node: ASTNode = match cur_tok {
|
Some(Keyword(Kw::Fn)) => try!(self.declaration()),
|
||||||
Keyword(Kw::Fn) => try!(self.declaration()),
|
Some(_) => ASTNode::ExprNode(try!(self.expression())),
|
||||||
_ => ASTNode::ExprNode(try!(self.expression())),
|
None => panic!("unexpected end of tokens"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(node)
|
Ok(node)
|
||||||
@ -260,16 +256,14 @@ impl Parser {
|
|||||||
|
|
||||||
fn primary_expression(&mut self) -> ParseResult<Expression> {
|
fn primary_expression(&mut self) -> ParseResult<Expression> {
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
let expr = match self.peek() {
|
Ok(match self.peek() {
|
||||||
Some(NumLiteral(n)) => { self.next(); Expression::Number(n) },
|
Some(NumLiteral(n)) => { self.next(); Expression::Number(n) },
|
||||||
Some(StrLiteral(s)) => { self.next(); Expression::StringLiteral(s) },
|
Some(StrLiteral(s)) => { self.next(); Expression::StringLiteral(s) },
|
||||||
Some(Identifier(_)) => { try!(self.identifier_expr()) },
|
Some(Identifier(_)) => { try!(self.identifier_expr()) },
|
||||||
Some(Token::LParen) => { try!(self.paren_expr()) }
|
Some(Token::LParen) => { try!(self.paren_expr()) }
|
||||||
Some(_) => return ParseError::result_from_str("Expected primary expression"),
|
Some(_) => return ParseError::result_from_str("Expected primary expression"),
|
||||||
None => return ParseError::result_from_str("Expected primary expression received EoI")
|
None => return ParseError::result_from_str("Expected primary expression received EoI")
|
||||||
};
|
})
|
||||||
|
|
||||||
Ok(expr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
Loading…
Reference in New Issue
Block a user