blob: d91e887e04dc0085480a21e3e12e8b95372e6e4f [file] [log] [blame]
Pablo Galindob4282dd2020-06-12 00:51:44 +01001# PEG grammar for Python
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003@trailer '''
4void *
5_PyPegen_parse(Parser *p)
6{
7 // Initialize keywords
8 p->keywords = reserved_keywords;
9 p->n_keyword_lists = n_keyword_lists;
Pablo Galindob2802482021-04-15 21:38:45 +010010 p->soft_keywords = soft_keywords;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010011
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);
Guido van Rossumc001c092020-04-30 12:12:19 -070020 } else if (p->start_rule == Py_func_type_input) {
21 result = func_type_rule(p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +010022 } else if (p->start_rule == Py_fstring_input) {
23 result = fstring_rule(p);
24 }
25
26 return result;
27}
28
29// The end
30'''
Guido van Rossumc001c092020-04-30 12:12:19 -070031file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +020032interactive[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) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010035fstring[expr_ty]: star_expressions
36
Guido van Rossumc001c092020-04-30 12:12:19 -070037# type_expressions allow */** but ignore them
Pablo Galindoa5634c42020-09-16 19:42:00 +010038type_expressions[asdl_expr_seq*]:
Guido van Rossumc001c092020-04-30 12:12:19 -070039 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030040 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
41 p,
42 CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
43 c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010044 | 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) }
Shantanu603d3542020-05-03 22:08:14 -070046 | '*' a=expression ',' '**' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030047 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
48 p,
49 CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
50 b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010051 | '*' 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}
Guido van Rossumc001c092020-04-30 12:12:19 -070054
Pablo Galindoa5634c42020-09-16 19:42:00 +010055statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000056statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +010057statement_newline[asdl_stmt_seq*]:
58 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000059 | simple_stmts
Victor Stinnerd27f8d22021-04-07 21:34:22 +020060 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010061 | ENDMARKER { _PyPegen_interactive_exit(p) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000062simple_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 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010065# NOTE: assignment MUST precede expression, else parsing a simple assignment
66# will throw a SyntaxError.
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000067simple_stmt[stmt_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +010068 | assignment
Victor Stinnerd27f8d22021-04-07 21:34:22 +020069 | e=star_expressions { _PyAST_Expr(e, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010070 | &'return' return_stmt
71 | &('import' | 'from') import_stmt
72 | &'raise' raise_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020073 | 'pass' { _PyAST_Pass(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010074 | &'del' del_stmt
75 | &'yield' yield_stmt
76 | &'assert' assert_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020077 | 'break' { _PyAST_Break(EXTRA) }
78 | 'continue' { _PyAST_Continue(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010079 | &'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
Brandt Bucher145bf262021-02-26 14:51:55 -080089 | match_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +010090
91# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030092assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010093 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030094 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030095 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030096 6,
97 "Variable annotation syntax is",
Victor Stinnerd27f8d22021-04-07 21:34:22 +020098 _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030099 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300100 | a=('(' b=single_target ')' { b }
101 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200102 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100103 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200104 _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300105 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200106 _PyAST_AugAssign(a, b->kind, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100107 | invalid_assignment
108
109augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300110 | '+=' { _PyPegen_augoperator(p, Add) }
111 | '-=' { _PyPegen_augoperator(p, Sub) }
112 | '*=' { _PyPegen_augoperator(p, Mult) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300113 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300114 | '/=' { _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) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100123
Pablo Galindoa5634c42020-09-16 19:42:00 +0100124global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200125 _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100126nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200127 _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100128
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200129yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100130
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200131assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100132
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300133del_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200134 | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300135 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100136
137import_stmt[stmt_ty]: import_name | import_from
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200138import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100139# 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 {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200142 _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100143 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200144 _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100145import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100146 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300147 | import_from_as_names !','
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400148 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300149 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100150import_from_as_names[asdl_alias_seq*]:
151 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100152import_from_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200153 | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100154 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400155 EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100156dotted_as_names[asdl_alias_seq*]:
157 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100158dotted_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200159 | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100160 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400161 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100162dotted_name[expr_ty]:
163 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
164 | NAME
165
166if_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100167 | 'if' a=named_expression ':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200168 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100169 | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
170 | invalid_if_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100171elif_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100172 | 'elif' a=named_expression ':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200173 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100174 | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
175 | invalid_elif_stmt
Pablo Galindo58fb1562021-02-02 19:54:22 +0000176else_block[asdl_stmt_seq*]: 'else' &&':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100177
178while_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100179 | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
180 | invalid_while_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100181
182for_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000183 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200184 _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000185 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200186 CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300187 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100188
189with_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100190 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200191 _PyAST_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100192 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200193 _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100194 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200195 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100196 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200197 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000198 | invalid_with_stmt
199
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100200with_item[withitem_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200201 | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300202 | invalid_with_item
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200203 | e=expression { _PyAST_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100204
205try_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200206 | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
207 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100208except_block[excepthandler_ty]:
Pablo Galindo206cbda2021-02-07 18:42:21 +0000209 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200210 _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
211 | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindo206cbda2021-02-07 18:42:21 +0000212 | invalid_except_block
Pablo Galindoa5634c42020-09-16 19:42:00 +0100213finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100214
Brandt Bucher145bf262021-02-26 14:51:55 -0800215match_stmt[stmt_ty]:
216 | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200217 CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000218 | invalid_match_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -0800219subject_expr[expr_ty]:
220 | value=star_named_expression ',' values=star_named_expressions? {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200221 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800222 | named_expression
223case_block[match_case_ty]:
224 | "case" pattern=patterns guard=guard? ':' body=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200225 _PyAST_match_case(pattern, guard, body, p->arena) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000226 | invalid_case_block
Brandt Bucher145bf262021-02-26 14:51:55 -0800227guard[expr_ty]: 'if' guard=named_expression { guard }
228
229patterns[expr_ty]:
230 | values[asdl_expr_seq*]=open_sequence_pattern {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200231 _PyAST_Tuple(values, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800232 | pattern
233pattern[expr_ty]:
234 | as_pattern
235 | or_pattern
236as_pattern[expr_ty]:
237 | pattern=or_pattern 'as' target=capture_pattern {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200238 _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800239or_pattern[expr_ty]:
240 | patterns[asdl_expr_seq*]='|'.closed_pattern+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200241 asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800242closed_pattern[expr_ty]:
243 | literal_pattern
244 | capture_pattern
245 | wildcard_pattern
246 | value_pattern
247 | group_pattern
248 | sequence_pattern
249 | mapping_pattern
250 | class_pattern
251
252literal_pattern[expr_ty]:
253 | signed_number !('+' | '-')
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200254 | real=signed_number '+' imag=NUMBER { _PyAST_BinOp(real, Add, imag, EXTRA) }
255 | real=signed_number '-' imag=NUMBER { _PyAST_BinOp(real, Sub, imag, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800256 | strings
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200257 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
258 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
259 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800260signed_number[expr_ty]:
261 | NUMBER
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200262 | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800263
264capture_pattern[expr_ty]:
265 | !"_" name=NAME !('.' | '(' | '=') {
266 _PyPegen_set_expr_context(p, name, Store) }
267
268wildcard_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200269 | "_" { _PyAST_Name(CHECK(PyObject*, _PyPegen_new_identifier(p, "_")), Store, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800270
271value_pattern[expr_ty]:
272 | attr=attr !('.' | '(' | '=') { attr }
273attr[expr_ty]:
274 | value=name_or_attr '.' attr=NAME {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200275 _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800276name_or_attr[expr_ty]:
277 | attr
278 | NAME
279
280group_pattern[expr_ty]:
281 | '(' pattern=pattern ')' { pattern }
282
283sequence_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200284 | '[' values=maybe_sequence_pattern? ']' { _PyAST_List(values, Load, EXTRA) }
285 | '(' values=open_sequence_pattern? ')' { _PyAST_Tuple(values, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800286open_sequence_pattern[asdl_seq*]:
287 | value=maybe_star_pattern ',' values=maybe_sequence_pattern? {
288 _PyPegen_seq_insert_in_front(p, value, values) }
289maybe_sequence_pattern[asdl_seq*]:
290 | values=','.maybe_star_pattern+ ','? { values }
291maybe_star_pattern[expr_ty]:
292 | star_pattern
293 | pattern
294star_pattern[expr_ty]:
295 | '*' value=(capture_pattern | wildcard_pattern) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200296 _PyAST_Starred(value, Store, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800297
298mapping_pattern[expr_ty]:
299 | '{' items=items_pattern? '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200300 _PyAST_Dict(CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, items)), CHECK(asdl_expr_seq*, _PyPegen_get_values(p, items)), EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800301items_pattern[asdl_seq*]:
302 | items=','.key_value_pattern+ ','? { items }
303key_value_pattern[KeyValuePair*]:
304 | key=(literal_pattern | value_pattern) ':' value=pattern {
305 _PyPegen_key_value_pair(p, key, value) }
306 | double_star_pattern
307double_star_pattern[KeyValuePair*]:
308 | '**' value=capture_pattern { _PyPegen_key_value_pair(p, NULL, value) }
309
310class_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200311 | func=name_or_attr '(' ')' { _PyAST_Call(func, NULL, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800312 | func=name_or_attr '(' args=positional_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200313 _PyAST_Call(func, args, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800314 | func=name_or_attr '(' keywords=keyword_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200315 _PyAST_Call(func, NULL, keywords, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800316 | func=name_or_attr '(' args=positional_patterns ',' keywords=keyword_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200317 _PyAST_Call(func, args, keywords, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800318positional_patterns[asdl_expr_seq*]:
319 | args[asdl_expr_seq*]=','.pattern+ { args }
320keyword_patterns[asdl_keyword_seq*]:
321 | keywords[asdl_keyword_seq*]=','.keyword_pattern+ { keywords }
322keyword_pattern[keyword_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200323 | arg=NAME '=' value=pattern { _PyAST_keyword(arg->v.Name.id, value, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800324
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100325return_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200326 | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100327
328raise_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200329 | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
330 | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100331
332function_def[stmt_ty]:
333 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
334 | function_def_raw
335
336function_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000337 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200338 _PyAST_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300339 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300340 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000341 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300342 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300343 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300344 5,
345 "Async functions are",
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200346 _PyAST_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300347 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300348 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
349 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100350func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700351 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
352 | invalid_double_type_comments
353 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100354
355params[arguments_ty]:
356 | invalid_parameters
357 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700358
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100359parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100360 | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100361 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700362 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100363 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100364 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100365 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700366 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100367 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700368
369# Some duplication here because we can't write (',' | &')'),
370# which is because we don't support empty alternatives (yet).
371#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100372slash_no_default[asdl_arg_seq*]:
373 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
374 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700375slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100376 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
377 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700378
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100379star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700380 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100381 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700382 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100383 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700384 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300385 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700386
Guido van Rossum3941d972020-05-01 09:42:03 -0700387kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700388
389# One parameter. This *includes* a following comma and type comment.
390#
391# There are three styles:
392# - No default
393# - With default
394# - Maybe with default
395#
396# There are two alternative forms of each, to deal with type comments:
397# - Ends in a comma followed by an optional type comment
398# - No comma, optional type comment, must be followed by close paren
399# The latter form is for a final parameter without trailing comma.
400#
401param_no_default[arg_ty]:
402 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
403 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
404param_with_default[NameDefaultPair*]:
405 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
406 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
407param_maybe_default[NameDefaultPair*]:
408 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
409 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200410param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700411
412annotation[expr_ty]: ':' a=expression { a }
413default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100414
Pablo Galindoa5634c42020-09-16 19:42:00 +0100415decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100416
417class_def[stmt_ty]:
418 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
419 | class_def_raw
420class_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000421 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200422 _PyAST_ClassDef(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100423 (b) ? ((expr_ty) b)->v.Call.args : NULL,
424 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
425 c, NULL, EXTRA) }
426
Pablo Galindoa5634c42020-09-16 19:42:00 +0100427block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100428 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000429 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100430 | invalid_block
431
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100432star_expressions[expr_ty]:
433 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200434 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
435 | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100436 | star_expression
437star_expression[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200438 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100439 | expression
440
Pablo Galindoa5634c42020-09-16 19:42:00 +0100441star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100442star_named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200443 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100444 | named_expression
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100445
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100446named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200447 | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100448 | invalid_named_expression
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100449 | expression !':='
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100450
Pablo Galindod9151cb2021-04-13 02:32:33 +0100451direct_named_expression[expr_ty]:
452 | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
453 | expression !':='
454
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100455annotated_rhs[expr_ty]: yield_expr | star_expressions
456
457expressions[expr_ty]:
458 | a=expression b=(',' c=expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200459 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
460 | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100461 | expression
462expression[expr_ty] (memo):
Pablo Galindob2802482021-04-15 21:38:45 +0100463 | invalid_expression
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200464 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100465 | disjunction
466 | lambdef
467
468lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300469 | 'lambda' a=[lambda_params] ':' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200470 _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100471
472lambda_params[arguments_ty]:
473 | invalid_lambda_parameters
474 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700475
476# lambda_parameters etc. duplicates parameters but without annotations
477# or type comments, and if there's no comma after a parameter, we expect
478# a colon, not a close parenthesis. (For more, see parameters above.)
479#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100480lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100481 | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100482 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700483 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100484 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100485 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100486 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700487 | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100488 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700489
Pablo Galindoa5634c42020-09-16 19:42:00 +0100490lambda_slash_no_default[asdl_arg_seq*]:
491 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
492 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700493lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100494 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
495 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700496
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100497lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700498 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100499 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700500 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100501 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700502 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300503 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700504
505lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
506
507lambda_param_no_default[arg_ty]:
508 | a=lambda_param ',' { a }
509 | a=lambda_param &':' { a }
510lambda_param_with_default[NameDefaultPair*]:
511 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
512 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
513lambda_param_maybe_default[NameDefaultPair*]:
514 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
515 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200516lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100517
518disjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200519 | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100520 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300521 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100522 EXTRA) }
523 | conjunction
524conjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200525 | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100526 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300527 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100528 EXTRA) }
529 | inversion
530inversion[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200531 | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100532 | comparison
533comparison[expr_ty]:
534 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200535 _PyAST_Compare(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300536 a,
537 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
538 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
539 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100540 | bitwise_or
541compare_op_bitwise_or_pair[CmpopExprPair*]:
542 | eq_bitwise_or
543 | noteq_bitwise_or
544 | lte_bitwise_or
545 | lt_bitwise_or
546 | gte_bitwise_or
547 | gt_bitwise_or
548 | notin_bitwise_or
549 | in_bitwise_or
550 | isnot_bitwise_or
551 | is_bitwise_or
552eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100553noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000554 | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100555lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
556lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
557gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
558gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
559notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
560in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
561isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
562is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
563
564bitwise_or[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200565 | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100566 | bitwise_xor
567bitwise_xor[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200568 | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100569 | bitwise_and
570bitwise_and[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200571 | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100572 | shift_expr
573shift_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200574 | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
575 | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100576 | sum
577
578sum[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200579 | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
580 | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100581 | term
582term[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200583 | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) }
584 | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) }
585 | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
586 | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
587 | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100588 | factor
589factor[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200590 | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
591 | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
592 | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100593 | power
594power[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200595 | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100596 | await_primary
597await_primary[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200598 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100599 | primary
600primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200601 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200602 | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
603 | a=primary b=genexp { _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100604 | a=primary '(' b=[arguments] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200605 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100606 (b) ? ((expr_ty) b)->v.Call.args : NULL,
607 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
608 EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200609 | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100610 | atom
611
612slices[expr_ty]:
613 | a=slice !',' { a }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200614 | a[asdl_expr_seq*]=','.slice+ [','] { _PyAST_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100615slice[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200616 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200617 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100618atom[expr_ty]:
619 | NAME
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200620 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
621 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
622 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100623 | &STRING strings
624 | NUMBER
625 | &'(' (tuple | group | genexp)
626 | &'[' (list | listcomp)
627 | &'{' (dict | set | dictcomp | setcomp)
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200628 | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100629
630strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
631list[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200632 | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100633listcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200634 | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100635 | invalid_comprehension
636tuple[expr_ty]:
637 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200638 _PyAST_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300639group[expr_ty]:
640 | '(' a=(yield_expr | named_expression) ')' { a }
641 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100642genexp[expr_ty]:
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100643 | '(' a=direct_named_expression b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100644 | invalid_comprehension
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200645set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100646setcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200647 | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100648 | invalid_comprehension
649dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300650 | '{' a=[double_starred_kvpairs] '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200651 _PyAST_Dict(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300652 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
653 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
654 EXTRA) }
Pablo Galindoda743502021-04-15 14:06:39 +0100655 | '{' invalid_double_starred_kvpairs '}'
656
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100657dictcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200658 | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300659 | invalid_dict_comprehension
660double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
661double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100662 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300663 | kvpair
664kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100665for_if_clauses[asdl_comprehension_seq*]:
666 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300667for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100668 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200669 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100670 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200671 _PyAST_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300672 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100673
674yield_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200675 | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
676 | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100677
678arguments[expr_ty] (memo):
679 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200680 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100681args[expr_ty]:
Pablo Galindod9151cb2021-04-13 02:32:33 +0100682 | a[asdl_expr_seq*]=','.(starred_expression | direct_named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200683 | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300684 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
685 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100686 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100687kwargs[asdl_seq*]:
688 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
689 | ','.kwarg_or_starred+
690 | ','.kwarg_or_double_starred+
691starred_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200692 | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100693kwarg_or_starred[KeywordOrStarred*]:
694 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200695 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100696 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300697 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100698kwarg_or_double_starred[KeywordOrStarred*]:
699 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200700 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
701 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300702 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100703
704# NOTE: star_targets may contain *bitwise_or, targets may not.
705star_targets[expr_ty]:
706 | a=star_target !',' { a }
707 | a=star_target b=(',' c=star_target { c })* [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200708 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200709star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
710star_targets_tuple_seq[asdl_expr_seq*]:
711 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
712 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100713star_target[expr_ty] (memo):
714 | '*' a=(!'*' star_target) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200715 _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200716 | target_with_star_atom
717target_with_star_atom[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200718 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
719 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100720 | star_atom
721star_atom[expr_ty]:
722 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200723 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200724 | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) }
725 | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100726
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300727single_target[expr_ty]:
728 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100729 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300730 | '(' a=single_target ')' { a }
731single_subscript_attribute_target[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200732 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
733 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100734
Pablo Galindoa5634c42020-09-16 19:42:00 +0100735del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100736del_target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200737 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) }
738 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100739 | del_t_atom
740del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300741 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100742 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200743 | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) }
744 | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100745
Pablo Galindoa5634c42020-09-16 19:42:00 +0100746targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100747target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200748 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
749 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100750 | t_atom
751t_primary[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200752 | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
753 | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300754 | a=t_primary b=genexp &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200755 _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100756 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200757 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100758 (b) ? ((expr_ty) b)->v.Call.args : NULL,
759 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
760 EXTRA) }
761 | a=atom &t_lookahead { a }
762t_lookahead: '(' | '[' | '.'
763t_atom[expr_ty]:
764 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
765 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200766 | '(' b=[targets] ')' { _PyAST_Tuple(b, Store, EXTRA) }
767 | '[' b=[targets] ']' { _PyAST_List(b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100768
769
770# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200771invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100772 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300773 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
774 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300775 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
776 | args ',' a=expression for_if_clauses {
777 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100778 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300779invalid_kwarg:
Pablo Galindo43c4fb62020-12-13 16:46:48 +0000780 | expression a='=' {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300781 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
782 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindob2802482021-04-15 21:38:45 +0100783
784invalid_expression:
785 # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
786 # Soft keywords need to also be ignored because they can be parsed as NAME NAME
787 | !(NAME STRING | SOFT_KEYWORD) a=disjunction expression {
788 RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, "invalid syntax. Perhaps you forgot a comma?") }
789
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100790invalid_named_expression:
791 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300792 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
793 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100794 | a=NAME b='=' bitwise_or !('='|':='|',') {
795 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
796 | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':='|',') {
797 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
798 _PyPegen_get_expr_name(a)) }
799
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100800invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300801 | a=invalid_ann_assign_target ':' expression {
802 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
803 a,
804 "only single target (not %s) can be annotated",
805 _PyPegen_get_expr_name(a)
806 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300807 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300808 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300809 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300810 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100811 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300812 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100813 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100814 | a=star_expressions augassign (yield_expr | star_expressions) {
Brandt Bucher145bf262021-02-26 14:51:55 -0800815 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300816 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100817 "'%s' is an illegal expression for augmented assignment",
818 _PyPegen_get_expr_name(a)
819 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300820invalid_ann_assign_target[expr_ty]:
821 | list
822 | tuple
823 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300824invalid_del_stmt:
825 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300826 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100827invalid_block:
828 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200829invalid_primary:
830 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100831invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300832 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
833 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000834 | ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
Pablo Galindo835f14f2021-01-31 22:52:56 +0000835 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300836invalid_dict_comprehension:
837 | '{' a='**' bitwise_or for_if_clauses '}' {
838 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100839invalid_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200840 | param_no_default* invalid_parameters_helper param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100841 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200842invalid_parameters_helper: # This is only there to avoid type errors
843 | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
844 | param_with_default+
Pablo Galindoc6483c92020-06-10 14:07:06 +0100845invalid_lambda_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200846 | lambda_param_no_default* invalid_lambda_parameters_helper lambda_param_no_default {
Pablo Galindoc6483c92020-06-10 14:07:06 +0100847 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200848invalid_lambda_parameters_helper:
849 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
850 | lambda_param_with_default+
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300851invalid_star_etc:
852 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300853 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300854invalid_lambda_star_etc:
855 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700856invalid_double_type_comments:
857 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
858 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300859invalid_with_item:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000860 | expression 'as' a=expression &(',' | ')' | ':') {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300861 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300862
863invalid_for_target:
864 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300865 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300866
867invalid_group:
868 | '(' a=starred_expression ')' {
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100869 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
Pablo Galindo8efad612021-03-24 19:34:17 +0000870 | '(' a='**' expression ')' {
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100871 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300872invalid_import_from_targets:
873 | import_from_as_names ',' {
874 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000875
876invalid_with_stmt:
877 | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
878 | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
Pablo Galindo206cbda2021-02-07 18:42:21 +0000879
880invalid_except_block:
881 | 'except' a=expression ',' expressions ['as' NAME ] ':' {
882 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
883 | 'except' expression ['as' NAME ] &&':'
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200884 | 'except' &&':'
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000885
886invalid_match_stmt:
887 | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000888invalid_case_block:
889 | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100890invalid_if_stmt:
891 | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
892invalid_elif_stmt:
893 | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
894invalid_while_stmt:
895 | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindoda743502021-04-15 14:06:39 +0100896
897invalid_double_starred_kvpairs:
898 | ','.double_starred_kvpair+ ',' invalid_kvpair
899 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
900 | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
901invalid_kvpair:
Pablo Galindob5b98bd2021-04-16 00:45:42 +0100902 | a=expression !(':') {
903 RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, "':' expected after dictionary key") }
Pablo Galindoda743502021-04-15 14:06:39 +0100904 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
905 | expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }