make parse_method! macro more naturalistic

This commit is contained in:
greg 2017-09-16 13:46:30 -07:00
parent 565461e1db
commit 66d10604ba

View File

@ -440,34 +440,19 @@ impl Operation {
} }
macro_rules! parse_method { macro_rules! parse_method {
($name:ident, $self:ident, $type:ty, $body:tt) => {
($name:ident(&mut $self:ident) -> $type:ty $body:block) => {
fn $name(&mut $self) -> $type { fn $name(&mut $self) -> $type {
let next_token = $self.peek(); let next_token = $self.peek();
let record = ParseRecord(format!("production {}, Token: {:?}", stringify!($name), next_token)); let record = ParseRecord(format!("production {}, Token: {:?}", stringify!($name), next_token));
$self.parse_record.push(record); $self.parse_record.push(record);
$body $body
} }
} };
} }
impl Parser { impl Parser {
/* parse_method!(program(&mut self) -> ParseResult<AST> {
fn program(&mut self) -> ParseResult<AST> {
let mut statements = Vec::new();
loop {
match self.peek() {
EOF => break,
Newline | Semicolon => {
self.next();
continue;
},
_ => statements.push(self.statement()?),
}
}
Ok(AST(statements))
}
*/
parse_method!(program, self, ParseResult<AST>, {
let mut statements = Vec::new(); let mut statements = Vec::new();
loop { loop {
match self.peek() { match self.peek() {
@ -482,7 +467,7 @@ impl Parser {
Ok(AST(statements)) Ok(AST(statements))
}); });
parse_method!(statement, self, ParseResult<Statement>, { parse_method!(statement(&mut self) -> ParseResult<Statement> {
//TODO handle error recovery here //TODO handle error recovery here
match self.peek() { match self.peek() {
Keyword(Alias) => self.type_alias().map(|alias| { Statement::Declaration(alias) }), Keyword(Alias) => self.type_alias().map(|alias| { Statement::Declaration(alias) }),
@ -530,9 +515,9 @@ impl Parser {
Ok(vec!()) Ok(vec!())
} }
fn expression(&mut self) -> ParseResult<Expression> { parse_method!(expression(&mut self) -> ParseResult<Expression> {
self.precedence_expr(Operation::min_precedence()) self.precedence_expr(Operation::min_precedence())
} });
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/ // this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> { fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {