mirror of
https://github.com/danog/parser.git
synced 2024-11-27 04:14:55 +01:00
Merge pull request #1 from Dohxis/feat/one-liner-if-statement
This commit is contained in:
commit
61c76ad49a
@ -337,16 +337,22 @@ impl Parser {
|
||||
|
||||
expect!(self, TokenKind::RightParen, "expected )");
|
||||
|
||||
// TODO: Support one-liner if statements.
|
||||
expect!(self, TokenKind::LeftBrace, "expected {");
|
||||
let body_end_token = if self.current.kind == TokenKind::LeftBrace {
|
||||
self.next();
|
||||
|
||||
TokenKind::RightBrace
|
||||
} else {
|
||||
TokenKind::SemiColon
|
||||
};
|
||||
|
||||
let mut then = Block::new();
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightBrace {
|
||||
while ! self.is_eof() && self.current.kind != body_end_token {
|
||||
then.push(self.statement()?);
|
||||
}
|
||||
|
||||
// TODO: Support one-liner if statements.
|
||||
if body_end_token == TokenKind::RightBrace {
|
||||
expect!(self, TokenKind::RightBrace, "expected }");
|
||||
}
|
||||
|
||||
Statement::If { condition, then }
|
||||
},
|
||||
@ -1141,6 +1147,22 @@ mod tests {
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_liner_if_statement() {
|
||||
assert_ast("\
|
||||
<?php
|
||||
|
||||
if($name)
|
||||
return $name;", &[
|
||||
Statement::If {
|
||||
condition: Expression::Variable("name".into()),
|
||||
then: vec![
|
||||
Statement::Return { value: Some(Expression::Variable("name".into())) }
|
||||
],
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo() {
|
||||
assert_ast("<?php echo 1;", &[
|
||||
|
Loading…
Reference in New Issue
Block a user