Skip to content

Commit af2ff38

Browse files
authored
Merge pull request microsoft#361 from TysonAndre/fix-delimiter-edge-case
Fix parsing of `a ? a ? a : b : b`
2 parents 2e3999f + cbbaba1 commit af2ff38

11 files changed

+360
-124
lines changed

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,7 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
21202120
}
21212121
break;
21222122
case TokenKind::QuestionToken:
2123-
if ($parentNode instanceof TernaryExpression) {
2123+
if ($parentNode instanceof TernaryExpression && !isset($parentNode->questionToken)) {
21242124
// Workaround to parse "a ? b : c ? d : e" as "(a ? b : c) ? d : e"
21252125
break 2;
21262126
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
true ? true ? true ? true : false : false : false;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"ExpressionStatement": {
16+
"expression": {
17+
"TernaryExpression": {
18+
"condition": {
19+
"ReservedWord": {
20+
"children": {
21+
"kind": "TrueReservedWord",
22+
"textLength": 4
23+
}
24+
}
25+
},
26+
"questionToken": {
27+
"kind": "QuestionToken",
28+
"textLength": 1
29+
},
30+
"ifExpression": {
31+
"TernaryExpression": {
32+
"condition": {
33+
"ReservedWord": {
34+
"children": {
35+
"kind": "TrueReservedWord",
36+
"textLength": 4
37+
}
38+
}
39+
},
40+
"questionToken": {
41+
"kind": "QuestionToken",
42+
"textLength": 1
43+
},
44+
"ifExpression": {
45+
"TernaryExpression": {
46+
"condition": {
47+
"ReservedWord": {
48+
"children": {
49+
"kind": "TrueReservedWord",
50+
"textLength": 4
51+
}
52+
}
53+
},
54+
"questionToken": {
55+
"kind": "QuestionToken",
56+
"textLength": 1
57+
},
58+
"ifExpression": {
59+
"ReservedWord": {
60+
"children": {
61+
"kind": "TrueReservedWord",
62+
"textLength": 4
63+
}
64+
}
65+
},
66+
"colonToken": {
67+
"kind": "ColonToken",
68+
"textLength": 1
69+
},
70+
"elseExpression": {
71+
"ReservedWord": {
72+
"children": {
73+
"kind": "FalseReservedWord",
74+
"textLength": 5
75+
}
76+
}
77+
}
78+
}
79+
},
80+
"colonToken": {
81+
"kind": "ColonToken",
82+
"textLength": 1
83+
},
84+
"elseExpression": {
85+
"ReservedWord": {
86+
"children": {
87+
"kind": "FalseReservedWord",
88+
"textLength": 5
89+
}
90+
}
91+
}
92+
}
93+
},
94+
"colonToken": {
95+
"kind": "ColonToken",
96+
"textLength": 1
97+
},
98+
"elseExpression": {
99+
"ReservedWord": {
100+
"children": {
101+
"kind": "FalseReservedWord",
102+
"textLength": 5
103+
}
104+
}
105+
}
106+
}
107+
},
108+
"semicolon": {
109+
"kind": "SemicolonToken",
110+
"textLength": 1
111+
}
112+
}
113+
}
114+
],
115+
"endOfFileToken": {
116+
"kind": "EndOfFileToken",
117+
"textLength": 0
118+
}
119+
}
120+
}

tests/cases/parser/ternaryExpressions5.php.tree

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@
1616
"expression": {
1717
"TernaryExpression": {
1818
"condition": {
19+
"Variable": {
20+
"dollar": null,
21+
"name": {
22+
"kind": "VariableName",
23+
"textLength": 2
24+
}
25+
}
26+
},
27+
"questionToken": {
28+
"kind": "QuestionToken",
29+
"textLength": 1
30+
},
31+
"ifExpression": {
32+
"Variable": {
33+
"dollar": null,
34+
"name": {
35+
"kind": "VariableName",
36+
"textLength": 2
37+
}
38+
}
39+
},
40+
"colonToken": {
41+
"kind": "ColonToken",
42+
"textLength": 1
43+
},
44+
"elseExpression": {
1945
"TernaryExpression": {
2046
"condition": {
2147
"Variable": {
@@ -53,32 +79,6 @@
5379
}
5480
}
5581
}
56-
},
57-
"questionToken": {
58-
"kind": "QuestionToken",
59-
"textLength": 1
60-
},
61-
"ifExpression": {
62-
"Variable": {
63-
"dollar": null,
64-
"name": {
65-
"kind": "VariableName",
66-
"textLength": 2
67-
}
68-
}
69-
},
70-
"colonToken": {
71-
"kind": "ColonToken",
72-
"textLength": 1
73-
},
74-
"elseExpression": {
75-
"Variable": {
76-
"dollar": null,
77-
"name": {
78-
"kind": "VariableName",
79-
"textLength": 2
80-
}
81-
}
8282
}
8383
}
8484
},

