Merge pull request #68 from ryangjchandler/fix/backed-enums-without-values

This commit is contained in:
Ryan Chandler 2022-09-13 12:16:15 +01:00 committed by GitHub
commit 1c90b5e907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -531,7 +531,8 @@ impl Lexer {
if qualified { if qualified {
TokenKind::QualifiedIdentifier(buffer.into()) TokenKind::QualifiedIdentifier(buffer.into())
} else { } 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'#' => { b'/' | b'#' => {

View File

@ -622,7 +622,7 @@ impl Parser {
let name = self.ident()?; let name = self.ident()?;
let mut value = None; let mut value = None;
if is_backed { if self.current.kind == TokenKind::Equals {
expect!(self, TokenKind::Equals, "expected ="); expect!(self, TokenKind::Equals, "expected =");
value = Some(self.expression(0)?); value = Some(self.expression(0)?);
@ -2212,8 +2212,8 @@ mod tests {
use super::Parser; use super::Parser;
use crate::{ use crate::{
ast::{ ast::{
Arg, ArrayItem, Case, ClassFlag, Constant, DeclareItem, ElseIf, IncludeKind, InfixOp, Arg, ArrayItem, BackedEnumType, Case, ClassFlag, Constant, DeclareItem, ElseIf,
MethodFlag, PropertyFlag, IncludeKind, InfixOp, MethodFlag, PropertyFlag,
}, },
Catch, Expression, Identifier, Param, Statement, Type, 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]) { fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None); let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap(); let tokens = lexer.tokenize(source).unwrap();