File tree Expand file tree Collapse file tree 4 files changed +64
-8
lines changed Expand file tree Collapse file tree 4 files changed +64
-8
lines changed Original file line number Diff line number Diff line change @@ -493,6 +493,7 @@ AST.prototype.checkNodes = function () {
493
493
require ( "./ast/encapsed" ) ,
494
494
require ( "./ast/encapsedpart" ) ,
495
495
require ( "./ast/entry" ) ,
496
+ require ( "./ast/matchentry" ) ,
496
497
require ( "./ast/error" ) ,
497
498
require ( "./ast/eval" ) ,
498
499
require ( "./ast/exit" ) ,
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -635,21 +635,25 @@ module.exports = {
635
635
return this . node ( "noop" ) ( ) ;
636
636
}
637
637
638
- const entry = this . node ( "entry " ) ;
638
+ const entry = this . node ( "matchentry " ) ;
639
639
640
- let key = null ;
640
+ const keys = [ ] ;
641
641
if ( this . token === this . tok . T_DEFAULT ) {
642
- key = this . node ( "defaultkeyword" ) ( "default" ) ;
642
+ keys . push ( this . node ( "defaultkeyword" ) ( "default" ) ) ;
643
643
this . next ( ) ;
644
644
} 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
+ }
646
650
}
647
651
if ( this . expect ( this . tok . T_DOUBLE_ARROW ) ) {
648
652
this . next ( ) ;
649
653
}
650
654
const value = this . read_expr ( ) ;
651
655
652
- return entry ( key , value , false , false ) ;
656
+ return entry ( keys , value , false , false ) ;
653
657
} ,
654
658
655
659
/**
Original file line number Diff line number Diff line change @@ -4,9 +4,31 @@ describe("match", () => {
4
4
it ( "can be parsed" , ( ) => {
5
5
const ast = parser . parseEval ( `
6
6
$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,
10
32
};
11
33
` ) ;
12
34
console . log ( ast . children [ 0 ] . expression . right . body ) ;
You can’t perform that action at this time.
0 commit comments