blob: 3813d8845be24d43c2343c51ea05404c10c0e473 [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 ) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700213func_type_comment[PyObject*]:
214 | 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
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100249kwds[arg_ty]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700250 | '**' a=param_no_default { a }
251
252# One parameter. This *includes* a following comma and type comment.
253#
254# There are three styles:
255# - No default
256# - With default
257# - Maybe with default
258#
259# There are two alternative forms of each, to deal with type comments:
260# - Ends in a comma followed by an optional type comment
261# - No comma, optional type comment, must be followed by close paren
262# The latter form is for a final parameter without trailing comma.
263#
264param_no_default[arg_ty]:
265 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
266 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
267param_with_default[NameDefaultPair*]:
268 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
269 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
270param_maybe_default[NameDefaultPair*]:
271 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
272 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
273param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
274
275annotation[expr_ty]: ':' a=expression { a }
276default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100277
278decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a }
279
280class_def[stmt_ty]:
281 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
282 | class_def_raw
283class_def_raw[stmt_ty]:
284 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
285 _Py_ClassDef(a->v.Name.id,
286 (b) ? ((expr_ty) b)->v.Call.args : NULL,
287 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
288 c, NULL, EXTRA) }
289
290block[asdl_seq*] (memo):
291 | NEWLINE INDENT a=statements DEDENT { a }
292 | simple_stmt
293 | invalid_block
294
295expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a }
296star_expressions[expr_ty]:
297 | a=star_expression b=(',' c=star_expression { c })+ [','] {
298 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
299 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
300 | star_expression
301star_expression[expr_ty] (memo):
302 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
303 | expression
304
305star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a }
306star_named_expression[expr_ty]:
307 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
308 | named_expression
309named_expression[expr_ty]:
310 | a=NAME ':=' b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
311 | expression !':='
312 | invalid_named_expression
313
314annotated_rhs[expr_ty]: yield_expr | star_expressions
315
316expressions[expr_ty]:
317 | a=expression b=(',' c=expression { c })+ [','] {
318 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
319 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
320 | expression
321expression[expr_ty] (memo):
322 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
323 | disjunction
324 | lambdef
325
326lambdef[expr_ty]:
327 | 'lambda' a=[lambda_parameters] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }
328lambda_parameters[arguments_ty]:
329 | a=lambda_slash_without_default b=[',' x=lambda_plain_names { x }] c=[',' y=lambda_names_with_default { y }] d=[',' z=[lambda_star_etc] { z }] {
330 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
331 | a=lambda_slash_with_default b=[',' y=lambda_names_with_default { y }] c=[',' z=[lambda_star_etc] { z }] {
332 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
333 | a=lambda_plain_names b=[',' y=lambda_names_with_default { y }] c=[',' z=[lambda_star_etc] { z }] {
334 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
335 | a=lambda_names_with_default b=[',' z=[lambda_star_etc] { z }] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
336 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
337lambda_slash_without_default[asdl_seq*]: a=lambda_plain_names ',' '/' { a }
338lambda_slash_with_default[SlashWithDefault*]: a=[n=lambda_plain_names ',' { n }] b=lambda_names_with_default ',' '/' {
339 _PyPegen_slash_with_default(p, a, b) }
340lambda_star_etc[StarEtc*]:
341 | '*' a=lambda_plain_name b=lambda_name_with_optional_default* c=[',' d=lambda_kwds { d }] [','] {
342 _PyPegen_star_etc(p, a, b, c) }
343 | '*' b=lambda_name_with_optional_default+ c=[',' d=lambda_kwds { d }] [','] {
344 _PyPegen_star_etc(p, NULL, b, c) }
345 | a=lambda_kwds [','] { _PyPegen_star_etc(p, NULL, NULL, a) }
346lambda_name_with_optional_default[NameDefaultPair*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700347 | ',' a=lambda_plain_name b=['=' e=expression { e }] { _PyPegen_name_default_pair(p, a, b, NULL) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100348lambda_names_with_default[asdl_seq*]: a=','.lambda_name_with_default+ { a }
349lambda_name_with_default[NameDefaultPair*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700350 | n=lambda_plain_name '=' e=expression { _PyPegen_name_default_pair(p, n, e, NULL) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100351lambda_plain_names[asdl_seq*]: a=','.(lambda_plain_name !'=')+ { a }
352lambda_plain_name[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
353lambda_kwds[arg_ty]: '**' a=lambda_plain_name { a }
354
355disjunction[expr_ty] (memo):
356 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
357 Or,
358 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
359 EXTRA) }
360 | conjunction
361conjunction[expr_ty] (memo):
362 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
363 And,
364 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
365 EXTRA) }
366 | inversion
367inversion[expr_ty] (memo):
368 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
369 | comparison
370comparison[expr_ty]:
371 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
372 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
373 | bitwise_or
374compare_op_bitwise_or_pair[CmpopExprPair*]:
375 | eq_bitwise_or
376 | noteq_bitwise_or
377 | lte_bitwise_or
378 | lt_bitwise_or
379 | gte_bitwise_or
380 | gt_bitwise_or
381 | notin_bitwise_or
382 | in_bitwise_or
383 | isnot_bitwise_or
384 | is_bitwise_or
385eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100386noteq_bitwise_or[CmpopExprPair*]:
387 | (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 +0100388lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
389lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
390gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
391gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
392notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
393in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
394isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
395is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
396
397bitwise_or[expr_ty]:
398 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
399 | bitwise_xor
400bitwise_xor[expr_ty]:
401 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
402 | bitwise_and
403bitwise_and[expr_ty]:
404 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
405 | shift_expr
406shift_expr[expr_ty]:
407 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
408 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
409 | sum
410
411sum[expr_ty]:
412 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
413 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
414 | term
415term[expr_ty]:
416 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
417 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
418 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
419 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300420 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100421 | factor
422factor[expr_ty] (memo):
423 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
424 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
425 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
426 | power
427power[expr_ty]:
428 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
429 | await_primary
430await_primary[expr_ty] (memo):
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300431 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100432 | primary
433primary[expr_ty]:
434 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
435 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
436 | a=primary '(' b=[arguments] ')' {
437 _Py_Call(a,
438 (b) ? ((expr_ty) b)->v.Call.args : NULL,
439 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
440 EXTRA) }
441 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
442 | atom
443
444slices[expr_ty]:
445 | a=slice !',' { a }
446 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
447slice[expr_ty]:
448 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
449 | a=expression { a }
450atom[expr_ty]:
451 | NAME
452 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
453 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
454 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
455 | '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
456 | &STRING strings
457 | NUMBER
458 | &'(' (tuple | group | genexp)
459 | &'[' (list | listcomp)
460 | &'{' (dict | set | dictcomp | setcomp)
461 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
462
463strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
464list[expr_ty]:
465 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
466listcomp[expr_ty]:
467 | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
468 | invalid_comprehension
469tuple[expr_ty]:
470 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
471 _Py_Tuple(a, Load, EXTRA) }
472group[expr_ty]: '(' a=(yield_expr | named_expression) ')' { a }
473genexp[expr_ty]:
474 | '(' a=expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
475 | invalid_comprehension
476set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
477setcomp[expr_ty]:
478 | '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
479 | invalid_comprehension
480dict[expr_ty]:
481 | '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)),
482 CHECK(_PyPegen_get_values(p, a)), EXTRA) }
483dictcomp[expr_ty]:
484 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
485kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a }
486kvpair[KeyValuePair*]:
487 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
488 | a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
489for_if_clauses[asdl_seq*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300490 | for_if_clause+
491for_if_clause[comprehension_ty]:
492 | ASYNC 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* {
493 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
494 | 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* {
495 _Py_comprehension(a, b, c, 0, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100496
497yield_expr[expr_ty]:
498 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
499 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
500
501arguments[expr_ty] (memo):
502 | a=args [','] &')' { a }
503 | incorrect_arguments
504args[expr_ty]:
505 | a=starred_expression b=[',' c=args { c }] {
506 _Py_Call(_PyPegen_dummy_name(p),
507 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
508 : CHECK(_PyPegen_singleton_seq(p, a)),
509 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
510 EXTRA) }
511 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
512 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
513 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
514 EXTRA) }
515 | a=named_expression b=[',' c=args { c }] {
516 _Py_Call(_PyPegen_dummy_name(p),
517 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
518 : CHECK(_PyPegen_singleton_seq(p, a)),
519 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
520 EXTRA) }
521kwargs[asdl_seq*]:
522 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
523 | ','.kwarg_or_starred+
524 | ','.kwarg_or_double_starred+
525starred_expression[expr_ty]:
526 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
527kwarg_or_starred[KeywordOrStarred*]:
528 | a=NAME '=' b=expression {
529 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
530 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
531kwarg_or_double_starred[KeywordOrStarred*]:
532 | a=NAME '=' b=expression {
533 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
534 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
535
536# NOTE: star_targets may contain *bitwise_or, targets may not.
537star_targets[expr_ty]:
538 | a=star_target !',' { a }
539 | a=star_target b=(',' c=star_target { c })* [','] {
540 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
541star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
542star_target[expr_ty] (memo):
543 | '*' a=(!'*' star_target) {
544 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
545 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
546 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
547 | star_atom
548star_atom[expr_ty]:
549 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
550 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
551 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
552 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
553
554inside_paren_ann_assign_target[expr_ty]:
555 | ann_assign_subscript_attribute_target
556 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
557 | '(' a=inside_paren_ann_assign_target ')' { a }
558
559ann_assign_subscript_attribute_target[expr_ty]:
560 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
561 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
562
563del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
564del_target[expr_ty] (memo):
565 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
566 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
567 | del_t_atom
568del_t_atom[expr_ty]:
569 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
570 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
571 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
572 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
573
574targets[asdl_seq*]: a=','.target+ [','] { a }
575target[expr_ty] (memo):
576 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
577 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
578 | t_atom
579t_primary[expr_ty]:
580 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
581 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
582 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
583 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
584 _Py_Call(a,
585 (b) ? ((expr_ty) b)->v.Call.args : NULL,
586 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
587 EXTRA) }
588 | a=atom &t_lookahead { a }
589t_lookahead: '(' | '[' | '.'
590t_atom[expr_ty]:
591 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
592 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
593 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
594 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
595
596
597# From here on, there are rules for invalid syntax with specialised error messages
598incorrect_arguments:
599 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
600 | expression for_if_clauses ',' [args | expression for_if_clauses] {
601 RAISE_SYNTAX_ERROR("Generator expression must be parenthesized") }
602 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
603invalid_named_expression:
604 | a=expression ':=' expression {
605 RAISE_SYNTAX_ERROR("cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
606invalid_assignment:
607 | list ':' { RAISE_SYNTAX_ERROR("only single target (not list) can be annotated") }
608 | tuple ':' { RAISE_SYNTAX_ERROR("only single target (not tuple) can be annotated") }
609 | expression ':' expression ['=' annotated_rhs] {
610 RAISE_SYNTAX_ERROR("illegal target for annotation") }
611 | a=expression ('=' | augassign) (yield_expr | star_expressions) {
Batuhan Taskaya76c1b4d2020-05-01 16:13:43 +0300612 RAISE_SYNTAX_ERROR_NO_COL_OFFSET("cannot assign to %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100613invalid_block:
614 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
615invalid_comprehension:
616 | ('[' | '(' | '{') '*' expression for_if_clauses {
617 RAISE_SYNTAX_ERROR("iterable unpacking cannot be used in comprehension") }
618invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700619 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100620 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700621invalid_double_type_comments:
622 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
623 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }