From 66d10604ba0cb6d5804c7875900bf75a7f685501 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 16 Sep 2017 13:46:30 -0700 Subject: [PATCH] make parse_method! macro more naturalistic --- src/schala_lang/parsing.rs | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 1003882..0ed4cbe 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -241,7 +241,7 @@ type_decl := 'type' type_format type_format := 'alias' '=' type | type_constructor type_constructor := capital_ident '=' type_rhs type_rhs := struct_decl | type_variant ('|' type_variant)* -struct_decl := 'struct' '{' (ident ':' type)* '}' +struct_decl := 'struct' '{' (ident ':' type)* '}' type_variant := capital_ident | tuple_type | capital_ident struct_decl tuple_type := // something like Variant(a,b) type := // something like Type[A[b]] @@ -440,34 +440,19 @@ impl Operation { } 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 { let next_token = $self.peek(); let record = ParseRecord(format!("production {}, Token: {:?}", stringify!($name), next_token)); $self.parse_record.push(record); $body } - } + }; } impl Parser { - /* - fn program(&mut self) -> ParseResult { - 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, { + parse_method!(program(&mut self) -> ParseResult { let mut statements = Vec::new(); loop { match self.peek() { @@ -482,7 +467,7 @@ impl Parser { Ok(AST(statements)) }); - parse_method!(statement, self, ParseResult, { + parse_method!(statement(&mut self) -> ParseResult { //TODO handle error recovery here match self.peek() { Keyword(Alias) => self.type_alias().map(|alias| { Statement::Declaration(alias) }), @@ -530,9 +515,9 @@ impl Parser { Ok(vec!()) } - fn expression(&mut self) -> ParseResult { + parse_method!(expression(&mut self) -> ParseResult { self.precedence_expr(Operation::min_precedence()) - } + }); // 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 {