1# PEG grammar for Python
2
3@trailer '''
4void *
5_PyPegen_parse(Parser *p)
6{
7    // Initialize keywords
8    p->keywords = reserved_keywords;
9    p->n_keyword_lists = n_keyword_lists;
10    p->soft_keywords = soft_keywords;
11
12    // Run parser
13    void *result = NULL;
14    if (p->start_rule == Py_file_input) {
15        result = file_rule(p);
16    } else if (p->start_rule == Py_single_input) {
17        result = interactive_rule(p);
18    } else if (p->start_rule == Py_eval_input) {
19        result = eval_rule(p);
20    } else if (p->start_rule == Py_func_type_input) {
21        result = func_type_rule(p);
22    } else if (p->start_rule == Py_fstring_input) {
23        result = fstring_rule(p);
24    }
25
26    return result;
27}
28
29// The end
30'''
31file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
32interactive[mod_ty]: a=statement_newline { _PyAST_Interactive(a, p->arena) }
33eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _PyAST_Expression(a, p->arena) }
34func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _PyAST_FunctionType(a, b, p->arena) }
35fstring[expr_ty]: star_expressions
36
37# type_expressions allow */** but ignore them
38type_expressions[asdl_expr_seq*]:
39    | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
40        (asdl_expr_seq*)_PyPegen_seq_append_to_end(
41            p,
42            CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
43            c) }
44    | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
45    | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
46    | '*' a=expression ',' '**' b=expression {
47        (asdl_expr_seq*)_PyPegen_seq_append_to_end(
48            p,
49            CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
50            b) }
51    | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
52    | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
53    | a[asdl_expr_seq*]=','.expression+ {a}
54
55statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
56statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a }
57statement_newline[asdl_stmt_seq*]:
58    | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
59    | simple_stmts
60    | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
61    | ENDMARKER { _PyPegen_interactive_exit(p) }
62simple_stmts[asdl_stmt_seq*]:
63    | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
64    | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a }
65# NOTE: assignment MUST precede expression, else parsing a simple assignment
66# will throw a SyntaxError.
67simple_stmt[stmt_ty] (memo):
68    | assignment
69    | e=star_expressions { _PyAST_Expr(e, EXTRA) }
70    | &'return' return_stmt
71    | &('import' | 'from') import_stmt
72    | &'raise' raise_stmt
73    | 'pass' { _PyAST_Pass(EXTRA) }
74    | &'del' del_stmt
75    | &'yield' yield_stmt
76    | &'assert' assert_stmt
77    | 'break' { _PyAST_Break(EXTRA) }
78    | 'continue' { _PyAST_Continue(EXTRA) }
79    | &'global' global_stmt
80    | &'nonlocal' nonlocal_stmt
81compound_stmt[stmt_ty]:
82    | &('def' | '@' | ASYNC) function_def
83    | &'if' if_stmt
84    | &('class' | '@') class_def
85    | &('with' | ASYNC) with_stmt
86    | &('for' | ASYNC) for_stmt
87    | &'try' try_stmt
88    | &'while' while_stmt
89    | match_stmt
90
91# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
92assignment[stmt_ty]:
93    | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
94        CHECK_VERSION(
95            stmt_ty,
96            6,
97            "Variable annotation syntax is",
98            _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
99        ) }
100    | a=('(' b=single_target ')' { b }
101         | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
102        CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) }
103    | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
104         _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
105    | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
106         _PyAST_AugAssign(a, b->kind, c, EXTRA) }
107    | invalid_assignment
108
109augassign[AugOperator*]:
110    | '+=' { _PyPegen_augoperator(p, Add) }
111    | '-=' { _PyPegen_augoperator(p, Sub) }
112    | '*=' { _PyPegen_augoperator(p, Mult) }
113    | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
114    | '/=' { _PyPegen_augoperator(p, Div) }
115    | '%=' { _PyPegen_augoperator(p, Mod) }
116    | '&=' { _PyPegen_augoperator(p, BitAnd) }
117    | '|=' { _PyPegen_augoperator(p, BitOr) }
118    | '^=' { _PyPegen_augoperator(p, BitXor) }
119    | '<<=' { _PyPegen_augoperator(p, LShift) }
120    | '>>=' { _PyPegen_augoperator(p, RShift) }
121    | '**=' { _PyPegen_augoperator(p, Pow) }
122    | '//=' { _PyPegen_augoperator(p, FloorDiv) }
123
124global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
125    _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
126nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
127    _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
128
129yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
130
131assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
132
133del_stmt[stmt_ty]:
134    | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) }
135    | invalid_del_stmt
136
137import_stmt[stmt_ty]: import_name | import_from
138import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
139# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
140import_from[stmt_ty]:
141    | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
142        _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
143    | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
144        _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
145import_from_targets[asdl_alias_seq*]:
146    | '(' a=import_from_as_names [','] ')' { a }
147    | import_from_as_names !','
148    | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) }
149    | invalid_import_from_targets
150import_from_as_names[asdl_alias_seq*]:
151    | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
152import_from_as_name[alias_ty]:
153    | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
154                                               (b) ? ((expr_ty) b)->v.Name.id : NULL,
155                                               EXTRA) }
156dotted_as_names[asdl_alias_seq*]:
157    | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
158dotted_as_name[alias_ty]:
159    | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
160                                                      (b) ? ((expr_ty) b)->v.Name.id : NULL,
161                                                      EXTRA) }
162dotted_name[expr_ty]:
163    | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
164    | NAME
165
166if_stmt[stmt_ty]:
167    | invalid_if_stmt
168    | 'if' a=named_expression ':' b=block c=elif_stmt {
169        _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
170    | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
171elif_stmt[stmt_ty]:
172    | invalid_elif_stmt
173    | 'elif' a=named_expression ':' b=block c=elif_stmt {
174        _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
175    | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
176else_block[asdl_stmt_seq*]:
177    | invalid_else_stmt
178    | 'else' &&':' b=block { b }
179
180while_stmt[stmt_ty]:
181    | invalid_while_stmt
182    | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
183
184for_stmt[stmt_ty]:
185    | invalid_for_stmt
186    | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
187        _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
188    | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
189        CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
190    | invalid_for_target
191
192with_stmt[stmt_ty]:
193    | invalid_with_stmt_indent
194    | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
195        _PyAST_With(a, b, NULL, EXTRA) }
196    | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
197        _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
198    | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
199       CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
200    | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
201       CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
202    | invalid_with_stmt
203
204with_item[withitem_ty]:
205    | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) }
206    | invalid_with_item
207    | e=expression { _PyAST_withitem(e, NULL, p->arena) }
208
209try_stmt[stmt_ty]:
210    | invalid_try_stmt
211    | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
212    | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
213except_block[excepthandler_ty]:
214    | invalid_except_stmt_indent
215    | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
216        _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
217    | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
218    | invalid_except_stmt
219finally_block[asdl_stmt_seq*]:
220    | invalid_finally_stmt
221    | 'finally' &&':' a=block { a }
222
223match_stmt[stmt_ty]:
224    | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
225        CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) }
226    | invalid_match_stmt
227subject_expr[expr_ty]:
228    | value=star_named_expression ',' values=star_named_expressions? {
229        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
230    | named_expression
231case_block[match_case_ty]:
232    | invalid_case_block
233    | "case" pattern=patterns guard=guard? ':' body=block {
234        _PyAST_match_case(pattern, guard, body, p->arena) }
235guard[expr_ty]: 'if' guard=named_expression { guard }
236
237patterns[pattern_ty]:
238    | patterns[asdl_pattern_seq*]=open_sequence_pattern {
239        _PyAST_MatchSequence(patterns, EXTRA) }
240    | pattern
241pattern[pattern_ty]:
242    | as_pattern
243    | or_pattern
244as_pattern[pattern_ty]:
245    | pattern=or_pattern 'as' target=pattern_capture_target {
246        _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
247    | invalid_as_pattern
248or_pattern[pattern_ty]:
249    | patterns[asdl_pattern_seq*]='|'.closed_pattern+ {
250        asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) }
251closed_pattern[pattern_ty]:
252    | literal_pattern
253    | capture_pattern
254    | wildcard_pattern
255    | value_pattern
256    | group_pattern
257    | sequence_pattern
258    | mapping_pattern
259    | class_pattern
260
261# Literal patterns are used for equality and identity constraints
262literal_pattern[pattern_ty]:
263    | value=signed_number !('+' | '-') { _PyAST_MatchValue(value, EXTRA) }
264    | value=complex_number { _PyAST_MatchValue(value, EXTRA) }
265    | value=strings { _PyAST_MatchValue(value, EXTRA) }
266    | 'None' { _PyAST_MatchSingleton(Py_None, EXTRA) }
267    | 'True' { _PyAST_MatchSingleton(Py_True, EXTRA) }
268    | 'False' { _PyAST_MatchSingleton(Py_False, EXTRA) }
269
270# Literal expressions are used to restrict permitted mapping pattern keys
271literal_expr[expr_ty]:
272    | signed_number !('+' | '-')
273    | complex_number
274    | strings
275    | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
276    | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
277    | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
278
279complex_number[expr_ty]:
280    | real=signed_real_number '+' imag=imaginary_number {
281        _PyAST_BinOp(real, Add, imag, EXTRA) }
282    | real=signed_real_number '-' imag=imaginary_number  {
283        _PyAST_BinOp(real, Sub, imag, EXTRA) }
284
285signed_number[expr_ty]:
286    | NUMBER
287    | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }
288
289signed_real_number[expr_ty]:
290    | real_number
291    | '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }
292
293real_number[expr_ty]:
294    | real=NUMBER { _PyPegen_ensure_real(p, real) }
295
296imaginary_number[expr_ty]:
297    | imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
298
299capture_pattern[pattern_ty]:
300    | target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }
301
302pattern_capture_target[expr_ty]:
303    | !"_" name=NAME !('.' | '(' | '=') {
304        _PyPegen_set_expr_context(p, name, Store) }
305
306wildcard_pattern[pattern_ty]:
307    | "_" { _PyAST_MatchAs(NULL, NULL, EXTRA) }
308
309value_pattern[pattern_ty]:
310    | attr=attr !('.' | '(' | '=') { _PyAST_MatchValue(attr, EXTRA) }
311attr[expr_ty]:
312    | value=name_or_attr '.' attr=NAME {
313        _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) }
314name_or_attr[expr_ty]:
315    | attr
316    | NAME
317
318group_pattern[pattern_ty]:
319    | '(' pattern=pattern ')' { pattern }
320
321sequence_pattern[pattern_ty]:
322    | '[' patterns=maybe_sequence_pattern? ']' { _PyAST_MatchSequence(patterns, EXTRA) }
323    | '(' patterns=open_sequence_pattern? ')' { _PyAST_MatchSequence(patterns, EXTRA) }
324open_sequence_pattern[asdl_seq*]:
325    | pattern=maybe_star_pattern ',' patterns=maybe_sequence_pattern? {
326        _PyPegen_seq_insert_in_front(p, pattern, patterns) }
327maybe_sequence_pattern[asdl_seq*]:
328    | patterns=','.maybe_star_pattern+ ','? { patterns }
329maybe_star_pattern[pattern_ty]:
330    | star_pattern
331    | pattern
332star_pattern[pattern_ty]:
333    | '*' target=pattern_capture_target {
334        _PyAST_MatchStar(target->v.Name.id, EXTRA) }
335    | '*' wildcard_pattern {
336        _PyAST_MatchStar(NULL, EXTRA) }
337
338mapping_pattern[pattern_ty]:
339    | '{' '}' {
340        _PyAST_MatchMapping(NULL, NULL, NULL, EXTRA) }
341    | '{' rest=double_star_pattern ','? '}' {
342        _PyAST_MatchMapping(NULL, NULL, rest->v.Name.id, EXTRA) }
343    | '{' items=items_pattern ',' rest=double_star_pattern ','? '}' {
344        _PyAST_MatchMapping(
345            CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)),
346            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
347            rest->v.Name.id,
348            EXTRA) }
349    | '{' items=items_pattern ','? '}' {
350        _PyAST_MatchMapping(
351            CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)),
352            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
353            NULL,
354            EXTRA) }
355items_pattern[asdl_seq*]:
356    | ','.key_value_pattern+
357key_value_pattern[KeyPatternPair*]:
358    | key=(literal_expr | attr) ':' pattern=pattern {
359        _PyPegen_key_pattern_pair(p, key, pattern) }
360double_star_pattern[expr_ty]:
361    | '**' target=pattern_capture_target { target }
362
363class_pattern[pattern_ty]:
364    | cls=name_or_attr '(' ')' {
365        _PyAST_MatchClass(cls, NULL, NULL, NULL, EXTRA) }
366    | cls=name_or_attr '(' patterns=positional_patterns ','? ')' {
367        _PyAST_MatchClass(cls, patterns, NULL, NULL, EXTRA) }
368    | cls=name_or_attr '(' keywords=keyword_patterns ','? ')' {
369        _PyAST_MatchClass(
370            cls, NULL,
371            CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p,
372                CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
373            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
374            EXTRA) }
375    | cls=name_or_attr '(' patterns=positional_patterns ',' keywords=keyword_patterns ','? ')' {
376        _PyAST_MatchClass(
377            cls,
378            patterns,
379            CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p,
380                CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
381            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
382            EXTRA) }
383    | invalid_class_pattern
384positional_patterns[asdl_pattern_seq*]:
385    | args[asdl_pattern_seq*]=','.pattern+ { args }
386keyword_patterns[asdl_seq*]:
387    | ','.keyword_pattern+
388keyword_pattern[KeyPatternPair*]:
389    | arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
390
391return_stmt[stmt_ty]:
392    | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
393
394raise_stmt[stmt_ty]:
395    | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
396    | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
397
398function_def[stmt_ty]:
399    | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
400    | function_def_raw
401
402function_def_raw[stmt_ty]:
403    | invalid_def_raw
404    | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
405        _PyAST_FunctionDef(n->v.Name.id,
406                        (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
407                        b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
408    | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
409        CHECK_VERSION(
410            stmt_ty,
411            5,
412            "Async functions are",
413            _PyAST_AsyncFunctionDef(n->v.Name.id,
414                            (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
415                            b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
416        ) }
417func_type_comment[Token*]:
418    | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t }  # Must be followed by indented block
419    | invalid_double_type_comments
420    | TYPE_COMMENT
421
422params[arguments_ty]:
423    | invalid_parameters
424    | parameters
425
426parameters[arguments_ty]:
427    | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] {
428        _PyPegen_make_arguments(p, a, NULL, b, c, d) }
429    | a=slash_with_default b=param_with_default* c=[star_etc] {
430        _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
431    | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
432        _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
433    | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
434    | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
435
436# Some duplication here because we can't write (',' | &')'),
437# which is because we don't support empty alternatives (yet).
438#
439slash_no_default[asdl_arg_seq*]:
440    | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
441    | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
442slash_with_default[SlashWithDefault*]:
443    | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
444    | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
445
446star_etc[StarEtc*]:
447    | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
448        _PyPegen_star_etc(p, a, b, c) }
449    | '*' ',' b=param_maybe_default+ c=[kwds] {
450        _PyPegen_star_etc(p, NULL, b, c) }
451    | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
452    | invalid_star_etc
453
454kwds[arg_ty]: '**' a=param_no_default { a }
455
456# One parameter.  This *includes* a following comma and type comment.
457#
458# There are three styles:
459# - No default
460# - With default
461# - Maybe with default
462#
463# There are two alternative forms of each, to deal with type comments:
464# - Ends in a comma followed by an optional type comment
465# - No comma, optional type comment, must be followed by close paren
466# The latter form is for a final parameter without trailing comma.
467#
468param_no_default[arg_ty]:
469    | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
470    | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
471param_with_default[NameDefaultPair*]:
472    | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
473    | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
474param_maybe_default[NameDefaultPair*]:
475    | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
476    | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
477param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
478
479annotation[expr_ty]: ':' a=expression { a }
480default[expr_ty]: '=' a=expression { a }
481
482decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
483
484class_def[stmt_ty]:
485    | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
486    | class_def_raw
487class_def_raw[stmt_ty]:
488    | invalid_class_def_raw
489    | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
490        _PyAST_ClassDef(a->v.Name.id,
491                     (b) ? ((expr_ty) b)->v.Call.args : NULL,
492                     (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
493                     c, NULL, EXTRA) }
494
495block[asdl_stmt_seq*] (memo):
496    | NEWLINE INDENT a=statements DEDENT { a }
497    | simple_stmts
498    | invalid_block
499
500star_expressions[expr_ty]:
501    | a=star_expression b=(',' c=star_expression { c })+ [','] {
502        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
503    | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
504    | star_expression
505star_expression[expr_ty] (memo):
506    | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
507    | expression
508
509star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
510star_named_expression[expr_ty]:
511    | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
512    | named_expression
513
514
515assignment_expression[expr_ty]:
516    | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
517
518named_expression[expr_ty]:
519    | assignment_expression
520    | invalid_named_expression
521    | expression !':='
522
523annotated_rhs[expr_ty]: yield_expr | star_expressions
524
525expressions[expr_ty]:
526    | a=expression b=(',' c=expression { c })+ [','] {
527        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
528    | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
529    | expression
530expression[expr_ty] (memo):
531    | invalid_expression
532    | invalid_legacy_expression
533    | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
534    | disjunction
535    | lambdef
536
537lambdef[expr_ty]:
538    | 'lambda' a=[lambda_params] ':' b=expression {
539        _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
540
541lambda_params[arguments_ty]:
542    | invalid_lambda_parameters
543    | lambda_parameters
544
545# lambda_parameters etc. duplicates parameters but without annotations
546# or type comments, and if there's no comma after a parameter, we expect
547# a colon, not a close parenthesis.  (For more, see parameters above.)
548#
549lambda_parameters[arguments_ty]:
550    | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] {
551        _PyPegen_make_arguments(p, a, NULL, b, c, d) }
552    | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
553        _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
554    | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
555        _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
556    | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
557    | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
558
559lambda_slash_no_default[asdl_arg_seq*]:
560    | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
561    | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
562lambda_slash_with_default[SlashWithDefault*]:
563    | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
564    | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
565
566lambda_star_etc[StarEtc*]:
567    | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
568        _PyPegen_star_etc(p, a, b, c) }
569    | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
570        _PyPegen_star_etc(p, NULL, b, c) }
571    | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
572    | invalid_lambda_star_etc
573
574lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
575
576lambda_param_no_default[arg_ty]:
577    | a=lambda_param ',' { a }
578    | a=lambda_param &':' { a }
579lambda_param_with_default[NameDefaultPair*]:
580    | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
581    | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
582lambda_param_maybe_default[NameDefaultPair*]:
583    | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
584    | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
585lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
586
587disjunction[expr_ty] (memo):
588    | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
589        Or,
590        CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
591        EXTRA) }
592    | conjunction
593conjunction[expr_ty] (memo):
594    | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp(
595        And,
596        CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
597        EXTRA) }
598    | inversion
599inversion[expr_ty] (memo):
600    | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
601    | comparison
602comparison[expr_ty]:
603    | a=bitwise_or b=compare_op_bitwise_or_pair+ {
604        _PyAST_Compare(
605            a,
606            CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
607            CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
608            EXTRA) }
609    | bitwise_or
610compare_op_bitwise_or_pair[CmpopExprPair*]:
611    | eq_bitwise_or
612    | noteq_bitwise_or
613    | lte_bitwise_or
614    | lt_bitwise_or
615    | gte_bitwise_or
616    | gt_bitwise_or
617    | notin_bitwise_or
618    | in_bitwise_or
619    | isnot_bitwise_or
620    | is_bitwise_or
621eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
622noteq_bitwise_or[CmpopExprPair*]:
623    | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
624lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
625lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
626gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
627gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
628notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
629in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
630isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
631is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
632
633bitwise_or[expr_ty]:
634    | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) }
635    | bitwise_xor
636bitwise_xor[expr_ty]:
637    | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) }
638    | bitwise_and
639bitwise_and[expr_ty]:
640    | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
641    | shift_expr
642shift_expr[expr_ty]:
643    | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
644    | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
645    | sum
646
647sum[expr_ty]:
648    | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
649    | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
650    | term
651term[expr_ty]:
652    | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) }
653    | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) }
654    | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
655    | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
656    | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
657    | factor
658factor[expr_ty] (memo):
659    | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
660    | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
661    | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
662    | power
663power[expr_ty]:
664    | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
665    | await_primary
666await_primary[expr_ty] (memo):
667    | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
668    | primary
669primary[expr_ty]:
670    | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
671    | a=primary b=genexp { _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
672    | a=primary '(' b=[arguments] ')' {
673        _PyAST_Call(a,
674                 (b) ? ((expr_ty) b)->v.Call.args : NULL,
675                 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
676                 EXTRA) }
677    | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) }
678    | atom
679
680slices[expr_ty]:
681    | a=slice !',' { a }
682    | a[asdl_expr_seq*]=','.slice+ [','] { _PyAST_Tuple(a, Load, EXTRA) }
683slice[expr_ty]:
684    | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) }
685    | a=named_expression { a }
686atom[expr_ty]:
687    | NAME
688    | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
689    | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
690    | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
691    | &STRING strings
692    | NUMBER
693    | &'(' (tuple | group | genexp)
694    | &'[' (list | listcomp)
695    | &'{' (dict | set | dictcomp | setcomp)
696    | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
697
698strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
699list[expr_ty]:
700    | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
701listcomp[expr_ty]:
702    | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) }
703    | invalid_comprehension
704tuple[expr_ty]:
705    | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
706        _PyAST_Tuple(a, Load, EXTRA) }
707group[expr_ty]:
708    | '(' a=(yield_expr | named_expression) ')' { a }
709    | invalid_group
710genexp[expr_ty]:
711    | '(' a=( assignment_expression | expression !':=') b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) }
712    | invalid_comprehension
713set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
714setcomp[expr_ty]:
715    | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) }
716    | invalid_comprehension
717dict[expr_ty]:
718    | '{' a=[double_starred_kvpairs] '}' {
719        _PyAST_Dict(
720            CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
721            CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
722            EXTRA) }
723    | '{' invalid_double_starred_kvpairs '}'
724
725dictcomp[expr_ty]:
726    | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
727    | invalid_dict_comprehension
728double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
729double_starred_kvpair[KeyValuePair*]:
730    | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
731    | kvpair
732kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
733for_if_clauses[asdl_comprehension_seq*]:
734    | a[asdl_comprehension_seq*]=for_if_clause+ { a }
735for_if_clause[comprehension_ty]:
736    | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
737        CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
738    | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
739        _PyAST_comprehension(a, b, c, 0, p->arena) }
740    | invalid_for_target
741
742yield_expr[expr_ty]:
743    | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
744    | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
745
746arguments[expr_ty] (memo):
747    | a=args [','] &')' { a }
748    | invalid_arguments
749args[expr_ty]:
750    | a[asdl_expr_seq*]=','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ b=[',' k=kwargs {k}] {
751        _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
752    | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
753                          CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
754                          CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
755                          EXTRA) }
756
757kwargs[asdl_seq*]:
758    | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
759    | ','.kwarg_or_starred+
760    | ','.kwarg_or_double_starred+
761starred_expression[expr_ty]:
762    | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
763kwarg_or_starred[KeywordOrStarred*]:
764    | invalid_kwarg
765    | a=NAME '=' b=expression {
766        _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
767    | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
768kwarg_or_double_starred[KeywordOrStarred*]:
769    | invalid_kwarg
770    | a=NAME '=' b=expression {
771        _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
772    | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) }
773
774# NOTE: star_targets may contain *bitwise_or, targets may not.
775star_targets[expr_ty]:
776    | a=star_target !',' { a }
777    | a=star_target b=(',' c=star_target { c })* [','] {
778        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
779star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
780star_targets_tuple_seq[asdl_expr_seq*]:
781    | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
782    | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
783star_target[expr_ty] (memo):
784    | '*' a=(!'*' star_target) {
785        _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
786    | target_with_star_atom
787target_with_star_atom[expr_ty] (memo):
788    | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
789    | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
790    | star_atom
791star_atom[expr_ty]:
792    | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
793    | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
794    | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) }
795    | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) }
796
797single_target[expr_ty]:
798    | single_subscript_attribute_target
799    | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
800    | '(' a=single_target ')' { a }
801single_subscript_attribute_target[expr_ty]:
802    | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
803    | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
804
805del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
806del_target[expr_ty] (memo):
807    | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) }
808    | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) }
809    | del_t_atom
810del_t_atom[expr_ty]:
811    | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
812    | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
813    | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) }
814    | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) }
815
816t_primary[expr_ty]:
817    | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
818    | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) }
819    | a=t_primary b=genexp &t_lookahead {
820        _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
821    | a=t_primary '(' b=[arguments] ')' &t_lookahead {
822        _PyAST_Call(a,
823                 (b) ? ((expr_ty) b)->v.Call.args : NULL,
824                 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
825                 EXTRA) }
826    | a=atom &t_lookahead { a }
827t_lookahead: '(' | '[' | '.'
828
829# From here on, there are rules for invalid syntax with specialised error messages
830invalid_arguments:
831    | a=args ',' '*' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable argument unpacking follows keyword argument unpacking") }
832    | a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
833        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, comprehension_ty)->target, "Generator expression must be parenthesized") }
834    | a=NAME b='=' expression for_if_clauses {
835        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
836    | a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) }
837    | args ',' a=expression b=for_if_clauses {
838        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
839    | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
840invalid_kwarg:
841    | a[Token*]=('True'|'False'|'None') b='=' {
842        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to %s", PyBytes_AS_STRING(a->bytes)) }
843    | a=NAME b='=' expression for_if_clauses {
844        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
845    | !(NAME '=') a=expression b='=' {
846        RAISE_SYNTAX_ERROR_KNOWN_RANGE(
847            a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
848
849expression_without_invalid[expr_ty]:
850    | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
851    | disjunction
852    | lambdef
853invalid_legacy_expression:
854    | a=NAME !'(' b=star_expressions {
855        _PyPegen_check_legacy_stmt(p, a) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
856            "Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}
857
858invalid_expression:
859    # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
860    # Soft keywords need to also be ignored because they can be parsed as NAME NAME
861   | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
862        _PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
863        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
864   | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
865
866invalid_named_expression:
867    | a=expression ':=' expression {
868        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
869            a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
870    | a=NAME '=' b=bitwise_or !('='|':=') {
871        p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
872    | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {
873        p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
874                                          _PyPegen_get_expr_name(a)) }
875
876invalid_assignment:
877    | a=invalid_ann_assign_target ':' expression {
878        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
879            a,
880            "only single target (not %s) can be annotated",
881            _PyPegen_get_expr_name(a)
882        )}
883    | a=star_named_expression ',' star_named_expressions* ':' expression {
884        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
885    | a=expression ':' expression {
886        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
887    | (star_targets '=')* a=star_expressions '=' {
888        RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
889    | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
890    | a=star_expressions augassign (yield_expr | star_expressions) {
891        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
892            a,
893            "'%s' is an illegal expression for augmented assignment",
894            _PyPegen_get_expr_name(a)
895        )}
896invalid_ann_assign_target[expr_ty]:
897    | list
898    | tuple
899    | '(' a=invalid_ann_assign_target ')' { a }
900invalid_del_stmt:
901    | 'del' a=star_expressions {
902        RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
903invalid_block:
904    | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
905invalid_comprehension:
906    | ('[' | '(' | '{') a=starred_expression for_if_clauses {
907        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
908    | ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses {
909        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, expr_ty),
910        "did you forget parentheses around the comprehension target?") }
911    | ('[' | '{') a=star_named_expression b=',' for_if_clauses {
912        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget parentheses around the comprehension target?") }
913invalid_dict_comprehension:
914    | '{' a='**' bitwise_or for_if_clauses '}' {
915        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
916invalid_parameters:
917    | param_no_default* invalid_parameters_helper a=param_no_default {
918        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
919invalid_parameters_helper: # This is only there to avoid type errors
920    | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
921    | param_with_default+
922invalid_lambda_parameters:
923    | lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default {
924        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
925invalid_lambda_parameters_helper:
926    | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
927    | lambda_param_with_default+
928invalid_star_etc:
929    | a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named arguments must follow bare *") }
930    | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
931invalid_lambda_star_etc:
932    | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
933invalid_double_type_comments:
934    | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
935        RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
936invalid_with_item:
937    | expression 'as' a=expression &(',' | ')' | ':') {
938        RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
939
940invalid_for_target:
941    | ASYNC? 'for' a=star_expressions {
942        RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
943
944invalid_group:
945    | '(' a=starred_expression ')' {
946        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
947    | '(' a='**' expression ')' {
948        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
949invalid_import_from_targets:
950    | import_from_as_names ',' NEWLINE {
951        RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
952
953invalid_with_stmt:
954    | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
955    | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
956invalid_with_stmt_indent:
957    | [ASYNC] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT {
958        RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
959    | [ASYNC] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT {
960        RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
961
962invalid_try_stmt:
963    | a='try' ':' NEWLINE !INDENT {
964        RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
965    | 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
966invalid_except_stmt:
967    | 'except' a=expression ',' expressions ['as' NAME ] ':' {
968        RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
969    | a='except' expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
970    | a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
971invalid_finally_stmt:
972    | a='finally' ':' NEWLINE !INDENT {
973        RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) }
974invalid_except_stmt_indent:
975    | a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
976        RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
977    | a='except' ':' NEWLINE !INDENT { RAISE_SYNTAX_ERROR("expected an indented block after except statement on line %d", a->lineno) }
978invalid_match_stmt:
979    | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
980    | a="match" subject=subject_expr ':' NEWLINE !INDENT {
981        RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
982invalid_case_block:
983    | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }
984    | a="case" patterns guard? ':' NEWLINE !INDENT {
985        RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
986invalid_as_pattern:
987    | or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
988    | or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
989invalid_class_pattern:
990    | name_or_attr '(' a=invalid_class_argument_pattern  { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
991        PyPegen_first_item(a, pattern_ty),
992        PyPegen_last_item(a, pattern_ty),
993        "positional patterns follow keyword patterns") }
994invalid_class_argument_pattern[asdl_pattern_seq*]:
995    | [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }
996invalid_if_stmt:
997    | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
998    | a='if' a=named_expression ':' NEWLINE !INDENT {
999        RAISE_INDENTATION_ERROR("expected an indented block after 'if' statement on line %d", a->lineno) }
1000invalid_elif_stmt:
1001    | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1002    | a='elif' named_expression ':' NEWLINE !INDENT {
1003        RAISE_INDENTATION_ERROR("expected an indented block after 'elif' statement on line %d", a->lineno) }
1004invalid_else_stmt:
1005    | a='else' ':' NEWLINE !INDENT {
1006        RAISE_INDENTATION_ERROR("expected an indented block after 'else' statement on line %d", a->lineno) }
1007invalid_while_stmt:
1008    | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1009    | a='while' named_expression ':' NEWLINE !INDENT {
1010        RAISE_INDENTATION_ERROR("expected an indented block after 'while' statement on line %d", a->lineno) }
1011invalid_for_stmt:
1012    | [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {
1013        RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) }
1014invalid_def_raw:
1015    | [ASYNC] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
1016        RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) }
1017invalid_class_def_raw:
1018    | a='class' NAME ['('[arguments] ')'] ':' NEWLINE !INDENT {
1019        RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) }
1020
1021invalid_double_starred_kvpairs:
1022    | ','.double_starred_kvpair+ ',' invalid_kvpair
1023    | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
1024    | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1025invalid_kvpair:
1026    | a=expression !(':') {
1027        RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
1028    | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
1029    | expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1030