-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-python.ebnf
563 lines (414 loc) · 11.8 KB
/
tree-sitter-python.ebnf
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
//
// From tree-sitter-python/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
module ::=
_statement*
_statement ::=
_simple_statements
| _compound_statement
_simple_statements ::=
_simple_statement ( ';' _simple_statement )* ';'? _newline
_simple_statement ::=
future_import_statement
| import_statement
| import_from_statement
| print_statement
| assert_statement
| expression_statement
| return_statement
| delete_statement
| raise_statement
| pass_statement
| break_statement
| continue_statement
| global_statement
| nonlocal_statement
| exec_statement
| type_alias_statement
import_statement ::=
'import' _import_list
import_prefix ::=
'.'+
relative_import ::=
import_prefix dotted_name?
future_import_statement ::=
'from' '__future__' 'import' ( _import_list | '(' _import_list ')' )
import_from_statement ::=
'from' ( relative_import | dotted_name ) 'import' ( wildcard_import | _import_list | '(' _import_list ')' )
_import_list ::=
( dotted_name | aliased_import ) ( ',' ( dotted_name | aliased_import ) )* ','?
aliased_import ::=
dotted_name 'as' identifier
wildcard_import ::=
'*'
print_statement ::=
'print' chevron ( ',' expression )* ','?
| 'print' expression ( ',' expression )* ','?
chevron ::=
'>>' expression
assert_statement ::=
'assert' expression ( ',' expression )*
expression_statement ::=
expression
| expression ( ',' expression )* ','?
| assignment
| augmented_assignment
| yield
named_expression ::=
_named_expression_lhs ':=' expression
_named_expression_lhs ::=
identifier
| keyword_identifier
return_statement ::=
'return' _expressions?
delete_statement ::=
'del' _expressions
_expressions ::=
expression
| expression_list
raise_statement ::=
'raise' _expressions? ( 'from' expression )?
pass_statement ::=
'pass'
break_statement ::=
'break'
continue_statement ::=
'continue'
_compound_statement ::=
if_statement
| for_statement
| while_statement
| try_statement
| with_statement
| function_definition
| class_definition
| decorated_definition
| match_statement
if_statement ::=
'if' expression ':' _suite elif_clause* else_clause?
elif_clause ::=
'elif' expression ':' _suite
else_clause ::=
'else' ':' _suite
match_statement ::=
'match' expression ( ',' expression )* ','? ':' _match_block
_match_block ::=
_indent case_clause* _dedent
| _newline
case_clause ::=
'case' case_pattern ( ',' case_pattern )* ','? if_clause? ':' _suite
for_statement ::=
'async'? 'for' _left_hand_side 'in' _expressions ':' _suite else_clause?
while_statement ::=
'while' expression ':' _suite else_clause?
try_statement ::=
'try' ':' _suite ( except_clause+ else_clause? finally_clause? | except_group_clause+ else_clause? finally_clause? | finally_clause )
except_clause ::=
'except' ( expression ( ( 'as' | ',' ) expression )? )? ':' _suite
except_group_clause ::=
'except*' expression ( 'as' expression )? ':' _suite
finally_clause ::=
'finally' ':' _suite
with_statement ::=
'async'? 'with' with_clause ':' _suite
with_clause ::=
with_item ( ',' with_item )* ','?
| '(' with_item ( ',' with_item )* ','? ')'
with_item ::=
expression
function_definition ::=
'async'? 'def' identifier type_parameter? parameters ( '->' type )? ':' _suite
parameters ::=
'(' _parameters? ')'
lambda_parameters ::=
_parameters
list_splat ::=
'*' expression
dictionary_splat ::=
'**' expression
global_statement ::=
'global' identifier ( ',' identifier )*
nonlocal_statement ::=
'nonlocal' identifier ( ',' identifier )*
exec_statement ::=
'exec' ( string | identifier ) ( 'in' expression ( ',' expression )* )?
type_alias_statement ::=
'type' type '=' type
class_definition ::=
'class' identifier type_parameter? argument_list? ':' _suite
type_parameter ::=
'[' type ( ',' type )* ']'
parenthesized_list_splat ::=
'(' ( parenthesized_list_splat | list_splat ) ')'
argument_list ::=
'(' ( ( expression | list_splat | dictionary_splat | parenthesized_list_splat | keyword_argument ) ( ',' ( expression | list_splat | dictionary_splat | parenthesized_list_splat | keyword_argument ) )* )? ','? ')'
decorated_definition ::=
decorator+ ( class_definition | function_definition )
decorator ::=
'@' expression _newline
_suite ::=
_simple_statements
| _indent block
| _newline
block ::=
_statement* _dedent
expression_list ::=
expression ( ',' | ( ',' expression )+ ','? )
dotted_name ::=
identifier ( '.' identifier )*
case_pattern ::=
_as_pattern | keyword_pattern | _simple_pattern
_simple_pattern ::=
class_pattern | splat_pattern | union_pattern | _list_pattern | _tuple_pattern | dict_pattern | string | concatenated_string | true | false | none | '-'? ( integer | float ) | complex_pattern | dotted_name | '_'
_as_pattern ::=
case_pattern 'as' identifier
union_pattern ::=
_simple_pattern '|' _simple_pattern+
_list_pattern ::=
'[' ( case_pattern ( ',' case_pattern )* ','? )? ']'
_tuple_pattern ::=
'(' ( case_pattern ( ',' case_pattern )* ','? )? ')'
dict_pattern ::=
'{' ( ( _key_value_pattern | splat_pattern ) ( ',' ( _key_value_pattern | splat_pattern ) )* ','? )? '}'
_key_value_pattern ::=
_simple_pattern ':' case_pattern
keyword_pattern ::=
identifier '=' _simple_pattern
splat_pattern ::=
( '*' | '**' ) ( identifier | '_' )
class_pattern ::=
dotted_name '(' ( case_pattern ( ',' case_pattern )* ','? )? ')'
complex_pattern ::=
'-'? ( integer | float ) ( '+' | '-' ) ( integer | float )
_parameters ::=
parameter ( ',' parameter )* ','?
_patterns ::=
pattern ( ',' pattern )* ','?
parameter ::=
identifier
| typed_parameter
| default_parameter
| typed_default_parameter
| list_splat_pattern
| tuple_pattern
| keyword_separator
| positional_separator
| dictionary_splat_pattern
pattern ::=
identifier
| keyword_identifier
| subscript
| attribute
| list_splat_pattern
| tuple_pattern
| list_pattern
tuple_pattern ::=
'(' _patterns? ')'
list_pattern ::=
'[' _patterns? ']'
default_parameter ::=
( identifier | tuple_pattern ) '=' expression
typed_default_parameter ::=
identifier ':' type '=' expression
list_splat_pattern ::=
'*' ( identifier | keyword_identifier | subscript | attribute )
dictionary_splat_pattern ::=
'**' ( identifier | keyword_identifier | subscript | attribute )
as_pattern ::=
expression 'as' expression
_expression_within_for_in_clause ::=
expression
| lambda_within_for_in_clause
expression ::=
comparison_operator
| not_operator
| boolean_operator
| lambda
| primary_expression
| conditional_expression
| named_expression
| as_pattern
primary_expression ::=
await
| binary_operator
| identifier
| keyword_identifier
| string
| concatenated_string
| integer
| float
| true
| false
| none
| unary_operator
| attribute
| subscript
| call
| list
| list_comprehension
| dictionary
| dictionary_comprehension
| set
| set_comprehension
| tuple
| parenthesized_expression
| generator_expression
| ellipsis
| list_splat_pattern
not_operator ::=
'not' expression
boolean_operator ::=
expression 'and' expression
| expression 'or' expression
binary_operator ::=
primary_expression '+' primary_expression
| primary_expression '-' primary_expression
| primary_expression '*' primary_expression
| primary_expression '@' primary_expression
| primary_expression '/' primary_expression
| primary_expression '%' primary_expression
| primary_expression '//' primary_expression
| primary_expression '**' primary_expression
| primary_expression '|' primary_expression
| primary_expression '&' primary_expression
| primary_expression '^' primary_expression
| primary_expression '<<' primary_expression
| primary_expression '>>' primary_expression
unary_operator ::=
( '+' | '-' | '~' ) primary_expression
comparison_operator ::=
primary_expression ( ( '<' | '<=' | '==' | '!=' | '>=' | '>' | '<>' | 'in' | ( 'not' 'in' ) | 'is' | ( 'is' 'not' ) ) primary_expression )+
lambda ::=
'lambda' lambda_parameters? ':' expression
lambda_within_for_in_clause ::=
'lambda' lambda_parameters? ':' _expression_within_for_in_clause
assignment ::=
_left_hand_side ( '=' _right_hand_side | ':' type | ':' type '=' _right_hand_side )
augmented_assignment ::=
_left_hand_side ( '+=' | '-=' | '*=' | '/=' | '@=' | '//=' | '%=' | '**=' | '>>=' | '<<=' | '&=' | '^=' | '|=' ) _right_hand_side
_left_hand_side ::=
pattern
| pattern_list
pattern_list ::=
pattern ( ',' | ( ',' pattern )+ ','? )
_right_hand_side ::=
expression
| expression_list
| assignment
| augmented_assignment
| pattern_list
| yield
yield ::=
'yield' ( 'from' expression | _expressions )
attribute ::=
primary_expression '.' identifier
subscript ::=
primary_expression '[' ( expression | slice ) ( ',' ( expression | slice ) )* ','? ']'
slice ::=
expression? ':' expression? ( ':' expression? )?
ellipsis ::=
'...'
call ::=
primary_expression ( generator_expression | argument_list )
typed_parameter ::=
( identifier | list_splat_pattern | dictionary_splat_pattern ) ':' type
type ::=
expression
| splat_type
| generic_type
| union_type
| constrained_type
| member_type
splat_type ::=
( '*' | '**' ) identifier
generic_type ::=
identifier type_parameter
union_type ::=
type '|' type
constrained_type ::=
type ':' type
member_type ::=
type '.' identifier
keyword_argument ::=
( identifier | keyword_identifier ) '=' expression
list ::=
'[' _collection_elements? ']'
set ::=
'{' _collection_elements '}'
tuple ::=
'(' _collection_elements? ')'
dictionary ::=
'{' ( ( pair | dictionary_splat ) ( ',' ( pair | dictionary_splat ) )* )? ','? '}'
pair ::=
expression ':' expression
list_comprehension ::=
'[' expression _comprehension_clauses ']'
dictionary_comprehension ::=
'{' pair _comprehension_clauses '}'
set_comprehension ::=
'{' expression _comprehension_clauses '}'
generator_expression ::=
'(' expression _comprehension_clauses ')'
_comprehension_clauses ::=
for_in_clause ( for_in_clause | if_clause )*
parenthesized_expression ::=
'(' ( expression | yield ) ')'
_collection_elements ::=
( expression | yield | list_splat | parenthesized_list_splat ) ( ',' ( expression | yield | list_splat | parenthesized_list_splat ) )* ','?
for_in_clause ::=
'async'? 'for' _left_hand_side 'in' ( _expression_within_for_in_clause ( ',' _expression_within_for_in_clause )* ) ','?
if_clause ::=
'if' expression
conditional_expression ::=
expression 'if' expression 'else' expression
concatenated_string ::=
string string+
string ::=
string_start ( interpolation | string_content )* string_end
string_content ::=
( escape_interpolation | escape_sequence | _not_escape_sequence | _string_content )+
interpolation ::=
'{' _f_expression '='? type_conversion? format_specifier? '}'
_f_expression ::=
expression
| expression_list
| pattern_list
| yield
escape_sequence ::=
'\\' ( 'u'[a-fA-F0-9]'{4}' | 'U'[a-fA-F0-9]'{8}' | 'x'[a-fA-F0-9]'{2}' | [0-9]'{3}' | '#x0D'?'#x0A' | ['"abfrntv\] | 'N\{'[^}]+'\}' )
_not_escape_sequence ::=
'\\'
format_specifier ::=
':' ( [^{}#x0A]+ | interpolation )*
type_conversion ::=
'!'[a-z]
integer ::=
( ( '0x' | '0X' ) '_'?[A-Fa-f0-9]++ [Ll]? | ( '0o' | '0O' ) '_'?[0-7]++ [Ll]? | ( '0b' | '0B' ) '_'?[0-1]++ [Ll]? | [0-9]+'_'?+ ( [Ll] | [jJ] ) )
float ::=
( ( [0-9]+'_'?+ '.' [0-9]+'_'?+? ( [eE][\+-]? [0-9]+'_'?+ )? | [0-9]+'_'?+? '.' [0-9]+'_'?+ ( [eE][\+-]? [0-9]+'_'?+ )? | [0-9]+'_'?+ [eE][\+-]? [0-9]+'_'?+ ) ( [Ll] | [jJ] )? )
identifier ::=
[_\p{XID_Start}][_\p{XID_Continue}]*
keyword_identifier ::=
( 'print' | 'exec' | 'async' | 'await' | 'match' )
| 'type'
true ::=
'True'
false ::=
'False'
none ::=
'None'
await ::=
'await' primary_expression
comment ::=
( '#' '.'* )
line_continuation ::=
( '\\' ( '\r' ? '\n' | '\0' ) )
positional_separator ::=
'/'
keyword_separator ::=
'*'