mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
parser: support backed enums with cases that have no backing value
This commit is contained in:
parent
e63fe36008
commit
067bac959a
@ -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'#' => {
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user