blob: c5a5dbe1724f3e2a0c8b59eae93fb838db832901 [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
37type_expressions[asdl_seq*]:
38 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
39 _PyPegen_seq_append_to_end(p, CHECK(_PyPegen_seq_append_to_end(p, a, b)), c) }
40 | a=','.expression+ ',' '*' b=expression { _PyPegen_seq_append_to_end(p, a, b) }
41 | a=','.expression+ ',' '**' b=expression { _PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070042 | '*' a=expression ',' '**' b=expression {
43 _PyPegen_seq_append_to_end(p, CHECK(_PyPegen_singleton_seq(p, a)), b) }
44 | '*' a=expression { _PyPegen_singleton_seq(p, a) }
45 | '**' a=expression { _PyPegen_singleton_seq(p, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -070046 | ','.expression+
47
Pablo Galindoc5fc1562020-04-22 23:29:27 +010048statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) }
49statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt
50statement_newline[asdl_seq*]:
51 | a=compound_stmt NEWLINE { _PyPegen_singleton_seq(p, a) }
52 | simple_stmt
53 | NEWLINE { _PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) }
54 | ENDMARKER { _PyPegen_interactive_exit(p) }
55simple_stmt[asdl_seq*]:
56 | a=small_stmt !';' NEWLINE { _PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
57 | a=';'.small_stmt+ [';'] NEWLINE { a }
58# NOTE: assignment MUST precede expression, else parsing a simple assignment
59# will throw a SyntaxError.
60small_stmt[stmt_ty] (memo):
61 | assignment
62 | e=star_expressions { _Py_Expr(e, EXTRA) }
63 | &'return' return_stmt
64 | &('import' | 'from') import_stmt
65 | &'raise' raise_stmt
66 | 'pass' { _Py_Pass(EXTRA) }
67 | &'del' del_stmt
68 | &'yield' yield_stmt
69 | &'assert' assert_stmt
70 | 'break' { _Py_Break(EXTRA) }
71 | 'continue' { _Py_Continue(EXTRA) }
72 | &'global' global_stmt
73 | &'nonlocal' nonlocal_stmt
74compound_stmt[stmt_ty]:
75 | &('def' | '@' | ASYNC) function_def
76 | &'if' if_stmt
77 | &('class' | '@') class_def
78 | &('with' | ASYNC) with_stmt
79 | &('for' | ASYNC) for_stmt
80 | &'try' try_stmt
81 | &'while' while_stmt
82
83# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030084assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010085 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030086 CHECK_VERSION(
87 6,
88 "Variable annotation syntax is",
89 _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
90 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030091 | a=('(' b=single_target ')' { b }
92 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030093 CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindo9f495902020-06-08 02:57:00 +010094 | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Guido van Rossumc001c092020-04-30 12:12:19 -070095 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +030096 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +010097 _Py_AugAssign(a, b->kind, c, EXTRA) }
98 | invalid_assignment
99
100augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300101 | '+=' { _PyPegen_augoperator(p, Add) }
102 | '-=' { _PyPegen_augoperator(p, Sub) }
103 | '*=' { _PyPegen_augoperator(p, Mult) }
104 | '@=' { CHECK_VERSION(5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
105 | '/=' { _PyPegen_augoperator(p, Div) }
106 | '%=' { _PyPegen_augoperator(p, Mod) }
107 | '&=' { _PyPegen_augoperator(p, BitAnd) }
108 | '|=' { _PyPegen_augoperator(p, BitOr) }
109 | '^=' { _PyPegen_augoperator(p, BitXor) }
110 | '<<=' { _PyPegen_augoperator(p, LShift) }
111 | '>>=' { _PyPegen_augoperator(p, RShift) }
112 | '**=' { _PyPegen_augoperator(p, Pow) }
113 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100114
115global_stmt[stmt_ty]: 'global' a=','.NAME+ {
116 _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
117nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ {
118 _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
119
120yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
121
122assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
123
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300124del_stmt[stmt_ty]:
125 | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) }
126 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100127
128import_stmt[stmt_ty]: import_name | import_from
129import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
130# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
131import_from[stmt_ty]:
132 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
133 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
134 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
135 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
136import_from_targets[asdl_seq*]:
137 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300138 | import_from_as_names !','
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100139 | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300140 | invalid_import_from_targets
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100141import_from_as_names[asdl_seq*]:
142 | a=','.import_from_as_name+ { a }
143import_from_as_name[alias_ty]:
144 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
145 (b) ? ((expr_ty) b)->v.Name.id : NULL,
146 p->arena) }
147dotted_as_names[asdl_seq*]:
148 | a=','.dotted_as_name+ { a }
149dotted_as_name[alias_ty]:
150 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
151 (b) ? ((expr_ty) b)->v.Name.id : NULL,
152 p->arena) }
153dotted_name[expr_ty]:
154 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
155 | NAME
156
157if_stmt[stmt_ty]:
158 | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
159 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
160elif_stmt[stmt_ty]:
161 | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
162 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
163else_block[asdl_seq*]: 'else' ':' b=block { b }
164
165while_stmt[stmt_ty]:
166 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
167
168for_stmt[stmt_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300169 | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300170 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300171 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300172 CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300173 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100174
175with_stmt[stmt_ty]:
Pablo Galindo99db2a12020-05-06 22:54:34 +0100176 | 'with' '(' a=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300177 _Py_With(a, b, NULL, EXTRA) }
178 | 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
179 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo99db2a12020-05-06 22:54:34 +0100180 | ASYNC 'with' '(' a=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300181 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
182 | ASYNC 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
183 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100184with_item[withitem_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300185 | e=expression 'as' t=target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
186 | invalid_with_item
187 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100188
189try_stmt[stmt_ty]:
190 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
191 | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
192except_block[excepthandler_ty]:
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300193 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100194 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
195 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
196finally_block[asdl_seq*]: 'finally' ':' a=block { a }
197
198return_stmt[stmt_ty]:
199 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
200
201raise_stmt[stmt_ty]:
202 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
203 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
204
205function_def[stmt_ty]:
206 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
207 | function_def_raw
208
209function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300210 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
211 _Py_FunctionDef(n->v.Name.id,
212 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
213 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
214 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
215 CHECK_VERSION(
216 5,
217 "Async functions are",
218 _Py_AsyncFunctionDef(n->v.Name.id,
219 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
220 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
221 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100222func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700223 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
224 | invalid_double_type_comments
225 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100226
227params[arguments_ty]:
228 | invalid_parameters
229 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700230
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100231parameters[arguments_ty]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700232 | a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100233 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700234 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100235 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700236 | a=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100237 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700238 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100239 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700240
241# Some duplication here because we can't write (',' | &')'),
242# which is because we don't support empty alternatives (yet).
243#
244slash_no_default[asdl_seq*]:
245 | a=param_no_default+ '/' ',' { a }
246 | a=param_no_default+ '/' &')' { a }
247slash_with_default[SlashWithDefault*]:
248 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
249 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, a, b) }
250
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100251star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700252 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100253 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700254 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100255 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700256 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300257 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700258
Guido van Rossum3941d972020-05-01 09:42:03 -0700259kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700260
261# One parameter. This *includes* a following comma and type comment.
262#
263# There are three styles:
264# - No default
265# - With default
266# - Maybe with default
267#
268# There are two alternative forms of each, to deal with type comments:
269# - Ends in a comma followed by an optional type comment
270# - No comma, optional type comment, must be followed by close paren
271# The latter form is for a final parameter without trailing comma.
272#
273param_no_default[arg_ty]:
274 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
275 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
276param_with_default[NameDefaultPair*]:
277 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
278 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
279param_maybe_default[NameDefaultPair*]:
280 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
281 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
282param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
283
284annotation[expr_ty]: ':' a=expression { a }
285default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100286
287decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a }
288
289class_def[stmt_ty]:
290 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
291 | class_def_raw
292class_def_raw[stmt_ty]:
293 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
294 _Py_ClassDef(a->v.Name.id,
295 (b) ? ((expr_ty) b)->v.Call.args : NULL,
296 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
297 c, NULL, EXTRA) }
298
299block[asdl_seq*] (memo):
300 | NEWLINE INDENT a=statements DEDENT { a }
301 | simple_stmt
302 | invalid_block
303
304expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a }
305star_expressions[expr_ty]:
306 | a=star_expression b=(',' c=star_expression { c })+ [','] {
307 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
308 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
309 | star_expression
310star_expression[expr_ty] (memo):
311 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
312 | expression
313
314star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a }
315star_named_expression[expr_ty]:
316 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
317 | named_expression
318named_expression[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300319 | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100320 | expression !':='
321 | invalid_named_expression
322
323annotated_rhs[expr_ty]: yield_expr | star_expressions
324
325expressions[expr_ty]:
326 | a=expression b=(',' c=expression { c })+ [','] {
327 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
328 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
329 | expression
330expression[expr_ty] (memo):
331 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
332 | disjunction
333 | lambdef
334
335lambdef[expr_ty]:
Pablo Galindoc6483c92020-06-10 14:07:06 +0100336 | 'lambda' a=[lambda_params] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }
337
338lambda_params[arguments_ty]:
339 | invalid_lambda_parameters
340 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700341
342# lambda_parameters etc. duplicates parameters but without annotations
343# or type comments, and if there's no comma after a parameter, we expect
344# a colon, not a close parenthesis. (For more, see parameters above.)
345#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100346lambda_parameters[arguments_ty]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700347 | 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 +0100348 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700349 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100350 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700351 | a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100352 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700353 | 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 +0100354 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700355
356lambda_slash_no_default[asdl_seq*]:
357 | a=lambda_param_no_default+ '/' ',' { a }
358 | a=lambda_param_no_default+ '/' &':' { a }
359lambda_slash_with_default[SlashWithDefault*]:
360 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
361 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, a, b) }
362
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100363lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700364 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100365 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700366 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100367 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700368 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300369 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700370
371lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
372
373lambda_param_no_default[arg_ty]:
374 | a=lambda_param ',' { a }
375 | a=lambda_param &':' { a }
376lambda_param_with_default[NameDefaultPair*]:
377 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
378 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
379lambda_param_maybe_default[NameDefaultPair*]:
380 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
381 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
382lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100383
384disjunction[expr_ty] (memo):
385 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
386 Or,
387 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
388 EXTRA) }
389 | conjunction
390conjunction[expr_ty] (memo):
391 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
392 And,
393 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
394 EXTRA) }
395 | inversion
396inversion[expr_ty] (memo):
397 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
398 | comparison
399comparison[expr_ty]:
400 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
401 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
402 | bitwise_or
403compare_op_bitwise_or_pair[CmpopExprPair*]:
404 | eq_bitwise_or
405 | noteq_bitwise_or
406 | lte_bitwise_or
407 | lt_bitwise_or
408 | gte_bitwise_or
409 | gt_bitwise_or
410 | notin_bitwise_or
411 | in_bitwise_or
412 | isnot_bitwise_or
413 | is_bitwise_or
414eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100415noteq_bitwise_or[CmpopExprPair*]:
416 | (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 +0100417lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
418lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
419gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
420gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
421notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
422in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
423isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
424is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
425
426bitwise_or[expr_ty]:
427 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
428 | bitwise_xor
429bitwise_xor[expr_ty]:
430 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
431 | bitwise_and
432bitwise_and[expr_ty]:
433 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
434 | shift_expr
435shift_expr[expr_ty]:
436 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
437 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
438 | sum
439
440sum[expr_ty]:
441 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
442 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
443 | term
444term[expr_ty]:
445 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
446 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
447 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
448 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300449 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100450 | factor
451factor[expr_ty] (memo):
452 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
453 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
454 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
455 | power
456power[expr_ty]:
457 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
458 | await_primary
459await_primary[expr_ty] (memo):
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300460 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100461 | primary
462primary[expr_ty]:
463 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
464 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
465 | a=primary '(' b=[arguments] ')' {
466 _Py_Call(a,
467 (b) ? ((expr_ty) b)->v.Call.args : NULL,
468 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
469 EXTRA) }
470 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
471 | atom
472
473slices[expr_ty]:
474 | a=slice !',' { a }
475 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
476slice[expr_ty]:
477 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
478 | a=expression { a }
479atom[expr_ty]:
480 | NAME
481 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
482 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
483 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100484 | &STRING strings
485 | NUMBER
486 | &'(' (tuple | group | genexp)
487 | &'[' (list | listcomp)
488 | &'{' (dict | set | dictcomp | setcomp)
489 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
490
491strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
492list[expr_ty]:
493 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
494listcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300495 | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100496 | invalid_comprehension
497tuple[expr_ty]:
498 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
499 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300500group[expr_ty]:
501 | '(' a=(yield_expr | named_expression) ')' { a }
502 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100503genexp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300504 | '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100505 | invalid_comprehension
506set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
507setcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300508 | '{' a=expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100509 | invalid_comprehension
510dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300511 | '{' a=[double_starred_kvpairs] '}' {
512 _Py_Dict(CHECK(_PyPegen_get_keys(p, a)), CHECK(_PyPegen_get_values(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100513dictcomp[expr_ty]:
514 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300515 | invalid_dict_comprehension
516double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
517double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100518 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300519 | kvpair
520kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100521for_if_clauses[asdl_seq*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300522 | for_if_clause+
523for_if_clause[comprehension_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300524 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300525 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300526 | 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300527 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300528 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100529
530yield_expr[expr_ty]:
531 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
532 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
533
534arguments[expr_ty] (memo):
535 | a=args [','] &')' { a }
536 | incorrect_arguments
537args[expr_ty]:
538 | a=starred_expression b=[',' c=args { c }] {
539 _Py_Call(_PyPegen_dummy_name(p),
540 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
541 : CHECK(_PyPegen_singleton_seq(p, a)),
542 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
543 EXTRA) }
544 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
545 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
546 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
547 EXTRA) }
548 | a=named_expression b=[',' c=args { c }] {
549 _Py_Call(_PyPegen_dummy_name(p),
550 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
551 : CHECK(_PyPegen_singleton_seq(p, a)),
552 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
553 EXTRA) }
554kwargs[asdl_seq*]:
555 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
556 | ','.kwarg_or_starred+
557 | ','.kwarg_or_double_starred+
558starred_expression[expr_ty]:
559 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
560kwarg_or_starred[KeywordOrStarred*]:
561 | a=NAME '=' b=expression {
562 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
563 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300564 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100565kwarg_or_double_starred[KeywordOrStarred*]:
566 | a=NAME '=' b=expression {
567 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
568 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300569 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100570
571# NOTE: star_targets may contain *bitwise_or, targets may not.
572star_targets[expr_ty]:
573 | a=star_target !',' { a }
574 | a=star_target b=(',' c=star_target { c })* [','] {
575 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
576star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
577star_target[expr_ty] (memo):
578 | '*' a=(!'*' star_target) {
579 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
580 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
581 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
582 | star_atom
583star_atom[expr_ty]:
584 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
585 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
586 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
587 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
588
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300589single_target[expr_ty]:
590 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100591 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300592 | '(' a=single_target ')' { a }
593single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100594 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
595 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
596
597del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
598del_target[expr_ty] (memo):
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300599 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
600 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100601 | del_t_atom
602del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300603 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100604 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
605 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
606 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
607
608targets[asdl_seq*]: a=','.target+ [','] { a }
609target[expr_ty] (memo):
610 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
611 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
612 | t_atom
613t_primary[expr_ty]:
614 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
615 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
616 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
617 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
618 _Py_Call(a,
619 (b) ? ((expr_ty) b)->v.Call.args : NULL,
620 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
621 EXTRA) }
622 | a=atom &t_lookahead { a }
623t_lookahead: '(' | '[' | '.'
624t_atom[expr_ty]:
625 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
626 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
627 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
628 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
629
630
631# From here on, there are rules for invalid syntax with specialised error messages
632incorrect_arguments:
633 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300634 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
635 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300636 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
637 | args ',' a=expression for_if_clauses {
638 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100639 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300640invalid_kwarg:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300641 | a=expression '=' {
642 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
643 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100644invalid_named_expression:
645 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300646 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
647 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100648invalid_assignment:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300649 | a=list ':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not list) can be annotated") }
650 | a=tuple ':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
651 | a=star_named_expression ',' star_named_expressions* ':' {
652 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
653 | a=expression ':' expression ['=' annotated_rhs] {
654 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100655 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300656 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100657 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100658 | a=star_expressions augassign (yield_expr | star_expressions) {
659 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
660 a,
661 "'%s' is an illegal expression for augmented assignment",
662 _PyPegen_get_expr_name(a)
663 )}
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300664invalid_del_stmt:
665 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300666 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100667invalid_block:
668 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
669invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300670 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
671 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300672invalid_dict_comprehension:
673 | '{' a='**' bitwise_or for_if_clauses '}' {
674 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100675invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700676 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100677 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100678invalid_lambda_parameters:
679 | lambda_param_no_default* (lambda_slash_with_default | lambda_param_with_default+) lambda_param_no_default {
680 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300681invalid_star_etc:
682 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300683 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300684invalid_lambda_star_etc:
685 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700686invalid_double_type_comments:
687 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
688 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300689invalid_with_item:
690 | expression 'as' a=expression {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300691 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300692
693invalid_for_target:
694 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300695 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300696
697invalid_group:
698 | '(' a=starred_expression ')' {
699 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300700invalid_import_from_targets:
701 | import_from_as_names ',' {
702 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }