blob: 9f4709491469d8afe5a23108851fbb8ef5e70636 [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]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300165 | 'if' a=named_expression ':' b=block c=elif_stmt {
166 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100167 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
168elif_stmt[stmt_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300169 | 'elif' a=named_expression ':' b=block c=elif_stmt {
170 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100171 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100172else_block[asdl_stmt_seq*]: 'else' ':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100173
174while_stmt[stmt_ty]:
175 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
176
177for_stmt[stmt_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300178 | '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) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300180 | 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 Galindoc5fc1562020-04-22 23:29:27 +0100193with_item[withitem_ty]:
Batuhan Taskaya48f305f2020-10-09 12:56:48 +0300194 | e=expression 'as' t=star_target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300195 | invalid_with_item
196 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100197
198try_stmt[stmt_ty]:
199 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100200 | '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 +0100201except_block[excepthandler_ty]:
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300202 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100203 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
204 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100205finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100206
207return_stmt[stmt_ty]:
208 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
209
210raise_stmt[stmt_ty]:
211 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
212 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
213
214function_def[stmt_ty]:
215 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
216 | function_def_raw
217
218function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300219 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
220 _Py_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300221 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300222 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
223 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
224 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300225 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300226 5,
227 "Async functions are",
228 _Py_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300229 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300230 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
231 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100232func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700233 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
234 | invalid_double_type_comments
235 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100236
237params[arguments_ty]:
238 | invalid_parameters
239 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700240
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100241parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100242 | 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 +0100243 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700244 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100245 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100246 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100247 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700248 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100249 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700250
251# Some duplication here because we can't write (',' | &')'),
252# which is because we don't support empty alternatives (yet).
253#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100254slash_no_default[asdl_arg_seq*]:
255 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
256 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700257slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100258 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
259 | 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 -0700260
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100261star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700262 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100263 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700264 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100265 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700266 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300267 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700268
Guido van Rossum3941d972020-05-01 09:42:03 -0700269kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700270
271# One parameter. This *includes* a following comma and type comment.
272#
273# There are three styles:
274# - No default
275# - With default
276# - Maybe with default
277#
278# There are two alternative forms of each, to deal with type comments:
279# - Ends in a comma followed by an optional type comment
280# - No comma, optional type comment, must be followed by close paren
281# The latter form is for a final parameter without trailing comma.
282#
283param_no_default[arg_ty]:
284 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
285 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
286param_with_default[NameDefaultPair*]:
287 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
288 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
289param_maybe_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[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
293
294annotation[expr_ty]: ':' a=expression { a }
295default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100296
Pablo Galindoa5634c42020-09-16 19:42:00 +0100297decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100298
299class_def[stmt_ty]:
300 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
301 | class_def_raw
302class_def_raw[stmt_ty]:
303 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
304 _Py_ClassDef(a->v.Name.id,
305 (b) ? ((expr_ty) b)->v.Call.args : NULL,
306 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
307 c, NULL, EXTRA) }
308
Pablo Galindoa5634c42020-09-16 19:42:00 +0100309block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100310 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000311 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100312 | invalid_block
313
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100314star_expressions[expr_ty]:
315 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300316 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
317 | a=star_expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100318 | star_expression
319star_expression[expr_ty] (memo):
320 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
321 | expression
322
Pablo Galindoa5634c42020-09-16 19:42:00 +0100323star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100324star_named_expression[expr_ty]:
325 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
326 | named_expression
327named_expression[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300328 | 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 +0100329 | expression !':='
330 | invalid_named_expression
331
332annotated_rhs[expr_ty]: yield_expr | star_expressions
333
334expressions[expr_ty]:
335 | a=expression b=(',' c=expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300336 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
337 | a=expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100338 | expression
339expression[expr_ty] (memo):
340 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
341 | disjunction
342 | lambdef
343
344lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300345 | 'lambda' a=[lambda_params] ':' b=expression {
346 _Py_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100347
348lambda_params[arguments_ty]:
349 | invalid_lambda_parameters
350 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700351
352# lambda_parameters etc. duplicates parameters but without annotations
353# or type comments, and if there's no comma after a parameter, we expect
354# a colon, not a close parenthesis. (For more, see parameters above.)
355#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100356lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100357 | 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 +0100358 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700359 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100360 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100361 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100362 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700363 | 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 +0100364 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700365
Pablo Galindoa5634c42020-09-16 19:42:00 +0100366lambda_slash_no_default[asdl_arg_seq*]:
367 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
368 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700369lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100370 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
371 | 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 -0700372
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100373lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700374 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100375 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700376 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100377 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700378 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300379 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700380
381lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
382
383lambda_param_no_default[arg_ty]:
384 | a=lambda_param ',' { a }
385 | a=lambda_param &':' { a }
386lambda_param_with_default[NameDefaultPair*]:
387 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
388 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
389lambda_param_maybe_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[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100393
394disjunction[expr_ty] (memo):
395 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
396 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300397 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100398 EXTRA) }
399 | conjunction
400conjunction[expr_ty] (memo):
401 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
402 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300403 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100404 EXTRA) }
405 | inversion
406inversion[expr_ty] (memo):
407 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
408 | comparison
409comparison[expr_ty]:
410 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300411 _Py_Compare(
412 a,
413 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
414 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
415 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100416 | bitwise_or
417compare_op_bitwise_or_pair[CmpopExprPair*]:
418 | eq_bitwise_or
419 | noteq_bitwise_or
420 | lte_bitwise_or
421 | lt_bitwise_or
422 | gte_bitwise_or
423 | gt_bitwise_or
424 | notin_bitwise_or
425 | in_bitwise_or
426 | isnot_bitwise_or
427 | is_bitwise_or
428eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100429noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000430 | (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 +0100431lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
432lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
433gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
434gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
435notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
436in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
437isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
438is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
439
440bitwise_or[expr_ty]:
441 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
442 | bitwise_xor
443bitwise_xor[expr_ty]:
444 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
445 | bitwise_and
446bitwise_and[expr_ty]:
447 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
448 | shift_expr
449shift_expr[expr_ty]:
450 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
451 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
452 | sum
453
454sum[expr_ty]:
455 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
456 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
457 | term
458term[expr_ty]:
459 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
460 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
461 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
462 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300463 | 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 +0100464 | factor
465factor[expr_ty] (memo):
466 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
467 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
468 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
469 | power
470power[expr_ty]:
471 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
472 | await_primary
473await_primary[expr_ty] (memo):
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300474 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100475 | primary
476primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200477 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100478 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300479 | 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 +0100480 | a=primary '(' b=[arguments] ')' {
481 _Py_Call(a,
482 (b) ? ((expr_ty) b)->v.Call.args : NULL,
483 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
484 EXTRA) }
485 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
486 | atom
487
488slices[expr_ty]:
489 | a=slice !',' { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100490 | a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100491slice[expr_ty]:
492 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200493 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100494atom[expr_ty]:
495 | NAME
496 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
497 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
498 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100499 | &STRING strings
500 | NUMBER
501 | &'(' (tuple | group | genexp)
502 | &'[' (list | listcomp)
503 | &'{' (dict | set | dictcomp | setcomp)
504 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
505
506strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
507list[expr_ty]:
508 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
509listcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300510 | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100511 | invalid_comprehension
512tuple[expr_ty]:
513 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
514 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300515group[expr_ty]:
516 | '(' a=(yield_expr | named_expression) ')' { a }
517 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100518genexp[expr_ty]:
Lysandros Nikolaoucb3e5ed2020-11-17 01:08:35 +0200519 | '(' a=named_expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100520 | invalid_comprehension
Pablo Galindob0aba1f2020-11-17 01:17:12 +0000521set[expr_ty]: '{' a=star_named_expressions '}' { _Py_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100522setcomp[expr_ty]:
Pablo Galindob0aba1f2020-11-17 01:17:12 +0000523 | '{' a=named_expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100524 | invalid_comprehension
525dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300526 | '{' a=[double_starred_kvpairs] '}' {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300527 _Py_Dict(
528 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
529 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
530 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100531dictcomp[expr_ty]:
532 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300533 | invalid_dict_comprehension
534double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
535double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100536 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300537 | kvpair
538kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100539for_if_clauses[asdl_comprehension_seq*]:
540 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300541for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100542 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300543 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100544 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300545 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300546 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100547
548yield_expr[expr_ty]:
549 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
550 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
551
552arguments[expr_ty] (memo):
553 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200554 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100555args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100556 | 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 +0100557 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300558 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
559 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100560 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100561kwargs[asdl_seq*]:
562 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
563 | ','.kwarg_or_starred+
564 | ','.kwarg_or_double_starred+
565starred_expression[expr_ty]:
566 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
567kwarg_or_starred[KeywordOrStarred*]:
568 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300569 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100570 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300571 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100572kwarg_or_double_starred[KeywordOrStarred*]:
573 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300574 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
575 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300576 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100577
578# NOTE: star_targets may contain *bitwise_or, targets may not.
579star_targets[expr_ty]:
580 | a=star_target !',' { a }
581 | a=star_target b=(',' c=star_target { c })* [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300582 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100583star_targets_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100584star_target[expr_ty] (memo):
585 | '*' a=(!'*' star_target) {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300586 _Py_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100587 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
588 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
589 | star_atom
590star_atom[expr_ty]:
591 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
592 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
593 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
594 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
595
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300596single_target[expr_ty]:
597 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100598 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300599 | '(' a=single_target ')' { a }
600single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100601 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
602 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
603
Pablo Galindoa5634c42020-09-16 19:42:00 +0100604del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100605del_target[expr_ty] (memo):
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300606 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
607 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100608 | del_t_atom
609del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300610 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100611 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
612 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
613 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
614
Pablo Galindoa5634c42020-09-16 19:42:00 +0100615targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100616target[expr_ty] (memo):
617 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
618 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
619 | t_atom
620t_primary[expr_ty]:
621 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
622 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300623 | a=t_primary b=genexp &t_lookahead {
624 _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100625 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
626 _Py_Call(a,
627 (b) ? ((expr_ty) b)->v.Call.args : NULL,
628 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
629 EXTRA) }
630 | a=atom &t_lookahead { a }
631t_lookahead: '(' | '[' | '.'
632t_atom[expr_ty]:
633 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
634 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
635 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
636 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
637
638
639# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200640invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100641 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300642 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
643 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300644 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
645 | args ',' a=expression for_if_clauses {
646 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100647 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300648invalid_kwarg:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300649 | a=expression '=' {
650 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
651 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100652invalid_named_expression:
653 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300654 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
655 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100656invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300657 | a=invalid_ann_assign_target ':' expression {
658 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
659 a,
660 "only single target (not %s) can be annotated",
661 _PyPegen_get_expr_name(a)
662 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300663 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300664 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300665 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300666 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100667 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300668 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100669 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100670 | a=star_expressions augassign (yield_expr | star_expressions) {
671 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300672 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100673 "'%s' is an illegal expression for augmented assignment",
674 _PyPegen_get_expr_name(a)
675 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300676invalid_ann_assign_target[expr_ty]:
677 | list
678 | tuple
679 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300680invalid_del_stmt:
681 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300682 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100683invalid_block:
684 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200685invalid_primary:
686 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100687invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300688 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
689 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300690invalid_dict_comprehension:
691 | '{' a='**' bitwise_or for_if_clauses '}' {
692 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100693invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700694 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100695 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100696invalid_lambda_parameters:
697 | lambda_param_no_default* (lambda_slash_with_default | lambda_param_with_default+) lambda_param_no_default {
698 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300699invalid_star_etc:
700 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300701 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300702invalid_lambda_star_etc:
703 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700704invalid_double_type_comments:
705 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
706 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300707invalid_with_item:
708 | expression 'as' a=expression {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300709 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300710
711invalid_for_target:
712 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300713 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300714
715invalid_group:
716 | '(' a=starred_expression ')' {
717 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300718invalid_import_from_targets:
719 | import_from_as_names ',' {
720 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }