blob: 44d435cc159ea3d83dd145869463537e55077de3 [file] [log] [blame]
Pablo Galindoc5fc1562020-04-22 23:29:27 +01001# Simplified grammar for Python
2
3@bytecode True
4@trailer '''
5void *
6_PyPegen_parse(Parser *p)
7{
8 // Initialize keywords
9 p->keywords = reserved_keywords;
10 p->n_keyword_lists = n_keyword_lists;
11
12 // Run parser
13 void *result = NULL;
14 if (p->start_rule == Py_file_input) {
15 result = file_rule(p);
16 } else if (p->start_rule == Py_single_input) {
17 result = interactive_rule(p);
18 } else if (p->start_rule == Py_eval_input) {
19 result = eval_rule(p);
Guido van Rossumc001c092020-04-30 12:12:19 -070020 } else if (p->start_rule == Py_func_type_input) {
21 result = func_type_rule(p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +010022 } else if (p->start_rule == Py_fstring_input) {
23 result = fstring_rule(p);
24 }
25
26 return result;
27}
28
29// The end
30'''
Guido van Rossumc001c092020-04-30 12:12:19 -070031file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010032interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) }
33eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }
Guido van Rossumc001c092020-04-30 12:12:19 -070034func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { FunctionType(a, b, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010035fstring[expr_ty]: star_expressions
36
Guido van Rossumc001c092020-04-30 12:12:19 -070037# type_expressions allow */** but ignore them
38type_expressions[asdl_seq*]:
39 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
40 _PyPegen_seq_append_to_end(p, CHECK(_PyPegen_seq_append_to_end(p, a, b)), c) }
41 | a=','.expression+ ',' '*' b=expression { _PyPegen_seq_append_to_end(p, a, b) }
42 | a=','.expression+ ',' '**' b=expression { _PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070043 | '*' a=expression ',' '**' b=expression {
44 _PyPegen_seq_append_to_end(p, CHECK(_PyPegen_singleton_seq(p, a)), b) }
45 | '*' a=expression { _PyPegen_singleton_seq(p, a) }
46 | '**' a=expression { _PyPegen_singleton_seq(p, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -070047 | ','.expression+
48
Pablo Galindoc5fc1562020-04-22 23:29:27 +010049statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) }
50statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt
51statement_newline[asdl_seq*]:
52 | a=compound_stmt NEWLINE { _PyPegen_singleton_seq(p, a) }
53 | simple_stmt
54 | NEWLINE { _PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) }
55 | ENDMARKER { _PyPegen_interactive_exit(p) }
56simple_stmt[asdl_seq*]:
57 | a=small_stmt !';' NEWLINE { _PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
58 | a=';'.small_stmt+ [';'] NEWLINE { a }
59# NOTE: assignment MUST precede expression, else parsing a simple assignment
60# will throw a SyntaxError.
61small_stmt[stmt_ty] (memo):
62 | assignment
63 | e=star_expressions { _Py_Expr(e, EXTRA) }
64 | &'return' return_stmt
65 | &('import' | 'from') import_stmt
66 | &'raise' raise_stmt
67 | 'pass' { _Py_Pass(EXTRA) }
68 | &'del' del_stmt
69 | &'yield' yield_stmt
70 | &'assert' assert_stmt
71 | 'break' { _Py_Break(EXTRA) }
72 | 'continue' { _Py_Continue(EXTRA) }
73 | &'global' global_stmt
74 | &'nonlocal' nonlocal_stmt
75compound_stmt[stmt_ty]:
76 | &('def' | '@' | ASYNC) function_def
77 | &'if' if_stmt
78 | &('class' | '@') class_def
79 | &('with' | ASYNC) with_stmt
80 | &('for' | ASYNC) for_stmt
81 | &'try' try_stmt
82 | &'while' while_stmt
83
84# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030085assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010086 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030087 CHECK_VERSION(
88 6,
89 "Variable annotation syntax is",
90 _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
91 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030092 | a=('(' b=single_target ')' { b }
93 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030094 CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
Miss Islington (bot)8df4f392020-06-08 02:22:06 -070095 | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Guido van Rossumc001c092020-04-30 12:12:19 -070096 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +030097 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +010098 _Py_AugAssign(a, b->kind, c, EXTRA) }
99 | invalid_assignment
100
101augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300102 | '+=' { _PyPegen_augoperator(p, Add) }
103 | '-=' { _PyPegen_augoperator(p, Sub) }
104 | '*=' { _PyPegen_augoperator(p, Mult) }
105 | '@=' { CHECK_VERSION(5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
106 | '/=' { _PyPegen_augoperator(p, Div) }
107 | '%=' { _PyPegen_augoperator(p, Mod) }
108 | '&=' { _PyPegen_augoperator(p, BitAnd) }
109 | '|=' { _PyPegen_augoperator(p, BitOr) }
110 | '^=' { _PyPegen_augoperator(p, BitXor) }
111 | '<<=' { _PyPegen_augoperator(p, LShift) }
112 | '>>=' { _PyPegen_augoperator(p, RShift) }
113 | '**=' { _PyPegen_augoperator(p, Pow) }
114 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100115
116global_stmt[stmt_ty]: 'global' a=','.NAME+ {
117 _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
118nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ {
119 _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
120
121yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
122
123assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
124
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300125del_stmt[stmt_ty]:
126 | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) }
127 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100128
129import_stmt[stmt_ty]: import_name | import_from
130import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
131# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
132import_from[stmt_ty]:
133 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
134 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
135 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
136 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
137import_from_targets[asdl_seq*]:
138 | '(' a=import_from_as_names [','] ')' { a }
Pablo Galindo275d7e12020-05-21 22:04:54 +0100139 | import_from_as_names !','
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100140 | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
Pablo Galindo275d7e12020-05-21 22:04:54 +0100141 | invalid_import_from_targets
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100142import_from_as_names[asdl_seq*]:
143 | a=','.import_from_as_name+ { a }
144import_from_as_name[alias_ty]:
145 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
146 (b) ? ((expr_ty) b)->v.Name.id : NULL,
147 p->arena) }
148dotted_as_names[asdl_seq*]:
149 | a=','.dotted_as_name+ { a }
150dotted_as_name[alias_ty]:
151 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
152 (b) ? ((expr_ty) b)->v.Name.id : NULL,
153 p->arena) }
154dotted_name[expr_ty]:
155 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
156 | NAME
157
158if_stmt[stmt_ty]:
159 | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
160 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
161elif_stmt[stmt_ty]:
162 | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
163 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
164else_block[asdl_seq*]: 'else' ':' b=block { b }
165
166while_stmt[stmt_ty]:
167 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
168
169for_stmt[stmt_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300170 | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300171 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300172 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300173 CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300174 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100175
176with_stmt[stmt_ty]:
Pablo Galindo99db2a12020-05-06 22:54:34 +0100177 | 'with' '(' a=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300178 _Py_With(a, b, NULL, EXTRA) }
179 | 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
180 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo99db2a12020-05-06 22:54:34 +0100181 | ASYNC 'with' '(' a=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300182 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
183 | ASYNC 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
184 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100185with_item[withitem_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300186 | e=expression 'as' t=target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
187 | invalid_with_item
188 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100189
190try_stmt[stmt_ty]:
191 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
192 | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
193except_block[excepthandler_ty]:
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300194 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100195 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
196 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
197finally_block[asdl_seq*]: 'finally' ':' a=block { a }
198
199return_stmt[stmt_ty]:
200 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
201
202raise_stmt[stmt_ty]:
203 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
204 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
205
206function_def[stmt_ty]:
207 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
208 | function_def_raw
209
210function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300211 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
212 _Py_FunctionDef(n->v.Name.id,
213 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
214 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
215 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
216 CHECK_VERSION(
217 5,
218 "Async functions are",
219 _Py_AsyncFunctionDef(n->v.Name.id,
220 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
221 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
222 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100223func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700224 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
225 | invalid_double_type_comments
226 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100227
228params[arguments_ty]:
229 | invalid_parameters
230 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700231
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100232parameters[arguments_ty]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700233 | a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100234 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700235 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100236 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700237 | a=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100238 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700239 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100240 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700241
242# Some duplication here because we can't write (',' | &')'),
243# which is because we don't support empty alternatives (yet).
244#
245slash_no_default[asdl_seq*]:
246 | a=param_no_default+ '/' ',' { a }
247 | a=param_no_default+ '/' &')' { a }
248slash_with_default[SlashWithDefault*]:
249 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
250 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, a, b) }
251
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100252star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700253 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100254 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700255 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100256 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700257 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300258 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700259
Guido van Rossum3941d972020-05-01 09:42:03 -0700260kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700261
262# One parameter. This *includes* a following comma and type comment.
263#
264# There are three styles:
265# - No default
266# - With default
267# - Maybe with default
268#
269# There are two alternative forms of each, to deal with type comments:
270# - Ends in a comma followed by an optional type comment
271# - No comma, optional type comment, must be followed by close paren
272# The latter form is for a final parameter without trailing comma.
273#
274param_no_default[arg_ty]:
275 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
276 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
277param_with_default[NameDefaultPair*]:
278 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
279 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
280param_maybe_default[NameDefaultPair*]:
281 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
282 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
283param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
284
285annotation[expr_ty]: ':' a=expression { a }
286default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100287
288decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a }
289
290class_def[stmt_ty]:
291 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
292 | class_def_raw
293class_def_raw[stmt_ty]:
294 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
295 _Py_ClassDef(a->v.Name.id,
296 (b) ? ((expr_ty) b)->v.Call.args : NULL,
297 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
298 c, NULL, EXTRA) }
299
300block[asdl_seq*] (memo):
301 | NEWLINE INDENT a=statements DEDENT { a }
302 | simple_stmt
303 | invalid_block
304
305expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a }
306star_expressions[expr_ty]:
307 | a=star_expression b=(',' c=star_expression { c })+ [','] {
308 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
309 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
310 | star_expression
311star_expression[expr_ty] (memo):
312 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
313 | expression
314
315star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a }
316star_named_expression[expr_ty]:
317 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
318 | named_expression
319named_expression[expr_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300320 | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100321 | expression !':='
322 | invalid_named_expression
323
324annotated_rhs[expr_ty]: yield_expr | star_expressions
325
326expressions[expr_ty]:
327 | a=expression b=(',' c=expression { c })+ [','] {
328 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
329 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
330 | expression
331expression[expr_ty] (memo):
332 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
333 | disjunction
334 | lambdef
335
336lambdef[expr_ty]:
Miss Islington (bot)d55ed7b2020-06-10 06:24:41 -0700337 | 'lambda' a=[lambda_params] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }
338
339lambda_params[arguments_ty]:
340 | invalid_lambda_parameters
341 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700342
343# lambda_parameters etc. duplicates parameters but without annotations
344# or type comments, and if there's no comma after a parameter, we expect
345# a colon, not a close parenthesis. (For more, see parameters above.)
346#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100347lambda_parameters[arguments_ty]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700348 | a=lambda_slash_no_default b=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100349 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700350 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100351 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700352 | a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100353 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700354 | 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 +0100355 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700356
357lambda_slash_no_default[asdl_seq*]:
358 | a=lambda_param_no_default+ '/' ',' { a }
359 | a=lambda_param_no_default+ '/' &':' { a }
360lambda_slash_with_default[SlashWithDefault*]:
361 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
362 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, a, b) }
363
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100364lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700365 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100366 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700367 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100368 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700369 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300370 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700371
372lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
373
374lambda_param_no_default[arg_ty]:
375 | a=lambda_param ',' { a }
376 | a=lambda_param &':' { a }
377lambda_param_with_default[NameDefaultPair*]:
378 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
379 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
380lambda_param_maybe_default[NameDefaultPair*]:
381 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
382 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
383lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100384
385disjunction[expr_ty] (memo):
386 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
387 Or,
388 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
389 EXTRA) }
390 | conjunction
391conjunction[expr_ty] (memo):
392 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
393 And,
394 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
395 EXTRA) }
396 | inversion
397inversion[expr_ty] (memo):
398 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
399 | comparison
400comparison[expr_ty]:
401 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
402 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
403 | bitwise_or
404compare_op_bitwise_or_pair[CmpopExprPair*]:
405 | eq_bitwise_or
406 | noteq_bitwise_or
407 | lte_bitwise_or
408 | lt_bitwise_or
409 | gte_bitwise_or
410 | gt_bitwise_or
411 | notin_bitwise_or
412 | in_bitwise_or
413 | isnot_bitwise_or
414 | is_bitwise_or
415eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100416noteq_bitwise_or[CmpopExprPair*]:
417 | (tok='!=' {_PyPegen_check_barry_as_flufl(p) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100418lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
419lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
420gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
421gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
422notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
423in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
424isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
425is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
426
427bitwise_or[expr_ty]:
428 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
429 | bitwise_xor
430bitwise_xor[expr_ty]:
431 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
432 | bitwise_and
433bitwise_and[expr_ty]:
434 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
435 | shift_expr
436shift_expr[expr_ty]:
437 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
438 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
439 | sum
440
441sum[expr_ty]:
442 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
443 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
444 | term
445term[expr_ty]:
446 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
447 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
448 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
449 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300450 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100451 | factor
452factor[expr_ty] (memo):
453 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
454 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
455 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
456 | power
457power[expr_ty]:
458 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
459 | await_primary
460await_primary[expr_ty] (memo):
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300461 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100462 | primary
463primary[expr_ty]:
464 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
465 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
466 | a=primary '(' b=[arguments] ')' {
467 _Py_Call(a,
468 (b) ? ((expr_ty) b)->v.Call.args : NULL,
469 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
470 EXTRA) }
471 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
472 | atom
473
474slices[expr_ty]:
475 | a=slice !',' { a }
476 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
477slice[expr_ty]:
478 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
479 | a=expression { a }
480atom[expr_ty]:
481 | NAME
482 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
483 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
484 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindo37824972020-06-11 19:29:13 +0100485 | '__peg_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100486 | &STRING strings
487 | NUMBER
488 | &'(' (tuple | group | genexp)
489 | &'[' (list | listcomp)
490 | &'{' (dict | set | dictcomp | setcomp)
491 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
492
493strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
494list[expr_ty]:
495 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
496listcomp[expr_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300497 | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100498 | invalid_comprehension
499tuple[expr_ty]:
500 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
501 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300502group[expr_ty]:
503 | '(' a=(yield_expr | named_expression) ')' { a }
504 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100505genexp[expr_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300506 | '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100507 | invalid_comprehension
508set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
509setcomp[expr_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300510 | '{' a=expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100511 | invalid_comprehension
512dict[expr_ty]:
Miss Islington (bot)d00aaf32020-05-21 15:58:16 -0700513 | '{' a=[double_starred_kvpairs] '}' {
514 _Py_Dict(CHECK(_PyPegen_get_keys(p, a)), CHECK(_PyPegen_get_values(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100515dictcomp[expr_ty]:
516 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Miss Islington (bot)d00aaf32020-05-21 15:58:16 -0700517 | invalid_dict_comprehension
518double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
519double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100520 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Miss Islington (bot)d00aaf32020-05-21 15:58:16 -0700521 | kvpair
522kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100523for_if_clauses[asdl_seq*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300524 | for_if_clause+
525for_if_clause[comprehension_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300526 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300527 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300528 | 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300529 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300530 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100531
532yield_expr[expr_ty]:
533 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
534 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
535
536arguments[expr_ty] (memo):
537 | a=args [','] &')' { a }
538 | incorrect_arguments
539args[expr_ty]:
Pablo Galindobe172952020-09-03 16:35:17 +0100540 | a=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100541 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
542 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
543 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
544 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100545kwargs[asdl_seq*]:
546 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
547 | ','.kwarg_or_starred+
548 | ','.kwarg_or_double_starred+
549starred_expression[expr_ty]:
550 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
551kwarg_or_starred[KeywordOrStarred*]:
552 | a=NAME '=' b=expression {
553 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
554 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300555 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100556kwarg_or_double_starred[KeywordOrStarred*]:
557 | a=NAME '=' b=expression {
558 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
559 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300560 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100561
562# NOTE: star_targets may contain *bitwise_or, targets may not.
563star_targets[expr_ty]:
564 | a=star_target !',' { a }
565 | a=star_target b=(',' c=star_target { c })* [','] {
566 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
567star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
568star_target[expr_ty] (memo):
569 | '*' a=(!'*' star_target) {
570 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
571 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
572 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
573 | star_atom
574star_atom[expr_ty]:
575 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
576 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
577 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
578 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
579
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300580single_target[expr_ty]:
581 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100582 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300583 | '(' a=single_target ')' { a }
584single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100585 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
586 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
587
588del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
589del_target[expr_ty] (memo):
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300590 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
591 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100592 | del_t_atom
593del_t_atom[expr_ty]:
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300594 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100595 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
596 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
597 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
598
599targets[asdl_seq*]: a=','.target+ [','] { a }
600target[expr_ty] (memo):
601 | 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 | t_atom
604t_primary[expr_ty]:
605 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
606 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
607 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
608 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
609 _Py_Call(a,
610 (b) ? ((expr_ty) b)->v.Call.args : NULL,
611 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
612 EXTRA) }
613 | a=atom &t_lookahead { a }
614t_lookahead: '(' | '[' | '.'
615t_atom[expr_ty]:
616 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
617 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
618 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
619 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
620
621
622# From here on, there are rules for invalid syntax with specialised error messages
623incorrect_arguments:
624 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300625 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
626 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Miss Islington (bot)55c89232020-05-21 18:14:55 -0700627 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
628 | args ',' a=expression for_if_clauses {
629 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100630 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300631invalid_kwarg:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300632 | a=expression '=' {
633 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
634 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100635invalid_named_expression:
636 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300637 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
638 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100639invalid_assignment:
Pablo Galindo102ca522020-06-28 00:40:41 +0100640 | a=invalid_ann_assign_target ':' expression {
641 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
642 a,
643 "only single target (not %s) can be annotated",
644 _PyPegen_get_expr_name(a)
645 )}
Lysandros Nikolaoud01a3e72020-06-27 02:14:12 +0300646 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300647 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaoud01a3e72020-06-27 02:14:12 +0300648 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300649 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Miss Islington (bot)8df4f392020-06-08 02:22:06 -0700650 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou71bb9212020-06-21 05:47:22 +0300651 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Miss Islington (bot)8df4f392020-06-08 02:22:06 -0700652 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100653 | a=star_expressions augassign (yield_expr | star_expressions) {
654 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaoud01a3e72020-06-27 02:14:12 +0300655 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100656 "'%s' is an illegal expression for augmented assignment",
657 _PyPegen_get_expr_name(a)
658 )}
Pablo Galindo102ca522020-06-28 00:40:41 +0100659invalid_ann_assign_target[expr_ty]:
660 | list
661 | tuple
662 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300663invalid_del_stmt:
664 | 'del' a=star_expressions {
Lysandros Nikolaou71bb9212020-06-21 05:47:22 +0300665 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100666invalid_block:
667 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
668invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300669 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
670 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Miss Islington (bot)d00aaf32020-05-21 15:58:16 -0700671invalid_dict_comprehension:
672 | '{' a='**' bitwise_or for_if_clauses '}' {
673 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100674invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700675 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100676 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Miss Islington (bot)d55ed7b2020-06-10 06:24:41 -0700677invalid_lambda_parameters:
678 | lambda_param_no_default* (lambda_slash_with_default | lambda_param_with_default+) lambda_param_no_default {
679 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300680invalid_star_etc:
681 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300682 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300683invalid_lambda_star_etc:
684 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700685invalid_double_type_comments:
686 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
687 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300688invalid_with_item:
689 | expression 'as' a=expression {
Lysandros Nikolaou71bb9212020-06-21 05:47:22 +0300690 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300691
692invalid_for_target:
693 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou71bb9212020-06-21 05:47:22 +0300694 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaoua5442b22020-06-19 03:03:58 +0300695
696invalid_group:
697 | '(' a=starred_expression ')' {
698 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Pablo Galindo275d7e12020-05-21 22:04:54 +0100699invalid_import_from_targets:
700 | import_from_as_names ',' {
701 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }