Add delimiter_block macro

This commit is contained in:
greg 2017-01-05 02:20:23 -08:00
parent c227ad656f
commit e1ce54aece

View File

@ -162,6 +162,26 @@ macro_rules! expect_identifier {
} }
} }
macro_rules! delimiter_block {
($_self:expr, $try_parse: ident, $break_pattern: pat) => {
{
let mut acc = Vec::new();
loop {
match $_self.peek() {
None => break,
Some(ref t) if is_delimiter(t) => { $_self.next(); continue; },
$break_pattern => break,
_ => {
let a = try!($_self.$try_parse());
acc.push(a);
}
}
}
acc
}
}
}
fn is_delimiter(token: &Token) -> bool { fn is_delimiter(token: &Token) -> bool {
match *token { match *token {
Newline | Semicolon => true, Newline | Semicolon => true,
@ -255,20 +275,12 @@ impl Parser {
} }
fn body(&mut self) -> ParseResult<Vec<Statement>> { fn body(&mut self) -> ParseResult<Vec<Statement>> {
let mut statements = Vec::new();
loop { let statements = delimiter_block!(
match self.peek() { self,
Some(ref t) if is_delimiter(t) => { statement,
self.next(); Some(Keyword(Kw::End))
continue; );
}
Some(Keyword(Kw::End)) => break,
_ => {
let statement = try!(self.statement());
statements.push(statement);
}
}
}
Ok(statements) Ok(statements)
} }