From e1ce54aecec2b829026491fde19b1bef934ffea8 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 5 Jan 2017 02:20:23 -0800 Subject: [PATCH] Add delimiter_block macro --- src/parser.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index ab45543..00be9c5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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> { - 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) }