Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class Parser {
private $nameOrKeywordOrReservedWordTokens;
private $nameOrReservedWordTokens;
private $nameOrStaticOrReservedWordTokens;
private $nameOrKeywordOrReservedWordTokensExceptNamespace;
private $reservedWordTokens;
private $keywordTokens;
private $argumentStartTokensSet;
Expand All @@ -148,6 +149,10 @@ public function __construct() {
$this->nameOrKeywordOrReservedWordTokens = \array_merge([TokenKind::Name], $this->keywordTokens, $this->reservedWordTokens);
$this->nameOrReservedWordTokens = \array_merge([TokenKind::Name], $this->reservedWordTokens);
$this->nameOrStaticOrReservedWordTokens = \array_merge([TokenKind::Name, TokenKind::StaticKeyword], $this->reservedWordTokens);
$this->nameOrKeywordOrReservedWordTokensExceptNamespace = \array_values(\array_diff(
$this->nameOrKeywordOrReservedWordTokens,
[TokenKind::NamespaceKeyword]
));
$this->parameterTypeDeclarationTokens =
[TokenKind::ArrayKeyword, TokenKind::CallableKeyword, TokenKind::BoolReservedWord,
TokenKind::FloatReservedWord, TokenKind::IntReservedWord, TokenKind::StringReservedWord,
Expand Down Expand Up @@ -1872,23 +1877,13 @@ private function parseQualifiedNameFn() {
DelimitedList\QualifiedNameParts::class,
TokenKind::BackslashToken,
function ($token) {
// a\static() <- INVALID (but not checked for right now)
// new a\static() <- INVALID
// new static() <- VALID
// a\static\b <- INVALID
// a\function <- INVALID
// a\true\b <-VALID
// a\b\true <-VALID
// a\static::b <-VALID
// TODO more tests
return $this->lookahead(TokenKind::BackslashToken)
? in_array($token->kind, $this->nameOrReservedWordTokens)
: in_array($token->kind, $this->nameOrStaticOrReservedWordTokens);
if ($token->kind === TokenKind::NamespaceKeyword) {
return false;
}
return \in_array($token->kind, $this->nameOrKeywordOrReservedWordTokensExceptNamespace, true);
},
function ($parentNode) {
$name = $this->lookahead(TokenKind::BackslashToken)
? $this->eat($this->nameOrReservedWordTokens)
: $this->eat($this->nameOrStaticOrReservedWordTokens); // TODO support keyword name
$name = $this->eat($this->nameOrKeywordOrReservedWordTokensExceptNamespace); // TODO support keyword name
$name->kind = TokenKind::Name; // bool/true/null/static should not be treated as keywords in this case
Comment on lines 1877 to 1887
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reinstate lookahead filtering for qualified-name keywords

The new parseQualifiedNameFn now accepts any keyword except namespace for every segment of a qualified name (in_array($this->nameOrKeywordOrReservedWordTokensExceptNamespace, true)). The previous implementation rejected keywords such as static or function when they were followed by another namespace separator, so constructs like new a\static() or a\function\b produced diagnostics. After this change those invalid identifiers parse as ordinary names, so the parser will silently accept syntactically invalid class or function references in non-namespace contexts. Consider keeping the lookahead logic and only broadening the allowed set where keywords are actually permitted (e.g. namespace declarations) to avoid suppressing errors elsewhere.

Useful? React with 👍 / 👎.

return $name;
}, $node);
Expand Down Expand Up @@ -3929,8 +3924,10 @@ private function parseNamespaceDefinition($parentNode) {
$namespaceDefinition->compoundStatementOrSemicolon = $this->parseCompoundStatement($namespaceDefinition);
} else {
if (!$namespaceDefinition->name) {
// only optional with compound statement block
$namespaceDefinition->name = new MissingToken(TokenKind::QualifiedName, $this->token->fullStart);
if (!$this->checkToken(TokenKind::SemicolonToken)) {
// name is only optional when followed by a semicolon (global namespace)
$namespaceDefinition->name = new MissingToken(TokenKind::QualifiedName, $this->token->fullStart);
}
}
$namespaceDefinition->compoundStatementOrSemicolon = $this->eatSemicolonOrAbortStatement();
}
Expand Down
9 changes: 1 addition & 8 deletions tests/cases/parser/namespaceDefinition2.php.diag
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
[
{
"kind": 0,
"message": "'QualifiedName' expected.",
"start": 64,
"length": 0
}
]
[]
6 changes: 1 addition & 5 deletions tests/cases/parser/namespaceDefinition2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
"kind": "NamespaceKeyword",
"textLength": 9
},
"name": {
"error": "MissingToken",
"kind": "QualifiedName",
"textLength": 0
},
"name": null,
"compoundStatementOrSemicolon": {
"kind": "SemicolonToken",
"textLength": 1
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/namespaceDefinition6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
namespace global;
1 change: 1 addition & 0 deletions tests/cases/parser/namespaceDefinition6.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
44 changes: 44 additions & 0 deletions tests/cases/parser/namespaceDefinition6.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"NamespaceDefinition": {
"namespaceKeyword": {
"kind": "NamespaceKeyword",
"textLength": 9
},
"name": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 6
}
]
}
},
"compoundStatementOrSemicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}