-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathPythonParser.g4
More file actions
569 lines (467 loc) · 13.9 KB
/
PythonParser.g4
File metadata and controls
569 lines (467 loc) · 13.9 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
Python grammar.
The MIT License (MIT).
Copyright (c) 2014, Bart Kiers, bart@big-o.nl
Copyright (c) 2019, Dmitriy Litovchenko, Dmitry.Litovchenko1@yandex.ru, Positive Technologies
Copyright (c) 2019, Nikita Subbotin, sub.nik.and@gmail.com, Positive Technologies
Copyright (c) 2019, Ivan Kochurkin, kvanttt@gmail.com, Positive Technologies
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
parser grammar PythonParser;
options { tokenVocab=PythonLexer; superClass=PythonParserBase; }
root
: (single_input
| file_input
| eval_input)? EOF
;
// A single interactive statement;
single_input
: LINE_BREAK
| simple_stmt
| compound_stmt LINE_BREAK
;
// A module or sequence of commands read from an input file
file_input
: (LINE_BREAK | stmt)+
;
// An input for the eval() and input() functions
eval_input
: testlist LINE_BREAK*
;
stmt
: simple_stmt
| compound_stmt
;
compound_stmt
: IF cond=test COLON suite elif_clause* else_clause? #if_stmt
| WHILE test COLON suite else_clause? #while_stmt
| ASYNC? FOR exprlist IN testlist COLON suite else_clause? #for_stmt
| TRY COLON suite (except_clause+ else_clause? finally_clause? | finally_clause) #try_stmt
| ASYNC? WITH with_item (COMMA with_item)* COLON suite #with_stmt
| decorator* (classdef | funcdef) #class_or_func_def_stmt
| match_stmt #match_stmt_wrapper
;
suite
: simple_stmt
| LINE_BREAK INDENT stmt+ DEDENT
;
decorator
: AT dotted_name (OPEN_PAREN arglist? CLOSE_PAREN)? LINE_BREAK
;
elif_clause
: ELIF test COLON suite
;
else_clause
: ELSE COLON suite
;
finally_clause
: FINALLY COLON suite
;
// Python 3.10+ match statement (PEP 634)
match_stmt
: MATCH subject_expr COLON LINE_BREAK INDENT case_block+ DEDENT
;
subject_expr
: star_named_expression COMMA star_named_expressions?
| named_expression
;
star_named_expressions
: (COMMA star_named_expression)+ COMMA?
;
star_named_expression
: STAR expr
| named_expression
;
named_expression
: name ASSIGN test
| test
;
case_block
: CASE pattern guard? COLON suite
;
guard
: IF test
;
// Pattern matching patterns
pattern
: or_pattern
| as_pattern
;
as_pattern
: or_pattern AS pattern_capture_target
;
or_pattern
: closed_pattern (OR_OP closed_pattern)*
;
closed_pattern
: literal_pattern
| capture_pattern
| wildcard_pattern
| class_pattern
| sequence_pattern
| mapping_pattern
| group_pattern
;
literal_pattern
: MINUS? number
| STRING+
| NONE
| TRUE
| FALSE
;
capture_pattern
: pattern_capture_target
;
pattern_capture_target
: name
;
wildcard_pattern
: NAME // Matches '_' specifically, handled semantically
;
class_pattern
: dotted_name OPEN_PAREN pattern_arguments? CLOSE_PAREN
;
pattern_arguments
: positional_patterns COMMA keyword_patterns COMMA?
| positional_patterns COMMA?
| keyword_patterns COMMA?
;
positional_patterns
: pattern (COMMA pattern)*
;
keyword_patterns
: keyword_pattern (COMMA keyword_pattern)*
;
keyword_pattern
: name ASSIGN pattern
;
sequence_pattern
: OPEN_BRACKET maybe_sequence_pattern? CLOSE_BRACKET
| OPEN_PAREN open_sequence_pattern? CLOSE_PAREN
;
open_sequence_pattern
: maybe_star_pattern COMMA maybe_sequence_pattern?
;
maybe_sequence_pattern
: maybe_star_pattern (COMMA maybe_star_pattern)* COMMA?
;
maybe_star_pattern
: star_pattern
| pattern
;
star_pattern
: STAR (pattern_capture_target | wildcard_pattern)
;
mapping_pattern
: OPEN_BRACE items_pattern? CLOSE_BRACE
;
items_pattern
: key_value_pattern (COMMA key_value_pattern)* COMMA?
;
key_value_pattern
: (literal_pattern | attr) COLON pattern
| double_star_pattern
;
double_star_pattern
: POWER pattern_capture_target
;
attr
: dotted_name
;
group_pattern
: OPEN_PAREN pattern CLOSE_PAREN
;
with_item
// NB compile.c makes sure that the default except clause is last
: test (AS expr)?
;
// Python 2 : EXCEPT test COMMA name
// Python 3 : EXCEPT test AS name
// Python 3.14+ (PEP 758): EXCEPT test, test, test without parentheses (only WITHOUT AS clause)
except_clause
: EXCEPT (test ({CheckVersion(2)}? COMMA name {SetVersion(2);} | {CheckVersion(3)}? AS name {SetVersion(3);})?)? COLON suite
| EXCEPT except_types COLON suite // Python 3.14+ without parentheses (no AS clause allowed)
;
// Python 3.14+ allows multiple exception types without parentheses
except_types
: test (COMMA test)+
;
classdef
: CLASS name type_params? (OPEN_PAREN arglist? CLOSE_PAREN)? COLON suite
;
funcdef
: ASYNC? DEF name type_params? OPEN_PAREN typedargslist? CLOSE_PAREN (ARROW test)? COLON suite
;
// Python 3.12+ type parameter syntax (PEP 695)
type_params
: OPEN_BRACKET type_param_list CLOSE_BRACKET
;
type_param_list
: type_param (COMMA type_param)* COMMA?
;
type_param
: name type_param_bound? // TypeVar: T, T: int
| STAR name // TypeVarTuple: *Ts
| POWER name // ParamSpec: **P
;
type_param_bound
: COLON test // T: SomeType or T: (Type1, Type2)
;
// Python 3.12+ type alias statement (PEP 695)
type_alias_stmt
: TYPE name type_params? ASSIGN test
;
// python 3 paramters
// parameters list may have a trailing comma
typedargslist
: (def_parameters COMMA)? (args (COMMA def_parameters)? (COMMA kwargs)? | kwargs) COMMA?
| def_parameters COMMA?
;
args
: STAR named_parameter
;
kwargs
: POWER named_parameter
;
def_parameters
: def_parameter (COMMA def_parameter)*
;
// TODO: bare STAR parameter must follow named ones
def_parameter
: named_parameter (ASSIGN test)?
| STAR
;
named_parameter
: name (COLON test)?
;
simple_stmt
: small_stmt (SEMI_COLON small_stmt)* SEMI_COLON? (LINE_BREAK | EOF)
;
// TODO 1: left part augmented assignment should be `test` only, no stars or lists
// TODO 2: semantically annotated declaration is not an assignment
small_stmt
: testlist_star_expr assign_part? #expr_stmt
| {CheckVersion(2)}? PRINT ((test (COMMA test)* COMMA?)
| RIGHT_SHIFT test ((COMMA test)+ COMMA?)) {SetVersion(2);} #print_stmt // Python 2
| DEL exprlist #del_stmt
| PASS #pass_stmt
| BREAK #break_stmt
| CONTINUE #continue_stmt
| RETURN testlist? #return_stmt
| RAISE (test (COMMA test (COMMA test)?)?)? (FROM test)? #raise_stmt
| yield_expr #yield_stmt
| IMPORT dotted_as_names #import_stmt
| FROM (import_dot_ellipsis* dotted_name | import_dot_ellipsis+)
IMPORT (STAR | OPEN_PAREN import_as_names CLOSE_PAREN | import_as_names) #from_stmt
| GLOBAL name (COMMA name)* #global_stmt
| {CheckVersion(2)}? EXEC expr (IN test (COMMA test)?)? {SetVersion(2);} #exec_stmt // Python 2
| ASSERT test (COMMA test)? #assert_stmt
| {CheckVersion(3)}? NONLOCAL name (COMMA name)* {SetVersion(3);} #nonlocal_stmt // Python 3
| type_alias_stmt #type_stmt // Python 3.12+
;
import_dot_ellipsis : (DOT | ELLIPSIS) ;
testlist_star_expr
: ((test | star_expr) COMMA)+ (test | star_expr)?
| testlist
;
star_expr
: STAR expr
;
assign_part
// if left expression in assign is bool literal, it's mean that is Python 2 here
: ASSIGN ( testlist_star_expr (ASSIGN testlist_star_expr)* (ASSIGN yield_expr)?
| yield_expr)
| {CheckVersion(3)}? COLON test (ASSIGN testlist)? {SetVersion(3);} // annassign Python3 rule
| op=( ADD_ASSIGN
| SUB_ASSIGN
| MULT_ASSIGN
| AT_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| AND_ASSIGN
| OR_ASSIGN
| XOR_ASSIGN
| LEFT_SHIFT_ASSIGN
| RIGHT_SHIFT_ASSIGN
| POWER_ASSIGN
| IDIV_ASSIGN
)
(yield_expr | testlist)
;
exprlist
: expr (COMMA expr)* COMMA?
;
import_as_names
: import_as_name (COMMA import_as_name)* COMMA?
;
// TODO: that means we can use keyword True as the name here: `from foo import bar as True` -- no
import_as_name
: name (AS name)?
;
dotted_as_names
: dotted_as_name (COMMA dotted_as_name)*
;
dotted_as_name
: dotted_name (AS name)?
;
/*
* Warning!
* According to https://docs.python.org/3/reference/expressions.html#lambda LAMBDA should be followed by
* `parameter_list` (in our case it is `typedargslist`)
* But that's not true! `typedargslist` may have parameters with type hinting, but that's not permitted in lambda
* definition
*/
// https://docs.python.org/3/reference/expressions.html#operator-precedence
test
: logical_test (IF logical_test ELSE test)?
| LAMBDA varargslist? COLON test
;
// the same as `typedargslist`, but with no types
varargslist
: (vardef_parameters COMMA)? (varargs (COMMA vardef_parameters)? (COMMA varkwargs)? | varkwargs) COMMA?
| vardef_parameters COMMA?
;
vardef_parameters
: vardef_parameter (COMMA vardef_parameter)*
;
// TODO: bare STAR parameter must follow named ones
vardef_parameter
: name (ASSIGN test)?
| STAR
;
varargs
: STAR name
;
varkwargs
: POWER name
;
logical_test
: comparison
| NOT logical_test
| logical_test op=AND logical_test
| logical_test op=OR logical_test
;
comparison
: comparison (LESS_THAN | GREATER_THAN | EQUALS | GT_EQ | LT_EQ | NOT_EQ_1 | NOT_EQ_2 | optional=NOT? IN | IS optional=NOT?) comparison
| expr
;
expr
: AWAIT? atom trailer*
| <assoc=right> expr op=POWER expr
| op=(ADD | MINUS | NOT_OP) expr
| expr op=(STAR | DIV | MOD | IDIV | AT) expr
| expr op=(ADD | MINUS) expr
| expr op=(LEFT_SHIFT | RIGHT_SHIFT) expr
| expr op=AND_OP expr
| expr op=XOR expr
| expr op=OR_OP expr
;
atom
: OPEN_PAREN (yield_expr | testlist_comp)? CLOSE_PAREN
| OPEN_BRACKET testlist_comp? CLOSE_BRACKET
| OPEN_BRACE dictorsetmaker? CLOSE_BRACE
| REVERSE_QUOTE testlist COMMA? REVERSE_QUOTE
| ELLIPSIS
| name
| PRINT
| EXEC
| MINUS? number
| NONE
| STRING+
;
dictorsetmaker
: (test COLON test | POWER expr) (COMMA (test COLON test | POWER expr))* COMMA? // key_datum_list
| test COLON test comp_for // dict_comprehension
| testlist_comp
;
testlist_comp
: (test | star_expr) (comp_for | (COMMA (test | star_expr))* COMMA?)
;
testlist
: test (COMMA test)* COMMA?
;
dotted_name
: dotted_name DOT name
| name
;
name
: NAME
| TRUE
| FALSE
// Python 3.10+ soft keywords (can be used as identifiers)
| MATCH
| CASE
// Python 3.12+ soft keywords
| TYPE
;
number
: integer
| IMAG_NUMBER
| FLOAT_NUMBER
;
integer
: DECIMAL_INTEGER
| OCT_INTEGER
| HEX_INTEGER
| BIN_INTEGER
;
yield_expr
: YIELD yield_arg?
;
yield_arg
: FROM test
| testlist
;
// TODO: this way we can pass: `f(x for x in i, a)`, but it's invalid.
// See: https://docs.python.org/3/reference/expressions.html#calls
trailer
: DOT name arguments?
| arguments
;
arguments
: OPEN_PAREN arglist? CLOSE_PAREN
| OPEN_BRACKET subscriptlist CLOSE_BRACKET
;
arglist
// The reason that keywords are test nodes instead of name is that using name
// results in an ambiguity. ast.c makes sure it's a name.
: argument (COMMA argument)* COMMA?
;
argument
: test (comp_for | ASSIGN test)?
| (POWER | STAR) test
;
// TODO: maybe inline?
subscriptlist
: subscript (COMMA subscript)* COMMA?
;
subscript
: ELLIPSIS
| test (COLON test? sliceop?)?
| COLON test? sliceop?
;
// TODO: maybe inline?
sliceop
: COLON test?
;
comp_for
: FOR exprlist IN logical_test comp_iter?
;
comp_iter
: comp_for
| IF test comp_iter?
;