From fb26293157b04d1c295e5deee9dcce6f955f334f Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 10 Mar 2018 22:56:16 -0800 Subject: [PATCH] More work --- src/schala_lang/autoparser.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/schala_lang/autoparser.rs b/src/schala_lang/autoparser.rs index 93eb16f..60c2d19 100644 --- a/src/schala_lang/autoparser.rs +++ b/src/schala_lang/autoparser.rs @@ -43,26 +43,38 @@ impl AutoParser { } } +macro_rules! expand_match_var { + (($pat:pat => $e:expr)) => { $pat }; + (nonterm ($pat:pat => $e:expr)) => { $pat }; +} + +macro_rules! expand_match_expr { + ($self:ident, ($pat:pat => $e:expr)) => { + { $self.next(); $e } + }; + ($self:ident, nonterm ($pat:pat => $e:expr)) => { + { $self.next(); $e } + }; +} + macro_rules! bnf_rule { - ($self:ident, $type:ty, $rule:ident := $( ($pat:pat => $e:expr) )|*) => { + ($self:ident, $type:ty, $rule:ident := $( $rule_clauses:tt )|*) => { fn $rule(&mut $self) -> ParseResult<$type> { - Ok(match $self.next() { - $( - $pat => $e, - )* + Ok(match $self.peek() { + $( + expand_match_var!($rule_clauses) => expand_match_expr!($self, $rule_clauses), + )* _ => return ParseError::new("Not found"), }) } }; } -macro_rules! expand_match_arm { - (($pat:pat => $e:expr)) => { $pat => $e }; -} - impl AutoParser { bnf_rule!(self, ExpressionType, literal := - (Keyword(Kw::True) => ExpressionType::BoolLiteral(true)) | (Keyword(Kw::False) => ExpressionType::BoolLiteral(false))); + (Keyword(Kw::True) => ExpressionType::BoolLiteral(true)) | + (Keyword(Kw::False) => ExpressionType::BoolLiteral(false)) + ); }