Skip to content

Properly parse @$x += $y as @($x += $y) #282

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
40 changes: 36 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,6 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
case TokenKind::AmpersandEqualsToken:
case TokenKind::CaretEqualsToken:
case TokenKind::BarEqualsToken:
case TokenKind::InstanceOfKeyword:
case TokenKind::QuestionQuestionEqualsToken:
// Workarounds for https://github.com/Microsoft/tolerant-php-parser/issues/19#issue-201714377
// Parse `!$a = $b` as `!($a = $b)` - PHP constrains the Left Hand Side of an assignment to a variable. A unary operator (`@`, `!`, etc.) is not a variable.
Expand All @@ -1753,6 +1752,15 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
$shouldOperatorTakePrecedenceOverUnary = true;
}
break;
case TokenKind::InstanceOfKeyword:
// Unlike assignment, the instanceof operator doesn't have restrictions on what can go in the left hand side.
// `!` is the only unary operator with lower precedence than instanceof.
if ($leftOperand instanceof UnaryOpExpression) {
if ($leftOperand->operator->kind === TokenKind::ExclamationToken) {
$shouldOperatorTakePrecedenceOverUnary = true;
}
}
break;
}

if ($shouldOperatorTakePrecedenceOverUnary) {
Expand Down Expand Up @@ -1910,9 +1918,13 @@ private function getBinaryOperatorPrecedenceAndAssociativity($token) {
private function makeBinaryExpression($leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode) {
$assignmentExpression = $operatorToken->kind === TokenKind::EqualsToken;
if ($assignmentExpression || \array_key_exists($operatorToken->kind, self::KNOWN_ASSIGNMENT_TOKEN_SET)) {
if ($leftOperand instanceof BinaryExpression && !\array_key_exists($leftOperand->operator->kind, self::KNOWN_ASSIGNMENT_TOKEN_SET)) {
// Handle cases without parenthesis, such as $x ** $y === $z, as $x ** ($y === $z)
return $this->shiftBinaryOperands($leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode);
if ($leftOperand instanceof BinaryExpression) {
if (!\array_key_exists($leftOperand->operator->kind, self::KNOWN_ASSIGNMENT_TOKEN_SET)) {
// Handle cases without parenthesis, such as $x ** $y === $z, as $x ** ($y === $z)
return $this->shiftBinaryOperands($leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode);
}
} elseif ($leftOperand instanceof UnaryOpExpression || $leftOperand instanceof ErrorControlExpression) {
return $this->shiftUnaryOperands($leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode);
}
}
$binaryExpression = $assignmentExpression ? new AssignmentExpression() : new BinaryExpression();
Expand All @@ -1928,6 +1940,26 @@ private function makeBinaryExpression($leftOperand, $operatorToken, $byRefToken,
return $binaryExpression;
}

/**
* @param ErrorControlExpression|UnaryOpExpression $leftOperand
*/
private function shiftUnaryOperands(UnaryExpression $leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode) {
$outerUnaryOpExpression = clone($leftOperand);
$inner = $this->makeBinaryExpression(
$leftOperand->operand,
$operatorToken,
$byRefToken,
$rightOperand,
$outerUnaryOpExpression
);
// Either ErrorControlExpression or a UnaryOpExpression
$outerUnaryOpExpression->parent = $parentNode;
// TODO should this binaryExpression be wrapped in a UnaryExpression?
$outerUnaryOpExpression->operand = $inner;

return $outerUnaryOpExpression;
}

private function shiftBinaryOperands(BinaryExpression $leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode) {
$inner = $this->makeBinaryExpression(
$leftOperand->rightOperand,
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// This should be parsed as @($x <<= $y);
@$x <<= $y;
1 change: 1 addition & 0 deletions tests/cases/parser/binaryAssignmentExpressions5.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
62 changes: 62 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions5.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"ErrorControlExpression": {
"operator": {
"kind": "AtSymbolToken",
"textLength": 1
},
"operand": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "LessThanLessThanEqualsToken",
"textLength": 3
},
"rightOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
2 changes: 2 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
var_export(-$x <<= 2);
1 change: 1 addition & 0 deletions tests/cases/parser/binaryAssignmentExpressions6.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
97 changes: 97 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions6.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"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": {
"UnaryOpExpression": {
"operator": {
"kind": "MinusToken",
"textLength": 1
},
"operand": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "LessThanLessThanEqualsToken",
"textLength": 3
},
"rightOperand": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
}
}
}
}
}
}
}
]
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
4 changes: 4 additions & 0 deletions tests/cases/parser/lhsVariable5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
// Should be parsed as `(@$x) instanceof stdClass.
// @ has higher precedence than instanceof
echo @$x instanceof stdClass;
1 change: 1 addition & 0 deletions tests/cases/parser/lhsVariable5.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
79 changes: 79 additions & 0 deletions tests/cases/parser/lhsVariable5.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"EchoExpression": {
"echoKeyword": {
"kind": "EchoKeyword",
"textLength": 4
},
"expressions": {
"ExpressionList": {
"children": [
{
"BinaryExpression": {
"leftOperand": {
"ErrorControlExpression": {
"operator": {
"kind": "AtSymbolToken",
"textLength": 1
},
"operand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
}
}
},
"operator": {
"kind": "InstanceOfKeyword",
"textLength": 10
},
"rightOperand": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 8
}
]
}
}
}
}
]
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
2 changes: 2 additions & 0 deletions tests/cases/parser/unaryAssignmentExpression1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
@$x = 2;
1 change: 1 addition & 0 deletions tests/cases/parser/unaryAssignmentExpression1.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading