Skip to content

Commit 219cc2d

Browse files
committed
updated names to follow PHP-Parser
1 parent 9e94c03 commit 219cc2d

File tree

5 files changed

+93
-117
lines changed

5 files changed

+93
-117
lines changed

src/ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ AST.prototype.checkNodes = function () {
493493
require("./ast/encapsed"),
494494
require("./ast/encapsedpart"),
495495
require("./ast/entry"),
496-
require("./ast/matchentry"),
497496
require("./ast/error"),
498497
require("./ast/eval"),
499498
require("./ast/exit"),
@@ -517,6 +516,7 @@ AST.prototype.checkNodes = function () {
517516
require("./ast/lookup"),
518517
require("./ast/magic"),
519518
require("./ast/match"),
519+
require("./ast/matcharm"),
520520
require("./ast/method"),
521521
require("./ast/name"),
522522
require("./ast/namespace"),

src/ast/match.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ const KIND = "match";
1212
* Defines a match expression
1313
* @constructor Match
1414
* @extends {Expression}
15-
* @property {Expression} test
16-
* @property {Block} body
15+
* @property {Expression} cond Condition expression to match against
16+
* @property {MatchArm[]} arms
1717
* @property {boolean} shortForm
1818
*/
1919
module.exports = Expression.extends(KIND, function Match(
20-
test,
21-
body,
20+
cond,
21+
arms,
2222
docs,
2323
location
2424
) {
2525
Expression.apply(this, [KIND, docs, location]);
26-
this.test = test;
27-
this.body = body;
26+
this.cond = cond;
27+
this.arms = arms;
2828
});

src/ast/matchentry.js renamed to src/ast/matcharm.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66
"use strict";
77

88
const Expression = require("./expression");
9-
const KIND = "matchentry";
9+
const KIND = "matcharm";
1010

1111
/**
1212
* An array entry - see [Array](#array)
1313
* @constructor Entry
1414
* @extends {Expression}
15-
* @property {Node|null} key The entry key/offset
16-
* @property {Node} value The entry value
17-
* @property {Boolean} byRef By reference
18-
* @property {Boolean} unpack Argument unpacking
15+
* @property {Expression[]|null} conds The match condition expression list - null indicated default arm
16+
* @property {Expression} body The match return value expression
1917
*/
20-
module.exports = Expression.extends(KIND, function MatchEntry(
21-
keys,
22-
value,
18+
module.exports = Expression.extends(KIND, function MatchArm(
19+
conds,
20+
body,
2321
docs,
2422
location
2523
) {
2624
Expression.apply(this, [KIND, docs, location]);
27-
this.keys = keys;
28-
this.value = value;
25+
this.conds = conds;
26+
this.body = body;
2927
});

src/parser/expr.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ module.exports = {
322322
return this.read_internal_functions_in_yacc();
323323

324324
case this.tok.T_MATCH:
325-
return this.read_match_statement();
325+
return this.read_match_expression();
326326
case this.tok.T_INT_CAST:
327327
return this.read_expr_cast("int");
328328

@@ -605,60 +605,53 @@ module.exports = {
605605
);
606606
},
607607

608-
read_match_statement: function () {
608+
read_match_expression: function () {
609609
const node = this.node("match");
610610
this.expect(this.tok.T_MATCH) && this.next();
611-
if (!this.version >= 800) {
612-
this.reaseError("Match statements are not allowed");
611+
if (this.version < 800) {
612+
this.reaseError("Match statements are not allowed before PHP 8");
613613
}
614-
let source = null;
615-
let body = null;
614+
let cond = null;
615+
let arms = [];
616616
if (this.expect("(")) this.next();
617-
source = this.read_expr();
617+
cond = this.read_expr();
618618
if (this.expect(")")) this.next();
619619
if (this.expect("{")) this.next();
620-
body = this.read_match_list();
620+
arms = this.read_match_arms();
621621
if (this.expect("}")) this.next();
622-
return node(source, body);
622+
return node(cond, arms);
623623
},
624624

625-
read_match_list: function () {
626-
return this.read_list(() => this.read_match_pair(), ",", true);
625+
read_match_arms: function () {
626+
return this.read_list(() => this.read_match_arm(), ",", true);
627627
},
628628

629-
read_match_pair: function () {
629+
read_match_arm: function () {
630630
if (this.token === "}") {
631631
return;
632632
}
633-
634633
if (this.token === ",") {
635634
return this.node("noop")();
636635
}
637-
638-
return this.node("matchentry")(
639-
this.read_match_pair_keys(),
640-
this.read_expr(),
641-
false,
642-
false
643-
);
636+
return this.node("matcharm")(this.read_match_arm_conds(), this.read_expr());
644637
},
645638

646-
read_match_pair_keys: function () {
647-
const keys = [];
639+
read_match_arm_conds: function () {
640+
let conds = [];
648641
if (this.token === this.tok.T_DEFAULT) {
649-
keys.push(this.node("defaultkeyword")("default"));
642+
conds = null;
650643
this.next();
651644
} else {
652-
keys.push(this.read_expr());
645+
conds.push(this.read_expr());
653646
while (this.token === ",") {
654647
this.next();
655-
keys.push(this.read_expr());
648+
conds.push(this.read_expr());
656649
}
657650
}
658651
if (this.expect(this.tok.T_DOUBLE_ARROW)) {
659652
this.next();
660653
}
661-
return keys;
654+
return conds;
662655
},
663656

664657
/**

test/snapshot/__snapshots__/match.test.js.snap

Lines changed: 59 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,56 @@ Program {
1313
},
1414
"operator": "=",
1515
"right": Match {
16-
"body": Array [
17-
MatchEntry {
18-
"keys": Array [
19-
Boolean {
20-
"kind": "boolean",
21-
"raw": "true",
22-
"value": true,
23-
},
24-
],
25-
"kind": "matchentry",
26-
"value": String {
16+
"arms": Array [
17+
MatchArm {
18+
"body": String {
2719
"isDoubleQuote": false,
2820
"kind": "string",
2921
"raw": "'yes'",
3022
"unicode": false,
3123
"value": "yes",
3224
},
33-
},
34-
MatchEntry {
35-
"keys": Array [
25+
"conds": Array [
3626
Boolean {
3727
"kind": "boolean",
38-
"raw": "false",
39-
"value": false,
28+
"raw": "true",
29+
"value": true,
4030
},
4131
],
42-
"kind": "matchentry",
43-
"value": String {
32+
"kind": "matcharm",
33+
},
34+
MatchArm {
35+
"body": String {
4436
"isDoubleQuote": false,
4537
"kind": "string",
4638
"raw": "'no'",
4739
"unicode": false,
4840
"value": "no",
4941
},
50-
},
51-
MatchEntry {
52-
"keys": Array [
53-
DefaultKeyword {
54-
"kind": "defaultkeyword",
55-
"raw": "default",
42+
"conds": Array [
43+
Boolean {
44+
"kind": "boolean",
45+
"raw": "false",
46+
"value": false,
5647
},
5748
],
58-
"kind": "matchentry",
59-
"value": NullKeyword {
49+
"kind": "matcharm",
50+
},
51+
MatchArm {
52+
"body": NullKeyword {
6053
"kind": "nullkeyword",
6154
"raw": "null",
6255
},
56+
"conds": null,
57+
"kind": "matcharm",
6358
},
6459
],
65-
"kind": "match",
66-
"test": Variable {
60+
"cond": Variable {
6761
"curly": false,
6862
"kind": "variable",
6963
"name": "a",
7064
},
65+
"kind": "match",
7166
},
7267
},
7368
"kind": "expressionstatement",
@@ -91,9 +86,16 @@ Program {
9186
},
9287
"operator": "=",
9388
"right": Match {
94-
"body": Array [
95-
MatchEntry {
96-
"keys": Array [
89+
"arms": Array [
90+
MatchArm {
91+
"body": String {
92+
"isDoubleQuote": false,
93+
"kind": "string",
94+
"raw": "'yes'",
95+
"unicode": false,
96+
"value": "yes",
97+
},
98+
"conds": Array [
9799
Call {
98100
"arguments": Array [
99101
Variable {
@@ -125,35 +127,23 @@ Program {
125127
},
126128
},
127129
],
128-
"kind": "matchentry",
129-
"value": String {
130-
"isDoubleQuote": false,
131-
"kind": "string",
132-
"raw": "'yes'",
133-
"unicode": false,
134-
"value": "yes",
135-
},
130+
"kind": "matcharm",
136131
},
137-
MatchEntry {
138-
"keys": Array [
139-
DefaultKeyword {
140-
"kind": "defaultkeyword",
141-
"raw": "default",
142-
},
143-
],
144-
"kind": "matchentry",
145-
"value": NullKeyword {
132+
MatchArm {
133+
"body": NullKeyword {
146134
"kind": "nullkeyword",
147135
"raw": "null",
148136
},
137+
"conds": null,
138+
"kind": "matcharm",
149139
},
150140
],
151-
"kind": "match",
152-
"test": Boolean {
141+
"cond": Boolean {
153142
"kind": "boolean",
154143
"raw": "true",
155144
"value": true,
156145
},
146+
"kind": "match",
157147
},
158148
},
159149
"kind": "expressionstatement",
@@ -177,9 +167,18 @@ Program {
177167
},
178168
"operator": "=",
179169
"right": Match {
180-
"body": Array [
181-
MatchEntry {
182-
"keys": Array [
170+
"arms": Array [
171+
MatchArm {
172+
"body": Call {
173+
"arguments": Array [],
174+
"kind": "call",
175+
"what": Name {
176+
"kind": "name",
177+
"name": "run",
178+
"resolution": "uqn",
179+
},
180+
},
181+
"conds": Array [
183182
Number {
184183
"kind": "number",
185184
"value": "0",
@@ -197,37 +196,23 @@ Program {
197196
"value": "3",
198197
},
199198
],
200-
"kind": "matchentry",
201-
"value": Call {
202-
"arguments": Array [],
203-
"kind": "call",
204-
"what": Name {
205-
"kind": "name",
206-
"name": "run",
207-
"resolution": "uqn",
208-
},
209-
},
199+
"kind": "matcharm",
210200
},
211-
MatchEntry {
212-
"keys": Array [
213-
DefaultKeyword {
214-
"kind": "defaultkeyword",
215-
"raw": "default",
216-
},
217-
],
218-
"kind": "matchentry",
219-
"value": NullKeyword {
201+
MatchArm {
202+
"body": NullKeyword {
220203
"kind": "nullkeyword",
221204
"raw": "null",
222205
},
206+
"conds": null,
207+
"kind": "matcharm",
223208
},
224209
],
225-
"kind": "match",
226-
"test": Name {
210+
"cond": Name {
227211
"kind": "name",
228212
"name": "trye",
229213
"resolution": "uqn",
230214
},
215+
"kind": "match",
231216
},
232217
},
233218
"kind": "expressionstatement",

0 commit comments

Comments
 (0)