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
30# ========================= START OF THE GRAMMAR =========================
31
32# General grammatical elements and rules:
33#
34# * Strings with double quotes (") denote SOFT KEYWORDS
35# * Strings with single quotes (') denote KEYWORDS
36# * Upper case names (NAME) denote tokens in the Grammar/Tokens file
37# * Rule names starting with "invalid_" are used for specialized syntax errors
38#     - These rules are NOT used in the first pass of the parser.
39#     - Only if the first pass fails to parse, a second pass including the invalid
40#       rules will be executed.
41#     - If the parser fails in the second phase with a generic syntax error, the
42#       location of the generic failure of the first pass will be used (this avoids
43#       reporting incorrect locations due to the invalid rules).
44#     - The order of the alternatives involving invalid rules matter
45#       (like any rule in PEG).
46#
47# Grammar Syntax (see PEP 617 for more information):
48#
49# rule_name: expression
50#   Optionally, a type can be included right after the rule name, which
51#   specifies the return type of the C or Python function corresponding to the
52#   rule:
53# rule_name[return_type]: expression
54#   If the return type is omitted, then a void * is returned in C and an Any in
55#   Python.
56# e1 e2
57#   Match e1, then match e2.
58# e1 | e2
59#   Match e1 or e2.
60#   The first alternative can also appear on the line after the rule name for
61#   formatting purposes. In that case, a | must be used before the first
62#   alternative, like so:
63#       rule_name[return_type]:
64#            | first_alt
65#            | second_alt
66# ( e )
67#   Match e (allows also to use other operators in the group like '(e)*')
68# [ e ] or e?
69#   Optionally match e.
70# e*
71#   Match zero or more occurrences of e.
72# e+
73#   Match one or more occurrences of e.
74# s.e+
75#   Match one or more occurrences of e, separated by s. The generated parse tree
76#   does not include the separator. This is otherwise identical to (e (s e)*).
77# &e
78#   Succeed if e can be parsed, without consuming any input.
79# !e
80#   Fail if e can be parsed, without consuming any input.
81# ~
82#   Commit to the current alternative, even if it fails to parse.
83#
84
85# STARTING RULES
86# ==============
87
88file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
89interactive[mod_ty]: a=statement_newline { _PyAST_Interactive(a, p->arena) }
90eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _PyAST_Expression(a, p->arena) }
91func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _PyAST_FunctionType(a, b, p->arena) }
92fstring[expr_ty]: star_expressions
93
94# GENERAL STATEMENTS
95# ==================
96
97statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
98
99statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a }
100
101statement_newline[asdl_stmt_seq*]:
102    | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
103    | simple_stmts
104    | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
105    | ENDMARKER { _PyPegen_interactive_exit(p) }
106
107simple_stmts[asdl_stmt_seq*]:
108    | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
109    | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a }
110
111# NOTE: assignment MUST precede expression, else parsing a simple assignment
112# will throw a SyntaxError.
113simple_stmt[stmt_ty] (memo):
114    | assignment
115    | e=star_expressions { _PyAST_Expr(e, EXTRA) }
116    | &'return' return_stmt
117    | &('import' | 'from') import_stmt
118    | &'raise' raise_stmt
119    | 'pass' { _PyAST_Pass(EXTRA) }
120    | &'del' del_stmt
121    | &'yield' yield_stmt
122    | &'assert' assert_stmt
123    | 'break' { _PyAST_Break(EXTRA) }
124    | 'continue' { _PyAST_Continue(EXTRA) }
125    | &'global' global_stmt
126    | &'nonlocal' nonlocal_stmt
127
128compound_stmt[stmt_ty]:
129    | &('def' | '@' | ASYNC) function_def
130    | &'if' if_stmt
131    | &('class' | '@') class_def
132    | &('with' | ASYNC) with_stmt
133    | &('for' | ASYNC) for_stmt
134    | &'try' try_stmt
135    | &'while' while_stmt
136    | match_stmt
137
138# SIMPLE STATEMENTS
139# =================
140
141# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
142assignment[stmt_ty]:
143    | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
144        CHECK_VERSION(
145            stmt_ty,
146            6,
147            "Variable annotation syntax is",
148            _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
149        ) }
150    | a=('(' b=single_target ')' { b }
151         | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
152        CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) }
153    | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
154         _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
155    | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
156         _PyAST_AugAssign(a, b->kind, c, EXTRA) }
157    | invalid_assignment
158
159annotated_rhs[expr_ty]: yield_expr | star_expressions
160
161augassign[AugOperator*]:
162    | '+=' { _PyPegen_augoperator(p, Add) }
163    | '-=' { _PyPegen_augoperator(p, Sub) }
164    | '*=' { _PyPegen_augoperator(p, Mult) }
165    | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
166    | '/=' { _PyPegen_augoperator(p, Div) }
167    | '%=' { _PyPegen_augoperator(p, Mod) }
168    | '&=' { _PyPegen_augoperator(p, BitAnd) }
169    | '|=' { _PyPegen_augoperator(p, BitOr) }
170    | '^=' { _PyPegen_augoperator(p, BitXor) }
171    | '<<=' { _PyPegen_augoperator(p, LShift) }
172    | '>>=' { _PyPegen_augoperator(p, RShift) }
173    | '**=' { _PyPegen_augoperator(p, Pow) }
174    | '//=' { _PyPegen_augoperator(p, FloorDiv) }
175
176return_stmt[stmt_ty]:
177    | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
178
179raise_stmt[stmt_ty]:
180    | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
181    | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
182
183global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
184    _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
185
186nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
187    _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
188
189del_stmt[stmt_ty]:
190    | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) }
191    | invalid_del_stmt
192
193yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
194
195assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
196
197import_stmt[stmt_ty]: import_name | import_from
198
199# Import statements
200# -----------------
201
202import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
203# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
204import_from[stmt_ty]:
205    | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
206        _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
207    | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
208        _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
209import_from_targets[asdl_alias_seq*]:
210    | '(' a=import_from_as_names [','] ')' { a }
211    | import_from_as_names !','
212    | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) }
213    | invalid_import_from_targets
214import_from_as_names[asdl_alias_seq*]:
215    | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
216import_from_as_name[alias_ty]:
217    | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
218                                               (b) ? ((expr_ty) b)->v.Name.id : NULL,
219                                               EXTRA) }
220dotted_as_names[asdl_alias_seq*]:
221    | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
222dotted_as_name[alias_ty]:
223    | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
224                                                      (b) ? ((expr_ty) b)->v.Name.id : NULL,
225                                                      EXTRA) }
226dotted_name[expr_ty]:
227    | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
228    | NAME
229
230# COMPOUND STATEMENTS
231# ===================
232
233# Common elements
234# ---------------
235
236block[asdl_stmt_seq*] (memo):
237    | NEWLINE INDENT a=statements DEDENT { a }
238    | simple_stmts
239    | invalid_block
240
241decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
242
243# Class definitions
244# -----------------
245
246class_def[stmt_ty]:
247    | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
248    | class_def_raw
249
250class_def_raw[stmt_ty]:
251    | invalid_class_def_raw
252    | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
253        _PyAST_ClassDef(a->v.Name.id,
254                     (b) ? ((expr_ty) b)->v.Call.args : NULL,
255                     (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
256                     c, NULL, EXTRA) }
257
258# Function definitions
259# --------------------
260
261function_def[stmt_ty]:
262    | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
263    | function_def_raw
264
265function_def_raw[stmt_ty]:
266    | invalid_def_raw
267    | 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
268        _PyAST_FunctionDef(n->v.Name.id,
269                        (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
270                        b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
271    | ASYNC 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
272        CHECK_VERSION(
273            stmt_ty,
274            5,
275            "Async functions are",
276            _PyAST_AsyncFunctionDef(n->v.Name.id,
277                            (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
278                            b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
279        ) }
280
281# Function parameters
282# -------------------
283
284params[arguments_ty]:
285    | invalid_parameters
286    | parameters
287
288parameters[arguments_ty]:
289    | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] {
290        CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) }
291    | a=slash_with_default b=param_with_default* c=[star_etc] {
292        CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) }
293    | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
294        _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
295    | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
296    | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
297
298# Some duplication here because we can't write (',' | &')'),
299# which is because we don't support empty alternatives (yet).
300
301slash_no_default[asdl_arg_seq*]:
302    | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
303    | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
304slash_with_default[SlashWithDefault*]:
305    | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
306    | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
307
308star_etc[StarEtc*]:
309    | invalid_star_etc
310    | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
311        _PyPegen_star_etc(p, a, b, c) }
312    | '*' a=param_no_default_star_annotation b=param_maybe_default* c=[kwds] {
313        _PyPegen_star_etc(p, a, b, c) }
314    | '*' ',' b=param_maybe_default+ c=[kwds] {
315        _PyPegen_star_etc(p, NULL, b, c) }
316    | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
317
318kwds[arg_ty]:
319    | invalid_kwds
320    | '**' a=param_no_default { a }
321
322# One parameter.  This *includes* a following comma and type comment.
323#
324# There are three styles:
325# - No default
326# - With default
327# - Maybe with default
328#
329# There are two alternative forms of each, to deal with type comments:
330# - Ends in a comma followed by an optional type comment
331# - No comma, optional type comment, must be followed by close paren
332# The latter form is for a final parameter without trailing comma.
333#
334
335param_no_default[arg_ty]:
336    | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
337    | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
338param_no_default_star_annotation[arg_ty]:
339    | a=param_star_annotation ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
340    | a=param_star_annotation tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
341param_with_default[NameDefaultPair*]:
342    | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
343    | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
344param_maybe_default[NameDefaultPair*]:
345    | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
346    | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
347param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
348param_star_annotation[arg_ty]: a=NAME b=star_annotation { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
349annotation[expr_ty]: ':' a=expression { a }
350star_annotation[expr_ty]: ':' a=star_expression { a }
351default[expr_ty]: '=' a=expression { a } | invalid_default
352
353# If statement
354# ------------
355
356if_stmt[stmt_ty]:
357    | invalid_if_stmt
358    | 'if' a=named_expression ':' b=block c=elif_stmt {
359        _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
360    | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
361elif_stmt[stmt_ty]:
362    | invalid_elif_stmt
363    | 'elif' a=named_expression ':' b=block c=elif_stmt {
364        _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
365    | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
366else_block[asdl_stmt_seq*]:
367    | invalid_else_stmt
368    | 'else' &&':' b=block { b }
369
370# While statement
371# ---------------
372
373while_stmt[stmt_ty]:
374    | invalid_while_stmt
375    | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
376
377# For statement
378# -------------
379
380for_stmt[stmt_ty]:
381    | invalid_for_stmt
382    | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
383        _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
384    | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
385        CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
386    | invalid_for_target
387
388# With statement
389# --------------
390
391with_stmt[stmt_ty]:
392    | invalid_with_stmt_indent
393    | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
394        CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
395    | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
396        _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
397    | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
398       CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
399    | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
400       CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
401    | invalid_with_stmt
402
403with_item[withitem_ty]:
404    | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) }
405    | invalid_with_item
406    | e=expression { _PyAST_withitem(e, NULL, p->arena) }
407
408# Try statement
409# -------------
410
411try_stmt[stmt_ty]:
412    | invalid_try_stmt
413    | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
414    | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
415    | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] {
416        CHECK_VERSION(stmt_ty, 11, "Exception groups are",
417                      _PyAST_TryStar(b, ex, el, f, EXTRA)) }
418
419
420# Except statement
421# ----------------
422
423except_block[excepthandler_ty]:
424    | invalid_except_stmt_indent
425    | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
426        _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
427    | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
428    | invalid_except_stmt
429except_star_block[excepthandler_ty]:
430    | invalid_except_star_stmt_indent
431    | 'except' '*' e=expression t=['as' z=NAME { z }] ':' b=block {
432        _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
433    | invalid_except_stmt
434finally_block[asdl_stmt_seq*]:
435    | invalid_finally_stmt
436    | 'finally' &&':' a=block { a }
437
438# Match statement
439# ---------------
440
441match_stmt[stmt_ty]:
442    | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
443        CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) }
444    | invalid_match_stmt
445
446subject_expr[expr_ty]:
447    | value=star_named_expression ',' values=star_named_expressions? {
448        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
449    | named_expression
450
451case_block[match_case_ty]:
452    | invalid_case_block
453    | "case" pattern=patterns guard=guard? ':' body=block {
454        _PyAST_match_case(pattern, guard, body, p->arena) }
455
456guard[expr_ty]: 'if' guard=named_expression { guard }
457
458patterns[pattern_ty]:
459    | patterns[asdl_pattern_seq*]=open_sequence_pattern {
460        _PyAST_MatchSequence(patterns, EXTRA) }
461    | pattern
462
463pattern[pattern_ty]:
464    | as_pattern
465    | or_pattern
466
467as_pattern[pattern_ty]:
468    | pattern=or_pattern 'as' target=pattern_capture_target {
469        _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
470    | invalid_as_pattern
471
472or_pattern[pattern_ty]:
473    | patterns[asdl_pattern_seq*]='|'.closed_pattern+ {
474        asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) }
475
476closed_pattern[pattern_ty] (memo):
477    | literal_pattern
478    | capture_pattern
479    | wildcard_pattern
480    | value_pattern
481    | group_pattern
482    | sequence_pattern
483    | mapping_pattern
484    | class_pattern
485
486# Literal patterns are used for equality and identity constraints
487literal_pattern[pattern_ty]:
488    | value=signed_number !('+' | '-') { _PyAST_MatchValue(value, EXTRA) }
489    | value=complex_number { _PyAST_MatchValue(value, EXTRA) }
490    | value=strings { _PyAST_MatchValue(value, EXTRA) }
491    | 'None' { _PyAST_MatchSingleton(Py_None, EXTRA) }
492    | 'True' { _PyAST_MatchSingleton(Py_True, EXTRA) }
493    | 'False' { _PyAST_MatchSingleton(Py_False, EXTRA) }
494
495# Literal expressions are used to restrict permitted mapping pattern keys
496literal_expr[expr_ty]:
497    | signed_number !('+' | '-')
498    | complex_number
499    | strings
500    | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
501    | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
502    | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
503
504complex_number[expr_ty]:
505    | real=signed_real_number '+' imag=imaginary_number {
506        _PyAST_BinOp(real, Add, imag, EXTRA) }
507    | real=signed_real_number '-' imag=imaginary_number  {
508        _PyAST_BinOp(real, Sub, imag, EXTRA) }
509
510signed_number[expr_ty]:
511    | NUMBER
512    | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }
513
514signed_real_number[expr_ty]:
515    | real_number
516    | '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }
517
518real_number[expr_ty]:
519    | real=NUMBER { _PyPegen_ensure_real(p, real) }
520
521imaginary_number[expr_ty]:
522    | imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
523
524capture_pattern[pattern_ty]:
525    | target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }
526
527pattern_capture_target[expr_ty]:
528    | !"_" name=NAME !('.' | '(' | '=') {
529        _PyPegen_set_expr_context(p, name, Store) }
530
531wildcard_pattern[pattern_ty]:
532    | "_" { _PyAST_MatchAs(NULL, NULL, EXTRA) }
533
534value_pattern[pattern_ty]:
535    | attr=attr !('.' | '(' | '=') { _PyAST_MatchValue(attr, EXTRA) }
536
537attr[expr_ty]:
538    | value=name_or_attr '.' attr=NAME {
539        _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) }
540
541name_or_attr[expr_ty]:
542    | attr
543    | NAME
544
545group_pattern[pattern_ty]:
546    | '(' pattern=pattern ')' { pattern }
547
548sequence_pattern[pattern_ty]:
549    | '[' patterns=maybe_sequence_pattern? ']' { _PyAST_MatchSequence(patterns, EXTRA) }
550    | '(' patterns=open_sequence_pattern? ')' { _PyAST_MatchSequence(patterns, EXTRA) }
551
552open_sequence_pattern[asdl_seq*]:
553    | pattern=maybe_star_pattern ',' patterns=maybe_sequence_pattern? {
554        _PyPegen_seq_insert_in_front(p, pattern, patterns) }
555
556maybe_sequence_pattern[asdl_seq*]:
557    | patterns=','.maybe_star_pattern+ ','? { patterns }
558
559maybe_star_pattern[pattern_ty]:
560    | star_pattern
561    | pattern
562
563star_pattern[pattern_ty] (memo):
564    | '*' target=pattern_capture_target {
565        _PyAST_MatchStar(target->v.Name.id, EXTRA) }
566    | '*' wildcard_pattern {
567        _PyAST_MatchStar(NULL, EXTRA) }
568
569mapping_pattern[pattern_ty]:
570    | '{' '}' {
571        _PyAST_MatchMapping(NULL, NULL, NULL, EXTRA) }
572    | '{' rest=double_star_pattern ','? '}' {
573        _PyAST_MatchMapping(NULL, NULL, rest->v.Name.id, EXTRA) }
574    | '{' items=items_pattern ',' rest=double_star_pattern ','? '}' {
575        _PyAST_MatchMapping(
576            CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)),
577            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
578            rest->v.Name.id,
579            EXTRA) }
580    | '{' items=items_pattern ','? '}' {
581        _PyAST_MatchMapping(
582            CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)),
583            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
584            NULL,
585            EXTRA) }
586
587items_pattern[asdl_seq*]:
588    | ','.key_value_pattern+
589
590key_value_pattern[KeyPatternPair*]:
591    | key=(literal_expr | attr) ':' pattern=pattern {
592        _PyPegen_key_pattern_pair(p, key, pattern) }
593
594double_star_pattern[expr_ty]:
595    | '**' target=pattern_capture_target { target }
596
597class_pattern[pattern_ty]:
598    | cls=name_or_attr '(' ')' {
599        _PyAST_MatchClass(cls, NULL, NULL, NULL, EXTRA) }
600    | cls=name_or_attr '(' patterns=positional_patterns ','? ')' {
601        _PyAST_MatchClass(cls, patterns, NULL, NULL, EXTRA) }
602    | cls=name_or_attr '(' keywords=keyword_patterns ','? ')' {
603        _PyAST_MatchClass(
604            cls, NULL,
605            CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p,
606                CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
607            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
608            EXTRA) }
609    | cls=name_or_attr '(' patterns=positional_patterns ',' keywords=keyword_patterns ','? ')' {
610        _PyAST_MatchClass(
611            cls,
612            patterns,
613            CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p,
614                CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
615            CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
616            EXTRA) }
617    | invalid_class_pattern
618
619positional_patterns[asdl_pattern_seq*]:
620    | args[asdl_pattern_seq*]=','.pattern+ { args }
621
622keyword_patterns[asdl_seq*]:
623    | ','.keyword_pattern+
624
625keyword_pattern[KeyPatternPair*]:
626    | arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
627
628# EXPRESSIONS
629# -----------
630
631expressions[expr_ty]:
632    | a=expression b=(',' c=expression { c })+ [','] {
633        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
634    | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
635    | expression
636
637expression[expr_ty] (memo):
638    | invalid_expression
639    | invalid_legacy_expression
640    | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
641    | disjunction
642    | lambdef
643
644yield_expr[expr_ty]:
645    | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
646    | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
647
648star_expressions[expr_ty]:
649    | a=star_expression b=(',' c=star_expression { c })+ [','] {
650        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
651    | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
652    | star_expression
653
654star_expression[expr_ty] (memo):
655    | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
656    | expression
657
658star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
659
660star_named_expression[expr_ty]:
661    | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
662    | named_expression
663
664assignment_expression[expr_ty]:
665    | a=NAME ':=' ~ b=expression {
666        CHECK_VERSION(expr_ty, 8, "Assignment expressions are",
667        _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA)) }
668
669named_expression[expr_ty]:
670    | assignment_expression
671    | invalid_named_expression
672    | expression !':='
673
674disjunction[expr_ty] (memo):
675    | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
676        Or,
677        CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
678        EXTRA) }
679    | conjunction
680
681conjunction[expr_ty] (memo):
682    | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp(
683        And,
684        CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
685        EXTRA) }
686    | inversion
687
688inversion[expr_ty] (memo):
689    | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
690    | comparison
691
692# Comparison operators
693# --------------------
694
695comparison[expr_ty]:
696    | a=bitwise_or b=compare_op_bitwise_or_pair+ {
697        _PyAST_Compare(
698            a,
699            CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
700            CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
701            EXTRA) }
702    | bitwise_or
703
704compare_op_bitwise_or_pair[CmpopExprPair*]:
705    | eq_bitwise_or
706    | noteq_bitwise_or
707    | lte_bitwise_or
708    | lt_bitwise_or
709    | gte_bitwise_or
710    | gt_bitwise_or
711    | notin_bitwise_or
712    | in_bitwise_or
713    | isnot_bitwise_or
714    | is_bitwise_or
715
716eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
717noteq_bitwise_or[CmpopExprPair*]:
718    | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
719lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
720lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
721gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
722gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
723notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
724in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
725isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
726is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
727
728# Bitwise operators
729# -----------------
730
731bitwise_or[expr_ty]:
732    | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) }
733    | bitwise_xor
734
735bitwise_xor[expr_ty]:
736    | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) }
737    | bitwise_and
738
739bitwise_and[expr_ty]:
740    | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
741    | shift_expr
742
743shift_expr[expr_ty]:
744    | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
745    | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
746    | sum
747
748# Arithmetic operators
749# --------------------
750
751sum[expr_ty]:
752    | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
753    | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
754    | term
755
756term[expr_ty]:
757    | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) }
758    | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) }
759    | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
760    | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
761    | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
762    | factor
763
764factor[expr_ty] (memo):
765    | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
766    | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
767    | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
768    | power
769
770power[expr_ty]:
771    | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
772    | await_primary
773
774# Primary elements
775# ----------------
776
777# Primary elements are things like "obj.something.something", "obj[something]", "obj(something)", "obj" ...
778
779await_primary[expr_ty] (memo):
780    | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
781    | primary
782
783primary[expr_ty]:
784    | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
785    | a=primary b=genexp { _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
786    | a=primary '(' b=[arguments] ')' {
787        _PyAST_Call(a,
788                 (b) ? ((expr_ty) b)->v.Call.args : NULL,
789                 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
790                 EXTRA) }
791    | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) }
792    | atom
793
794slices[expr_ty]:
795    | a=slice !',' { a }
796    | a[asdl_expr_seq*]=','.(slice | starred_expression)+ [','] { _PyAST_Tuple(a, Load, EXTRA) }
797
798slice[expr_ty]:
799    | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) }
800    | a=named_expression { a }
801
802atom[expr_ty]:
803    | NAME
804    | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
805    | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
806    | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
807    | &STRING strings
808    | NUMBER
809    | &'(' (tuple | group | genexp)
810    | &'[' (list | listcomp)
811    | &'{' (dict | set | dictcomp | setcomp)
812    | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
813
814group[expr_ty]:
815    | '(' a=(yield_expr | named_expression) ')' { a }
816    | invalid_group
817
818# Lambda functions
819# ----------------
820
821lambdef[expr_ty]:
822    | 'lambda' a=[lambda_params] ':' b=expression {
823        _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
824
825lambda_params[arguments_ty]:
826    | invalid_lambda_parameters
827    | lambda_parameters
828
829# lambda_parameters etc. duplicates parameters but without annotations
830# or type comments, and if there's no comma after a parameter, we expect
831# a colon, not a close parenthesis.  (For more, see parameters above.)
832#
833lambda_parameters[arguments_ty]:
834    | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] {
835        CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) }
836    | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
837        CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) }
838    | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
839        _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
840    | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
841    | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
842
843lambda_slash_no_default[asdl_arg_seq*]:
844    | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
845    | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
846
847lambda_slash_with_default[SlashWithDefault*]:
848    | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
849    | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
850
851lambda_star_etc[StarEtc*]:
852    | invalid_lambda_star_etc
853    | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
854        _PyPegen_star_etc(p, a, b, c) }
855    | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
856        _PyPegen_star_etc(p, NULL, b, c) }
857    | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
858
859lambda_kwds[arg_ty]:
860    | invalid_lambda_kwds
861    | '**' a=lambda_param_no_default { a }
862
863lambda_param_no_default[arg_ty]:
864    | a=lambda_param ',' { a }
865    | a=lambda_param &':' { a }
866lambda_param_with_default[NameDefaultPair*]:
867    | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
868    | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
869lambda_param_maybe_default[NameDefaultPair*]:
870    | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
871    | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
872lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
873
874# LITERALS
875# ========
876
877strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
878
879list[expr_ty]:
880    | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
881
882tuple[expr_ty]:
883    | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
884        _PyAST_Tuple(a, Load, EXTRA) }
885
886set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
887
888# Dicts
889# -----
890
891dict[expr_ty]:
892    | '{' a=[double_starred_kvpairs] '}' {
893        _PyAST_Dict(
894            CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
895            CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
896            EXTRA) }
897    | '{' invalid_double_starred_kvpairs '}'
898
899double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
900
901double_starred_kvpair[KeyValuePair*]:
902    | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
903    | kvpair
904
905kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
906
907# Comprehensions & Generators
908# ---------------------------
909
910for_if_clauses[asdl_comprehension_seq*]:
911    | a[asdl_comprehension_seq*]=for_if_clause+ { a }
912
913for_if_clause[comprehension_ty]:
914    | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
915        CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
916    | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
917        _PyAST_comprehension(a, b, c, 0, p->arena) }
918    | invalid_for_target
919
920listcomp[expr_ty]:
921    | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) }
922    | invalid_comprehension
923
924setcomp[expr_ty]:
925    | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) }
926    | invalid_comprehension
927
928genexp[expr_ty]:
929    | '(' a=( assignment_expression | expression !':=') b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) }
930    | invalid_comprehension
931
932dictcomp[expr_ty]:
933    | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
934    | invalid_dict_comprehension
935
936# FUNCTION CALL ARGUMENTS
937# =======================
938
939arguments[expr_ty] (memo):
940    | a=args [','] &')' { a }
941    | invalid_arguments
942
943args[expr_ty]:
944    | a[asdl_expr_seq*]=','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ b=[',' k=kwargs {k}] {
945        _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
946    | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
947                          CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
948                          CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
949                          EXTRA) }
950
951kwargs[asdl_seq*]:
952    | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
953    | ','.kwarg_or_starred+
954    | ','.kwarg_or_double_starred+
955
956starred_expression[expr_ty]:
957    | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
958
959kwarg_or_starred[KeywordOrStarred*]:
960    | invalid_kwarg
961    | a=NAME '=' b=expression {
962        _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
963    | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
964
965kwarg_or_double_starred[KeywordOrStarred*]:
966    | invalid_kwarg
967    | a=NAME '=' b=expression {
968        _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
969    | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) }
970
971# ASSIGNMENT TARGETS
972# ==================
973
974# Generic targets
975# ---------------
976
977# NOTE: star_targets may contain *bitwise_or, targets may not.
978star_targets[expr_ty]:
979    | a=star_target !',' { a }
980    | a=star_target b=(',' c=star_target { c })* [','] {
981        _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
982
983star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
984
985star_targets_tuple_seq[asdl_expr_seq*]:
986    | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
987    | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
988
989star_target[expr_ty] (memo):
990    | '*' a=(!'*' star_target) {
991        _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
992    | target_with_star_atom
993
994target_with_star_atom[expr_ty] (memo):
995    | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
996    | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
997    | star_atom
998
999star_atom[expr_ty]:
1000    | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
1001    | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
1002    | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) }
1003    | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) }
1004
1005single_target[expr_ty]:
1006    | single_subscript_attribute_target
1007    | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
1008    | '(' a=single_target ')' { a }
1009
1010single_subscript_attribute_target[expr_ty]:
1011    | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
1012    | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
1013
1014t_primary[expr_ty]:
1015    | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
1016    | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) }
1017    | a=t_primary b=genexp &t_lookahead {
1018        _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
1019    | a=t_primary '(' b=[arguments] ')' &t_lookahead {
1020        _PyAST_Call(a,
1021                 (b) ? ((expr_ty) b)->v.Call.args : NULL,
1022                 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
1023                 EXTRA) }
1024    | a=atom &t_lookahead { a }
1025
1026t_lookahead: '(' | '[' | '.'
1027
1028# Targets for del statements
1029# --------------------------
1030
1031del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
1032
1033del_target[expr_ty] (memo):
1034    | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) }
1035    | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) }
1036    | del_t_atom
1037
1038del_t_atom[expr_ty]:
1039    | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
1040    | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
1041    | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) }
1042    | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) }
1043
1044# TYPING ELEMENTS
1045# ---------------
1046
1047# type_expressions allow */** but ignore them
1048type_expressions[asdl_expr_seq*]:
1049    | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
1050        (asdl_expr_seq*)_PyPegen_seq_append_to_end(
1051            p,
1052            CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
1053            c) }
1054    | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
1055    | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
1056    | '*' a=expression ',' '**' b=expression {
1057        (asdl_expr_seq*)_PyPegen_seq_append_to_end(
1058            p,
1059            CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
1060            b) }
1061    | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
1062    | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
1063    | a[asdl_expr_seq*]=','.expression+ {a}
1064
1065func_type_comment[Token*]:
1066    | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t }  # Must be followed by indented block
1067    | invalid_double_type_comments
1068    | TYPE_COMMENT
1069
1070# ========================= END OF THE GRAMMAR ===========================
1071
1072
1073
1074# ========================= START OF INVALID RULES =======================
1075
1076# From here on, there are rules for invalid syntax with specialised error messages
1077invalid_arguments:
1078    | a=args ',' '*' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable argument unpacking follows keyword argument unpacking") }
1079    | a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
1080        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
1081    | a=NAME b='=' expression for_if_clauses {
1082        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
1083    | a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) }
1084    | args ',' a=expression b=for_if_clauses {
1085        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
1086    | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
1087invalid_kwarg:
1088    | a[Token*]=('True'|'False'|'None') b='=' {
1089        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to %s", PyBytes_AS_STRING(a->bytes)) }
1090    | a=NAME b='=' expression for_if_clauses {
1091        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
1092    | !(NAME '=') a=expression b='=' {
1093        RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1094            a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
1095
1096# IMPORTANT: Note that the "_without_invalid" suffix causes the rule to not call invalid rules under it
1097expression_without_invalid[expr_ty]:
1098    | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
1099    | disjunction
1100    | lambdef
1101invalid_legacy_expression:
1102    | a=NAME !'(' b=star_expressions {
1103        _PyPegen_check_legacy_stmt(p, a) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
1104            "Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}
1105
1106invalid_expression:
1107    # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
1108    # Soft keywords need to also be ignored because they can be parsed as NAME NAME
1109   | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
1110        _PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
1111        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
1112   | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
1113
1114invalid_named_expression(memo):
1115    | a=expression ':=' expression {
1116        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1117            a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
1118    | a=NAME '=' b=bitwise_or !('='|':=') {
1119        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
1120    | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {
1121        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
1122                                          _PyPegen_get_expr_name(a)) }
1123
1124invalid_assignment:
1125    | a=invalid_ann_assign_target ':' expression {
1126        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1127            a,
1128            "only single target (not %s) can be annotated",
1129            _PyPegen_get_expr_name(a)
1130        )}
1131    | a=star_named_expression ',' star_named_expressions* ':' expression {
1132        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
1133    | a=expression ':' expression {
1134        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
1135    | (star_targets '=')* a=star_expressions '=' {
1136        RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
1137    | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
1138    | a=star_expressions augassign (yield_expr | star_expressions) {
1139        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1140            a,
1141            "'%s' is an illegal expression for augmented assignment",
1142            _PyPegen_get_expr_name(a)
1143        )}
1144invalid_ann_assign_target[expr_ty]:
1145    | list
1146    | tuple
1147    | '(' a=invalid_ann_assign_target ')' { a }
1148invalid_del_stmt:
1149    | 'del' a=star_expressions {
1150        RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
1151invalid_block:
1152    | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
1153invalid_comprehension:
1154    | ('[' | '(' | '{') a=starred_expression for_if_clauses {
1155        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
1156    | ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses {
1157        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, expr_ty),
1158        "did you forget parentheses around the comprehension target?") }
1159    | ('[' | '{') a=star_named_expression b=',' for_if_clauses {
1160        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget parentheses around the comprehension target?") }
1161invalid_dict_comprehension:
1162    | '{' a='**' bitwise_or for_if_clauses '}' {
1163        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
1164invalid_parameters:
1165    | param_no_default* invalid_parameters_helper a=param_no_default {
1166        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
1167    | param_no_default* a='(' param_no_default+ ','? b=')' {
1168        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Function parameters cannot be parenthesized") }
1169    | a="/" ',' {
1170        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "at least one argument must precede /") }
1171    | (slash_no_default | slash_with_default) param_maybe_default* a='/' {
1172        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ may appear only once") }
1173    | (slash_no_default | slash_with_default)? param_maybe_default* '*' (',' | param_no_default) param_maybe_default* a='/' {
1174        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ must be ahead of *") }
1175    | param_maybe_default+ '/' a='*' {
1176        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected comma between / and *") }
1177invalid_default:
1178    | a='=' &(')'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected default value expression") }
1179invalid_star_etc:
1180    | a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named arguments must follow bare *") }
1181    | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
1182    | '*' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional argument cannot have default value") }
1183    | '*' (param_no_default | ',') param_maybe_default* a='*' (param_no_default | ',') {
1184        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* argument may appear only once") }
1185invalid_kwds:
1186    | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword argument cannot have default value") }
1187    | '**' param ',' a=param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") }
1188    | '**' param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") }
1189invalid_parameters_helper: # This is only there to avoid type errors
1190    | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
1191    | param_with_default+
1192invalid_lambda_parameters:
1193    | lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default {
1194        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
1195    | lambda_param_no_default* a='(' ','.lambda_param+ ','? b=')' {
1196        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Lambda expression parameters cannot be parenthesized") }
1197    | a="/" ',' {
1198        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "at least one argument must precede /") }
1199    | (lambda_slash_no_default | lambda_slash_with_default) lambda_param_maybe_default* a='/' {
1200        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ may appear only once") }
1201    | (lambda_slash_no_default | lambda_slash_with_default)? lambda_param_maybe_default* '*' (',' | lambda_param_no_default) lambda_param_maybe_default* a='/' {
1202        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ must be ahead of *") }
1203    | lambda_param_maybe_default+ '/' a='*' {
1204        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected comma between / and *") }
1205invalid_lambda_parameters_helper:
1206    | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
1207    | lambda_param_with_default+
1208invalid_lambda_star_etc:
1209    | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
1210    | '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional argument cannot have default value") }
1211    | '*' (lambda_param_no_default | ',') lambda_param_maybe_default* a='*' (lambda_param_no_default | ',') {
1212        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* argument may appear only once") }
1213invalid_lambda_kwds:
1214    | '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword argument cannot have default value") }
1215    | '**' lambda_param ',' a=lambda_param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") }
1216    | '**' lambda_param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") }
1217invalid_double_type_comments:
1218    | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
1219        RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
1220invalid_with_item:
1221    | expression 'as' a=expression &(',' | ')' | ':') {
1222        RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
1223
1224invalid_for_target:
1225    | ASYNC? 'for' a=star_expressions {
1226        RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
1227
1228invalid_group:
1229    | '(' a=starred_expression ')' {
1230        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
1231    | '(' a='**' expression ')' {
1232        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
1233invalid_import_from_targets:
1234    | import_from_as_names ',' NEWLINE {
1235        RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
1236
1237invalid_with_stmt:
1238    | [ASYNC] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1239    | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1240invalid_with_stmt_indent:
1241    | [ASYNC] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT {
1242        RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
1243    | [ASYNC] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT {
1244        RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
1245
1246invalid_try_stmt:
1247    | a='try' ':' NEWLINE !INDENT {
1248        RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
1249    | 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
1250    | 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' {
1251        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot have both 'except' and 'except*' on the same 'try'") }
1252    | 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' {
1253        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
1254invalid_except_stmt:
1255    | 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {
1256        RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
1257    | a='except' '*'? expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1258    | a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1259    | a='except' '*' (NEWLINE | ':') { RAISE_SYNTAX_ERROR("expected one or more exception types") }
1260invalid_finally_stmt:
1261    | a='finally' ':' NEWLINE !INDENT {
1262        RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) }
1263invalid_except_stmt_indent:
1264    | a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
1265        RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
1266    | a='except' ':' NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
1267invalid_except_star_stmt_indent:
1268    | a='except' '*' expression ['as' NAME ] ':' NEWLINE !INDENT {
1269        RAISE_INDENTATION_ERROR("expected an indented block after 'except*' statement on line %d", a->lineno) }
1270invalid_match_stmt:
1271    | "match" subject_expr NEWLINE { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
1272    | a="match" subject=subject_expr ':' NEWLINE !INDENT {
1273        RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
1274invalid_case_block:
1275    | "case" patterns guard? NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1276    | a="case" patterns guard? ':' NEWLINE !INDENT {
1277        RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
1278invalid_as_pattern:
1279    | or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
1280    | or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
1281invalid_class_pattern:
1282    | name_or_attr '(' a=invalid_class_argument_pattern  { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1283        PyPegen_first_item(a, pattern_ty),
1284        PyPegen_last_item(a, pattern_ty),
1285        "positional patterns follow keyword patterns") }
1286invalid_class_argument_pattern[asdl_pattern_seq*]:
1287    | [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }
1288invalid_if_stmt:
1289    | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1290    | a='if' a=named_expression ':' NEWLINE !INDENT {
1291        RAISE_INDENTATION_ERROR("expected an indented block after 'if' statement on line %d", a->lineno) }
1292invalid_elif_stmt:
1293    | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1294    | a='elif' named_expression ':' NEWLINE !INDENT {
1295        RAISE_INDENTATION_ERROR("expected an indented block after 'elif' statement on line %d", a->lineno) }
1296invalid_else_stmt:
1297    | a='else' ':' NEWLINE !INDENT {
1298        RAISE_INDENTATION_ERROR("expected an indented block after 'else' statement on line %d", a->lineno) }
1299invalid_while_stmt:
1300    | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1301    | a='while' named_expression ':' NEWLINE !INDENT {
1302        RAISE_INDENTATION_ERROR("expected an indented block after 'while' statement on line %d", a->lineno) }
1303invalid_for_stmt:
1304    | [ASYNC] 'for' star_targets 'in' star_expressions NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1305    | [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {
1306        RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) }
1307invalid_def_raw:
1308    | [ASYNC] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
1309        RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) }
1310invalid_class_def_raw:
1311    | 'class' NAME ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1312    | a='class' NAME ['(' [arguments] ')'] ':' NEWLINE !INDENT {
1313        RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) }
1314
1315invalid_double_starred_kvpairs:
1316    | ','.double_starred_kvpair+ ',' invalid_kvpair
1317    | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
1318    | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1319invalid_kvpair:
1320    | a=expression !(':') {
1321        RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
1322    | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
1323    | expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1324