blob: 0ff2dcca884f1c1f480eb3695b261aaf8e965463 [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);
20 } else if (p->start_rule == Py_fstring_input) {
21 result = fstring_rule(p);
22 }
23
24 return result;
25}
26
27// The end
28'''
29file[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
30interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) }
31eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }
32fstring[expr_ty]: star_expressions
33
34statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) }
35statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt
36statement_newline[asdl_seq*]:
37 | a=compound_stmt NEWLINE { _PyPegen_singleton_seq(p, a) }
38 | simple_stmt
39 | NEWLINE { _PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) }
40 | ENDMARKER { _PyPegen_interactive_exit(p) }
41simple_stmt[asdl_seq*]:
42 | a=small_stmt !';' NEWLINE { _PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
43 | a=';'.small_stmt+ [';'] NEWLINE { a }
44# NOTE: assignment MUST precede expression, else parsing a simple assignment
45# will throw a SyntaxError.
46small_stmt[stmt_ty] (memo):
47 | assignment
48 | e=star_expressions { _Py_Expr(e, EXTRA) }
49 | &'return' return_stmt
50 | &('import' | 'from') import_stmt
51 | &'raise' raise_stmt
52 | 'pass' { _Py_Pass(EXTRA) }
53 | &'del' del_stmt
54 | &'yield' yield_stmt
55 | &'assert' assert_stmt
56 | 'break' { _Py_Break(EXTRA) }
57 | 'continue' { _Py_Continue(EXTRA) }
58 | &'global' global_stmt
59 | &'nonlocal' nonlocal_stmt
60compound_stmt[stmt_ty]:
61 | &('def' | '@' | ASYNC) function_def
62 | &'if' if_stmt
63 | &('class' | '@') class_def
64 | &('with' | ASYNC) with_stmt
65 | &('for' | ASYNC) for_stmt
66 | &'try' try_stmt
67 | &'while' while_stmt
68
69# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
70assignment:
71 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
72 _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA) }
73 | a=('(' b=inside_paren_ann_assign_target ')' { b }
74 | ann_assign_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
75 _Py_AnnAssign(a, b, c, 0, EXTRA)}
76 | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) {
77 _Py_Assign(a, b, NULL, EXTRA) }
78 | a=target b=augassign c=(yield_expr | star_expressions) {
79 _Py_AugAssign(a, b->kind, c, EXTRA) }
80 | invalid_assignment
81
82augassign[AugOperator*]:
83 | '+=' {_PyPegen_augoperator(p, Add)}
84 | '-=' {_PyPegen_augoperator(p, Sub)}
85 | '*=' {_PyPegen_augoperator(p, Mult)}
86 | '@=' {_PyPegen_augoperator(p, MatMult)}
87 | '/=' {_PyPegen_augoperator(p, Div)}
88 | '%=' {_PyPegen_augoperator(p, Mod)}
89 | '&=' {_PyPegen_augoperator(p, BitAnd)}
90 | '|=' {_PyPegen_augoperator(p, BitOr)}
91 | '^=' {_PyPegen_augoperator(p, BitXor)}
92 | '<<=' {_PyPegen_augoperator(p, LShift)}
93 | '>>=' {_PyPegen_augoperator(p, RShift)}
94 | '**=' {_PyPegen_augoperator(p, Pow)}
95 | '//=' {_PyPegen_augoperator(p, FloorDiv)}
96
97global_stmt[stmt_ty]: 'global' a=','.NAME+ {
98 _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
99nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ {
100 _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
101
102yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
103
104assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
105
106del_stmt[stmt_ty]: 'del' a=del_targets { _Py_Delete(a, EXTRA) }
107
108import_stmt[stmt_ty]: import_name | import_from
109import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
110# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
111import_from[stmt_ty]:
112 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
113 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
114 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
115 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
116import_from_targets[asdl_seq*]:
117 | '(' a=import_from_as_names [','] ')' { a }
118 | import_from_as_names
119 | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
120import_from_as_names[asdl_seq*]:
121 | a=','.import_from_as_name+ { a }
122import_from_as_name[alias_ty]:
123 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
124 (b) ? ((expr_ty) b)->v.Name.id : NULL,
125 p->arena) }
126dotted_as_names[asdl_seq*]:
127 | a=','.dotted_as_name+ { a }
128dotted_as_name[alias_ty]:
129 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
130 (b) ? ((expr_ty) b)->v.Name.id : NULL,
131 p->arena) }
132dotted_name[expr_ty]:
133 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
134 | NAME
135
136if_stmt[stmt_ty]:
137 | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
138 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
139elif_stmt[stmt_ty]:
140 | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
141 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
142else_block[asdl_seq*]: 'else' ':' b=block { b }
143
144while_stmt[stmt_ty]:
145 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
146
147for_stmt[stmt_ty]:
148 | is_async=[ASYNC] 'for' t=star_targets 'in' ex=star_expressions ':' b=block el=[else_block] {
149 (is_async ? _Py_AsyncFor : _Py_For)(t, ex, b, el, NULL, EXTRA) }
150
151with_stmt[stmt_ty]:
152 | is_async=[ASYNC] 'with' '(' a=','.with_item+ ')' ':' b=block {
153 (is_async ? _Py_AsyncWith : _Py_With)(a, b, NULL, EXTRA) }
154 | is_async=[ASYNC] 'with' a=','.with_item+ ':' b=block {
155 (is_async ? _Py_AsyncWith : _Py_With)(a, b, NULL, EXTRA) }
156with_item[withitem_ty]:
157 | e=expression o=['as' t=target { t }] { _Py_withitem(e, o, p->arena) }
158
159try_stmt[stmt_ty]:
160 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
161 | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
162except_block[excepthandler_ty]:
163 | 'except' e=expression t=['as' z=target { z }] ':' b=block {
164 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
165 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
166finally_block[asdl_seq*]: 'finally' ':' a=block { a }
167
168return_stmt[stmt_ty]:
169 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
170
171raise_stmt[stmt_ty]:
172 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
173 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
174
175function_def[stmt_ty]:
176 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
177 | function_def_raw
178
179function_def_raw[stmt_ty]:
180 | is_async=[ASYNC] 'def' n=NAME '(' params=[params] ')' a=['->' z=annotation { z }] ':' b=block {
181 (is_async ? _Py_AsyncFunctionDef : _Py_FunctionDef)(n->v.Name.id,
182 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
183 b, NULL, a, NULL, EXTRA) }
184
185params[arguments_ty]:
186 | invalid_parameters
187 | parameters
188parameters[arguments_ty]:
189 | a=slash_without_default b=[',' x=plain_names { x }] c=[',' y=names_with_default { y }] d=[',' z=[star_etc] { z }] {
190 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
191 | a=slash_with_default b=[',' y=names_with_default { y }] c=[',' z=[star_etc] { z }] {
192 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
193 | a=plain_names b=[',' y=names_with_default { y }] c=[',' z=[star_etc] { z }] {
194 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
195 | a=names_with_default b=[',' z=[star_etc] { z }] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
196 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
197slash_without_default[asdl_seq*]: a=plain_names ',' '/' { a }
198slash_with_default[SlashWithDefault*]: a=[n=plain_names ',' { n }] b=names_with_default ',' '/' {
199 _PyPegen_slash_with_default(p, a, b) }
200star_etc[StarEtc*]:
201 | '*' a=plain_name b=name_with_optional_default* c=[',' d=kwds { d }] [','] {
202 _PyPegen_star_etc(p, a, b, c) }
203 | '*' b=name_with_optional_default+ c=[',' d=kwds { d }] [','] {
204 _PyPegen_star_etc(p, NULL, b, c) }
205 | a=kwds [','] { _PyPegen_star_etc(p, NULL, NULL, a) }
206name_with_optional_default[NameDefaultPair*]:
207 | ',' a=plain_name b=['=' e=expression { e }] { _PyPegen_name_default_pair(p, a, b) }
208names_with_default[asdl_seq*]: a=','.name_with_default+ { a }
209name_with_default[NameDefaultPair*]:
210 | n=plain_name '=' e=expression { _PyPegen_name_default_pair(p, n, e) }
211plain_names[asdl_seq*] (memo): a=','.(plain_name !'=')+ { a }
212plain_name[arg_ty]:
213 | a=NAME b=[':' z=annotation { z }] { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
214kwds[arg_ty]:
215 | '**' a=plain_name { a }
216annotation[expr_ty]: expression
217
218decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a }
219
220class_def[stmt_ty]:
221 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
222 | class_def_raw
223class_def_raw[stmt_ty]:
224 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
225 _Py_ClassDef(a->v.Name.id,
226 (b) ? ((expr_ty) b)->v.Call.args : NULL,
227 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
228 c, NULL, EXTRA) }
229
230block[asdl_seq*] (memo):
231 | NEWLINE INDENT a=statements DEDENT { a }
232 | simple_stmt
233 | invalid_block
234
235expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a }
236star_expressions[expr_ty]:
237 | a=star_expression b=(',' c=star_expression { c })+ [','] {
238 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
239 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
240 | star_expression
241star_expression[expr_ty] (memo):
242 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
243 | expression
244
245star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a }
246star_named_expression[expr_ty]:
247 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
248 | named_expression
249named_expression[expr_ty]:
250 | a=NAME ':=' b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
251 | expression !':='
252 | invalid_named_expression
253
254annotated_rhs[expr_ty]: yield_expr | star_expressions
255
256expressions[expr_ty]:
257 | a=expression b=(',' c=expression { c })+ [','] {
258 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
259 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
260 | expression
261expression[expr_ty] (memo):
262 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
263 | disjunction
264 | lambdef
265
266lambdef[expr_ty]:
267 | 'lambda' a=[lambda_parameters] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }
268lambda_parameters[arguments_ty]:
269 | a=lambda_slash_without_default b=[',' x=lambda_plain_names { x }] c=[',' y=lambda_names_with_default { y }] d=[',' z=[lambda_star_etc] { z }] {
270 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
271 | a=lambda_slash_with_default b=[',' y=lambda_names_with_default { y }] c=[',' z=[lambda_star_etc] { z }] {
272 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
273 | a=lambda_plain_names b=[',' y=lambda_names_with_default { y }] c=[',' z=[lambda_star_etc] { z }] {
274 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
275 | a=lambda_names_with_default b=[',' z=[lambda_star_etc] { z }] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
276 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
277lambda_slash_without_default[asdl_seq*]: a=lambda_plain_names ',' '/' { a }
278lambda_slash_with_default[SlashWithDefault*]: a=[n=lambda_plain_names ',' { n }] b=lambda_names_with_default ',' '/' {
279 _PyPegen_slash_with_default(p, a, b) }
280lambda_star_etc[StarEtc*]:
281 | '*' a=lambda_plain_name b=lambda_name_with_optional_default* c=[',' d=lambda_kwds { d }] [','] {
282 _PyPegen_star_etc(p, a, b, c) }
283 | '*' b=lambda_name_with_optional_default+ c=[',' d=lambda_kwds { d }] [','] {
284 _PyPegen_star_etc(p, NULL, b, c) }
285 | a=lambda_kwds [','] { _PyPegen_star_etc(p, NULL, NULL, a) }
286lambda_name_with_optional_default[NameDefaultPair*]:
287 | ',' a=lambda_plain_name b=['=' e=expression { e }] { _PyPegen_name_default_pair(p, a, b) }
288lambda_names_with_default[asdl_seq*]: a=','.lambda_name_with_default+ { a }
289lambda_name_with_default[NameDefaultPair*]:
290 | n=lambda_plain_name '=' e=expression { _PyPegen_name_default_pair(p, n, e) }
291lambda_plain_names[asdl_seq*]: a=','.(lambda_plain_name !'=')+ { a }
292lambda_plain_name[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
293lambda_kwds[arg_ty]: '**' a=lambda_plain_name { a }
294
295disjunction[expr_ty] (memo):
296 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
297 Or,
298 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
299 EXTRA) }
300 | conjunction
301conjunction[expr_ty] (memo):
302 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
303 And,
304 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
305 EXTRA) }
306 | inversion
307inversion[expr_ty] (memo):
308 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
309 | comparison
310comparison[expr_ty]:
311 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
312 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
313 | bitwise_or
314compare_op_bitwise_or_pair[CmpopExprPair*]:
315 | eq_bitwise_or
316 | noteq_bitwise_or
317 | lte_bitwise_or
318 | lt_bitwise_or
319 | gte_bitwise_or
320 | gt_bitwise_or
321 | notin_bitwise_or
322 | in_bitwise_or
323 | isnot_bitwise_or
324 | is_bitwise_or
325eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100326noteq_bitwise_or[CmpopExprPair*]:
327 | (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 +0100328lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
329lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
330gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
331gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
332notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
333in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
334isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
335is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
336
337bitwise_or[expr_ty]:
338 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
339 | bitwise_xor
340bitwise_xor[expr_ty]:
341 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
342 | bitwise_and
343bitwise_and[expr_ty]:
344 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
345 | shift_expr
346shift_expr[expr_ty]:
347 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
348 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
349 | sum
350
351sum[expr_ty]:
352 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
353 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
354 | term
355term[expr_ty]:
356 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
357 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
358 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
359 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
360 | a=term '@' b=factor { _Py_BinOp(a, MatMult, b, EXTRA) }
361 | factor
362factor[expr_ty] (memo):
363 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
364 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
365 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
366 | power
367power[expr_ty]:
368 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
369 | await_primary
370await_primary[expr_ty] (memo):
371 | AWAIT a=primary { _Py_Await(a, EXTRA) }
372 | primary
373primary[expr_ty]:
374 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
375 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
376 | a=primary '(' b=[arguments] ')' {
377 _Py_Call(a,
378 (b) ? ((expr_ty) b)->v.Call.args : NULL,
379 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
380 EXTRA) }
381 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
382 | atom
383
384slices[expr_ty]:
385 | a=slice !',' { a }
386 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
387slice[expr_ty]:
388 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
389 | a=expression { a }
390atom[expr_ty]:
391 | NAME
392 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
393 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
394 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
395 | '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") }
396 | &STRING strings
397 | NUMBER
398 | &'(' (tuple | group | genexp)
399 | &'[' (list | listcomp)
400 | &'{' (dict | set | dictcomp | setcomp)
401 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
402
403strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
404list[expr_ty]:
405 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
406listcomp[expr_ty]:
407 | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
408 | invalid_comprehension
409tuple[expr_ty]:
410 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
411 _Py_Tuple(a, Load, EXTRA) }
412group[expr_ty]: '(' a=(yield_expr | named_expression) ')' { a }
413genexp[expr_ty]:
414 | '(' a=expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
415 | invalid_comprehension
416set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
417setcomp[expr_ty]:
418 | '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
419 | invalid_comprehension
420dict[expr_ty]:
421 | '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)),
422 CHECK(_PyPegen_get_values(p, a)), EXTRA) }
423dictcomp[expr_ty]:
424 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
425kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a }
426kvpair[KeyValuePair*]:
427 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
428 | a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
429for_if_clauses[asdl_seq*]:
430 | a=(y=[ASYNC] 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })*
431 { _Py_comprehension(a, b, c, y != NULL, p->arena) })+ { a }
432
433yield_expr[expr_ty]:
434 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
435 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
436
437arguments[expr_ty] (memo):
438 | a=args [','] &')' { a }
439 | incorrect_arguments
440args[expr_ty]:
441 | a=starred_expression b=[',' c=args { c }] {
442 _Py_Call(_PyPegen_dummy_name(p),
443 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
444 : CHECK(_PyPegen_singleton_seq(p, a)),
445 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
446 EXTRA) }
447 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
448 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
449 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
450 EXTRA) }
451 | a=named_expression b=[',' c=args { c }] {
452 _Py_Call(_PyPegen_dummy_name(p),
453 (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args))
454 : CHECK(_PyPegen_singleton_seq(p, a)),
455 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
456 EXTRA) }
457kwargs[asdl_seq*]:
458 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
459 | ','.kwarg_or_starred+
460 | ','.kwarg_or_double_starred+
461starred_expression[expr_ty]:
462 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
463kwarg_or_starred[KeywordOrStarred*]:
464 | a=NAME '=' b=expression {
465 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
466 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
467kwarg_or_double_starred[KeywordOrStarred*]:
468 | a=NAME '=' b=expression {
469 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
470 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
471
472# NOTE: star_targets may contain *bitwise_or, targets may not.
473star_targets[expr_ty]:
474 | a=star_target !',' { a }
475 | a=star_target b=(',' c=star_target { c })* [','] {
476 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
477star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a }
478star_target[expr_ty] (memo):
479 | '*' a=(!'*' star_target) {
480 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
481 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
482 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
483 | star_atom
484star_atom[expr_ty]:
485 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
486 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
487 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
488 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
489
490inside_paren_ann_assign_target[expr_ty]:
491 | ann_assign_subscript_attribute_target
492 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
493 | '(' a=inside_paren_ann_assign_target ')' { a }
494
495ann_assign_subscript_attribute_target[expr_ty]:
496 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
497 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
498
499del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
500del_target[expr_ty] (memo):
501 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
502 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
503 | del_t_atom
504del_t_atom[expr_ty]:
505 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
506 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
507 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
508 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
509
510targets[asdl_seq*]: a=','.target+ [','] { a }
511target[expr_ty] (memo):
512 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
513 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
514 | t_atom
515t_primary[expr_ty]:
516 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
517 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
518 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
519 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
520 _Py_Call(a,
521 (b) ? ((expr_ty) b)->v.Call.args : NULL,
522 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
523 EXTRA) }
524 | a=atom &t_lookahead { a }
525t_lookahead: '(' | '[' | '.'
526t_atom[expr_ty]:
527 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
528 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
529 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
530 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
531
532
533# From here on, there are rules for invalid syntax with specialised error messages
534incorrect_arguments:
535 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
536 | expression for_if_clauses ',' [args | expression for_if_clauses] {
537 RAISE_SYNTAX_ERROR("Generator expression must be parenthesized") }
538 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
539invalid_named_expression:
540 | a=expression ':=' expression {
541 RAISE_SYNTAX_ERROR("cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
542invalid_assignment:
543 | list ':' { RAISE_SYNTAX_ERROR("only single target (not list) can be annotated") }
544 | tuple ':' { RAISE_SYNTAX_ERROR("only single target (not tuple) can be annotated") }
545 | expression ':' expression ['=' annotated_rhs] {
546 RAISE_SYNTAX_ERROR("illegal target for annotation") }
547 | a=expression ('=' | augassign) (yield_expr | star_expressions) {
548 RAISE_SYNTAX_ERROR("cannot assign to %s", _PyPegen_get_expr_name(a)) }
549invalid_block:
550 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
551invalid_comprehension:
552 | ('[' | '(' | '{') '*' expression for_if_clauses {
553 RAISE_SYNTAX_ERROR("iterable unpacking cannot be used in comprehension") }
554invalid_parameters:
555 | [plain_names ','] (slash_with_default | names_with_default) ',' plain_names {
556 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }