Skip to content

Commit 4aea485

Browse files
authored
Merge pull request glayzzle#361 from glayzzle/feat-use-identifier-for-goto-and-label
feat: use `identifier` for goto and label
2 parents 62cf815 + 1c7e1dc commit 4aea485

File tree

9 files changed

+327
-15
lines changed

9 files changed

+327
-15
lines changed

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 [

test/snapshot/__snapshots__/location.test.js.snap

Lines changed: 183 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5873,12 +5873,83 @@ Program {
58735873
}
58745874
`;
58755875
5876+
exports[`Test locations goto #2 1`] = `
5877+
Program {
5878+
"children": Array [
5879+
Goto {
5880+
"kind": "goto",
5881+
"label": Identifier {
5882+
"kind": "identifier",
5883+
"loc": Location {
5884+
"end": Position {
5885+
"column": 13,
5886+
"line": 1,
5887+
"offset": 13,
5888+
},
5889+
"source": "longName",
5890+
"start": Position {
5891+
"column": 5,
5892+
"line": 1,
5893+
"offset": 5,
5894+
},
5895+
},
5896+
"name": "longName",
5897+
},
5898+
"loc": Location {
5899+
"end": Position {
5900+
"column": 14,
5901+
"line": 1,
5902+
"offset": 14,
5903+
},
5904+
"source": "goto longName;",
5905+
"start": Position {
5906+
"column": 0,
5907+
"line": 1,
5908+
"offset": 0,
5909+
},
5910+
},
5911+
},
5912+
],
5913+
"errors": Array [],
5914+
"kind": "program",
5915+
"loc": Location {
5916+
"end": Position {
5917+
"column": 14,
5918+
"line": 1,
5919+
"offset": 14,
5920+
},
5921+
"source": "goto longName;",
5922+
"start": Position {
5923+
"column": 0,
5924+
"line": 1,
5925+
"offset": 0,
5926+
},
5927+
},
5928+
}
5929+
`;
5930+
58765931
exports[`Test locations goto 1`] = `
58775932
Program {
58785933
"children": Array [
58795934
Goto {
58805935
"kind": "goto",
5881-
"label": "a",
5936+
"label": Identifier {
5937+
"kind": "identifier",
5938+
"loc": Location {
5939+
"end": Position {
5940+
"column": 6,
5941+
"line": 1,
5942+
"offset": 6,
5943+
},
5944+
"source": "a",
5945+
"start": Position {
5946+
"column": 5,
5947+
"line": 1,
5948+
"offset": 5,
5949+
},
5950+
},
5951+
"name": "a",
5952+
},
58825953
"loc": Location {
58835954
"end": Position {
58845955
"column": 7,
@@ -6766,6 +6837,100 @@ Program {
67666837
}
67676838
`;
67686839
6840+
exports[`Test locations label #2 1`] = `
6841+
Program {
6842+
"children": Array [
6843+
Label {
6844+
"kind": "label",
6845+
"loc": Location {
6846+
"end": Position {
6847+
"column": 9,
6848+
"line": 1,
6849+
"offset": 9,
6850+
},
6851+
"source": "longName:",
6852+
"start": Position {
6853+
"column": 0,
6854+
"line": 1,
6855+
"offset": 0,
6856+
},
6857+
},
6858+
"name": Identifier {
6859+
"kind": "identifier",
6860+
"loc": Location {
6861+
"end": Position {
6862+
"column": 8,
6863+
"line": 1,
6864+
"offset": 8,
6865+
},
6866+
"source": "longName",
6867+
"start": Position {
6868+
"column": 0,
6869+
"line": 1,
6870+
"offset": 0,
6871+
},
6872+
},
6873+
"name": "longName",
6874+
},
6875+
},
6876+
Echo {
6877+
"expressions": Array [
6878+
String {
6879+
"isDoubleQuote": true,
6880+
"kind": "string",
6881+
"loc": Location {
6882+
"end": Position {
6883+
"column": 26,
6884+
"line": 1,
6885+
"offset": 26,
6886+
},
6887+
"source": "\\"something\\"",
6888+
"start": Position {
6889+
"column": 15,
6890+
"line": 1,
6891+
"offset": 15,
6892+
},
6893+
},
6894+
"raw": "\\"something\\"",
6895+
"unicode": false,
6896+
"value": "something",
6897+
},
6898+
],
6899+
"kind": "echo",
6900+
"loc": Location {
6901+
"end": Position {
6902+
"column": 27,
6903+
"line": 1,
6904+
"offset": 27,
6905+
},
6906+
"source": "echo \\"something\\";",
6907+
"start": Position {
6908+
"column": 10,
6909+
"line": 1,
6910+
"offset": 10,
6911+
},
6912+
},
6913+
"shortForm": false,
6914+
},
6915+
],
6916+
"errors": Array [],
6917+
"kind": "program",
6918+
"loc": Location {
6919+
"end": Position {
6920+
"column": 27,
6921+
"line": 1,
6922+
"offset": 27,
6923+
},
6924+
"source": "longName: echo \\"something\\";",
6925+
"start": Position {
6926+
"column": 0,
6927+
"line": 1,
6928+
"offset": 0,
6929+
},
6930+
},
6931+
}
6932+
`;
6933+
67696934
exports[`Test locations label 1`] = `
67706935
Program {
67716936
"children": Array [
@@ -6784,7 +6949,23 @@ Program {
67846949
"offset": 0,
67856950
},
67866951
},
6787-
"name": "a",
6952+
"name": Identifier {
6953+
"kind": "identifier",
6954+
"loc": Location {
6955+
"end": Position {
6956+
"column": 1,
6957+
"line": 1,
6958+
"offset": 1,
6959+
},
6960+
"source": "a",
6961+
"start": Position {
6962+
"column": 0,
6963+
"line": 1,
6964+
"offset": 0,
6965+
},
6966+
},
6967+
"name": "a",
6968+
},
67886969
},
67896970
Echo {
67906971
"expressions": Array [

0 commit comments

Comments
 (0)