Skip to content

Commit 1c7e1dc

Browse files
feat: use identifier for goto and label
1 parent 6e63a00 commit 1c7e1dc

File tree

10 files changed

+330
-21
lines changed

10 files changed

+330
-21
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/statement.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,15 @@ module.exports = {
353353
case this.tok.T_STRING: {
354354
const result = this.node();
355355
const current = [this.token, this.lexer.getState()];
356-
const label = this.text();
356+
const labelNameText = this.text();
357+
let labelName = this.node("identifier");
357358
// AST : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L457
358359
if (this.next().token === ":") {
360+
labelName = labelName(labelNameText);
359361
this.next();
360-
return result("label", label);
362+
return result("label", labelName);
363+
} else {
364+
labelName.destroy();
361365
}
362366

363367
// default fallback expr / T_STRING '::' (etc...)
@@ -371,12 +375,15 @@ module.exports = {
371375

372376
case this.tok.T_GOTO: {
373377
const result = this.node("goto");
374-
let label = null;
378+
let labelName = null;
375379
if (this.next().expect(this.tok.T_STRING)) {
376-
label = this.text();
377-
this.next().expectEndOfStatement();
380+
labelName = this.node("identifier");
381+
const name = this.text();
382+
this.next();
383+
labelName = labelName(name);
384+
this.expectEndOfStatement();
378385
}
379-
return result(label);
386+
return result(labelName);
380387
}
381388

382389
default: {

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,7 +3118,23 @@ Program {
31183118
"offset": 1660,
31193119
},
31203120
},
3121-
"name": "next",
3121+
"name": Identifier {
3122+
"kind": "identifier",
3123+
"loc": Location {
3124+
"end": Position {
3125+
"column": 4,
3126+
"line": 83,
3127+
"offset": 1664,
3128+
},
3129+
"source": "next",
3130+
"start": Position {
3131+
"column": 0,
3132+
"line": 83,
3133+
"offset": 1660,
3134+
},
3135+
},
3136+
"name": "next",
3137+
},
31223138
},
31233139
ExpressionStatement {
31243140
"expression": Assign {
@@ -3663,7 +3679,23 @@ Program {
36633679
"alternate": null,
36643680
"body": Goto {
36653681
"kind": "goto",
3666-
"label": "next",
3682+
"label": Identifier {
3683+
"kind": "identifier",
3684+
"loc": Location {
3685+
"end": Position {
3686+
"column": 49,
3687+
"line": 90,
3688+
"offset": 1847,
3689+
},
3690+
"source": "next",
3691+
"start": Position {
3692+
"column": 45,
3693+
"line": 90,
3694+
"offset": 1843,
3695+
},
3696+
},
3697+
"name": "next",
3698+
},
36673699
"loc": Location {
36683700
"end": Position {
36693701
"column": 50,

test/snapshot/__snapshots__/goto.test.js.snap

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,39 @@ Program {
55
"children": Array [
66
Goto {
77
"kind": "goto",
8-
"label": "a",
8+
"label": Identifier {
9+
"kind": "identifier",
10+
"name": "a",
11+
},
12+
},
13+
Echo {
14+
"expressions": Array [
15+
String {
16+
"isDoubleQuote": true,
17+
"kind": "string",
18+
"raw": "\\"Foo\\"",
19+
"unicode": false,
20+
"value": "Foo",
21+
},
22+
],
23+
"kind": "echo",
24+
"shortForm": false,
25+
},
26+
],
27+
"errors": Array [],
28+
"kind": "program",
29+
}
30+
`;
31+
32+
exports[`goto simple 2`] = `
33+
Program {
34+
"children": Array [
35+
Goto {
36+
"kind": "goto",
37+
"label": Identifier {
38+
"kind": "identifier",
39+
"name": "longName",
40+
},
941
},
1042
Echo {
1143
"expressions": Array [

test/snapshot/__snapshots__/label.test.js.snap

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,39 @@ Program {
55
"children": Array [
66
Label {
77
"kind": "label",
8-
"name": "a",
8+
"name": Identifier {
9+
"kind": "identifier",
10+
"name": "a",
11+
},
12+
},
13+
Echo {
14+
"expressions": Array [
15+
String {
16+
"isDoubleQuote": true,
17+
"kind": "string",
18+
"raw": "\\"Foo\\"",
19+
"unicode": false,
20+
"value": "Foo",
21+
},
22+
],
23+
"kind": "echo",
24+
"shortForm": false,
25+
},
26+
],
27+
"errors": Array [],
28+
"kind": "program",
29+
}
30+
`;
31+
32+
exports[`label simple 2`] = `
33+
Program {
34+
"children": Array [
35+
Label {
36+
"kind": "label",
37+
"name": Identifier {
38+
"kind": "identifier",
39+
"name": "longName",
40+
},
941
},
1042
Echo {
1143
"expressions": Array [

0 commit comments

Comments
 (0)