make parse_method! macro more naturalistic

This commit is contained in:
greg 2017-09-16 13:46:30 -07:00
parent 565461e1db
commit 66d10604ba
1 changed files with 8 additions and 23 deletions

View File

@ -241,7 +241,7 @@ type_decl := 'type' type_format
type_format := 'alias' '=' type | type_constructor type_format := 'alias' '=' type | type_constructor
type_constructor := capital_ident '=' type_rhs type_constructor := capital_ident '=' type_rhs
type_rhs := struct_decl | type_variant ('|' type_variant)* 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 type_variant := capital_ident | tuple_type | capital_ident struct_decl
tuple_type := // something like Variant(a,b) tuple_type := // something like Variant(a,b)
type := // something like Type[A[b]] type := // something like Type[A[b]]
@ -440,34 +440,19 @@ impl Operation {
} }
macro_rules! parse_method { 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 { fn $name(&mut $self) -> $type {
let next_token = $self.peek(); let next_token = $self.peek();
let record = ParseRecord(format!("production {}, Token: {:?}", stringify!($name), next_token)); let record = ParseRecord(format!("production {}, Token: {:?}", stringify!($name), next_token));
$self.parse_record.push(record); $self.parse_record.push(record);
$body $body
} }
} };
} }
impl Parser { impl Parser {
/* parse_method!(program(&mut self) -> ParseResult<AST> {
fn program(&mut self) -> ParseResult<AST> {
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<AST>, {
let mut statements = Vec::new(); let mut statements = Vec::new();
loop { loop {
match self.peek() { match self.peek() {
@ -482,7 +467,7 @@ impl Parser {
Ok(AST(statements)) Ok(AST(statements))
}); });
parse_method!(statement, self, ParseResult<Statement>, { parse_method!(statement(&mut self) -> ParseResult<Statement> {
//TODO handle error recovery here //TODO handle error recovery here
match self.peek() { match self.peek() {
Keyword(Alias) => self.type_alias().map(|alias| { Statement::Declaration(alias) }), Keyword(Alias) => self.type_alias().map(|alias| { Statement::Declaration(alias) }),
@ -530,9 +515,9 @@ impl Parser {
Ok(vec!()) Ok(vec!())
} }
fn expression(&mut self) -> ParseResult<Expression> { parse_method!(expression(&mut self) -> ParseResult<Expression> {
self.precedence_expr(Operation::min_precedence()) self.precedence_expr(Operation::min_precedence())
} });
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/ // 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<Expression> { fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {