Fix tests for conditionals

This commit is contained in:
greg 2017-01-09 20:41:42 -08:00
parent 5923cc2317
commit 1631bb0a04

View File

@ -209,6 +209,20 @@ macro_rules! expect_identifier {
} }
} }
macro_rules! skip_whitespace {
($_self: expr) => {
loop {
match $_self.peek() {
Some(ref t) if is_delimiter(t) => {
$_self.next();
continue;
}
_ => break,
}
}
}
}
macro_rules! delimiter_block { macro_rules! delimiter_block {
($_self: expr, $try_parse: ident, $($break_pattern: pat)|+) => { ($_self: expr, $try_parse: ident, $($break_pattern: pat)|+) => {
{ {
@ -407,33 +421,19 @@ impl Parser {
use self::Expression::*; use self::Expression::*;
expect!(self, Keyword(Kw::If)); expect!(self, Keyword(Kw::If));
let test = try!(self.expression()); let test = try!(self.expression());
loop { skip_whitespace!(self);
match self.peek() {
Some(ref t) if is_delimiter(t) => {
self.next();
continue;
}
_ => break,
}
}
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
loop { skip_whitespace!(self);
match self.peek() {
Some(ref t) if is_delimiter(t) => {
self.next();
continue;
}
_ => break,
}
}
let then_block = delimiter_block!( let then_block = delimiter_block!(
self, self,
expression, expression,
Some(RCurlyBrace) Some(RCurlyBrace)
); );
expect!(self, RCurlyBrace); expect!(self, RCurlyBrace);
skip_whitespace!(self);
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();
skip_whitespace!(self);
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
let else_exprs = delimiter_block!( let else_exprs = delimiter_block!(
self, self,
@ -547,13 +547,30 @@ mod tests {
_ => panic!(), _ => panic!(),
} }
/* let t2 = r"
let t2 = "if null\n{\n20\n}\nelse {\n40\n}"; if null {
20
} else {
40
}
";
let tokens2 = tokenizer::tokenize(t2).unwrap(); let tokens2 = tokenizer::tokenize(t2).unwrap();
match parse(&tokens2, &[]).unwrap()[..] { match parse(&tokens2, &[]).unwrap()[..] {
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (), [ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
_ => panic!(), _ => panic!(),
} }
*/
let t2 = r"
if null {
20 } else
{
40
}
";
let tokens3 = tokenizer::tokenize(t2).unwrap();
match parse(&tokens3, &[]).unwrap()[..] {
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
_ => panic!(),
}
} }
} }