blob: cbd4bc010dc1ec820648bf55832b074e1f3cbe55 [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) }
43 | ','.expression+
44
Pablo Galindoc5fc1562020-04-22 23:29:27 +010045statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) }
46statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt
47statement_newline[asdl_seq*]:
48 | a=compound_stmt NEWLINE { _PyPegen_singleton_seq(p, a) }
49 | simple_stmt
50 | NEWLINE { _PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) }
51 | ENDMARKER { _PyPegen_interactive_exit(p) }
52simple_stmt[asdl_seq*]:
53 | a=small_stmt !';' NEWLINE { _PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
54 | a=';'.small_stmt+ [';'] NEWLINE { a }
55# NOTE: assignment MUST precede expression, else parsing a simple assignment
56# will throw a SyntaxError.
57small_stmt[stmt_ty] (memo):
58 | assignment
59 | e=star_expressions { _Py_Expr(e, EXTRA) }
60 | &'return' return_stmt
61 | &('import' | 'from') import_stmt
62 | &'raise' raise_stmt
63 | 'pass' { _Py_Pass(EXTRA) }
64 | &'del' del_stmt
65 | &'yield' yield_stmt
66 | &'assert' assert_stmt
67 | 'break' { _Py_Break(EXTRA) }
68 | 'continue' { _Py_Continue(EXTRA) }
69 | &'global' global_stmt
70 | &'nonlocal' nonlocal_stmt
71compound_stmt[stmt_ty]:
72 | &('def' | '@' | ASYNC) function_def
73 | &'if' if_stmt
74 | &('class' | '@') class_def
75 | &('with' | ASYNC) with_stmt
76 | &('for' | ASYNC) for_stmt
77 | &'try' try_stmt
78 | &'while' while_stmt
79
80# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
81assignment:
82 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030083 CHECK_VERSION(
84 6,
85 "Variable annotation syntax is",
86 _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
87 ) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010088 | a=('(' b=inside_paren_ann_assign_target ')' { b }
89 | ann_assign_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030090 CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
Guido van Rossumc001c092020-04-30 12:12:19 -070091 | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) tc=[TYPE_COMMENT] {
92 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010093 | a=target b=augassign c=(yield_expr | star_expressions) {
94 _Py_AugAssign(a, b->kind, c, EXTRA) }
95 | invalid_assignment
96
97augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030098 | '+=' { _PyPegen_augoperator(p, Add) }
99 | '-=' { _PyPegen_augoperator(p, Sub) }
100 | '*=' { _PyPegen_augoperator(p, Mult) }
101 | '@=' { CHECK_VERSION(5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
102 | '/=' { _PyPegen_augoperator(p, Div) }
103 | '%=' { _PyPegen_augoperator(p, Mod) }
104 | '&=' { _PyPegen_augoperator(p, BitAnd) }
105 | '|=' { _PyPegen_augoperator(p, BitOr) }
106 | '^=' { _PyPegen_augoperator(p, BitXor) }
107 | '<<=' { _PyPegen_augoperator(p, LShift) }
108 | '>>=' { _PyPegen_augoperator(p, RShift) }
109 | '**=' { _PyPegen_augoperator(p, Pow) }
110 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100111
112global_stmt[stmt_ty]: 'global' a=','.NAME+ {
113 _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
114nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ {
115 _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
116
117yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
118
119assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
120
121del_stmt[stmt_ty]: 'del' a=del_targets { _Py_Delete(a, EXTRA) }
122
123import_stmt[stmt_ty]: import_name | import_from
124import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
125# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
126import_from[stmt_ty]:
127 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
128 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
129 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
130 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
131import_from_targets[asdl_seq*]:
132 | '(' a=import_from_as_names [','] ')' { a }
133 | import_from_as_names
134 | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
135import_from_as_names[asdl_seq*]:
136 | a=','.import_from_as_name+ { a }
137import_from_as_name[alias_ty]:
138 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
139 (b) ? ((expr_ty) b)->v.Name.id : NULL,
140 p->arena) }
141dotted_as_names[asdl_seq*]:
142 | a=','.dotted_as_name+ { a }
143dotted_as_name[alias_ty]:
144 | a=dotted_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_name[expr_ty]:
148 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
149 | NAME
150
151if_stmt[stmt_ty]:
152 | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
153 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
154elif_stmt[stmt_ty]:
155 | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
156 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
157else_block[asdl_seq*]: 'else' ':' b=block { b }
158
159while_stmt[stmt_ty]:
160 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
161
162for_stmt[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300163 | 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
164 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
165 | ASYNC 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
166 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 +0100167
168with_stmt[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300169 | 'with' '(' a=','.with_item+ ')' ':' b=block {
170 _Py_With(a, b, NULL, EXTRA) }
171 | 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
172 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
173 | ASYNC 'with' '(' a=','.with_item+ ')' ':' b=block {
174 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
175 | ASYNC 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
176 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100177with_item[withitem_ty]:
178 | e=expression o=['as' t=target { t }] { _Py_withitem(e, o, p->arena) }
179
180try_stmt[stmt_ty]:
181 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
182 | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
183except_block[excepthandler_ty]:
184 | 'except' e=expression t=['as' z=target { z }] ':' b=block {
185 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
186 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
187finally_block[asdl_seq*]: 'finally' ':' a=block { a }
188
189return_stmt[stmt_ty]:
190 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
191
192raise_stmt[stmt_ty]:
193 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
194 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
195
196function_def[stmt_ty]:
197 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
198 | function_def_raw
199
200function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300201 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
202 _Py_FunctionDef(n->v.Name.id,
203 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
204 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
205 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
206 CHECK_VERSION(
207 5,
208 "Async functions are",
209 _Py_AsyncFunctionDef(n->v.Name.id,
210 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
211 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
212 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100213func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700214 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
215 | invalid_double_type_comments
216 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100217
218params[arguments_ty]:
219 | invalid_parameters
220 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700221
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100222parameters[arguments_ty]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700223 | a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100224 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700225 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100226 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700227 | a=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100228 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700229 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100230 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700231
232# Some duplication here because we can't write (',' | &')'),
233# which is because we don't support empty alternatives (yet).
234#
235slash_no_default[asdl_seq*]:
236 | a=param_no_default+ '/' ',' { a }
237 | a=param_no_default+ '/' &')' { a }
238slash_with_default[SlashWithDefault*]:
239 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
240 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, a, b) }
241
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100242star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700243 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100244 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700245 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100246 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700247 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
248
Guido van Rossum3941d972020-05-01 09:42:03 -0700249kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700250
251# One parameter. This *includes* a following comma and type comment.
252#
253# There are three styles:
254# - No default
255# - With default
256# - Maybe with default
257#
258# There are two alternative forms of each, to deal with type comments:
259# - Ends in a comma followed by an optional type comment
260# - No comma, optional type comment, must be followed by close paren
261# The latter form is for a final parameter without trailing comma.
262#
263param_no_default[arg_ty]:
264 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
265 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
266param_with_default[NameDefaultPair*]:
267 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
268 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
269param_maybe_default[NameDefaultPair*]:
270 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
271 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
272param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
273
274annotation[expr_ty]: ':' a=expression { a }
275default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100276
277decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a }
278
279class_def[stmt_ty]:
280 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
281 | class_def_raw
282class_def_raw[stmt_ty]:
283 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
284 _Py_ClassDef(a->v.Name.id,
285 (b) ? ((expr_ty) b)->v.Call.args : NULL,
286 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
287 c, NULL, EXTRA) }
288
289block[asdl_seq*] (memo):
290 | NEWLINE INDENT a=statements DEDENT { a }
291 | simple_stmt
292 | invalid_block
293
294expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a }
295star_expressions[expr_ty]:
296 | a=star_expression b=(',' c=star_expression { c })+ [','] {
297 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
298 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
299 | star_expression
300star_expression[expr_ty] (memo):
301 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
302 | expression
303
304star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a }
305star_named_expression[expr_ty]:
306 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
307 | named_expression
308named_expression[expr_ty]:
309 | a=NAME ':=' b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
310 | expression !':='
311 | invalid_named_expression
312
313annotated_rhs[expr_ty]: yield_expr | star_expressions
314
315expressions[expr_ty]:
316 | a=expression b=(',' c=expression { c })+ [','] {
317 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
318 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
319 | expression
320expression[expr_ty] (memo):
321 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
322 | disjunction
323 | lambdef
324
325lambdef[expr_ty]:
326 | '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 -0700327
328# lambda_parameters etc. duplicates parameters but without annotations
329# or type comments, and if there's no comma after a parameter, we expect
330# a colon, not a close parenthesis. (For more, see parameters above.)
331#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100332lambda_parameters[arguments_ty]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700333 | 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 +0100334 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700335 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100336 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700337 | a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100338 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700339 | 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 +0100340 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700341
342lambda_slash_no_default[asdl_seq*]:
343 | a=lambda_param_no_default+ '/' ',' { a }
344 | a=lambda_param_no_default+ '/' &':' { a }
345lambda_slash_with_default[SlashWithDefault*]:
346 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) }
347 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, a, b) }
348
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100349lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700350 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100351 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700352 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100353 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700354 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
355
356lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
357
358lambda_param_no_default[arg_ty]:
359 | a=lambda_param ',' { a }
360 | a=lambda_param &':' { a }
361lambda_param_with_default[NameDefaultPair*]:
362 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
363 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
364lambda_param_maybe_default[NameDefaultPair*]:
365 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
366 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
367lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100368
369disjunction[expr_ty] (memo):
370 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
371 Or,
372 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
373 EXTRA) }
374 | conjunction
375conjunction[expr_ty] (memo):
376 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
377 And,
378 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
379 EXTRA) }
380 | inversion
381inversion[expr_ty] (memo):
382 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
383 | comparison
384comparison[expr_ty]:
385 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
386 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
387 | bitwise_or
388compare_op_bitwise_or_pair[CmpopExprPair*]:
389 | eq_bitwise_or
390 | noteq_bitwise_or
391 | lte_bitwise_or
392 | lt_bitwise_or
393 | gte_bitwise_or
394 | gt_bitwise_or
395 | notin_bitwise_or
396 | in_bitwise_or
397 | isnot_bitwise_or
398 | is_bitwise_or
399eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100400noteq_bitwise_or[CmpopExprPair*]:
401 | (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 +0100402lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
403lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
404gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
405gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
406notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
407in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
408isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
409is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
410
411bitwise_or[expr_ty]:
412 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
413 | bitwise_xor
414bitwise_xor[expr_ty]:
415 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
416 | bitwise_and
417bitwise_and[expr_ty]:
418 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
419 | shift_expr
420shift_expr[expr_ty]:
421 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
422 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
423 | sum
424
425sum[expr_ty]:
426 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
427 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
428 | term
429term[expr_ty]:
430 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
431 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
432 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
433 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300434 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100435 | factor
436factor[expr_ty] (memo):
437 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
438 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
439 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
440 | power
441power[expr_ty]:
442 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
443 | await_primary
444await_primary[expr_ty] (memo):
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300445 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100446 | primary
447primary[expr_ty]:
448 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
449 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
450 | a=primary '(' b=[arguments] ')' {
451 _Py_Call(a,
452 (b) ? ((expr_ty) b)->v.Call.args : NULL,
453 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
454 EXTRA) }
455 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
456 | atom
457
458slices[expr_ty]:
459 | a=slice !',' { a }
460 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
461slice[expr_ty]:
462 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
463 | a=expression { a }
464atom[expr_ty]:
465 | NAME
466 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
467 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
468 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
469 | '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
470 | &STRING strings
471 | NUMBER
472 | &'(' (tuple | group | genexp)
473 | &'[' (list | listcomp)
474 | &'{' (dict | set | dictcomp | setcomp)
475 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
476
477strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
478list[expr_ty]:
479 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
480listcomp[expr_ty]:
481 | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
482 | invalid_comprehension
483tuple[expr_ty]:
484 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
485 _Py_Tuple(a, Load, EXTRA) }
486group[expr_ty]: '(' a=(yield_expr | named_expression) ')' { a }
487genexp[expr_ty]:
488 | '(' a=expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
489 | invalid_comprehension
490set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
491setcomp[expr_ty]:
492 | '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
493 | invalid_comprehension
494dict[expr_ty]:
495 | '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)),
496 CHECK(_PyPegen_get_values(p, a)), EXTRA) }
497dictcomp[expr_ty]:
498 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
499kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a }
500kvpair[KeyValuePair*]:
501 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
502 | a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
503for_if_clauses[asdl_seq*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300504 | for_if_clause+
505for_if_clause[comprehension_ty]:
506 | ASYNC 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* {
507 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
508 | 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* {
509 _Py_comprehension(a, b, c, 0, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100510
511yield_expr[expr_ty]:
512 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
513 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
514
515arguments[expr_ty] (memo):
516 | a=args [','] &')' { a }
517 | incorrect_arguments
518args[expr_ty]:
519 | a=starred_expression b=[',' c=args { c }] {
520 _Py_Call(_PyPegen_dummy_name(p),
521 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
522 : CHECK(_PyPegen_singleton_seq(p, a)),
523 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
524 EXTRA) }
525 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
526 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
527 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
528 EXTRA) }
529 | a=named_expression b=[',' c=args { c }] {
530 _Py_Call(_PyPegen_dummy_name(p),
531 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
532 : CHECK(_PyPegen_singleton_seq(p, a)),
533 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
534 EXTRA) }
535kwargs[asdl_seq*]:
536 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
537 | ','.kwarg_or_starred+
538 | ','.kwarg_or_double_starred+
539starred_expression[expr_ty]:
540 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
541kwarg_or_starred[KeywordOrStarred*]:
542 | a=NAME '=' b=expression {
543 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
544 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
545kwarg_or_double_starred[KeywordOrStarred*]:
546 | a=NAME '=' b=expression {
547 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
548 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
549
550# NOTE: star_targets may contain *bitwise_or, targets may not.
551star_targets[expr_ty]:
552 | a=star_target !',' { a }
553 | a=star_target b=(',' c=star_target { c })* [','] {
554 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
555star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
556star_target[expr_ty] (memo):
557 | '*' a=(!'*' star_target) {
558 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
559 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
560 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
561 | star_atom
562star_atom[expr_ty]:
563 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
564 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
565 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
566 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
567
568inside_paren_ann_assign_target[expr_ty]:
569 | ann_assign_subscript_attribute_target
570 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
571 | '(' a=inside_paren_ann_assign_target ')' { a }
572
573ann_assign_subscript_attribute_target[expr_ty]:
574 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
575 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
576
577del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
578del_target[expr_ty] (memo):
579 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
580 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
581 | del_t_atom
582del_t_atom[expr_ty]:
583 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
584 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
585 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
586 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
587
588targets[asdl_seq*]: a=','.target+ [','] { a }
589target[expr_ty] (memo):
590 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
591 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
592 | t_atom
593t_primary[expr_ty]:
594 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
595 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
596 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
597 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
598 _Py_Call(a,
599 (b) ? ((expr_ty) b)->v.Call.args : NULL,
600 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
601 EXTRA) }
602 | a=atom &t_lookahead { a }
603t_lookahead: '(' | '[' | '.'
604t_atom[expr_ty]:
605 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
606 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
607 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
608 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
609
610
611# From here on, there are rules for invalid syntax with specialised error messages
612incorrect_arguments:
613 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
614 | expression for_if_clauses ',' [args | expression for_if_clauses] {
615 RAISE_SYNTAX_ERROR("Generator expression must be parenthesized") }
616 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
617invalid_named_expression:
618 | a=expression ':=' expression {
619 RAISE_SYNTAX_ERROR("cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
620invalid_assignment:
621 | list ':' { RAISE_SYNTAX_ERROR("only single target (not list) can be annotated") }
622 | tuple ':' { RAISE_SYNTAX_ERROR("only single target (not tuple) can be annotated") }
623 | expression ':' expression ['=' annotated_rhs] {
624 RAISE_SYNTAX_ERROR("illegal target for annotation") }
625 | a=expression ('=' | augassign) (yield_expr | star_expressions) {
Batuhan Taskaya76c1b4d2020-05-01 16:13:43 +0300626 RAISE_SYNTAX_ERROR_NO_COL_OFFSET("cannot assign to %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100627invalid_block:
628 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
629invalid_comprehension:
630 | ('[' | '(' | '{') '*' expression for_if_clauses {
631 RAISE_SYNTAX_ERROR("iterable unpacking cannot be used in comprehension") }
632invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700633 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100634 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700635invalid_double_type_comments:
636 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
637 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }