Skip to content

Support parsing new (call_function())() #314

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 1 commit into from
Feb 18, 2020
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
22 changes: 20 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ private function parsePostfixExpressionRest($expression, $allowUpdateExpression
return $this->parsePostfixExpressionRest($expression);
}

if ($tokenKind === TokenKind::OpenParenToken && !$this->isParsingObjectCreationExpression) {
if ($tokenKind === TokenKind::OpenParenToken && !$this->isParsingUnparenthesizedObjectCreationExpression($expression)) {
$expression = $this->parseCallExpressionRest($expression);

if (!$this->checkToken(TokenKind::OpenParenToken)) {
Expand Down Expand Up @@ -2822,19 +2822,37 @@ private function parseScopedPropertyAccessExpression($expression, $fallbackParen
return $scopedPropertyAccessExpression;
}

public function isParsingUnparenthesizedObjectCreationExpression($expression) {
if (!$this->isParsingObjectCreationExpression) {
return false;
}
if ($expression instanceof Token) {
return true;
}
while ($expression->parent) {
$expression = $expression->parent;
if ($expression instanceof ObjectCreationExpression) {
return true;
} elseif ($expression instanceof ParenthesizedExpression) {
return false;
}
}
}

private $isParsingObjectCreationExpression = false;

private function parseObjectCreationExpression($parentNode) {
$objectCreationExpression = new ObjectCreationExpression();
$objectCreationExpression->parent = $parentNode;
$objectCreationExpression->newKeword = $this->eat1(TokenKind::NewKeyword);
// TODO - add tests for this scenario
$oldIsParsingObjectCreationExpression = $this->isParsingObjectCreationExpression;
$this->isParsingObjectCreationExpression = true;
$objectCreationExpression->classTypeDesignator =
$this->eatOptional1(TokenKind::ClassKeyword) ??
$this->parseExpression($objectCreationExpression);

$this->isParsingObjectCreationExpression = false;
$this->isParsingObjectCreationExpression = $oldIsParsingObjectCreationExpression;

$objectCreationExpression->openParen = $this->eatOptional1(TokenKind::OpenParenToken);
if ($objectCreationExpression->openParen !== null) {
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/instanceofExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// php 8.0 instanceof
$x = $y instanceof (function_call());
1 change: 1 addition & 0 deletions tests/cases/parser/instanceofExpression.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
100 changes: 100 additions & 0 deletions tests/cases/parser/instanceofExpression.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"AssignmentExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "EqualsToken",
"textLength": 1
},
"byRef": null,
"rightOperand": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "InstanceOfKeyword",
"textLength": 10
},
"rightOperand": {
"ParenthesizedExpression": {
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"expression": {
"CallExpression": {
"callableExpression": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 13
}
]
}
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"argumentExpressionList": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
2 changes: 2 additions & 0 deletions tests/cases/parser/objectCreationExpression19.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$x = new (function_call())();
1 change: 1 addition & 0 deletions tests/cases/parser/objectCreationExpression19.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
103 changes: 103 additions & 0 deletions tests/cases/parser/objectCreationExpression19.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"AssignmentExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "EqualsToken",
"textLength": 1
},
"byRef": null,
"rightOperand": {
"ObjectCreationExpression": {
"newKeword": {
"kind": "NewKeyword",
"textLength": 3
},
"classTypeDesignator": {
"ParenthesizedExpression": {
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"expression": {
"CallExpression": {
"callableExpression": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 13
}
]
}
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"argumentExpressionList": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"argumentExpressionList": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
},
"classBaseClause": null,
"classInterfaceClause": null,
"classMembers": null
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
2 changes: 2 additions & 0 deletions tests/cases/parser/objectCreationExpression20.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
new (new (new york())())();
1 change: 1 addition & 0 deletions tests/cases/parser/objectCreationExpression20.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading