mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +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 )");
|
expect!(self, TokenKind::RightParen, "expected )");
|
||||||
|
|
||||||
// TODO: Support one-liner if statements.
|
let body_end_token = if self.current.kind == TokenKind::LeftBrace {
|
||||||
expect!(self, TokenKind::LeftBrace, "expected {");
|
self.next();
|
||||||
|
|
||||||
|
TokenKind::RightBrace
|
||||||
|
} else {
|
||||||
|
TokenKind::SemiColon
|
||||||
|
};
|
||||||
|
|
||||||
let mut then = Block::new();
|
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()?);
|
then.push(self.statement()?);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Support one-liner if statements.
|
if body_end_token == TokenKind::RightBrace {
|
||||||
expect!(self, TokenKind::RightBrace, "expected }");
|
expect!(self, TokenKind::RightBrace, "expected }");
|
||||||
|
}
|
||||||
|
|
||||||
Statement::If { condition, then }
|
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]
|
#[test]
|
||||||
fn echo() {
|
fn echo() {
|
||||||
assert_ast("<?php echo 1;", &[
|
assert_ast("<?php echo 1;", &[
|
||||||
|
Loading…
Reference in New Issue
Block a user