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))); }