blob: 9bf2697a7e2d3139e6df16fd92e0f496da1bdaff [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)) }
Guido van Rossumc001c092020-04-30 12:12:19 -070095 | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) tc=[TYPE_COMMENT] {
96 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +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
125del_stmt[stmt_ty]: 'del' a=del_targets { _Py_Delete(a, EXTRA) }
126
127import_stmt[stmt_ty]: import_name | import_from
128import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
129# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
130import_from[stmt_ty]:
131 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
132 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
133 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
134 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
135import_from_targets[asdl_seq*]:
136 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300137 | import_from_as_names !','
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100138 | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300139 | invalid_import_from_targets
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100140import_from_as_names[asdl_seq*]:
141 | a=','.import_from_as_name+ { a }
142import_from_as_name[alias_ty]:
143 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
144 (b) ? ((expr_ty) b)->v.Name.id : NULL,
145 p->arena) }
146dotted_as_names[asdl_seq*]:
147 | a=','.dotted_as_name+ { a }
148dotted_as_name[alias_ty]:
149 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
150 (b) ? ((expr_ty) b)->v.Name.id : NULL,
151 p->arena) }
152dotted_name[expr_ty]:
153 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
154 | NAME
155
156if_stmt[stmt_ty]:
157 | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
158 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
159elif_stmt[stmt_ty]:
160 | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
161 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
162else_block[asdl_seq*]: 'else' ':' b=block { b }
163
164while_stmt[stmt_ty]:
165 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
166
167for_stmt[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300168 | 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
169 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
170 | ASYNC 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
171 CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100172
173with_stmt[stmt_ty]:
Pablo Galindo99db2a12020-05-06 22:54:34 +0100174 | 'with' '(' a=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300175 _Py_With(a, b, NULL, EXTRA) }
176 | 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
177 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo99db2a12020-05-06 22:54:34 +0100178 | ASYNC 'with' '(' a=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300179 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
180 | ASYNC 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
181 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100182with_item[withitem_ty]:
183 | e=expression o=['as' t=target { t }] { _Py_withitem(e, o, p->arena) }
184
185try_stmt[stmt_ty]:
186 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
187 | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
188except_block[excepthandler_ty]:
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300189 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100190 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
191 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
192finally_block[asdl_seq*]: 'finally' ':' a=block { a }
193
194return_stmt[stmt_ty]:
195 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
196
197raise_stmt[stmt_ty]:
198 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
199 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
200
201function_def[stmt_ty]:
202 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
203 | function_def_raw
204
205function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300206 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
207 _Py_FunctionDef(n->v.Name.id,
208 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
209 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
210 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
211 CHECK_VERSION(
212 5,
213 "Async functions are",
214 _Py_AsyncFunctionDef(n->v.Name.id,
215 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
216 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
217 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100218func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700219 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
220 | invalid_double_type_comments
221 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100222
223params[arguments_ty]:
224 | invalid_parameters
225 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700226
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100227parameters[arguments_ty]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700228 | a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100229 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700230 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100231 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700232 | a=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100233 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700234 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100235 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700236
237# Some duplication here because we can't write (',' | &')'),
238# which is because we don't support empty alternatives (yet).
239#
240slash_no_default[asdl_seq*]:
241 | a=param_no_default+ '/' ',' { a }
242 | a=param_no_default+ '/' &')' { a }
243slash_with_default[SlashWithDefault*]:
244 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
245 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, a, b) }
246
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100247star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700248 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100249 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700250 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100251 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700252 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300253 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700254
Guido van Rossum3941d972020-05-01 09:42:03 -0700255kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700256
257# One parameter. This *includes* a following comma and type comment.
258#
259# There are three styles:
260# - No default
261# - With default
262# - Maybe with default
263#
264# There are two alternative forms of each, to deal with type comments:
265# - Ends in a comma followed by an optional type comment
266# - No comma, optional type comment, must be followed by close paren
267# The latter form is for a final parameter without trailing comma.
268#
269param_no_default[arg_ty]:
270 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
271 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
272param_with_default[NameDefaultPair*]:
273 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
274 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
275param_maybe_default[NameDefaultPair*]:
276 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
277 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
278param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
279
280annotation[expr_ty]: ':' a=expression { a }
281default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100282
283decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a }
284
285class_def[stmt_ty]:
286 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
287 | class_def_raw
288class_def_raw[stmt_ty]:
289 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
290 _Py_ClassDef(a->v.Name.id,
291 (b) ? ((expr_ty) b)->v.Call.args : NULL,
292 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
293 c, NULL, EXTRA) }
294
295block[asdl_seq*] (memo):
296 | NEWLINE INDENT a=statements DEDENT { a }
297 | simple_stmt
298 | invalid_block
299
300expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a }
301star_expressions[expr_ty]:
302 | a=star_expression b=(',' c=star_expression { c })+ [','] {
303 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
304 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
305 | star_expression
306star_expression[expr_ty] (memo):
307 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
308 | expression
309
310star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a }
311star_named_expression[expr_ty]:
312 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
313 | named_expression
314named_expression[expr_ty]:
315 | a=NAME ':=' b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
316 | expression !':='
317 | invalid_named_expression
318
319annotated_rhs[expr_ty]: yield_expr | star_expressions
320
321expressions[expr_ty]:
322 | a=expression b=(',' c=expression { c })+ [','] {
323 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
324 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
325 | expression
326expression[expr_ty] (memo):
327 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
328 | disjunction
329 | lambdef
330
331lambdef[expr_ty]:
332 | 'lambda' a=[lambda_parameters] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700333
334# lambda_parameters etc. duplicates parameters but without annotations
335# or type comments, and if there's no comma after a parameter, we expect
336# a colon, not a close parenthesis. (For more, see parameters above.)
337#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100338lambda_parameters[arguments_ty]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700339 | 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 +0100340 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700341 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100342 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700343 | a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100344 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700345 | 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 +0100346 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700347
348lambda_slash_no_default[asdl_seq*]:
349 | a=lambda_param_no_default+ '/' ',' { a }
350 | a=lambda_param_no_default+ '/' &':' { a }
351lambda_slash_with_default[SlashWithDefault*]:
352 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
353 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, a, b) }
354
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100355lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700356 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100357 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700358 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100359 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700360 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300361 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700362
363lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
364
365lambda_param_no_default[arg_ty]:
366 | a=lambda_param ',' { a }
367 | a=lambda_param &':' { a }
368lambda_param_with_default[NameDefaultPair*]:
369 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
370 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
371lambda_param_maybe_default[NameDefaultPair*]:
372 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
373 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
374lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100375
376disjunction[expr_ty] (memo):
377 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
378 Or,
379 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
380 EXTRA) }
381 | conjunction
382conjunction[expr_ty] (memo):
383 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
384 And,
385 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
386 EXTRA) }
387 | inversion
388inversion[expr_ty] (memo):
389 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
390 | comparison
391comparison[expr_ty]:
392 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
393 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
394 | bitwise_or
395compare_op_bitwise_or_pair[CmpopExprPair*]:
396 | eq_bitwise_or
397 | noteq_bitwise_or
398 | lte_bitwise_or
399 | lt_bitwise_or
400 | gte_bitwise_or
401 | gt_bitwise_or
402 | notin_bitwise_or
403 | in_bitwise_or
404 | isnot_bitwise_or
405 | is_bitwise_or
406eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100407noteq_bitwise_or[CmpopExprPair*]:
408 | (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 +0100409lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
410lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
411gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
412gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
413notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
414in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
415isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
416is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
417
418bitwise_or[expr_ty]:
419 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
420 | bitwise_xor
421bitwise_xor[expr_ty]:
422 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
423 | bitwise_and
424bitwise_and[expr_ty]:
425 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
426 | shift_expr
427shift_expr[expr_ty]:
428 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
429 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
430 | sum
431
432sum[expr_ty]:
433 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
434 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
435 | term
436term[expr_ty]:
437 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
438 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
439 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
440 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300441 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100442 | factor
443factor[expr_ty] (memo):
444 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
445 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
446 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
447 | power
448power[expr_ty]:
449 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
450 | await_primary
451await_primary[expr_ty] (memo):
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300452 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100453 | primary
454primary[expr_ty]:
455 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
456 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
457 | a=primary '(' b=[arguments] ')' {
458 _Py_Call(a,
459 (b) ? ((expr_ty) b)->v.Call.args : NULL,
460 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
461 EXTRA) }
462 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
463 | atom
464
465slices[expr_ty]:
466 | a=slice !',' { a }
467 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
468slice[expr_ty]:
469 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
470 | a=expression { a }
471atom[expr_ty]:
472 | NAME
473 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
474 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
475 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
476 | '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
477 | &STRING strings
478 | NUMBER
479 | &'(' (tuple | group | genexp)
480 | &'[' (list | listcomp)
481 | &'{' (dict | set | dictcomp | setcomp)
482 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
483
484strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
485list[expr_ty]:
486 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
487listcomp[expr_ty]:
488 | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
489 | invalid_comprehension
490tuple[expr_ty]:
491 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
492 _Py_Tuple(a, Load, EXTRA) }
493group[expr_ty]: '(' a=(yield_expr | named_expression) ')' { a }
494genexp[expr_ty]:
495 | '(' a=expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
496 | invalid_comprehension
497set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
498setcomp[expr_ty]:
499 | '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
500 | invalid_comprehension
501dict[expr_ty]:
502 | '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)),
503 CHECK(_PyPegen_get_values(p, a)), EXTRA) }
504dictcomp[expr_ty]:
505 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
506kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a }
507kvpair[KeyValuePair*]:
508 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
509 | a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
510for_if_clauses[asdl_seq*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300511 | for_if_clause+
512for_if_clause[comprehension_ty]:
513 | ASYNC 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* {
514 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
515 | 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* {
516 _Py_comprehension(a, b, c, 0, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100517
518yield_expr[expr_ty]:
519 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
520 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
521
522arguments[expr_ty] (memo):
523 | a=args [','] &')' { a }
524 | incorrect_arguments
525args[expr_ty]:
526 | a=starred_expression b=[',' c=args { c }] {
527 _Py_Call(_PyPegen_dummy_name(p),
528 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
529 : CHECK(_PyPegen_singleton_seq(p, a)),
530 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
531 EXTRA) }
532 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
533 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
534 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
535 EXTRA) }
536 | a=named_expression b=[',' c=args { c }] {
537 _Py_Call(_PyPegen_dummy_name(p),
538 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
539 : CHECK(_PyPegen_singleton_seq(p, a)),
540 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
541 EXTRA) }
542kwargs[asdl_seq*]:
543 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
544 | ','.kwarg_or_starred+
545 | ','.kwarg_or_double_starred+
546starred_expression[expr_ty]:
547 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
548kwarg_or_starred[KeywordOrStarred*]:
549 | a=NAME '=' b=expression {
550 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
551 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300552 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100553kwarg_or_double_starred[KeywordOrStarred*]:
554 | a=NAME '=' b=expression {
555 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
556 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300557 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100558
559# NOTE: star_targets may contain *bitwise_or, targets may not.
560star_targets[expr_ty]:
561 | a=star_target !',' { a }
562 | a=star_target b=(',' c=star_target { c })* [','] {
563 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
564star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
565star_target[expr_ty] (memo):
566 | '*' a=(!'*' star_target) {
567 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
568 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
569 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
570 | star_atom
571star_atom[expr_ty]:
572 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
573 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
574 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
575 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
576
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300577single_target[expr_ty]:
578 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100579 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300580 | '(' a=single_target ')' { a }
581single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100582 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
583 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
584
585del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
Shantanu27c0d9b2020-05-11 14:53:58 -0700586# The lookaheads to del_target_end ensure that we don't match expressions where a prefix of the
587# expression matches our rule, thereby letting these cases fall through to invalid_del_target.
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100588del_target[expr_ty] (memo):
Shantanu27c0d9b2020-05-11 14:53:58 -0700589 | a=t_primary '.' b=NAME &del_target_end { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
590 | a=t_primary '[' b=slices ']' &del_target_end { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100591 | del_t_atom
592del_t_atom[expr_ty]:
Shantanu27c0d9b2020-05-11 14:53:58 -0700593 | a=NAME &del_target_end { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100594 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
595 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
596 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
Shantanu27c0d9b2020-05-11 14:53:58 -0700597 | invalid_del_target
598del_target_end: ')' | ']' | ',' | ';' | NEWLINE
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100599
600targets[asdl_seq*]: a=','.target+ [','] { a }
601target[expr_ty] (memo):
602 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
603 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
604 | t_atom
605t_primary[expr_ty]:
606 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
607 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
608 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
609 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
610 _Py_Call(a,
611 (b) ? ((expr_ty) b)->v.Call.args : NULL,
612 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
613 EXTRA) }
614 | a=atom &t_lookahead { a }
615t_lookahead: '(' | '[' | '.'
616t_atom[expr_ty]:
617 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
618 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
619 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
620 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
621
622
623# From here on, there are rules for invalid syntax with specialised error messages
624incorrect_arguments:
625 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300626 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
627 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100628 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300629invalid_kwarg:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300630 | a=expression '=' {
631 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
632 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100633invalid_named_expression:
634 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300635 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
636 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100637invalid_assignment:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300638 | a=list ':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not list) can be annotated") }
639 | a=tuple ':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
640 | a=star_named_expression ',' star_named_expressions* ':' {
641 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
642 | a=expression ':' expression ['=' annotated_rhs] {
643 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100644 | a=star_expressions '=' (yield_expr | star_expressions) {
645 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
646 _PyPegen_get_invalid_target(a),
647 "cannot assign to %s", _PyPegen_get_expr_name(_PyPegen_get_invalid_target(a))) }
648 | a=star_expressions augassign (yield_expr | star_expressions) {
649 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
650 a,
651 "'%s' is an illegal expression for augmented assignment",
652 _PyPegen_get_expr_name(a)
653 )}
654
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100655invalid_block:
656 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
657invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300658 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
659 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100660invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700661 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100662 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300663invalid_star_etc:
664 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300665 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300666invalid_lambda_star_etc:
667 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700668invalid_double_type_comments:
669 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
670 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Shantanu27c0d9b2020-05-11 14:53:58 -0700671invalid_del_target:
672 | a=star_expression &del_target_end {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300673 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot delete %s", _PyPegen_get_expr_name(a)) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300674invalid_import_from_targets:
675 | import_from_as_names ',' {
676 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }