Skip to content

Commit 72cde99

Browse files
committed
#205 - revert to old behavior & introduce a generic fix
1 parent 6e63a00 commit 72cde99

14 files changed

+127
-141
lines changed

src/ast.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,15 @@ AST.prototype.resolvePrecedence = function(result, parser) {
323323
result = buffer;
324324
}
325325
}
326-
} else if (
327-
result.kind === "silent" &&
328-
!result.expr.parenthesizedExpression
329-
) {
330-
if (result.expr.kind === 'assign') return result;
326+
} else if (result.kind === "silent" && !result.expr.parenthesizedExpression) {
327+
if (result.expr.kind === "assign") return result;
331328
// overall least precedence
332329
if (result.expr.right) {
333330
buffer = result.expr;
334331
result.expr = buffer.left;
335332
buffer.left = result;
336333
this.swapLocations(buffer, buffer.left, buffer.right, parser);
337-
result = buffer;
334+
result = buffer;
338335
}
339336
}
340337
return result;

src/parser/array.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ module.exports = {
7171
return this.node("noop")();
7272
}
7373
if (this.token === "&") {
74-
return this.node("byref")(this.next().read_variable(true, false));
74+
return this.read_byref(this.read_variable.bind(this, true, false));
7575
} else {
7676
const entry = this.node(ArrayEntry);
7777
const expr = this.read_expr();
7878
if (this.token === this.tok.T_DOUBLE_ARROW) {
7979
if (this.next().token === "&") {
8080
return entry(
8181
expr,
82-
this.node("byref")(this.next().read_variable(true, false))
82+
this.read_byref(this.read_variable.bind(this, true, false))
8383
);
8484
} else {
8585
return entry(expr, this.read_expr());

src/parser/expr.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,18 @@ module.exports = {
354354
if (isConst) this.error("VARIABLE");
355355
let right;
356356
if (this.next().token == "&") {
357-
right = this.node("byref");
358-
if (this.next().token === this.tok.T_NEW) {
359-
if (this.php7) {
360-
this.error();
361-
}
362-
right = right(this.read_new_expr());
363-
} else {
364-
right = right(this.read_variable(false, false));
365-
}
357+
right = this.read_byref(
358+
function() {
359+
if (this.token === this.tok.T_NEW) {
360+
if (this.php7) {
361+
this.error();
362+
}
363+
return this.read_new_expr();
364+
} else {
365+
return this.read_variable(false, false);
366+
}
367+
}.bind(this)
368+
);
366369
} else {
367370
right = this.read_expr();
368371
}

src/parser/function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module.exports = {
145145
*/
146146
read_lexical_var: function() {
147147
if (this.token === "&") {
148-
return this.node("byref")(this.next().read_lexical_var());
148+
return this.read_byref(this.read_lexical_var.bind(this));
149149
}
150150
const result = this.node("variable");
151151
this.expect(this.tok.T_VARIABLE);

src/parser/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ module.exports = {
105105
return this.read_list(this.read_namespace_name, ",", false);
106106
},
107107

108+
/**
109+
* Reads the byref token and assign it to the specified node
110+
* @param {*} cb
111+
*/
112+
read_byref: function(cb) {
113+
let byref = this.node("byref");
114+
this.next();
115+
byref = byref();
116+
const result = cb();
117+
this.ast.swapLocations(result, byref, result, this);
118+
result.byref = true;
119+
return result;
120+
},
121+
108122
/**
109123
* Reads a list of variables declarations
110124
*

src/parser/variable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ module.exports = {
2727

2828
// check the byref flag
2929
if (this.token === "&") {
30-
return this.node("byref")(this.next().read_variable(read_only, encapsed));
30+
return this.read_byref(
31+
this.read_variable.bind(this, read_only, encapsed)
32+
);
3133
}
3234

3335
// reads the entry point

test/snapshot/__snapshots__/array.test.js.snap

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,11 @@ Program {
143143
"value": "0",
144144
},
145145
"kind": "entry",
146-
"value": ByRef {
147-
"kind": "byref",
148-
"what": Variable {
149-
"curly": false,
150-
"kind": "variable",
151-
"name": "foo",
152-
},
146+
"value": Variable {
147+
"byref": true,
148+
"curly": false,
149+
"kind": "variable",
150+
"name": "foo",
153151
},
154152
},
155153
Entry {

test/snapshot/__snapshots__/byref.test.js.snap

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@ Program {
1212
"name": "a",
1313
},
1414
"operator": "=",
15-
"right": ByRef {
16-
"kind": "byref",
17-
"what": Call {
18-
"arguments": Array [
19-
ByRef {
20-
"kind": "byref",
21-
"what": Variable {
22-
"curly": false,
23-
"kind": "variable",
24-
"name": "b",
25-
},
26-
},
27-
],
28-
"kind": "call",
29-
"what": ClassReference {
30-
"kind": "classreference",
31-
"name": "foo",
32-
"resolution": "uqn",
15+
"right": Call {
16+
"arguments": Array [
17+
Variable {
18+
"byref": true,
19+
"curly": false,
20+
"kind": "variable",
21+
"name": "b",
3322
},
23+
],
24+
"byref": true,
25+
"kind": "call",
26+
"what": ClassReference {
27+
"kind": "classreference",
28+
"name": "foo",
29+
"resolution": "uqn",
3430
},
3531
},
3632
},
@@ -91,16 +87,14 @@ Program {
9187
"name": "a",
9288
},
9389
"operator": "=",
94-
"right": ByRef {
95-
"kind": "byref",
96-
"what": New {
97-
"arguments": Array [],
98-
"kind": "new",
99-
"what": ClassReference {
100-
"kind": "classreference",
101-
"name": "foo",
102-
"resolution": "uqn",
103-
},
90+
"right": New {
91+
"arguments": Array [],
92+
"byref": true,
93+
"kind": "new",
94+
"what": ClassReference {
95+
"kind": "classreference",
96+
"name": "foo",
97+
"resolution": "uqn",
10498
},
10599
},
106100
},
@@ -118,13 +112,11 @@ Program {
118112
ExpressionStatement {
119113
"expression": OffsetLookup {
120114
"kind": "offsetlookup",
121-
"offset": ByRef {
122-
"kind": "byref",
123-
"what": Variable {
124-
"curly": false,
125-
"kind": "variable",
126-
"name": "bar",
127-
},
115+
"offset": Variable {
116+
"byref": true,
117+
"curly": false,
118+
"kind": "variable",
119+
"name": "bar",
128120
},
129121
"what": Variable {
130122
"curly": false,
@@ -144,13 +136,11 @@ exports[`byref return statement 1`] = `
144136
Program {
145137
"children": Array [
146138
Return {
147-
"expr": ByRef {
148-
"kind": "byref",
149-
"what": Variable {
150-
"curly": false,
151-
"kind": "variable",
152-
"name": "foo",
153-
},
139+
"expr": Variable {
140+
"byref": true,
141+
"curly": false,
142+
"kind": "variable",
143+
"name": "foo",
154144
},
155145
"kind": "return",
156146
},
@@ -164,20 +154,18 @@ exports[`byref static lookup 1`] = `
164154
Program {
165155
"children": Array [
166156
ExpressionStatement {
167-
"expression": ByRef {
168-
"kind": "byref",
169-
"what": StaticLookup {
170-
"kind": "staticlookup",
171-
"offset": Variable {
172-
"curly": false,
173-
"kind": "variable",
174-
"name": "bar",
175-
},
176-
"what": ClassReference {
177-
"kind": "classreference",
178-
"name": "foo",
179-
"resolution": "uqn",
180-
},
157+
"expression": StaticLookup {
158+
"byref": true,
159+
"kind": "staticlookup",
160+
"offset": Variable {
161+
"curly": false,
162+
"kind": "variable",
163+
"name": "bar",
164+
},
165+
"what": ClassReference {
166+
"kind": "classreference",
167+
"name": "foo",
168+
"resolution": "uqn",
181169
},
182170
},
183171
"kind": "expressionstatement",
@@ -192,13 +180,11 @@ exports[`byref variable 1`] = `
192180
Program {
193181
"children": Array [
194182
ExpressionStatement {
195-
"expression": ByRef {
196-
"kind": "byref",
197-
"what": Variable {
198-
"curly": false,
199-
"kind": "variable",
200-
"name": "foo",
201-
},
183+
"expression": Variable {
184+
"byref": true,
185+
"curly": false,
186+
"kind": "variable",
187+
"name": "foo",
202188
},
203189
"kind": "expressionstatement",
204190
},

test/snapshot/__snapshots__/closure.test.js.snap

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,11 @@ Program {
549549
"nullable": false,
550550
"type": null,
551551
"uses": Array [
552-
ByRef {
553-
"kind": "byref",
554-
"what": Variable {
555-
"curly": false,
556-
"kind": "variable",
557-
"name": "message",
558-
},
552+
Variable {
553+
"byref": true,
554+
"curly": false,
555+
"kind": "variable",
556+
"name": "message",
559557
},
560558
],
561559
},

test/snapshot/__snapshots__/function.test.js.snap

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,11 @@ Program {
342342
"raw": "array",
343343
},
344344
"uses": Array [
345-
ByRef {
346-
"kind": "byref",
347-
"what": Variable {
348-
"curly": false,
349-
"kind": "variable",
350-
"name": "c",
351-
},
345+
Variable {
346+
"byref": true,
347+
"curly": false,
348+
"kind": "variable",
349+
"name": "c",
352350
},
353351
Variable {
354352
"curly": false,

test/snapshot/__snapshots__/loop.test.js.snap

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,11 @@ Program {
451451
"key": null,
452452
"kind": "foreach",
453453
"shortForm": false,
454-
"source": ByRef {
455-
"kind": "byref",
456-
"what": Variable {
457-
"curly": false,
458-
"kind": "variable",
459-
"name": "foo",
460-
},
454+
"source": Variable {
455+
"byref": true,
456+
"curly": false,
457+
"kind": "variable",
458+
"name": "foo",
461459
},
462460
"value": Variable {
463461
"curly": false,

test/snapshot/__snapshots__/new.test.js.snap

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ Program {
1212
"name": "a",
1313
},
1414
"operator": "=",
15-
"right": ByRef {
16-
"kind": "byref",
17-
"what": New {
18-
"arguments": Array [],
19-
"kind": "new",
20-
"what": ClassReference {
21-
"kind": "classreference",
22-
"name": "Foo",
23-
"resolution": "uqn",
24-
},
15+
"right": New {
16+
"arguments": Array [],
17+
"byref": true,
18+
"kind": "new",
19+
"what": ClassReference {
20+
"kind": "classreference",
21+
"name": "Foo",
22+
"resolution": "uqn",
2523
},
2624
},
2725
},

0 commit comments

Comments
 (0)