Starting on macro work

This commit is contained in:
greg 2018-03-10 22:10:13 -08:00
parent 7afb9d47fc
commit c0289f238f
1 changed files with 21 additions and 10 deletions

View File

@ -37,21 +37,32 @@ impl AutoParser {
let ast = self.program(); let ast = self.program();
(ast, vec![]) (ast, vec![])
} }
}
impl AutoParser {
fn program(&mut self) -> ParseResult<AST> { fn program(&mut self) -> ParseResult<AST> {
let etype = self.literal()?; let etype = self.literal()?;
Ok(AST(vec![Statement::ExpressionStatement(Expression(etype, None))])) Ok(AST(vec![Statement::ExpressionStatement(Expression(etype, None))]))
} }
}
fn literal(&mut self) -> ParseResult<ExpressionType> { macro_rules! bnf_rule {
Ok(match self.next() { ($self:ident, $type:ty, $rule:ident := $( ($pat:pat => $e:expr) )|*) => {
Keyword(Kw::True) => ExpressionType::BoolLiteral(true), fn $rule(&mut $self) -> ParseResult<$type> {
Keyword(Kw::False) => ExpressionType::BoolLiteral(false), Ok(match $self.next() {
_ => return ParseError::new("bad!") $(
}) $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)));
} }