Skip to content

Fix namespace expression usage in arg list #295

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

Merged
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
20 changes: 19 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ private function isExpressionStartFn() {
return true;
case TokenKind::NamespaceKeyword:
// TODO currently only supports qualified-names, but eventually parse namespace declarations
return $this->checkToken(TokenKind::BackslashToken);
return $this->isNamespaceKeywordStartOfExpression($token);

// literal
case TokenKind::DecimalLiteralToken: // TODO merge dec, oct, hex, bin, float -> NumericLiteral
Expand Down Expand Up @@ -905,6 +905,24 @@ private function isExpressionStartFn() {
};
}

/**
* Handles the fact that $token may either be getCurrentToken or the token immediately before it in isExpressionStartFn().
* An expression can be namespace\CONST, namespace\fn(), or namespace\ClassName
*/
private function isNamespaceKeywordStartOfExpression(Token $token) : bool {
$nextToken = $this->getCurrentToken();
if ($nextToken->kind === TokenKind::BackslashToken) {
return true;
}
if ($nextToken !== $token) {
return false;
}
$oldPosition = $this->lexer->getCurrentPosition();
$nextToken = $this->lexer->scanNextToken();
$this->lexer->setCurrentPosition($oldPosition);
return $nextToken->kind === TokenKind::BackslashToken;
}

/**
* @param Node $parentNode
* @return Token|MissingToken|Node
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/namespaced_call_in_arg_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
var_export(namespace\is_string('x'));
1 change: 1 addition & 0 deletions tests/cases/parser/namespaced_call_in_arg_list.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
121 changes: 121 additions & 0 deletions tests/cases/parser/namespaced_call_in_arg_list.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"CallExpression": {
"callableExpression": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 10
}
]
}
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"argumentExpressionList": {
"ArgumentExpressionList": {
"children": [
{
"ArgumentExpression": {
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
"CallExpression": {
"callableExpression": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": {
"RelativeSpecifier": {
"namespaceKeyword": {
"kind": "NamespaceKeyword",
"textLength": 9
},
"backslash": {
"kind": "BackslashToken",
"textLength": 1
}
}
},
"nameParts": [
{
"kind": "Name",
"textLength": 9
}
]
}
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"argumentExpressionList": {
"ArgumentExpressionList": {
"children": [
{
"ArgumentExpression": {
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
"StringLiteral": {
"startQuote": null,
"children": {
"kind": "StringLiteralToken",
"textLength": 3
},
"endQuote": null
}
}
}
}
]
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
}
}
}
]
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}