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, name: Identifier,
value: Option<Expression>, value: Option<Expression>,
}, },
Block {
body: Block,
},
Noop, Noop,
} }

View File

@ -893,6 +893,12 @@ impl Parser {
finally, finally,
} }
} }
TokenKind::LeftBrace => {
self.next();
let body = self.block(&TokenKind::RightBrace)?;
self.rbrace()?;
Statement::Block { body }
}
_ => { _ => {
let expr = self.expression(0)?; 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] #[test]
fn noop() { fn noop() {
assert_ast("<?php ;", &[Statement::Noop]); assert_ast("<?php ;", &[Statement::Noop]);