Parser simplifications, renames, etc.

This commit is contained in:
greg 2017-01-05 01:58:22 -08:00
parent b45d09e81a
commit c227ad656f

View File

@ -206,7 +206,7 @@ impl Parser {
fn declaration(&mut self) -> ParseResult<Statement> { fn declaration(&mut self) -> ParseResult<Statement> {
expect!(self, Keyword(Kw::Fn)); expect!(self, Keyword(Kw::Fn));
let prototype = try!(self.prototype()); let prototype = try!(self.prototype());
let body: Vec<Statement> = try!(self.body()); let body = try!(self.body());
expect!(self, Keyword(Kw::End)); expect!(self, Keyword(Kw::End));
Ok(Statement::FuncDefNode(Function { Ok(Statement::FuncDefNode(Function {
prototype: prototype, prototype: prototype,
@ -215,9 +215,9 @@ impl Parser {
} }
fn prototype(&mut self) -> ParseResult<Prototype> { fn prototype(&mut self) -> ParseResult<Prototype> {
let name: Rc<String> = expect_identifier!(self); let name = expect_identifier!(self);
expect!(self, LParen); expect!(self, LParen);
let parameters: Vec<Rc<String>> = try!(self.identlist()); let parameters = try!(self.identlist());
expect!(self, RParen); expect!(self, RParen);
Ok(Prototype { Ok(Prototype {
name: name, name: name,
@ -226,34 +226,32 @@ impl Parser {
} }
fn identlist(&mut self) -> ParseResult<Vec<Rc<String>>> { fn identlist(&mut self) -> ParseResult<Vec<Rc<String>>> {
let mut args: Vec<Rc<String>> = Vec::new(); let mut args = Vec::new();
while let Some(Identifier(name)) = self.peek() { while let Some(Identifier(name)) = self.peek() {
args.push(name.clone()); args.push(name.clone());
self.next(); self.next();
if let Some(Comma) = self.peek() { match self.peek() {
self.next(); Some(Comma) => {self.next();},
} else { _ => break,
break;
} }
} }
Ok(args) Ok(args)
} }
fn exprlist(&mut self) -> ParseResult<Vec<Expression>> { fn exprlist(&mut self) -> ParseResult<Vec<Expression>> {
let mut args: Vec<Expression> = Vec::new(); let mut exprs = Vec::new();
loop { loop {
if let Some(RParen) = self.peek() { if let Some(RParen) = self.peek() {
break; break;
} }
let exp = try!(self.expression()); let exp = try!(self.expression());
args.push(exp); exprs.push(exp);
if let Some(Comma) = self.peek() { match self.peek() {
self.next(); Some(Comma) => {self.next();},
} else { _ => break,
break;
} }
} }
Ok(args) Ok(exprs)
} }
fn body(&mut self) -> ParseResult<Vec<Statement>> { fn body(&mut self) -> ParseResult<Vec<Statement>> {
@ -266,8 +264,8 @@ impl Parser {
} }
Some(Keyword(Kw::End)) => break, Some(Keyword(Kw::End)) => break,
_ => { _ => {
let ast_node = try!(self.statement()); let statement = try!(self.statement());
statements.push(ast_node); statements.push(statement);
} }
} }
} }
@ -381,7 +379,7 @@ impl Parser {
None | None |
Some(Keyword(Kw::Else)) | Some(Keyword(Kw::Else)) |
Some(Keyword(Kw::End)) => break, Some(Keyword(Kw::End)) => break,
Some(Semicolon) | Some(Newline) => { Some(ref t) if is_delimiter(t) => {
self.next(); self.next();
continue; continue;
} }