blob: bb70bbb565d3216556b59c463322e4ef6ddd92c5 [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) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010031interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) }
32eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }
Guido van Rossumc001c092020-04-30 12:12:19 -070033func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { 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
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030059 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _Py_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
68 | e=star_expressions { _Py_Expr(e, EXTRA) }
69 | &'return' return_stmt
70 | &('import' | 'from') import_stmt
71 | &'raise' raise_stmt
72 | 'pass' { _Py_Pass(EXTRA) }
73 | &'del' del_stmt
74 | &'yield' yield_stmt
75 | &'assert' assert_stmt
76 | 'break' { _Py_Break(EXTRA) }
77 | 'continue' { _Py_Continue(EXTRA) }
78 | &'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
88
89# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030090assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010091 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030092 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030093 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030094 6,
95 "Variable annotation syntax is",
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030096 _Py_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030097 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030098 | a=('(' b=single_target ')' { b }
99 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300100 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100101 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Guido van Rossumc001c092020-04-30 12:12:19 -0700102 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300103 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100104 _Py_AugAssign(a, b->kind, c, EXTRA) }
105 | invalid_assignment
106
107augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300108 | '+=' { _PyPegen_augoperator(p, Add) }
109 | '-=' { _PyPegen_augoperator(p, Sub) }
110 | '*=' { _PyPegen_augoperator(p, Mult) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300111 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300112 | '/=' { _PyPegen_augoperator(p, Div) }
113 | '%=' { _PyPegen_augoperator(p, Mod) }
114 | '&=' { _PyPegen_augoperator(p, BitAnd) }
115 | '|=' { _PyPegen_augoperator(p, BitOr) }
116 | '^=' { _PyPegen_augoperator(p, BitXor) }
117 | '<<=' { _PyPegen_augoperator(p, LShift) }
118 | '>>=' { _PyPegen_augoperator(p, RShift) }
119 | '**=' { _PyPegen_augoperator(p, Pow) }
120 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100121
Pablo Galindoa5634c42020-09-16 19:42:00 +0100122global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300123 _Py_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100124nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300125 _Py_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100126
127yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
128
129assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
130
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300131del_stmt[stmt_ty]:
132 | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) }
133 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100134
135import_stmt[stmt_ty]: import_name | import_from
136import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
137# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
138import_from[stmt_ty]:
139 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
140 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
141 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
142 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100143import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100144 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300145 | import_from_as_names !','
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300146 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300147 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100148import_from_as_names[asdl_alias_seq*]:
149 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100150import_from_as_name[alias_ty]:
151 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
152 (b) ? ((expr_ty) b)->v.Name.id : NULL,
153 p->arena) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100154dotted_as_names[asdl_alias_seq*]:
155 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100156dotted_as_name[alias_ty]:
157 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
158 (b) ? ((expr_ty) b)->v.Name.id : NULL,
159 p->arena) }
160dotted_name[expr_ty]:
161 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
162 | NAME
163
164if_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000165 | 'if' a=named_expression &&':' b=block c=elif_stmt {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300166 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000167 | 'if' a=named_expression &&':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100168elif_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000169 | 'elif' a=named_expression &&':' b=block c=elif_stmt {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300170 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000171 | 'elif' a=named_expression &&':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
172else_block[asdl_stmt_seq*]: 'else' &&':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100173
174while_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000175 | 'while' a=named_expression &&':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100176
177for_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000178 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300179 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000180 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300181 CHECK_VERSION(stmt_ty, 5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300182 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100183
184with_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100185 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300186 _Py_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100187 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300188 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100189 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300190 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100191 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300192 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000193 | invalid_with_stmt
194
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100195with_item[withitem_ty]:
Batuhan Taskaya48f305f2020-10-09 12:56:48 +0300196 | e=expression 'as' t=star_target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300197 | invalid_with_item
198 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100199
200try_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000201 | 'try' &&':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
202 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100203except_block[excepthandler_ty]:
Pablo Galindo206cbda2021-02-07 18:42:21 +0000204 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100205 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
Pablo Galindo206cbda2021-02-07 18:42:21 +0000206 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
207 | invalid_except_block
Pablo Galindoa5634c42020-09-16 19:42:00 +0100208finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100209
210return_stmt[stmt_ty]:
211 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
212
213raise_stmt[stmt_ty]:
214 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
215 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
216
217function_def[stmt_ty]:
218 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
219 | function_def_raw
220
221function_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000222 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300223 _Py_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300224 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300225 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000226 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300227 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300228 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300229 5,
230 "Async functions are",
231 _Py_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300232 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300233 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
234 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100235func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700236 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
237 | invalid_double_type_comments
238 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100239
240params[arguments_ty]:
241 | invalid_parameters
242 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700243
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100244parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100245 | 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 +0100246 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700247 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100248 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100249 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100250 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700251 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100252 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700253
254# Some duplication here because we can't write (',' | &')'),
255# which is because we don't support empty alternatives (yet).
256#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100257slash_no_default[asdl_arg_seq*]:
258 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
259 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700260slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100261 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
262 | 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 -0700263
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100264star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700265 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100266 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700267 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100268 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700269 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300270 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700271
Guido van Rossum3941d972020-05-01 09:42:03 -0700272kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700273
274# One parameter. This *includes* a following comma and type comment.
275#
276# There are three styles:
277# - No default
278# - With default
279# - Maybe with default
280#
281# There are two alternative forms of each, to deal with type comments:
282# - Ends in a comma followed by an optional type comment
283# - No comma, optional type comment, must be followed by close paren
284# The latter form is for a final parameter without trailing comma.
285#
286param_no_default[arg_ty]:
287 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
288 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
289param_with_default[NameDefaultPair*]:
290 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
291 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
292param_maybe_default[NameDefaultPair*]:
293 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
294 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
295param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
296
297annotation[expr_ty]: ':' a=expression { a }
298default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100299
Pablo Galindoa5634c42020-09-16 19:42:00 +0100300decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100301
302class_def[stmt_ty]:
303 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
304 | class_def_raw
305class_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000306 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100307 _Py_ClassDef(a->v.Name.id,
308 (b) ? ((expr_ty) b)->v.Call.args : NULL,
309 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
310 c, NULL, EXTRA) }
311
Pablo Galindoa5634c42020-09-16 19:42:00 +0100312block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100313 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000314 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100315 | invalid_block
316
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100317star_expressions[expr_ty]:
318 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300319 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
320 | a=star_expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100321 | star_expression
322star_expression[expr_ty] (memo):
323 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
324 | expression
325
Pablo Galindoa5634c42020-09-16 19:42:00 +0100326star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100327star_named_expression[expr_ty]:
328 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
329 | named_expression
330named_expression[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300331 | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100332 | expression !':='
333 | invalid_named_expression
334
335annotated_rhs[expr_ty]: yield_expr | star_expressions
336
337expressions[expr_ty]:
338 | a=expression b=(',' c=expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300339 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
340 | a=expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100341 | expression
342expression[expr_ty] (memo):
343 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
344 | disjunction
345 | lambdef
346
347lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300348 | 'lambda' a=[lambda_params] ':' b=expression {
349 _Py_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100350
351lambda_params[arguments_ty]:
352 | invalid_lambda_parameters
353 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700354
355# lambda_parameters etc. duplicates parameters but without annotations
356# or type comments, and if there's no comma after a parameter, we expect
357# a colon, not a close parenthesis. (For more, see parameters above.)
358#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100359lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100360 | 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 +0100361 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700362 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_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*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100365 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700366 | 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 +0100367 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700368
Pablo Galindoa5634c42020-09-16 19:42:00 +0100369lambda_slash_no_default[asdl_arg_seq*]:
370 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
371 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700372lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100373 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
374 | 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 -0700375
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100376lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700377 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100378 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700379 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100380 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700381 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300382 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700383
384lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
385
386lambda_param_no_default[arg_ty]:
387 | a=lambda_param ',' { a }
388 | a=lambda_param &':' { a }
389lambda_param_with_default[NameDefaultPair*]:
390 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
391 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
392lambda_param_maybe_default[NameDefaultPair*]:
393 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
394 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
395lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100396
397disjunction[expr_ty] (memo):
398 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
399 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300400 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100401 EXTRA) }
402 | conjunction
403conjunction[expr_ty] (memo):
404 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
405 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300406 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100407 EXTRA) }
408 | inversion
409inversion[expr_ty] (memo):
410 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
411 | comparison
412comparison[expr_ty]:
413 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300414 _Py_Compare(
415 a,
416 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
417 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
418 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100419 | bitwise_or
420compare_op_bitwise_or_pair[CmpopExprPair*]:
421 | eq_bitwise_or
422 | noteq_bitwise_or
423 | lte_bitwise_or
424 | lt_bitwise_or
425 | gte_bitwise_or
426 | gt_bitwise_or
427 | notin_bitwise_or
428 | in_bitwise_or
429 | isnot_bitwise_or
430 | is_bitwise_or
431eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100432noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000433 | (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 +0100434lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
435lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
436gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
437gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
438notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
439in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
440isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
441is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
442
443bitwise_or[expr_ty]:
444 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
445 | bitwise_xor
446bitwise_xor[expr_ty]:
447 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
448 | bitwise_and
449bitwise_and[expr_ty]:
450 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
451 | shift_expr
452shift_expr[expr_ty]:
453 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
454 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
455 | sum
456
457sum[expr_ty]:
458 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
459 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
460 | term
461term[expr_ty]:
462 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
463 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
464 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
465 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300466 | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100467 | factor
468factor[expr_ty] (memo):
469 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
470 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
471 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
472 | power
473power[expr_ty]:
474 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
475 | await_primary
476await_primary[expr_ty] (memo):
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300477 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100478 | primary
479primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200480 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100481 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300482 | a=primary b=genexp { _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100483 | a=primary '(' b=[arguments] ')' {
484 _Py_Call(a,
485 (b) ? ((expr_ty) b)->v.Call.args : NULL,
486 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
487 EXTRA) }
488 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
489 | atom
490
491slices[expr_ty]:
492 | a=slice !',' { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100493 | a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100494slice[expr_ty]:
495 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200496 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100497atom[expr_ty]:
498 | NAME
499 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
500 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
501 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100502 | &STRING strings
503 | NUMBER
504 | &'(' (tuple | group | genexp)
505 | &'[' (list | listcomp)
506 | &'{' (dict | set | dictcomp | setcomp)
507 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
508
509strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
510list[expr_ty]:
511 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
512listcomp[expr_ty]:
Pablo Galindo835f14f2021-01-31 22:52:56 +0000513 | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100514 | invalid_comprehension
515tuple[expr_ty]:
516 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
517 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300518group[expr_ty]:
519 | '(' a=(yield_expr | named_expression) ')' { a }
520 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100521genexp[expr_ty]:
Pablo Galindo835f14f2021-01-31 22:52:56 +0000522 | '(' a=named_expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100523 | invalid_comprehension
Pablo Galindob0aba1f2020-11-17 01:17:12 +0000524set[expr_ty]: '{' a=star_named_expressions '}' { _Py_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100525setcomp[expr_ty]:
Pablo Galindo835f14f2021-01-31 22:52:56 +0000526 | '{' a=named_expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100527 | invalid_comprehension
528dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300529 | '{' a=[double_starred_kvpairs] '}' {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300530 _Py_Dict(
531 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
532 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
533 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100534dictcomp[expr_ty]:
535 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300536 | invalid_dict_comprehension
537double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
538double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100539 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300540 | kvpair
541kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100542for_if_clauses[asdl_comprehension_seq*]:
543 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300544for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100545 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300546 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100547 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300548 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300549 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100550
551yield_expr[expr_ty]:
552 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
553 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
554
555arguments[expr_ty] (memo):
556 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200557 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100558args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100559 | a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100560 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300561 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
562 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100563 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100564kwargs[asdl_seq*]:
565 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
566 | ','.kwarg_or_starred+
567 | ','.kwarg_or_double_starred+
568starred_expression[expr_ty]:
569 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
570kwarg_or_starred[KeywordOrStarred*]:
571 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300572 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100573 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300574 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100575kwarg_or_double_starred[KeywordOrStarred*]:
576 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300577 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
578 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300579 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100580
581# NOTE: star_targets may contain *bitwise_or, targets may not.
582star_targets[expr_ty]:
583 | a=star_target !',' { a }
584 | a=star_target b=(',' c=star_target { c })* [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300585 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200586star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
587star_targets_tuple_seq[asdl_expr_seq*]:
588 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
589 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100590star_target[expr_ty] (memo):
591 | '*' a=(!'*' star_target) {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300592 _Py_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200593 | target_with_star_atom
594target_with_star_atom[expr_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100595 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
596 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
597 | star_atom
598star_atom[expr_ty]:
599 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200600 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
601 | '(' a=[star_targets_tuple_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
602 | '[' a=[star_targets_list_seq] ']' { _Py_List(a, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100603
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300604single_target[expr_ty]:
605 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100606 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300607 | '(' a=single_target ')' { a }
608single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100609 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
610 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
611
Pablo Galindoa5634c42020-09-16 19:42:00 +0100612del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100613del_target[expr_ty] (memo):
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300614 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
615 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100616 | del_t_atom
617del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300618 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100619 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
620 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
621 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
622
Pablo Galindoa5634c42020-09-16 19:42:00 +0100623targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100624target[expr_ty] (memo):
625 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
626 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
627 | t_atom
628t_primary[expr_ty]:
629 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
630 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300631 | a=t_primary b=genexp &t_lookahead {
632 _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100633 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
634 _Py_Call(a,
635 (b) ? ((expr_ty) b)->v.Call.args : NULL,
636 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
637 EXTRA) }
638 | a=atom &t_lookahead { a }
639t_lookahead: '(' | '[' | '.'
640t_atom[expr_ty]:
641 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
642 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
643 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
644 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
645
646
647# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200648invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100649 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300650 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
651 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300652 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
653 | args ',' a=expression for_if_clauses {
654 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100655 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300656invalid_kwarg:
Pablo Galindo43c4fb62020-12-13 16:46:48 +0000657 | expression a='=' {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300658 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
659 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100660invalid_named_expression:
661 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300662 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
663 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100664invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300665 | a=invalid_ann_assign_target ':' expression {
666 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
667 a,
668 "only single target (not %s) can be annotated",
669 _PyPegen_get_expr_name(a)
670 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300671 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300672 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300673 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300674 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100675 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300676 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100677 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100678 | a=star_expressions augassign (yield_expr | star_expressions) {
679 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300680 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100681 "'%s' is an illegal expression for augmented assignment",
682 _PyPegen_get_expr_name(a)
683 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300684invalid_ann_assign_target[expr_ty]:
685 | list
686 | tuple
687 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300688invalid_del_stmt:
689 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300690 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100691invalid_block:
692 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200693invalid_primary:
694 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100695invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300696 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
697 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000698 | ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
Pablo Galindo835f14f2021-01-31 22:52:56 +0000699 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300700invalid_dict_comprehension:
701 | '{' a='**' bitwise_or for_if_clauses '}' {
702 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100703invalid_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200704 | param_no_default* invalid_parameters_helper param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100705 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200706invalid_parameters_helper: # This is only there to avoid type errors
707 | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
708 | param_with_default+
Pablo Galindoc6483c92020-06-10 14:07:06 +0100709invalid_lambda_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200710 | lambda_param_no_default* invalid_lambda_parameters_helper lambda_param_no_default {
Pablo Galindoc6483c92020-06-10 14:07:06 +0100711 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200712invalid_lambda_parameters_helper:
713 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
714 | lambda_param_with_default+
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300715invalid_star_etc:
716 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300717 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300718invalid_lambda_star_etc:
719 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700720invalid_double_type_comments:
721 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
722 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300723invalid_with_item:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000724 | expression 'as' a=expression &(',' | ')' | ':') {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300725 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300726
727invalid_for_target:
728 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300729 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300730
731invalid_group:
732 | '(' a=starred_expression ')' {
733 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300734invalid_import_from_targets:
735 | import_from_as_names ',' {
736 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000737
738invalid_with_stmt:
739 | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
740 | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
Pablo Galindo206cbda2021-02-07 18:42:21 +0000741
742invalid_except_block:
743 | 'except' a=expression ',' expressions ['as' NAME ] ':' {
744 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
745 | 'except' expression ['as' NAME ] &&':'
746 | 'except' &&':'