Skip to content

Commit c0e1f5c

Browse files
committed
added support for multiple match lhd tokens
1 parent 6e62c9a commit c0e1f5c

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

src/ast.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ AST.prototype.checkNodes = function () {
493493
require("./ast/encapsed"),
494494
require("./ast/encapsedpart"),
495495
require("./ast/entry"),
496+
require("./ast/matchentry"),
496497
require("./ast/error"),
497498
require("./ast/eval"),
498499
require("./ast/exit"),

src/ast/matchentry.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2018 Glayzzle (BSD3 License)
3+
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
4+
* @url http://glayzzle.com
5+
*/
6+
"use strict";
7+
8+
const Expression = require("./expression");
9+
const KIND = "matchentry";
10+
11+
/**
12+
* An array entry - see [Array](#array)
13+
* @constructor Entry
14+
* @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
19+
*/
20+
module.exports = Expression.extends(KIND, function MatchEntry(
21+
keys,
22+
value,
23+
docs,
24+
location
25+
) {
26+
Expression.apply(this, [KIND, docs, location]);
27+
this.keys = keys;
28+
this.value = value;
29+
});

src/parser/expr.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,21 +635,25 @@ module.exports = {
635635
return this.node("noop")();
636636
}
637637

638-
const entry = this.node("entry");
638+
const entry = this.node("matchentry");
639639

640-
let key = null;
640+
const keys = [];
641641
if (this.token === this.tok.T_DEFAULT) {
642-
key = this.node("defaultkeyword")("default");
642+
keys.push(this.node("defaultkeyword")("default"));
643643
this.next();
644644
} else {
645-
key = this.read_expr();
645+
keys.push(this.read_expr());
646+
while (this.token === ",") {
647+
this.next();
648+
keys.push(this.read_expr());
649+
}
646650
}
647651
if (this.expect(this.tok.T_DOUBLE_ARROW)) {
648652
this.next();
649653
}
650654
const value = this.read_expr();
651655

652-
return entry(key, value, false, false);
656+
return entry(keys, value, false, false);
653657
},
654658

655659
/**

test/snapshot/match.test.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,31 @@ describe("match", () => {
44
it("can be parsed", () => {
55
const ast = parser.parseEval(`
66
$test = match($a) {
7-
true => 'yes',
8-
false => 'no',
9-
default => null,
7+
true => 'yes',
8+
false => 'no',
9+
default => null,
10+
};
11+
`);
12+
console.log(ast.children[0].expression.right.body);
13+
// expect (ast).toMatchSnapshot();
14+
});
15+
16+
it("can have lhs, functions", () => {
17+
const ast = parser.parseEval(`
18+
$test = match(true) {
19+
test($a), abc($b) => 'yes',
20+
default => null,
21+
};
22+
`);
23+
console.log(ast.children[0].expression.right.body);
24+
// expect (ast).toMatchSnapshot();
25+
});
26+
27+
it("can have multiple values", () => {
28+
const ast = parser.parseEval(`
29+
$test = match(trye) {
30+
0,1,2,3 => run(),
31+
default => null,
1032
};
1133
`);
1234
console.log(ast.children[0].expression.right.body);

0 commit comments

Comments
 (0)