parser: support backed enums with cases that have no backing value

This commit is contained in:
Ryan Chandler 2022-09-13 12:15:29 +01:00
parent e63fe36008
commit 067bac959a
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 32 additions and 4 deletions

View File

@ -531,7 +531,8 @@ impl Lexer {
if qualified {
TokenKind::QualifiedIdentifier(buffer.into())
} else {
identifier_to_keyword(&buffer).unwrap_or_else(|| TokenKind::Identifier(buffer.into()))
identifier_to_keyword(&buffer)
.unwrap_or_else(|| TokenKind::Identifier(buffer.into()))
}
}
b'/' | b'#' => {

View File

@ -622,7 +622,7 @@ impl Parser {
let name = self.ident()?;
let mut value = None;
if is_backed {
if self.current.kind == TokenKind::Equals {
expect!(self, TokenKind::Equals, "expected =");
value = Some(self.expression(0)?);
@ -2212,8 +2212,8 @@ mod tests {
use super::Parser;
use crate::{
ast::{
Arg, ArrayItem, Case, ClassFlag, Constant, DeclareItem, ElseIf, IncludeKind, InfixOp,
MethodFlag, PropertyFlag,
Arg, ArrayItem, BackedEnumType, Case, ClassFlag, Constant, DeclareItem, ElseIf,
IncludeKind, InfixOp, MethodFlag, PropertyFlag,
},
Catch, Expression, Identifier, Param, Statement, Type,
};
@ -3567,6 +3567,33 @@ mod tests {
);
}
#[test]
fn backed_enum_without_values() {
assert_ast(
"<?php enum Foo: string {
case Bar;
case Baz = 'Baz';
}",
&[Statement::Enum {
name: "Foo".as_bytes().into(),
implements: vec![],
backed_type: Some(BackedEnumType::String),
body: vec![
Statement::EnumCase {
name: "Bar".as_bytes().into(),
value: None,
},
Statement::EnumCase {
name: "Baz".as_bytes().into(),
value: Some(Expression::ConstantString {
value: "Baz".into(),
}),
},
],
}],
);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();