Skip to content

Commit be9b71d

Browse files
authored
KCL: Parser should ignore whitespace in array subscript (#7782)
Thanks to Lee for pointing this out
1 parent 5d9fadd commit be9b71d

File tree

2 files changed

+324
-2
lines changed

2 files changed

+324
-2
lines changed

rust/kcl-lib/src/parsing/parser.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,11 @@ fn member_expression_dot(i: &mut TokenSlice) -> ModalResult<(Expr, usize, bool)>
13591359
Ok((property, end, computed))
13601360
}
13611361

1362-
fn member_expression_subscript_complex_expr(i: &mut TokenSlice) -> ModalResult<(Expr, usize, bool)> {
1362+
fn member_expression_subscript(i: &mut TokenSlice) -> ModalResult<(Expr, usize, bool)> {
13631363
let _ = open_bracket.parse_next(i)?;
1364+
ignore_whitespace(i);
13641365
let property = expression.parse_next(i)?;
1366+
ignore_whitespace(i);
13651367
let end = close_bracket.parse_next(i)?.end;
13661368
let computed = true;
13671369
Ok((property, end, computed))
@@ -1371,7 +1373,7 @@ fn member_expression_subscript_complex_expr(i: &mut TokenSlice) -> ModalResult<(
13711373
/// Can be arbitrarily nested, e.g. `people[i]['adam'].age`.
13721374
fn build_member_expression(object: Expr, i: &mut TokenSlice) -> ModalResult<Node<MemberExpression>> {
13731375
// Now a sequence of members.
1374-
let member = alt((member_expression_dot, member_expression_subscript_complex_expr)).context(expected("a member/property, e.g. size.x and size['height'] and size[0] are all different ways to access a member/property of 'size'"));
1376+
let member = alt((member_expression_dot, member_expression_subscript)).context(expected("a member/property, e.g. size.x and size['height'] and size[0] are all different ways to access a member/property of 'size'"));
13751377
let mut members: Vec<_> = repeat(1.., member)
13761378
.context(expected("a sequence of at least one members/properties"))
13771379
.parse_next(i)?;
@@ -5689,6 +5691,12 @@ my14 = 4 ^ 2 - 3 ^ 2 * 2
56895691
excl = [0..<10]"#
56905692
);
56915693
snapshot_test!(space_between_unary_and_operand, r#"x = - 1"#);
5694+
snapshot_test!(
5695+
space_between_expr_and_array,
5696+
r#"outer_points = [1, 2, 3]
5697+
i = 0
5698+
outer_points[ if i + 1 < 5 { i + 1 } else { 0 } ]"#
5699+
);
56925700
}
56935701

56945702
#[allow(unused)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
---
2+
source: kcl-lib/src/parsing/parser.rs
3+
expression: actual
4+
---
5+
{
6+
"body": [
7+
{
8+
"commentStart": 0,
9+
"declaration": {
10+
"commentStart": 0,
11+
"end": 24,
12+
"id": {
13+
"commentStart": 0,
14+
"end": 12,
15+
"moduleId": 0,
16+
"name": "outer_points",
17+
"start": 0,
18+
"type": "Identifier"
19+
},
20+
"init": {
21+
"commentStart": 15,
22+
"elements": [
23+
{
24+
"commentStart": 16,
25+
"end": 17,
26+
"moduleId": 0,
27+
"raw": "1",
28+
"start": 16,
29+
"type": "Literal",
30+
"type": "Literal",
31+
"value": {
32+
"value": 1.0,
33+
"suffix": "None"
34+
}
35+
},
36+
{
37+
"commentStart": 19,
38+
"end": 20,
39+
"moduleId": 0,
40+
"raw": "2",
41+
"start": 19,
42+
"type": "Literal",
43+
"type": "Literal",
44+
"value": {
45+
"value": 2.0,
46+
"suffix": "None"
47+
}
48+
},
49+
{
50+
"commentStart": 22,
51+
"end": 23,
52+
"moduleId": 0,
53+
"raw": "3",
54+
"start": 22,
55+
"type": "Literal",
56+
"type": "Literal",
57+
"value": {
58+
"value": 3.0,
59+
"suffix": "None"
60+
}
61+
}
62+
],
63+
"end": 24,
64+
"moduleId": 0,
65+
"start": 15,
66+
"type": "ArrayExpression",
67+
"type": "ArrayExpression"
68+
},
69+
"moduleId": 0,
70+
"start": 0,
71+
"type": "VariableDeclarator"
72+
},
73+
"end": 24,
74+
"kind": "const",
75+
"moduleId": 0,
76+
"start": 0,
77+
"type": "VariableDeclaration",
78+
"type": "VariableDeclaration"
79+
},
80+
{
81+
"commentStart": 25,
82+
"declaration": {
83+
"commentStart": 25,
84+
"end": 30,
85+
"id": {
86+
"commentStart": 25,
87+
"end": 26,
88+
"moduleId": 0,
89+
"name": "i",
90+
"start": 25,
91+
"type": "Identifier"
92+
},
93+
"init": {
94+
"commentStart": 29,
95+
"end": 30,
96+
"moduleId": 0,
97+
"raw": "0",
98+
"start": 29,
99+
"type": "Literal",
100+
"type": "Literal",
101+
"value": {
102+
"value": 0.0,
103+
"suffix": "None"
104+
}
105+
},
106+
"moduleId": 0,
107+
"start": 25,
108+
"type": "VariableDeclarator"
109+
},
110+
"end": 30,
111+
"kind": "const",
112+
"moduleId": 0,
113+
"start": 25,
114+
"type": "VariableDeclaration",
115+
"type": "VariableDeclaration"
116+
},
117+
{
118+
"commentStart": 31,
119+
"end": 80,
120+
"expression": {
121+
"commentStart": 31,
122+
"computed": true,
123+
"end": 80,
124+
"moduleId": 0,
125+
"object": {
126+
"abs_path": false,
127+
"commentStart": 31,
128+
"end": 43,
129+
"moduleId": 0,
130+
"name": {
131+
"commentStart": 31,
132+
"end": 43,
133+
"moduleId": 0,
134+
"name": "outer_points",
135+
"start": 31,
136+
"type": "Identifier"
137+
},
138+
"path": [],
139+
"start": 31,
140+
"type": "Name",
141+
"type": "Name"
142+
},
143+
"property": {
144+
"commentStart": 45,
145+
"cond": {
146+
"commentStart": 48,
147+
"end": 57,
148+
"left": {
149+
"commentStart": 48,
150+
"end": 53,
151+
"left": {
152+
"abs_path": false,
153+
"commentStart": 48,
154+
"end": 49,
155+
"moduleId": 0,
156+
"name": {
157+
"commentStart": 48,
158+
"end": 49,
159+
"moduleId": 0,
160+
"name": "i",
161+
"start": 48,
162+
"type": "Identifier"
163+
},
164+
"path": [],
165+
"start": 48,
166+
"type": "Name",
167+
"type": "Name"
168+
},
169+
"moduleId": 0,
170+
"operator": "+",
171+
"right": {
172+
"commentStart": 52,
173+
"end": 53,
174+
"moduleId": 0,
175+
"raw": "1",
176+
"start": 52,
177+
"type": "Literal",
178+
"type": "Literal",
179+
"value": {
180+
"value": 1.0,
181+
"suffix": "None"
182+
}
183+
},
184+
"start": 48,
185+
"type": "BinaryExpression",
186+
"type": "BinaryExpression"
187+
},
188+
"moduleId": 0,
189+
"operator": "<",
190+
"right": {
191+
"commentStart": 56,
192+
"end": 57,
193+
"moduleId": 0,
194+
"raw": "5",
195+
"start": 56,
196+
"type": "Literal",
197+
"type": "Literal",
198+
"value": {
199+
"value": 5.0,
200+
"suffix": "None"
201+
}
202+
},
203+
"start": 48,
204+
"type": "BinaryExpression",
205+
"type": "BinaryExpression"
206+
},
207+
"digest": null,
208+
"else_ifs": [],
209+
"end": 78,
210+
"final_else": {
211+
"body": [
212+
{
213+
"commentStart": 75,
214+
"end": 76,
215+
"expression": {
216+
"commentStart": 75,
217+
"end": 76,
218+
"moduleId": 0,
219+
"raw": "0",
220+
"start": 75,
221+
"type": "Literal",
222+
"type": "Literal",
223+
"value": {
224+
"value": 0.0,
225+
"suffix": "None"
226+
}
227+
},
228+
"moduleId": 0,
229+
"start": 75,
230+
"type": "ExpressionStatement",
231+
"type": "ExpressionStatement"
232+
}
233+
],
234+
"commentStart": 75,
235+
"end": 77,
236+
"moduleId": 0,
237+
"start": 75
238+
},
239+
"moduleId": 0,
240+
"start": 45,
241+
"then_val": {
242+
"body": [
243+
{
244+
"commentStart": 60,
245+
"end": 65,
246+
"expression": {
247+
"commentStart": 60,
248+
"end": 65,
249+
"left": {
250+
"abs_path": false,
251+
"commentStart": 60,
252+
"end": 61,
253+
"moduleId": 0,
254+
"name": {
255+
"commentStart": 60,
256+
"end": 61,
257+
"moduleId": 0,
258+
"name": "i",
259+
"start": 60,
260+
"type": "Identifier"
261+
},
262+
"path": [],
263+
"start": 60,
264+
"type": "Name",
265+
"type": "Name"
266+
},
267+
"moduleId": 0,
268+
"operator": "+",
269+
"right": {
270+
"commentStart": 64,
271+
"end": 65,
272+
"moduleId": 0,
273+
"raw": "1",
274+
"start": 64,
275+
"type": "Literal",
276+
"type": "Literal",
277+
"value": {
278+
"value": 1.0,
279+
"suffix": "None"
280+
}
281+
},
282+
"start": 60,
283+
"type": "BinaryExpression",
284+
"type": "BinaryExpression"
285+
},
286+
"moduleId": 0,
287+
"start": 60,
288+
"type": "ExpressionStatement",
289+
"type": "ExpressionStatement"
290+
}
291+
],
292+
"commentStart": 60,
293+
"end": 66,
294+
"moduleId": 0,
295+
"start": 60
296+
},
297+
"type": "IfExpression",
298+
"type": "IfExpression"
299+
},
300+
"start": 31,
301+
"type": "MemberExpression",
302+
"type": "MemberExpression"
303+
},
304+
"moduleId": 0,
305+
"start": 31,
306+
"type": "ExpressionStatement",
307+
"type": "ExpressionStatement"
308+
}
309+
],
310+
"commentStart": 0,
311+
"end": 80,
312+
"moduleId": 0,
313+
"start": 0
314+
}

0 commit comments

Comments
 (0)