Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support php 8.2 readonly classes, constants in traits, true type #368

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Node/Statement/ClassDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class ClassDeclaration extends StatementNode implements NamespacedNameInterface,
/** @var AttributeGroup[]|null */
public $attributes;

/** @var Token */
/** @var Token abstract/final/readonly modifier */
public $abstractOrFinalModifier;

/** @var Token[] additional abstract/final/readonly modifiers */
public $modifiers;

/** @var Token */
public $classKeyword;

Expand All @@ -43,6 +46,7 @@ class ClassDeclaration extends StatementNode implements NamespacedNameInterface,
const CHILD_NAMES = [
'attributes',
'abstractOrFinalModifier',
'modifiers',
'classKeyword',
'name',
'classBaseClause',
Expand Down
26 changes: 20 additions & 6 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ public function __construct() {
[TokenKind::ArrayKeyword, TokenKind::CallableKeyword, TokenKind::BoolReservedWord,
TokenKind::FloatReservedWord, TokenKind::IntReservedWord, TokenKind::StringReservedWord,
TokenKind::ObjectReservedWord, TokenKind::NullReservedWord, TokenKind::FalseReservedWord,
TokenKind::TrueReservedWord,
TokenKind::IterableReservedWord, TokenKind::MixedReservedWord]; // TODO update spec
$this->returnTypeDeclarationTokens = \array_merge([TokenKind::VoidReservedWord, TokenKind::NullReservedWord, TokenKind::FalseReservedWord, TokenKind::StaticKeyword], $this->parameterTypeDeclarationTokens);
$this->returnTypeDeclarationTokens = \array_merge([TokenKind::VoidReservedWord, TokenKind::NullReservedWord, TokenKind::FalseReservedWord, TokenKind::TrueReservedWord, TokenKind::StaticKeyword], $this->parameterTypeDeclarationTokens);
}

/**
Expand Down Expand Up @@ -557,10 +558,8 @@ private function parseStatementFn() {
// class-declaration
case TokenKind::FinalKeyword:
case TokenKind::AbstractKeyword:
if (!$this->lookahead(TokenKind::ClassKeyword)) {
$this->advanceToken();
return new SkippedToken($token);
}
case TokenKind::ReadonlyKeyword:
// fallthrough
case TokenKind::ClassKeyword:
return $this->parseClassDeclaration($parentNode);

Expand Down Expand Up @@ -657,10 +656,20 @@ private function parseClassElementFn() {
};
}

/** @return Token[] */
private function parseClassModifiers(): array {
$modifiers = [];
while ($token = $this->eatOptional(TokenKind::AbstractKeyword, TokenKind::FinalKeyword, TokenKind::ReadonlyKeyword)) {
$modifiers[] = $token;
}
return $modifiers;
}

private function parseClassDeclaration($parentNode) : Node {
$classNode = new ClassDeclaration(); // TODO verify not nested
$classNode->parent = $parentNode;
$classNode->abstractOrFinalModifier = $this->eatOptional(TokenKind::AbstractKeyword, TokenKind::FinalKeyword);
$classNode->abstractOrFinalModifier = $this->eatOptional(TokenKind::AbstractKeyword, TokenKind::FinalKeyword, TokenKind::ReadonlyKeyword);
$classNode->modifiers = $this->parseClassModifiers();
$classNode->classKeyword = $this->eat1(TokenKind::ClassKeyword);
$classNode->name = $this->eat($this->nameOrReservedWordTokens); // TODO should be any
$classNode->name->kind = TokenKind::Name;
Expand Down Expand Up @@ -1036,6 +1045,7 @@ private function isStatementStart(Token $token) {
case TokenKind::ClassKeyword:
case TokenKind::AbstractKeyword:
case TokenKind::FinalKeyword:
case TokenKind::ReadonlyKeyword:

// interface-declaration
case TokenKind::InterfaceKeyword:
Expand Down Expand Up @@ -3533,6 +3543,7 @@ private function isTraitMemberDeclarationStart($token) {
case TokenKind::AbstractKeyword:
case TokenKind::FinalKeyword:
case TokenKind::ReadonlyKeyword:
case TokenKind::ConstKeyword:

// method-declaration
case TokenKind::FunctionKeyword:
Expand All @@ -3553,6 +3564,9 @@ private function parseTraitElementFn() {

$token = $this->getCurrentToken();
switch ($token->kind) {
case TokenKind::ConstKeyword:
return $this->parseClassConstDeclaration($parentNode, $modifiers);

case TokenKind::FunctionKeyword:
return $this->parseMethodDeclaration($parentNode, $modifiers);

Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"kind": "AbstractKeyword",
"textLength": 8
},
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"kind": "AbstractKeyword",
"textLength": 8
},
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"kind": "AbstractKeyword",
"textLength": 8
},
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/abstractMethodDeclaration6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"kind": "AbstractKeyword",
"textLength": 8
},
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
9 changes: 1 addition & 8 deletions tests/cases/parser/abstractMethodDeclaration7.php.diag
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
[
{
"kind": 0,
"message": "Unexpected 'abstract'",
"start": 29,
"length": 8
}
]
[]
15 changes: 8 additions & 7 deletions tests/cases/parser/abstractMethodDeclaration7.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
}
}
},
{
"error": "SkippedToken",
"kind": "AbstractKeyword",
"textLength": 8
},
{
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": {
"kind": "FinalKeyword",
"textLength": 5
"kind": "AbstractKeyword",
"textLength": 8
},
"modifiers": [
{
"kind": "FinalKeyword",
"textLength": 5
}
],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classBaseClause1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classBaseClause2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classBaseClause6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration7.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration8.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration9.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classInterfaceClause1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classInterfaceClause2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classInterfaceClause3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classInterfaceClause4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classInterfaceClause5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classInterfaceClause6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classMethods1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classMethods3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classMethods4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classMethods5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/constructorDeclaration1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/listExpression9.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/namespaceDefinition3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/namespaces2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/namespaces3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/namespaces4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/namespaces5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
Expand Down