Moved conditionals to delimiter_block! syntax

This commit is contained in:
greg 2017-01-05 02:51:41 -08:00
parent db92292569
commit 9801f53a17
1 changed files with 16 additions and 35 deletions

View File

@ -163,14 +163,14 @@ macro_rules! expect_identifier {
} }
macro_rules! delimiter_block { macro_rules! delimiter_block {
($_self:expr, $try_parse: ident, $break_pattern: pat) => { ($_self:expr, $try_parse: ident, $($break_pattern: pat)|+) => {
{ {
let mut acc = Vec::new(); let mut acc = Vec::new();
loop { loop {
match $_self.peek() { match $_self.peek() {
None => break, None => break,
Some(ref t) if is_delimiter(t) => { $_self.next(); continue; }, Some(ref t) if is_delimiter(t) => { $_self.next(); continue; },
$break_pattern => break, $($break_pattern)|+ => break,
_ => { _ => {
let a = try!($_self.$try_parse()); let a = try!($_self.$try_parse());
acc.push(a); acc.push(a);
@ -374,6 +374,7 @@ impl Parser {
expect!(self, Keyword(Kw::If)); expect!(self, Keyword(Kw::If));
let test = try!(self.expression()); let test = try!(self.expression());
loop { loop {
match self.peek() { match self.peek() {
Some(ref t) if is_delimiter(t) => { Some(ref t) if is_delimiter(t) => {
@ -385,39 +386,19 @@ impl Parser {
} }
expect!(self, Keyword(Kw::Then)); expect!(self, Keyword(Kw::Then));
let mut then_block = VecDeque::new(); let then_block = delimiter_block!(
loop { self,
match self.peek() { expression,
None | Some(Keyword(Kw::Else)) | Some(Keyword(Kw::End))
Some(Keyword(Kw::Else)) | );
Some(Keyword(Kw::End)) => break,
Some(ref t) if is_delimiter(t) => {
self.next();
continue;
}
_ => {
let exp = try!(self.expression());
then_block.push_back(exp);
}
}
}
let else_block = if let Some(Keyword(Kw::Else)) = self.peek() { let else_block = if let Some(Keyword(Kw::Else)) = self.peek() {
self.next(); self.next();
let mut else_exprs = VecDeque::new(); let else_exprs = delimiter_block!(
loop { self,
match self.peek() { expression,
None | Some(Keyword(Kw::End))
Some(Keyword(Kw::End)) => break, );
Some(Semicolon) | Some(Newline) => {
self.next();
continue;
}
_ => {
let exp = try!(self.expression());
else_exprs.push_back(exp);
}
}
}
Some(else_exprs) Some(else_exprs)
} else { } else {
None None
@ -425,8 +406,8 @@ impl Parser {
expect!(self, Keyword(Kw::End)); expect!(self, Keyword(Kw::End));
Ok(Conditional(Box::new(test), Ok(Conditional(Box::new(test),
Box::new(Block(then_block)), Box::new(Block(VecDeque::from(then_block))),
else_block.map(|list| Box::new(Block(list))))) else_block.map(|list| Box::new(Block(VecDeque::from(list))))))
} }
fn identifier_expr(&mut self) -> ParseResult<Expression> { fn identifier_expr(&mut self) -> ParseResult<Expression> {