Skip to content

Bug fixes for parsing associativity #268

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
Dec 29, 2018
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
46 changes: 45 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1844,13 +1844,13 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
TokenKind::LessThanGreaterThanToken => [17, Associativity::None],
TokenKind::EqualsEqualsEqualsToken => [17, Associativity::None],
TokenKind::ExclamationEqualsEqualsToken => [17, Associativity::None],
TokenKind::LessThanEqualsGreaterThanToken => [17, Associativity::None],

// relational-expression (X)
TokenKind::LessThanToken => [18, Associativity::None],
TokenKind::GreaterThanToken => [18, Associativity::None],
TokenKind::LessThanEqualsToken => [18, Associativity::None],
TokenKind::GreaterThanEqualsToken => [18, Associativity::None],
TokenKind::LessThanEqualsGreaterThanToken => [18, Associativity::None],

// shift-expression (L)
TokenKind::LessThanLessThanToken => [19, Associativity::Left],
Expand Down Expand Up @@ -1883,8 +1883,33 @@ private function getBinaryOperatorPrecedenceAndAssociativity($token) {
return self::UNKNOWN_PRECEDENCE_AND_ASSOCIATIVITY;
}

/**
* @internal Do not use outside this class, this may be changed or removed.
*/
const KNOWN_ASSIGNMENT_TOKEN_SET = [
TokenKind::AsteriskAsteriskEqualsToken => true,
TokenKind::AsteriskEqualsToken => true,
TokenKind::SlashEqualsToken => true,
TokenKind::PercentEqualsToken => true,
TokenKind::PlusEqualsToken => true,
TokenKind::MinusEqualsToken => true,
TokenKind::DotEqualsToken => true,
TokenKind::LessThanLessThanEqualsToken => true,
TokenKind::GreaterThanGreaterThanEqualsToken => true,
TokenKind::AmpersandEqualsToken => true,
TokenKind::CaretEqualsToken => true,
TokenKind::BarEqualsToken => true,
// InstanceOf has other remaining issues, but this heuristic is an improvement for many common cases such as `$x && $y = $z`
];

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);
}
}
$binaryExpression = $assignmentExpression ? new AssignmentExpression() : new BinaryExpression();
$binaryExpression->parent = $parentNode;
$leftOperand->parent = $binaryExpression;
Expand All @@ -1898,6 +1923,25 @@ private function makeBinaryExpression($leftOperand, $operatorToken, $byRefToken,
return $binaryExpression;
}

private function shiftBinaryOperands(BinaryExpression $leftOperand, $operatorToken, $byRefToken, $rightOperand, $parentNode) {
$inner = $this->makeBinaryExpression(
$leftOperand->rightOperand,
$operatorToken,
$byRefToken,
$rightOperand,
$parentNode
);
$outer = $this->makeBinaryExpression(
$leftOperand->leftOperand,
$leftOperand->operator,
null,
$inner,
$parentNode
);
$inner->parent = $outer;
return $outer;
}

private function parseDoStatement($parentNode) {
$doStatement = new DoStatement();
$doStatement->parent = $parentNode;
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$x && $y = $z;
1 change: 1 addition & 0 deletions tests/cases/parser/binaryAssignmentExpressions1.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
72 changes: 72 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions1.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "AmpersandAmpersandToken",
"textLength": 2
},
"rightOperand": {
"AssignmentExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "EqualsToken",
"textLength": 1
},
"byRef": null,
"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/binaryAssignmentExpressions2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$x = $y || $z;
1 change: 1 addition & 0 deletions tests/cases/parser/binaryAssignmentExpressions2.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
72 changes: 72 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions2.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"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": "BarBarToken",
"textLength": 2
},
"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/binaryAssignmentExpressions3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$x && $y += $z;
1 change: 1 addition & 0 deletions tests/cases/parser/binaryAssignmentExpressions3.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
71 changes: 71 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions3.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "AmpersandAmpersandToken",
"textLength": 2
},
"rightOperand": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "PlusEqualsToken",
"textLength": 2
},
"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/binaryAssignmentExpressions4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$x += $y && $z;
1 change: 1 addition & 0 deletions tests/cases/parser/binaryAssignmentExpressions4.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
71 changes: 71 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions4.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "PlusEqualsToken",
"textLength": 2
},
"rightOperand": {
"BinaryExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "AmpersandAmpersandToken",
"textLength": 2
},
"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/spaceshipExpression1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$x < $a <=> $b;
1 change: 1 addition & 0 deletions tests/cases/parser/spaceshipExpression1.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading