From c0289f238fcd7594be1e0bf70268a1c227219a4b Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 10 Mar 2018 22:10:13 -0800 Subject: [PATCH] Starting on macro work --- src/schala_lang/autoparser.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/schala_lang/autoparser.rs b/src/schala_lang/autoparser.rs index da1c27c..93eb16f 100644 --- a/src/schala_lang/autoparser.rs +++ b/src/schala_lang/autoparser.rs @@ -37,21 +37,32 @@ impl AutoParser { let ast = self.program(); (ast, vec![]) } -} - -impl AutoParser { fn program(&mut self) -> ParseResult { let etype = self.literal()?; Ok(AST(vec![Statement::ExpressionStatement(Expression(etype, None))])) } +} - fn literal(&mut self) -> ParseResult { - Ok(match self.next() { - Keyword(Kw::True) => ExpressionType::BoolLiteral(true), - Keyword(Kw::False) => ExpressionType::BoolLiteral(false), - _ => return ParseError::new("bad!") - }) - } +macro_rules! bnf_rule { + ($self:ident, $type:ty, $rule:ident := $( ($pat:pat => $e:expr) )|*) => { + fn $rule(&mut $self) -> ParseResult<$type> { + Ok(match $self.next() { + $( + $pat => $e, + )* + _ => 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))); }