Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit c75e7f1

Browse files
committed
1 parent d69c6c6 commit c75e7f1

File tree

3 files changed

+131
-112
lines changed

3 files changed

+131
-112
lines changed

src/ast.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ AST.precedence = {};
157157
["+", "-", "."],
158158
["*", "/", "%"],
159159
["!"],
160-
["instanceof"]
160+
["instanceof"],
161+
["cast"]
161162
// TODO: typecasts
162163
// TODO: [ (array)
163164
// TODO: clone, new
@@ -197,6 +198,23 @@ AST.prototype.resolvePrecedence = function(result) {
197198
}
198199
}
199200
}
201+
} else if (
202+
result.kind === "cast" &&
203+
result.what &&
204+
!result.what.parenthesizedExpression
205+
) {
206+
// https://github.com/glayzzle/php-parser/issues/172
207+
if (result.what.kind === "bin") {
208+
buffer = result.what;
209+
result.what = result.what.left;
210+
buffer.left = this.resolvePrecedence(result);
211+
result = buffer;
212+
} else if (result.what.kind === "retif") {
213+
buffer = result.what;
214+
result.what = result.what.test;
215+
buffer.test = this.resolvePrecedence(result);
216+
result = buffer;
217+
}
200218
} else if (result.kind === "unary") {
201219
// https://github.com/glayzzle/php-parser/issues/75
202220
if (result.what && !result.what.parenthesizedExpression) {

test/precedence.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ describe("Test precedence", function() {
122122
it("test static lookup offsets", function() {
123123
shouldBeSame("(Foo::$bar)['baz']();", "Foo::$bar['baz']();");
124124
});
125-
/*it("test cast", function() {
125+
it("test cast", function() {
126126
shouldBeSame("$a = (string)$b . $c", "$a = ((string)$b) . $c");
127127
shouldBeSame("$a = (string)$b->foo . $c", "$a = ((string)$b->foo) . $c");
128-
});*/
128+
shouldBeSame("(bool) $var ? 1 : 2;", "((bool)$var) ? 1 : 2;");
129+
});
129130
});

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,26 +3583,26 @@ Program {
35833583
},
35843584
},
35853585
"shortForm": false,
3586-
"test": Cast {
3587-
"kind": "cast",
3588-
"loc": Location {
3589-
"end": Position {
3590-
"column": 38,
3591-
"line": 90,
3592-
"offset": 1836,
3593-
},
3594-
"source": "(int)$index < count($persians)",
3595-
"start": Position {
3596-
"column": 8,
3597-
"line": 90,
3598-
"offset": 1806,
3586+
"test": Bin {
3587+
"kind": "bin",
3588+
"left": Cast {
3589+
"kind": "cast",
3590+
"loc": Location {
3591+
"end": Position {
3592+
"column": 38,
3593+
"line": 90,
3594+
"offset": 1836,
3595+
},
3596+
"source": "(int)$index < count($persians)",
3597+
"start": Position {
3598+
"column": 8,
3599+
"line": 90,
3600+
"offset": 1806,
3601+
},
35993602
},
3600-
},
3601-
"raw": "(int)",
3602-
"type": "int",
3603-
"what": Bin {
3604-
"kind": "bin",
3605-
"left": Variable {
3603+
"raw": "(int)",
3604+
"type": "int",
3605+
"what": Variable {
36063606
"byref": false,
36073607
"curly": false,
36083608
"kind": "variable",
@@ -3621,76 +3621,76 @@ Program {
36213621
},
36223622
"name": "index",
36233623
},
3624+
},
3625+
"loc": Location {
3626+
"end": Position {
3627+
"column": 38,
3628+
"line": 90,
3629+
"offset": 1836,
3630+
},
3631+
"source": "$index < count($persians)",
3632+
"start": Position {
3633+
"column": 13,
3634+
"line": 90,
3635+
"offset": 1811,
3636+
},
3637+
},
3638+
"right": Call {
3639+
"arguments": Array [
3640+
Variable {
3641+
"byref": false,
3642+
"curly": false,
3643+
"kind": "variable",
3644+
"loc": Location {
3645+
"end": Position {
3646+
"column": 37,
3647+
"line": 90,
3648+
"offset": 1835,
3649+
},
3650+
"source": "$persians",
3651+
"start": Position {
3652+
"column": 28,
3653+
"line": 90,
3654+
"offset": 1826,
3655+
},
3656+
},
3657+
"name": "persians",
3658+
},
3659+
],
3660+
"kind": "call",
36243661
"loc": Location {
36253662
"end": Position {
36263663
"column": 38,
36273664
"line": 90,
36283665
"offset": 1836,
36293666
},
3630-
"source": "$index < count($persians)",
3667+
"source": "($persians)",
36313668
"start": Position {
3632-
"column": 13,
3669+
"column": 27,
36333670
"line": 90,
3634-
"offset": 1811,
3671+
"offset": 1825,
36353672
},
36363673
},
3637-
"right": Call {
3638-
"arguments": Array [
3639-
Variable {
3640-
"byref": false,
3641-
"curly": false,
3642-
"kind": "variable",
3643-
"loc": Location {
3644-
"end": Position {
3645-
"column": 37,
3646-
"line": 90,
3647-
"offset": 1835,
3648-
},
3649-
"source": "$persians",
3650-
"start": Position {
3651-
"column": 28,
3652-
"line": 90,
3653-
"offset": 1826,
3654-
},
3655-
},
3656-
"name": "persians",
3657-
},
3658-
],
3659-
"kind": "call",
3674+
"what": ClassReference {
3675+
"kind": "classreference",
36603676
"loc": Location {
36613677
"end": Position {
3662-
"column": 38,
3663-
"line": 90,
3664-
"offset": 1836,
3665-
},
3666-
"source": "($persians)",
3667-
"start": Position {
36683678
"column": 27,
36693679
"line": 90,
36703680
"offset": 1825,
36713681
},
3672-
},
3673-
"what": ClassReference {
3674-
"kind": "classreference",
3675-
"loc": Location {
3676-
"end": Position {
3677-
"column": 27,
3678-
"line": 90,
3679-
"offset": 1825,
3680-
},
3681-
"source": "count",
3682-
"start": Position {
3683-
"column": 22,
3684-
"line": 90,
3685-
"offset": 1820,
3686-
},
3682+
"source": "count",
3683+
"start": Position {
3684+
"column": 22,
3685+
"line": 90,
3686+
"offset": 1820,
36873687
},
3688-
"name": "count",
3689-
"resolution": "uqn",
36903688
},
3689+
"name": "count",
3690+
"resolution": "uqn",
36913691
},
3692-
"type": "<",
36933692
},
3693+
"type": "<",
36943694
},
36953695
},
36963696
Return {
@@ -5978,26 +5978,26 @@ next:
59785978
},
59795979
},
59805980
"shortForm": false,
5981-
"test": Cast {
5982-
"kind": "cast",
5983-
"loc": Location {
5984-
"end": Position {
5985-
"column": 48,
5986-
"line": 99,
5987-
"offset": 2054,
5988-
},
5989-
"source": "(int)calculateMeaningOfLife() === 42",
5990-
"start": Position {
5991-
"column": 12,
5992-
"line": 99,
5993-
"offset": 2018,
5981+
"test": Bin {
5982+
"kind": "bin",
5983+
"left": Cast {
5984+
"kind": "cast",
5985+
"loc": Location {
5986+
"end": Position {
5987+
"column": 48,
5988+
"line": 99,
5989+
"offset": 2054,
5990+
},
5991+
"source": "(int)calculateMeaningOfLife() === 42",
5992+
"start": Position {
5993+
"column": 12,
5994+
"line": 99,
5995+
"offset": 2018,
5996+
},
59945997
},
5995-
},
5996-
"raw": "(int)",
5997-
"type": "int",
5998-
"what": Bin {
5999-
"kind": "bin",
6000-
"left": Call {
5998+
"raw": "(int)",
5999+
"type": "int",
6000+
"what": Call {
60016001
"arguments": Array [],
60026002
"kind": "call",
60036003
"loc": Location {
@@ -6032,38 +6032,38 @@ next:
60326032
"resolution": "uqn",
60336033
},
60346034
},
6035+
},
6036+
"loc": Location {
6037+
"end": Position {
6038+
"column": 48,
6039+
"line": 99,
6040+
"offset": 2054,
6041+
},
6042+
"source": "calculateMeaningOfLife() === 42",
6043+
"start": Position {
6044+
"column": 17,
6045+
"line": 99,
6046+
"offset": 2023,
6047+
},
6048+
},
6049+
"right": Number {
6050+
"kind": "number",
60356051
"loc": Location {
60366052
"end": Position {
60376053
"column": 48,
60386054
"line": 99,
60396055
"offset": 2054,
60406056
},
6041-
"source": "calculateMeaningOfLife() === 42",
6057+
"source": "42",
60426058
"start": Position {
6043-
"column": 17,
6059+
"column": 46,
60446060
"line": 99,
6045-
"offset": 2023,
6046-
},
6047-
},
6048-
"right": Number {
6049-
"kind": "number",
6050-
"loc": Location {
6051-
"end": Position {
6052-
"column": 48,
6053-
"line": 99,
6054-
"offset": 2054,
6055-
},
6056-
"source": "42",
6057-
"start": Position {
6058-
"column": 46,
6059-
"line": 99,
6060-
"offset": 2052,
6061-
},
6061+
"offset": 2052,
60626062
},
6063-
"value": "42",
60646063
},
6065-
"type": "===",
6064+
"value": "42",
60666065
},
6066+
"type": "===",
60676067
},
60686068
},
60696069
],

0 commit comments

Comments
 (0)