parser: support readonly class properties

This commit is contained in:
Ryan Chandler 2022-09-13 12:08:41 +01:00
parent 736964de7a
commit 4f64297907
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 24 additions and 1 deletions

View File

@ -76,6 +76,7 @@ pub enum PropertyFlag {
Protected,
Private,
Static,
Readonly,
}
impl From<TokenKind> for PropertyFlag {
@ -85,6 +86,7 @@ impl From<TokenKind> for PropertyFlag {
TokenKind::Protected => Self::Protected,
TokenKind::Private => Self::Private,
TokenKind::Static => Self::Static,
TokenKind::Readonly => Self::Readonly,
_ => unreachable!("token {:?} can't be converted into property flag.", k),
}
}

View File

@ -1177,7 +1177,8 @@ impl Parser {
| TokenKind::Public
| TokenKind::Private
| TokenKind::Protected
| TokenKind::Static => {
| TokenKind::Static
| TokenKind::Readonly => {
let mut flags = vec![self.current.kind.clone()];
self.next();
@ -1189,6 +1190,7 @@ impl Parser {
TokenKind::Private,
TokenKind::Protected,
TokenKind::Static,
TokenKind::Readonly,
]
.contains(&self.current.kind)
{
@ -3549,6 +3551,25 @@ mod tests {
);
}
#[test]
fn readonly_class_props() {
assert_ast(
"<?php class Foo { public readonly $bar; }",
&[Statement::Class {
name: "Foo".as_bytes().into(),
extends: None,
implements: vec![],
body: vec![Statement::Property {
var: "bar".as_bytes().into(),
value: None,
r#type: None,
flags: vec![PropertyFlag::Public, PropertyFlag::Readonly],
}],
flag: None,
}],
);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();