blob: 324793c88af2a255f815c023d254b324681a5400 [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;
10
11 // Run parser
12 void *result = NULL;
13 if (p->start_rule == Py_file_input) {
14 result = file_rule(p);
15 } else if (p->start_rule == Py_single_input) {
16 result = interactive_rule(p);
17 } else if (p->start_rule == Py_eval_input) {
18 result = eval_rule(p);
Guido van Rossumc001c092020-04-30 12:12:19 -070019 } else if (p->start_rule == Py_func_type_input) {
20 result = func_type_rule(p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +010021 } else if (p->start_rule == Py_fstring_input) {
22 result = fstring_rule(p);
23 }
24
25 return result;
26}
27
28// The end
29'''
Guido van Rossumc001c092020-04-30 12:12:19 -070030file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +020031interactive[mod_ty]: a=statement_newline { _PyAST_Interactive(a, p->arena) }
32eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _PyAST_Expression(a, p->arena) }
33func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _PyAST_FunctionType(a, b, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010034fstring[expr_ty]: star_expressions
35
Guido van Rossumc001c092020-04-30 12:12:19 -070036# type_expressions allow */** but ignore them
Pablo Galindoa5634c42020-09-16 19:42:00 +010037type_expressions[asdl_expr_seq*]:
Guido van Rossumc001c092020-04-30 12:12:19 -070038 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030039 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
40 p,
41 CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
42 c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010043 | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
44 | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070045 | '*' a=expression ',' '**' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030046 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
47 p,
48 CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
49 b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010050 | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
51 | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
52 | a[asdl_expr_seq*]=','.expression+ {a}
Guido van Rossumc001c092020-04-30 12:12:19 -070053
Pablo Galindoa5634c42020-09-16 19:42:00 +010054statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000055statement[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 +010056statement_newline[asdl_stmt_seq*]:
57 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000058 | simple_stmts
Victor Stinnerd27f8d22021-04-07 21:34:22 +020059 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010060 | ENDMARKER { _PyPegen_interactive_exit(p) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000061simple_stmts[asdl_stmt_seq*]:
62 | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
63 | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010064# NOTE: assignment MUST precede expression, else parsing a simple assignment
65# will throw a SyntaxError.
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000066simple_stmt[stmt_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +010067 | assignment
Victor Stinnerd27f8d22021-04-07 21:34:22 +020068 | e=star_expressions { _PyAST_Expr(e, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010069 | &'return' return_stmt
70 | &('import' | 'from') import_stmt
71 | &'raise' raise_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020072 | 'pass' { _PyAST_Pass(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010073 | &'del' del_stmt
74 | &'yield' yield_stmt
75 | &'assert' assert_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020076 | 'break' { _PyAST_Break(EXTRA) }
77 | 'continue' { _PyAST_Continue(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010078 | &'global' global_stmt
79 | &'nonlocal' nonlocal_stmt
80compound_stmt[stmt_ty]:
81 | &('def' | '@' | ASYNC) function_def
82 | &'if' if_stmt
83 | &('class' | '@') class_def
84 | &('with' | ASYNC) with_stmt
85 | &('for' | ASYNC) for_stmt
86 | &'try' try_stmt
87 | &'while' while_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -080088 | match_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +010089
90# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030091assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010092 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030093 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030094 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030095 6,
96 "Variable annotation syntax is",
Victor Stinnerd27f8d22021-04-07 21:34:22 +020097 _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030098 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030099 | a=('(' b=single_target ')' { b }
100 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200101 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100102 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200103 _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300104 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200105 _PyAST_AugAssign(a, b->kind, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100106 | invalid_assignment
107
108augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300109 | '+=' { _PyPegen_augoperator(p, Add) }
110 | '-=' { _PyPegen_augoperator(p, Sub) }
111 | '*=' { _PyPegen_augoperator(p, Mult) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300112 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300113 | '/=' { _PyPegen_augoperator(p, Div) }
114 | '%=' { _PyPegen_augoperator(p, Mod) }
115 | '&=' { _PyPegen_augoperator(p, BitAnd) }
116 | '|=' { _PyPegen_augoperator(p, BitOr) }
117 | '^=' { _PyPegen_augoperator(p, BitXor) }
118 | '<<=' { _PyPegen_augoperator(p, LShift) }
119 | '>>=' { _PyPegen_augoperator(p, RShift) }
120 | '**=' { _PyPegen_augoperator(p, Pow) }
121 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100122
Pablo Galindoa5634c42020-09-16 19:42:00 +0100123global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200124 _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100125nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200126 _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100127
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200128yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100129
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200130assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100131
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300132del_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200133 | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300134 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100135
136import_stmt[stmt_ty]: import_name | import_from
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200137import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100138# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
139import_from[stmt_ty]:
140 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200141 _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100142 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200143 _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100144import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100145 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300146 | import_from_as_names !','
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400147 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300148 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100149import_from_as_names[asdl_alias_seq*]:
150 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100151import_from_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200152 | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100153 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400154 EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100155dotted_as_names[asdl_alias_seq*]:
156 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100157dotted_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200158 | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100159 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400160 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100161dotted_name[expr_ty]:
162 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
163 | NAME
164
165if_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100166 | 'if' a=named_expression ':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200167 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100168 | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
169 | invalid_if_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100170elif_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100171 | 'elif' a=named_expression ':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200172 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100173 | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
174 | invalid_elif_stmt
Pablo Galindo58fb1562021-02-02 19:54:22 +0000175else_block[asdl_stmt_seq*]: 'else' &&':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100176
177while_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100178 | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
179 | invalid_while_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100180
181for_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000182 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200183 _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000184 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200185 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 +0300186 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100187
188with_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100189 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200190 _PyAST_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100191 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200192 _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100193 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200194 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100195 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200196 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 +0000197 | invalid_with_stmt
198
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100199with_item[withitem_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200200 | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300201 | invalid_with_item
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200202 | e=expression { _PyAST_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100203
204try_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200205 | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
206 | '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 +0100207except_block[excepthandler_ty]:
Pablo Galindo206cbda2021-02-07 18:42:21 +0000208 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200209 _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
210 | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindo206cbda2021-02-07 18:42:21 +0000211 | invalid_except_block
Pablo Galindoa5634c42020-09-16 19:42:00 +0100212finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100213
Brandt Bucher145bf262021-02-26 14:51:55 -0800214match_stmt[stmt_ty]:
215 | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200216 CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000217 | invalid_match_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -0800218subject_expr[expr_ty]:
219 | value=star_named_expression ',' values=star_named_expressions? {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200220 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800221 | named_expression
222case_block[match_case_ty]:
223 | "case" pattern=patterns guard=guard? ':' body=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200224 _PyAST_match_case(pattern, guard, body, p->arena) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000225 | invalid_case_block
Brandt Bucher145bf262021-02-26 14:51:55 -0800226guard[expr_ty]: 'if' guard=named_expression { guard }
227
228patterns[expr_ty]:
229 | values[asdl_expr_seq*]=open_sequence_pattern {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200230 _PyAST_Tuple(values, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800231 | pattern
232pattern[expr_ty]:
233 | as_pattern
234 | or_pattern
235as_pattern[expr_ty]:
236 | pattern=or_pattern 'as' target=capture_pattern {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200237 _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800238or_pattern[expr_ty]:
239 | patterns[asdl_expr_seq*]='|'.closed_pattern+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200240 asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800241closed_pattern[expr_ty]:
242 | literal_pattern
243 | capture_pattern
244 | wildcard_pattern
245 | value_pattern
246 | group_pattern
247 | sequence_pattern
248 | mapping_pattern
249 | class_pattern
250
251literal_pattern[expr_ty]:
252 | signed_number !('+' | '-')
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200253 | real=signed_number '+' imag=NUMBER { _PyAST_BinOp(real, Add, imag, EXTRA) }
254 | real=signed_number '-' imag=NUMBER { _PyAST_BinOp(real, Sub, imag, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800255 | strings
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200256 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
257 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
258 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800259signed_number[expr_ty]:
260 | NUMBER
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200261 | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800262
263capture_pattern[expr_ty]:
264 | !"_" name=NAME !('.' | '(' | '=') {
265 _PyPegen_set_expr_context(p, name, Store) }
266
267wildcard_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200268 | "_" { _PyAST_Name(CHECK(PyObject*, _PyPegen_new_identifier(p, "_")), Store, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800269
270value_pattern[expr_ty]:
271 | attr=attr !('.' | '(' | '=') { attr }
272attr[expr_ty]:
273 | value=name_or_attr '.' attr=NAME {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200274 _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800275name_or_attr[expr_ty]:
276 | attr
277 | NAME
278
279group_pattern[expr_ty]:
280 | '(' pattern=pattern ')' { pattern }
281
282sequence_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200283 | '[' values=maybe_sequence_pattern? ']' { _PyAST_List(values, Load, EXTRA) }
284 | '(' values=open_sequence_pattern? ')' { _PyAST_Tuple(values, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800285open_sequence_pattern[asdl_seq*]:
286 | value=maybe_star_pattern ',' values=maybe_sequence_pattern? {
287 _PyPegen_seq_insert_in_front(p, value, values) }
288maybe_sequence_pattern[asdl_seq*]:
289 | values=','.maybe_star_pattern+ ','? { values }
290maybe_star_pattern[expr_ty]:
291 | star_pattern
292 | pattern
293star_pattern[expr_ty]:
294 | '*' value=(capture_pattern | wildcard_pattern) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200295 _PyAST_Starred(value, Store, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800296
297mapping_pattern[expr_ty]:
298 | '{' items=items_pattern? '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200299 _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 -0800300items_pattern[asdl_seq*]:
301 | items=','.key_value_pattern+ ','? { items }
302key_value_pattern[KeyValuePair*]:
303 | key=(literal_pattern | value_pattern) ':' value=pattern {
304 _PyPegen_key_value_pair(p, key, value) }
305 | double_star_pattern
306double_star_pattern[KeyValuePair*]:
307 | '**' value=capture_pattern { _PyPegen_key_value_pair(p, NULL, value) }
308
309class_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200310 | func=name_or_attr '(' ')' { _PyAST_Call(func, NULL, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800311 | func=name_or_attr '(' args=positional_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200312 _PyAST_Call(func, args, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800313 | func=name_or_attr '(' keywords=keyword_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200314 _PyAST_Call(func, NULL, keywords, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800315 | func=name_or_attr '(' args=positional_patterns ',' keywords=keyword_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200316 _PyAST_Call(func, args, keywords, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800317positional_patterns[asdl_expr_seq*]:
318 | args[asdl_expr_seq*]=','.pattern+ { args }
319keyword_patterns[asdl_keyword_seq*]:
320 | keywords[asdl_keyword_seq*]=','.keyword_pattern+ { keywords }
321keyword_pattern[keyword_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200322 | arg=NAME '=' value=pattern { _PyAST_keyword(arg->v.Name.id, value, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800323
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100324return_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200325 | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100326
327raise_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200328 | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
329 | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100330
331function_def[stmt_ty]:
332 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
333 | function_def_raw
334
335function_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000336 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200337 _PyAST_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300338 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300339 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000340 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300341 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300342 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300343 5,
344 "Async functions are",
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200345 _PyAST_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300346 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300347 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
348 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100349func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700350 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
351 | invalid_double_type_comments
352 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100353
354params[arguments_ty]:
355 | invalid_parameters
356 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700357
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100358parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100359 | 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 +0100360 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700361 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100362 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100363 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100364 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700365 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100366 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700367
368# Some duplication here because we can't write (',' | &')'),
369# which is because we don't support empty alternatives (yet).
370#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100371slash_no_default[asdl_arg_seq*]:
372 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
373 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700374slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100375 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
376 | 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 -0700377
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100378star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700379 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100380 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700381 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100382 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700383 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300384 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700385
Guido van Rossum3941d972020-05-01 09:42:03 -0700386kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700387
388# One parameter. This *includes* a following comma and type comment.
389#
390# There are three styles:
391# - No default
392# - With default
393# - Maybe with default
394#
395# There are two alternative forms of each, to deal with type comments:
396# - Ends in a comma followed by an optional type comment
397# - No comma, optional type comment, must be followed by close paren
398# The latter form is for a final parameter without trailing comma.
399#
400param_no_default[arg_ty]:
401 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
402 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
403param_with_default[NameDefaultPair*]:
404 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
405 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
406param_maybe_default[NameDefaultPair*]:
407 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
408 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200409param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700410
411annotation[expr_ty]: ':' a=expression { a }
412default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100413
Pablo Galindoa5634c42020-09-16 19:42:00 +0100414decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100415
416class_def[stmt_ty]:
417 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
418 | class_def_raw
419class_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000420 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200421 _PyAST_ClassDef(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100422 (b) ? ((expr_ty) b)->v.Call.args : NULL,
423 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
424 c, NULL, EXTRA) }
425
Pablo Galindoa5634c42020-09-16 19:42:00 +0100426block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100427 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000428 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100429 | invalid_block
430
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100431star_expressions[expr_ty]:
432 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200433 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
434 | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100435 | star_expression
436star_expression[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200437 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100438 | expression
439
Pablo Galindoa5634c42020-09-16 19:42:00 +0100440star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100441star_named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200442 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100443 | named_expression
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100444
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100445named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200446 | 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 +0100447 | invalid_named_expression
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100448 | expression !':='
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100449
450annotated_rhs[expr_ty]: yield_expr | star_expressions
451
452expressions[expr_ty]:
453 | a=expression b=(',' c=expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200454 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
455 | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100456 | expression
457expression[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200458 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100459 | disjunction
460 | lambdef
461
462lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300463 | 'lambda' a=[lambda_params] ':' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200464 _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100465
466lambda_params[arguments_ty]:
467 | invalid_lambda_parameters
468 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700469
470# lambda_parameters etc. duplicates parameters but without annotations
471# or type comments, and if there's no comma after a parameter, we expect
472# a colon, not a close parenthesis. (For more, see parameters above.)
473#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100474lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100475 | 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 +0100476 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700477 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100478 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100479 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100480 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700481 | 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 +0100482 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700483
Pablo Galindoa5634c42020-09-16 19:42:00 +0100484lambda_slash_no_default[asdl_arg_seq*]:
485 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
486 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700487lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100488 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
489 | 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 -0700490
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100491lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700492 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100493 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700494 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100495 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700496 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300497 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700498
499lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
500
501lambda_param_no_default[arg_ty]:
502 | a=lambda_param ',' { a }
503 | a=lambda_param &':' { a }
504lambda_param_with_default[NameDefaultPair*]:
505 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
506 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
507lambda_param_maybe_default[NameDefaultPair*]:
508 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
509 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200510lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100511
512disjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200513 | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100514 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300515 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100516 EXTRA) }
517 | conjunction
518conjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200519 | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100520 And,
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 | inversion
524inversion[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200525 | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100526 | comparison
527comparison[expr_ty]:
528 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200529 _PyAST_Compare(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300530 a,
531 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
532 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
533 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100534 | bitwise_or
535compare_op_bitwise_or_pair[CmpopExprPair*]:
536 | eq_bitwise_or
537 | noteq_bitwise_or
538 | lte_bitwise_or
539 | lt_bitwise_or
540 | gte_bitwise_or
541 | gt_bitwise_or
542 | notin_bitwise_or
543 | in_bitwise_or
544 | isnot_bitwise_or
545 | is_bitwise_or
546eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100547noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000548 | (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 +0100549lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
550lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
551gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
552gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
553notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
554in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
555isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
556is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
557
558bitwise_or[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200559 | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100560 | bitwise_xor
561bitwise_xor[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200562 | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100563 | bitwise_and
564bitwise_and[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200565 | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100566 | shift_expr
567shift_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200568 | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
569 | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100570 | sum
571
572sum[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200573 | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
574 | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100575 | term
576term[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200577 | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) }
578 | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) }
579 | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
580 | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
581 | 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 +0100582 | factor
583factor[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200584 | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
585 | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
586 | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100587 | power
588power[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200589 | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100590 | await_primary
591await_primary[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200592 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100593 | primary
594primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200595 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200596 | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
597 | 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 +0100598 | a=primary '(' b=[arguments] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200599 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100600 (b) ? ((expr_ty) b)->v.Call.args : NULL,
601 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
602 EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200603 | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100604 | atom
605
606slices[expr_ty]:
607 | a=slice !',' { a }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200608 | a[asdl_expr_seq*]=','.slice+ [','] { _PyAST_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100609slice[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200610 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200611 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100612atom[expr_ty]:
613 | NAME
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200614 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
615 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
616 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100617 | &STRING strings
618 | NUMBER
619 | &'(' (tuple | group | genexp)
620 | &'[' (list | listcomp)
621 | &'{' (dict | set | dictcomp | setcomp)
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200622 | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100623
624strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
625list[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200626 | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100627listcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200628 | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100629 | invalid_comprehension
630tuple[expr_ty]:
631 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200632 _PyAST_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300633group[expr_ty]:
634 | '(' a=(yield_expr | named_expression) ')' { a }
635 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100636genexp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200637 | '(' a=named_expression b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100638 | invalid_comprehension
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200639set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100640setcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200641 | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100642 | invalid_comprehension
643dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300644 | '{' a=[double_starred_kvpairs] '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200645 _PyAST_Dict(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300646 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
647 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
648 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100649dictcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200650 | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300651 | invalid_dict_comprehension
652double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
653double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100654 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300655 | kvpair
656kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100657for_if_clauses[asdl_comprehension_seq*]:
658 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300659for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100660 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200661 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100662 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200663 _PyAST_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300664 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100665
666yield_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200667 | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
668 | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100669
670arguments[expr_ty] (memo):
671 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200672 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100673args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100674 | a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200675 | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300676 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
677 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100678 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100679kwargs[asdl_seq*]:
680 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
681 | ','.kwarg_or_starred+
682 | ','.kwarg_or_double_starred+
683starred_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200684 | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100685kwarg_or_starred[KeywordOrStarred*]:
686 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200687 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100688 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300689 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100690kwarg_or_double_starred[KeywordOrStarred*]:
691 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200692 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
693 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300694 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100695
696# NOTE: star_targets may contain *bitwise_or, targets may not.
697star_targets[expr_ty]:
698 | a=star_target !',' { a }
699 | a=star_target b=(',' c=star_target { c })* [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200700 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200701star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
702star_targets_tuple_seq[asdl_expr_seq*]:
703 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
704 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100705star_target[expr_ty] (memo):
706 | '*' a=(!'*' star_target) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200707 _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200708 | target_with_star_atom
709target_with_star_atom[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200710 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
711 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100712 | star_atom
713star_atom[expr_ty]:
714 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200715 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200716 | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) }
717 | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100718
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300719single_target[expr_ty]:
720 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100721 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300722 | '(' a=single_target ')' { a }
723single_subscript_attribute_target[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200724 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
725 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100726
Pablo Galindoa5634c42020-09-16 19:42:00 +0100727del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100728del_target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200729 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) }
730 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100731 | del_t_atom
732del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300733 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100734 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200735 | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) }
736 | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100737
Pablo Galindoa5634c42020-09-16 19:42:00 +0100738targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100739target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200740 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
741 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100742 | t_atom
743t_primary[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200744 | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
745 | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300746 | a=t_primary b=genexp &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200747 _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100748 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200749 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100750 (b) ? ((expr_ty) b)->v.Call.args : NULL,
751 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
752 EXTRA) }
753 | a=atom &t_lookahead { a }
754t_lookahead: '(' | '[' | '.'
755t_atom[expr_ty]:
756 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
757 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200758 | '(' b=[targets] ')' { _PyAST_Tuple(b, Store, EXTRA) }
759 | '[' b=[targets] ']' { _PyAST_List(b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100760
761
762# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200763invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100764 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300765 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
766 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300767 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
768 | args ',' a=expression for_if_clauses {
769 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100770 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300771invalid_kwarg:
Pablo Galindo43c4fb62020-12-13 16:46:48 +0000772 | expression a='=' {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300773 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
774 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100775invalid_named_expression:
776 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300777 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
778 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100779 | a=NAME b='=' bitwise_or !('='|':='|',') {
780 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
781 | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':='|',') {
782 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
783 _PyPegen_get_expr_name(a)) }
784
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100785invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300786 | a=invalid_ann_assign_target ':' expression {
787 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
788 a,
789 "only single target (not %s) can be annotated",
790 _PyPegen_get_expr_name(a)
791 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300792 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300793 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300794 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300795 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100796 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300797 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100798 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100799 | a=star_expressions augassign (yield_expr | star_expressions) {
Brandt Bucher145bf262021-02-26 14:51:55 -0800800 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300801 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100802 "'%s' is an illegal expression for augmented assignment",
803 _PyPegen_get_expr_name(a)
804 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300805invalid_ann_assign_target[expr_ty]:
806 | list
807 | tuple
808 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300809invalid_del_stmt:
810 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300811 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100812invalid_block:
813 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200814invalid_primary:
815 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100816invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300817 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
818 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000819 | ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
Pablo Galindo835f14f2021-01-31 22:52:56 +0000820 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300821invalid_dict_comprehension:
822 | '{' a='**' bitwise_or for_if_clauses '}' {
823 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100824invalid_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200825 | param_no_default* invalid_parameters_helper param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100826 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200827invalid_parameters_helper: # This is only there to avoid type errors
828 | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
829 | param_with_default+
Pablo Galindoc6483c92020-06-10 14:07:06 +0100830invalid_lambda_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200831 | lambda_param_no_default* invalid_lambda_parameters_helper lambda_param_no_default {
Pablo Galindoc6483c92020-06-10 14:07:06 +0100832 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200833invalid_lambda_parameters_helper:
834 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
835 | lambda_param_with_default+
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300836invalid_star_etc:
837 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300838 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300839invalid_lambda_star_etc:
840 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700841invalid_double_type_comments:
842 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
843 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300844invalid_with_item:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000845 | expression 'as' a=expression &(',' | ')' | ':') {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300846 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300847
848invalid_for_target:
849 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300850 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300851
852invalid_group:
853 | '(' a=starred_expression ')' {
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100854 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
Pablo Galindo8efad612021-03-24 19:34:17 +0000855 | '(' a='**' expression ')' {
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100856 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300857invalid_import_from_targets:
858 | import_from_as_names ',' {
859 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000860
861invalid_with_stmt:
862 | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
863 | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
Pablo Galindo206cbda2021-02-07 18:42:21 +0000864
865invalid_except_block:
866 | 'except' a=expression ',' expressions ['as' NAME ] ':' {
867 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
868 | 'except' expression ['as' NAME ] &&':'
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200869 | 'except' &&':'
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000870
871invalid_match_stmt:
872 | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000873invalid_case_block:
874 | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100875invalid_if_stmt:
876 | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
877invalid_elif_stmt:
878 | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
879invalid_while_stmt:
880 | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }