1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
--- xonsh-0.14.4/xonsh/parsers/v310.orig.py 2024-01-18 17:58:44.463700213 +0200
+++ xonsh-0.14.4/xonsh/parsers/v310.py 2024-01-18 19:03:33.985074856 +0200
@@ -76,26 +76,24 @@
"""
loc = self.get_line_cols(p, 1)
- match list(p):
- case [_, _, pattern, _, suite]:
- p[0] = ast.match_case(pattern=pattern, body=suite, **loc)
- case [_, _, pattern, _, guard, _, suite]:
- p[0] = ast.match_case(pattern=pattern, body=suite, guard=guard, **loc)
- case _:
- raise AssertionError()
+ if list(p) == [_, _, pattern, _, suite]:
+ p[0] = ast.match_case(pattern=pattern, body=suite, **loc)
+ elif list(p) == [_, _, pattern, _, guard, _, suite]:
+ p[0] = ast.match_case(pattern=pattern, body=suite, guard=guard, **loc)
+ else:
+ raise AssertionError()
def p_case_block_list_nonempty(self, p):
"""
case_block_list_nonempty : case_block
| case_block case_block_list_nonempty
"""
- match list(p):
- case [_, case_block]:
- p[0] = [case_block]
- case [_, case_block, case_block_list_nonempty]:
- p[0] = [case_block] + case_block_list_nonempty
- case _:
- raise AssertionError()
+ if list(p) == [_, case_block]:
+ p[0] = [case_block]
+ elif list(p) == [_, case_block, case_block_list_nonempty]:
+ p[0] = [case_block] + case_block_list_nonempty
+ else:
+ raise AssertionError()
# subject expression
def p_subject_expr_single_value(self, p):
@@ -103,33 +101,31 @@
subject_expr : test_or_star_expr comma_opt
"""
- match list(p):
- case [_, test_or_star_expr, None]:
- # single value
- p[0] = test_or_star_expr
- case [_, test_or_star_expr, ","]:
- # tuple with one element
- p[0] = ast.Tuple(
- elts=[test_or_star_expr], ctx=ast.Load(), **self.get_line_cols(p, 1)
- )
- case _:
- raise AssertionError()
+ if list(p) == [_, test_or_star_expr, None]:
+ # single value
+ p[0] = test_or_star_expr
+ elif list(p) == [_, test_or_star_expr, ","]:
+ # tuple with one element
+ p[0] = ast.Tuple(
+ elts=[test_or_star_expr], ctx=ast.Load(), **self.get_line_cols(p, 1)
+ )
+ else:
+ raise AssertionError()
def p_subject_expr_multiple_values(self, p):
"""
subject_expr : test_or_star_expr comma_test_or_star_expr_list comma_opt
"""
- match list(p):
- case [_, test_or_star_expr, comma_test_or_star_expr_list, "," | None]:
- # tuple with more than one element
- p[0] = ast.Tuple(
- elts=[test_or_star_expr] + comma_test_or_star_expr_list,
- ctx=ast.Load(),
- **self.get_line_cols(p, 1),
- )
- case _:
- raise AssertionError()
+ if list(p) == [_, test_or_star_expr, comma_test_or_star_expr_list, "," | None]:
+ # tuple with more than one element
+ p[0] = ast.Tuple(
+ elts=[test_or_star_expr] + comma_test_or_star_expr_list,
+ ctx=ast.Load(),
+ **self.get_line_cols(p, 1),
+ )
+ else:
+ raise AssertionError()
# patterns
def p_closed_pattern(self, p):
@@ -167,22 +163,20 @@
_, or_pattern_list = p
- match or_pattern_list:
- case [single_value]:
- p[0] = single_value
- case multiple_values:
- p[0] = ast.MatchOr(patterns=multiple_values, **self.get_line_cols(p, 1))
+ if or_pattern_list == [single_value]:
+ p[0] = single_value
+ elif or_pattern_list == multiple_values:
+ p[0] = ast.MatchOr(patterns=multiple_values, **self.get_line_cols(p, 1))
def p_or_pattern_list(self, p):
"""
or_pattern_list : closed_pattern
| closed_pattern PIPE or_pattern_list
"""
- match list(p):
- case [_, closed_pattern]:
- p[0] = [closed_pattern]
- case [_, closed_pattern, "|", or_pattern_list]:
- p[0] = [closed_pattern] + or_pattern_list
+ if list(p) == [_, closed_pattern]:
+ p[0] = [closed_pattern]
+ elif list(p) == [_, closed_pattern, "|", or_pattern_list]:
+ p[0] = [closed_pattern] + or_pattern_list
# group pattern
def p_group_pattern(self, p):
@@ -198,11 +192,10 @@
literal_pattern : literal_expr
"""
- match p[1]:
- case None | True | False:
- p[0] = ast.MatchSingleton(value=p[1], **self.get_line_cols(p, 1))
- case _:
- p[0] = ast.MatchValue(value=p[1], **self.get_line_cols(p, 1))
+ if p[1] == None | True | False:
+ p[0] = ast.MatchSingleton(value=p[1], **self.get_line_cols(p, 1))
+ else:
+ p[0] = ast.MatchValue(value=p[1], **self.get_line_cols(p, 1))
def p_literal_expr_number_or_string_literal_list(self, p):
"""
@@ -212,11 +205,10 @@
p[0] = p[1]
- match p[1]:
- case ast.JoinedStr():
- raise AssertionError("patterns may not match formatted string literals")
- # TODO: raise SyntaxError instead
- # (doing so currently somehow causes an IndexError in tools.py:get_logical_line)
+ if p[1] == ast.JoinedStr():
+ raise AssertionError("patterns may not match formatted string literals")
+ # TODO: raise SyntaxError instead
+ # (doing so currently somehow causes an IndexError in tools.py:get_logical_line)
# TODO: f"hi" "hi" does not parse in xonsh
@@ -227,15 +219,14 @@
| false_tok
"""
- match p[1].value:
- case "None":
- value = None
- case "True":
- value = True
- case "False":
- value = False
- case _:
- raise AssertionError()
+ if p[1].value == "None":
+ value = None
+ elif "True":
+ value = True
+ elif "False":
+ value = False
+ else:
+ raise AssertionError()
p[0] = value
@@ -253,19 +244,18 @@
build_complex = False
loc = self.get_line_cols(p, 1)
- match list(p):
- case [_, x]:
- p[0] = x
- case [_, "-", x]:
- p[0] = ast.UnaryOp(op=ast.USub(), operand=x, **loc)
- case [_, left, ("+" | "-") as op_char, right]:
- build_complex = True
- negate_left_side = False
- case [_, "-", left, ("+" | "-") as op_char, right]:
- build_complex = True
- negate_left_side = True
- case _:
- raise AssertionError()
+ if list(p) == [_, x]:
+ p[0] = x
+ elif list(p) == [_, "-", x]:
+ p[0] = ast.UnaryOp(op=ast.USub(), operand=x, **loc)
+ elif list(p) == [_, left, ("+", op_char) | ("-", op_char), right]:
+ build_complex = True
+ negate_left_side = False
+ elif list(p) == [_, "-", left, ("+", op_char) | ("-", op_char), right]:
+ build_complex = True
+ negate_left_side = True
+ else:
+ raise AssertionError()
if build_complex:
# TODO raise syntax error instead (see reason in p_literal_expr_number_or_string_literal_list)
@@ -322,17 +312,16 @@
| LPAREN RPAREN
"""
- match list(p):
- case [_, _, ast.MatchSequence() as seq, _]:
- p[0] = seq
- case [_, _, single_item, _]:
- p[0] = ast.MatchSequence(
- patterns=[single_item], **self.get_line_cols(p, 1)
- )
- case [_, _, _]:
- p[0] = ast.MatchSequence(patterns=[], **self.get_line_cols(p, 1))
- case _:
- raise AssertionError()
+ if list(p) == [_, _, (ast.MatchSequence(), seq), _]:
+ p[0] = seq
+ elif list(p) == [_, _, single_item, _]:
+ p[0] = ast.MatchSequence(
+ patterns=[single_item], **self.get_line_cols(p, 1)
+ )
+ elif list(p) == [_, _, _]:
+ p[0] = ast.MatchSequence(patterns=[], **self.get_line_cols(p, 1))
+ else:
+ raise AssertionError()
def p_maybe_sequence_pattern(self, p):
"""
@@ -340,30 +329,24 @@
| maybe_star_pattern COMMA maybe_sequence_pattern
"""
- match list(p):
- case [_, maybe_star_pattern, ","]:
- p[0] = ast.MatchSequence(
- patterns=[maybe_star_pattern], **self.get_line_cols(p, 1)
- )
- case [_, maybe_star_pattern, None]:
- p[0] = maybe_star_pattern
- case [
- _,
- maybe_star_pattern,
- ",",
- ast.MatchSequence(patterns=list(maybe_sequence_pattern)),
- ]:
- p[0] = ast.MatchSequence(
- patterns=[maybe_star_pattern] + maybe_sequence_pattern,
- **self.get_line_cols(p, 1),
- )
- case [_, maybe_star_pattern, ",", maybe_sequence_pattern]:
- p[0] = ast.MatchSequence(
- patterns=[maybe_star_pattern, maybe_sequence_pattern],
- **self.get_line_cols(p, 1),
- )
- case _:
- raise AssertionError()
+ if list(p) == [_, maybe_star_pattern, ","]:
+ p[0] = ast.MatchSequence(
+ patterns=[maybe_star_pattern], **self.get_line_cols(p, 1)
+ )
+ elif list(p) == [_, maybe_star_pattern, None]:
+ p[0] = maybe_star_pattern
+ elif list(p) == [_, maybe_star_pattern, ",", ast.MatchSequence(patterns=list(maybe_sequence_pattern)),]:
+ p[0] = ast.MatchSequence(
+ patterns=[maybe_star_pattern] + maybe_sequence_pattern,
+ **self.get_line_cols(p, 1),
+ )
+ elif list(p) == [_, maybe_star_pattern, ",", maybe_sequence_pattern]:
+ p[0] = ast.MatchSequence(
+ patterns=[maybe_star_pattern, maybe_sequence_pattern],
+ **self.get_line_cols(p, 1),
+ )
+ else:
+ raise AssertionError()
def p_open_sequence_pattern(self, p):
"""
@@ -429,19 +412,18 @@
| name_str EQUALS pattern COMMA class_pattern_keyword_part
"""
- match list(p):
- case [_]:
- p[0] = ([], [])
- case [_, pattern]:
- p[0] = ([pattern], [])
- case [_, pattern, ",", (names, patterns)]:
- p[0] = ([pattern] + names, patterns)
- case [_, name, "=", pattern]:
- p[0] = ([], [(name, pattern)])
- case [_, name, "=", pattern, ",", class_pattern_keyword_part]:
- p[0] = ([], [(name, pattern)] + class_pattern_keyword_part)
- case _:
- raise AssertionError()
+ if list(p) == [_]:
+ p[0] = ([], [])
+ elif list(p) == [_, pattern]:
+ p[0] = ([pattern], [])
+ elif list(p) == [_, pattern, ",", (names, patterns)]:
+ p[0] = ([pattern] + names, patterns)
+ elif list(p) == [_, name, "=", pattern]:
+ p[0] = ([], [(name, pattern)])
+ elif list(p) == [_, name, "=", pattern, ",", class_pattern_keyword_part]:
+ p[0] = ([], [(name, pattern)] + class_pattern_keyword_part)
+ else:
+ raise AssertionError()
# returns ( [pattern], [ (name, pattern) ] )
def p_class_pattern_positional_part_skip(self, p):
@@ -457,13 +439,12 @@
| pattern COMMA class_pattern_positional_part
"""
- match list(p):
- case [_, pattern]:
- p[0] = ([pattern], [])
- case [_, pattern, ",", (names, patterns)]:
- p[0] = ([pattern] + names, patterns)
- case _:
- raise AssertionError()
+ if list(p) == [_, pattern]:
+ p[0] = ([pattern], [])
+ elif list(p) == [_, pattern, ",", (names, patterns)]:
+ p[0] = ([pattern] + names, patterns)
+ else:
+ raise AssertionError()
# returns [ (name, pattern) ]
def p_class_pattern_keyword_part(self, p):
@@ -474,15 +455,14 @@
| name_str EQUALS pattern COMMA class_pattern_keyword_part
"""
- match list(p):
- case [_] | [_, ","]:
- p[0] = []
- case [_, name, "=", pattern]:
- p[0] = [(name, pattern)]
- case [_, name, "=", pattern, ",", class_pattern_keyword_part]:
- p[0] = [(name, pattern)] + class_pattern_keyword_part
- case _:
- raise AssertionError()
+ if list(p) == [_] | [_, ","]:
+ p[0] = []
+ elif list(p) == [_, name, "=", pattern]:
+ p[0] = [(name, pattern)]
+ elif list(p) == [_, name, "=", pattern, ",", class_pattern_keyword_part]:
+ p[0] = [(name, pattern)] + class_pattern_keyword_part
+ else:
+ raise AssertionError()
# Mapping pattern
@@ -505,43 +485,40 @@
| key_value_pattern COMMA mapping_pattern_args_item_part
| double_star_pattern
"""
- match list(p):
- case [_]:
- p[0] = [], [], None
- case [_, (key, value)]:
- p[0] = [key], [value], None
- case [_, (key, value), ",", (keys, values, rest)]:
- p[0] = [key] + keys, [value] + values, rest
- case [_, str(double_star_pattern)]:
- p[0] = [], [], double_star_pattern
- case _:
- raise AssertionError()
+ if list(p) == [_]:
+ p[0] = [], [], None
+ elif list(p) == [_, (key, value)]:
+ p[0] = [key], [value], None
+ elif list(p) == [_, (key, value), ",", (keys, values, rest)]:
+ p[0] = [key] + keys, [value] + values, rest
+ elif list(p) == [_, str(double_star_pattern)]:
+ p[0] = [], [], double_star_pattern
+ else:
+ raise AssertionError()
def p_mapping_pattern_args_item_part_skip(self, p):
"""
mapping_pattern_args_item_part :
| double_star_pattern
"""
- match list(p):
- case [_]:
- p[0] = [], [], None
- case [_, rest]:
- p[0] = [], [], rest
- case _:
- raise AssertionError()
+ if list(p) == [_]:
+ p[0] = [], [], None
+ elif list(p) == [_, rest]:
+ p[0] = [], [], rest
+ else:
+ raise AssertionError()
def p_mapping_pattern_args_item_part(self, p):
"""
mapping_pattern_args_item_part : key_value_pattern
| key_value_pattern COMMA mapping_pattern_args_item_part
"""
- match list(p):
- case [_, (key, value)]:
- p[0] = [key], [value], None
- case [_, (key, value), ",", (keys, values, rest)]:
- p[0] = [key] + keys, [value] + values, rest
- case _:
- raise AssertionError()
+ if list(p) == [_, (key, value)]:
+ p[0] = [key], [value], None
+ elif list(p) == [_, (key, value), ",", (keys, values, rest)]:
+ p[0] = [key] + keys, [value] + values, rest
+ else:
+ raise AssertionError()
def p_double_star_pattern(self, p):
"""
|