parser: support empty yield expressions

This commit is contained in:
Ryan Chandler 2022-09-16 23:08:38 +01:00
parent fc9aa62cff
commit 6820d09938
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 16 additions and 5 deletions

View File

@ -557,7 +557,7 @@ pub enum Expression {
value: Box<Expression>,
},
Yield {
value: Box<Expression>,
value: Option<Box<Expression>>,
},
Negate {
value: Box<Self>,

View File

@ -1473,12 +1473,16 @@ impl Parser {
TokenKind::Yield => {
self.next();
let value = self.expression(Precedence::Lowest)?;
if self.current.kind == TokenKind::SemiColon {
Expression::Yield { value: None }
} else {
let value = self.expression(Precedence::Lowest)?;
// FIXME: Check for presence of => here to allow yielding key and value.
// FIXME: Check for presence of => here to allow yielding key and value.
Expression::Yield {
value: Box::new(value),
Expression::Yield {
value: Some(Box::new(value)),
}
}
}
TokenKind::Clone => {
@ -4674,6 +4678,13 @@ mod tests {
);
}
#[test]
fn empty_yield() {
assert_ast("<?php yield;", &[
expr!(Expression::Yield { value: None })
]);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();