tests/cases/parser/ternaryExpressions6.php.tree

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@
1616
"expression": {
1717
"TernaryExpression": {
1818
"condition": {
19+
"Variable": {
20+
"dollar": null,
21+
"name": {
22+
"kind": "VariableName",
23+
"textLength": 2
24+
}
25+
}
26+
},
27+
"questionToken": {
28+
"kind": "QuestionToken",
29+
"textLength": 1
30+
},
31+
"ifExpression": {
32+
"Variable": {
33+
"dollar": null,
34+
"name": {
35+
"kind": "VariableName",
36+
"textLength": 2
37+
}
38+
}
39+
},
40+
"colonToken": {
41+
"kind": "ColonToken",
42+
"textLength": 1
43+
},
44+
"elseExpression": {
1945
"TernaryExpression": {
2046
"condition": {
2147
"Variable": {
@@ -30,15 +56,7 @@
3056
"kind": "QuestionToken",
3157
"textLength": 1
3258
},
33-
"ifExpression": {
34-
"Variable": {
35-
"dollar": null,
36-
"name": {
37-
"kind": "VariableName",
38-
"textLength": 2
39-
}
40-
}
41-
},
59+
"ifExpression": null,
4260
"colonToken": {
4361
"kind": "ColonToken",
4462
"textLength": 1
@@ -53,24 +71,6 @@
5371
}
5472
}
5573
}
56-
},
57-
"questionToken": {
58-
"kind": "QuestionToken",
59-
"textLength": 1
60-
},
61-
"ifExpression": null,
62-
"colonToken": {
63-
"kind": "ColonToken",
64-
"textLength": 1
65-
},
66-
"elseExpression": {
67-
"Variable": {
68-
"dollar": null,
69-
"name": {
70-
"kind": "VariableName",
71-
"textLength": 2
72-
}
73-
}
7474
}
7575
}
7676
},

tests/cases/parser/ternaryExpressions7.php.tree

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
"expression": {
1717
"TernaryExpression": {
1818
"condition": {
19+
"Variable": {
20+
"dollar": null,
21+
"name": {
22+
"kind": "VariableName",
23+
"textLength": 2
24+
}
25+
}
26+
},
27+
"questionToken": {
28+
"kind": "QuestionToken",
29+
"textLength": 1
30+
},
31+
"ifExpression": null,
32+
"colonToken": {
33+
"kind": "ColonToken",
34+
"textLength": 1
35+
},
36+
"elseExpression": {
1937
"TernaryExpression": {
2038
"condition": {
2139
"Variable": {
@@ -30,7 +48,15 @@
3048
"kind": "QuestionToken",
3149
"textLength": 1
3250
},
33-
"ifExpression": null,
51+
"ifExpression": {
52+
"Variable": {
53+
"dollar": null,
54+
"name": {
55+
"kind": "VariableName",
56+
"textLength": 2
57+
}
58+
}
59+
},
3460
"colonToken": {
3561
"kind": "ColonToken",
3662
"textLength": 1
@@ -45,32 +71,6 @@
4571
}
4672
}
4773
}
48-
},
49-
"questionToken": {
50-
"kind": "QuestionToken",
51-
"textLength": 1
52-
},
53-
"ifExpression": {
54-
"Variable": {
55-
"dollar": null,
56-
"name": {
57-
"kind": "VariableName",
58-
"textLength": 2
59-
}
60-
}
61-
},
62-
"colonToken": {
63-
"kind": "ColonToken",
64-
"textLength": 1
65-
},
66-
"elseExpression": {
67-
"Variable": {
68-
"dollar": null,
69-
"name": {
70-
"kind": "VariableName",
71-
"textLength": 2
72-
}
73-
}
7474
}
7575
}
7676
},

0 commit comments

Comments
 (0)