File tree Expand file tree Collapse file tree 2 files changed +68
-2
lines changed Expand file tree Collapse file tree 2 files changed +68
-2
lines changed Original file line number Diff line number Diff line change 530
530
531
531
nudLbracket : function ( ) {
532
532
if ( this . lookahead ( 0 ) === "Number" || this . lookahead ( 0 ) === "Colon" ) {
533
- return this . parseIndexExpression ( ) ;
533
+ var right = this . parseIndexExpression ( ) ;
534
+ return this . projectIfSlice ( { type : "Identity" } , right ) ;
534
535
} else if ( this . lookahead ( 0 ) === "Star" &&
535
536
this . lookahead ( 1 ) === "Rbracket" ) {
536
537
this . advance ( ) ;
556
557
}
557
558
} ,
558
559
560
+ projectIfSlice : function ( left , right ) {
561
+ var indexExpr = { type : "IndexExpression" , children : [ left , right ] } ;
562
+ if ( right . type === "Slice" ) {
563
+ return {
564
+ type : "Projection" ,
565
+ children : [ indexExpr , this . parseProjectionRHS ( this . bindingPower . Star ) ]
566
+ } ;
567
+ } else {
568
+ return indexExpr ;
569
+ }
570
+ } ,
571
+
559
572
parseSliceExpression : function ( ) {
560
573
// [start:end:step] where each part is optional, as well as the last
561
574
// colon.
653
666
var right ;
654
667
if ( token . type === "Number" || token . type === "Colon" ) {
655
668
right = this . parseIndexExpression ( ) ;
656
- return { type : "IndexExpression" , children : [ left , right ] } ;
669
+ return this . projectIfSlice ( left , right ) ;
657
670
} else {
658
671
this . match ( "Star" ) ;
659
672
this . match ( "Rbracket" ) ;
Original file line number Diff line number Diff line change 127
127
"error" : " syntax"
128
128
}
129
129
]
130
+ }, {
131
+ "given" : {
132
+ "foo" : [{"a" : 1 }, {"a" : 2 }, {"a" : 3 }],
133
+ "bar" : [{"a" : {"b" : 1 }}, {"a" : {"b" : 2 }},
134
+ {"a" : {"b" : 3 }}],
135
+ "baz" : 50
136
+ },
137
+ "cases" : [
138
+ {
139
+ "expression" : " foo[:2].a" ,
140
+ "result" : [1 , 2 ]
141
+ },
142
+ {
143
+ "expression" : " foo[:2].b" ,
144
+ "result" : []
145
+ },
146
+ {
147
+ "expression" : " foo[:2].a.b" ,
148
+ "result" : []
149
+ },
150
+ {
151
+ "expression" : " bar[::-1].a.b" ,
152
+ "result" : [3 , 2 , 1 ]
153
+ },
154
+ {
155
+ "expression" : " bar[:2].a.b" ,
156
+ "result" : [1 , 2 ]
157
+ },
158
+ {
159
+ "expression" : " baz[:2].a" ,
160
+ "result" : null
161
+ }
162
+ ]
163
+ }, {
164
+ "given" : [{"a" : 1 }, {"a" : 2 }, {"a" : 3 }],
165
+ "cases" : [
166
+ {
167
+ "expression" : " [:]" ,
168
+ "result" : [{"a" : 1 }, {"a" : 2 }, {"a" : 3 }]
169
+ },
170
+ {
171
+ "expression" : " [:2].a" ,
172
+ "result" : [1 , 2 ]
173
+ },
174
+ {
175
+ "expression" : " [::-1].a" ,
176
+ "result" : [3 , 2 , 1 ]
177
+ },
178
+ {
179
+ "expression" : " [:2].b" ,
180
+ "result" : []
181
+ }
182
+ ]
130
183
}]
You can’t perform that action at this time.
0 commit comments