Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions trunk_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
expect!(self, TokenKind::RightBrace, "expected }");
if body_end_token == TokenKind::RightBrace {
expect!(self, TokenKind::RightBrace, "expected }");
}

Statement::If { condition, then }
},
Expand Down Expand Up @@ -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;", &[
Expand Down