Skip to content

Commit c9edc29

Browse files
committed
support for star expression in lists
1 parent eaa1062 commit c9edc29

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

baron/grammator_data_structures.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,21 @@ def listmaker_one(pack):
132132
(test,) = pack
133133
return [test]
134134

135+
@pg.production("listmaker : star_expr")
136+
def listmaker_one_star(pack):
137+
(test,) = pack
138+
return [test]
139+
135140
@pg.production("listmaker : test comma listmaker")
136141
def listmaker_more(pack):
137142
(test, comma, listmaker) = pack
138143
return [test, comma] + listmaker
139144

145+
@pg.production("listmaker : star_expr comma listmaker")
146+
def listmaker_more_start(pack):
147+
(test, comma, listmaker) = pack
148+
return [test, comma] + listmaker
149+
140150
@pg.production("atom : LEFT_BRACKET dictmaker RIGHT_BRACKET")
141151
def dict(pack):
142152
(left_bracket, dictmaker, right_bracket,) = pack

tests/test_grammator_data_structures.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,74 @@ def test_list_more():
220220
}
221221
])
222222

223+
def test_list_star_one():
224+
"[*a]"
225+
parse_simple([
226+
('LEFT_SQUARE_BRACKET', '[', [], []),
227+
('STAR', '*', [], []),
228+
('NAME', 'a'),
229+
('RIGHT_SQUARE_BRACKET', ']', [], []),
230+
], [
231+
{
232+
"type": "list",
233+
"first_formatting": [],
234+
"second_formatting": [],
235+
"third_formatting": [],
236+
"fourth_formatting": [],
237+
"value": [
238+
{
239+
"formatting": [],
240+
"type": "star_expression",
241+
"value": {
242+
"type": "name",
243+
"value": "a",
244+
}
245+
}
246+
],
247+
}
248+
])
249+
250+
def test_list_star_more():
251+
"[*a, 'foo']"
252+
parse_simple([
253+
('LEFT_SQUARE_BRACKET', '[', [], []),
254+
('STAR', '*', [], []),
255+
('NAME', 'a'),
256+
('COMMA', ',', [], [('SPACE', ' ')]),
257+
('STRING', "'foo'"),
258+
('RIGHT_SQUARE_BRACKET', ']', [], []),
259+
], [
260+
{
261+
"type": "list",
262+
"first_formatting": [],
263+
"second_formatting": [],
264+
"third_formatting": [],
265+
"fourth_formatting": [],
266+
"value": [
267+
{
268+
"type": "star_expression",
269+
"formatting": [],
270+
"value": {
271+
"type": "name",
272+
"value": "a",
273+
}
274+
},
275+
{
276+
"first_formatting": [],
277+
"second_formatting": [{"type": "space", "value": " "}],
278+
"type": "comma",
279+
},
280+
{
281+
"first_formatting": [],
282+
"second_formatting": [],
283+
"type": "string",
284+
"value": "'foo'",
285+
}
286+
],
287+
}
288+
])
289+
290+
223291

224292
def test_dict_empty():
225293
"{ }"
@@ -301,6 +369,7 @@ def test_dict_one_double_star():
301369
)
302370

303371

372+
304373
def test_dict_more_colon():
305374
"{a: b, b: c, c: d}"
306375
parse_simple([

0 commit comments

Comments
 (0)