Add delimiter_block macro

This commit is contained in:
greg 2017-01-05 02:20:23 -08:00
parent c227ad656f
commit e1ce54aece
1 changed files with 26 additions and 14 deletions

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 {
match *token {
Newline | Semicolon => true,
@ -255,20 +275,12 @@ impl Parser {
}
fn body(&mut self) -> ParseResult<Vec<Statement>> {
let mut statements = Vec::new();
loop {
match self.peek() {
Some(ref t) if is_delimiter(t) => {
self.next();
continue;
}
Some(Keyword(Kw::End)) => break,
_ => {
let statement = try!(self.statement());
statements.push(statement);
}
}
}
let statements = delimiter_block!(
self,
statement,
Some(Keyword(Kw::End))
);
Ok(statements)
}