Add support for block statements

This commit is contained in:
Evan Shaw 2022-09-11 21:01:31 +12:00
parent 9298288aee
commit 681c201c3c
2 changed files with 22 additions and 0 deletions

View File

@ -271,6 +271,9 @@ pub enum Statement {
name: Identifier,
value: Option<Expression>,
},
Block {
body: Block,
},
Noop,
}

View File

@ -893,6 +893,12 @@ impl Parser {
finally,
}
}
TokenKind::LeftBrace => {
self.next();
let body = self.block(&TokenKind::RightBrace)?;
self.rbrace()?;
Statement::Block { body }
}
_ => {
let expr = self.expression(0)?;
@ -2957,6 +2963,19 @@ mod tests {
);
}
#[test]
fn block() {
assert_ast("<?php {}", &[Statement::Block { body: vec![] }]);
assert_ast(
"<?php { $a; }",
&[Statement::Block {
body: vec![Statement::Expression {
expr: Expression::Variable { name: "a".into() },
}],
}],
);
}
#[test]
fn noop() {
assert_ast("<?php ;", &[Statement::Noop]);