blob: d1a36f0e4d09408086d4c089b542e2ada329f711 [file] [log] [blame]
Pablo Galindob4282dd2020-06-12 00:51:44 +01001# PEG grammar for Python
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003@trailer '''
4void *
5_PyPegen_parse(Parser *p)
6{
7 // Initialize keywords
8 p->keywords = reserved_keywords;
9 p->n_keyword_lists = n_keyword_lists;
10
11 // Run parser
12 void *result = NULL;
13 if (p->start_rule == Py_file_input) {
14 result = file_rule(p);
15 } else if (p->start_rule == Py_single_input) {
16 result = interactive_rule(p);
17 } else if (p->start_rule == Py_eval_input) {
18 result = eval_rule(p);
Guido van Rossumc001c092020-04-30 12:12:19 -070019 } else if (p->start_rule == Py_func_type_input) {
20 result = func_type_rule(p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +010021 } else if (p->start_rule == Py_fstring_input) {
22 result = fstring_rule(p);
23 }
24
25 return result;
26}
27
28// The end
29'''
Guido van Rossumc001c092020-04-30 12:12:19 -070030file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010031interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) }
32eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }
Guido van Rossumc001c092020-04-30 12:12:19 -070033func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { FunctionType(a, b, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010034fstring[expr_ty]: star_expressions
35
Guido van Rossumc001c092020-04-30 12:12:19 -070036# type_expressions allow */** but ignore them
Pablo Galindoa5634c42020-09-16 19:42:00 +010037type_expressions[asdl_expr_seq*]:
Guido van Rossumc001c092020-04-30 12:12:19 -070038 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030039 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
40 p,
41 CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
42 c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010043 | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
44 | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070045 | '*' a=expression ',' '**' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030046 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
47 p,
48 CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
49 b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010050 | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
51 | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
52 | a[asdl_expr_seq*]=','.expression+ {a}
Guido van Rossumc001c092020-04-30 12:12:19 -070053
Pablo Galindoa5634c42020-09-16 19:42:00 +010054statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000055statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +010056statement_newline[asdl_stmt_seq*]:
57 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000058 | simple_stmts
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030059 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _Py_Pass(EXTRA))) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010060 | ENDMARKER { _PyPegen_interactive_exit(p) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000061simple_stmts[asdl_stmt_seq*]:
62 | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
63 | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010064# NOTE: assignment MUST precede expression, else parsing a simple assignment
65# will throw a SyntaxError.
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000066simple_stmt[stmt_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +010067 | assignment
68 | e=star_expressions { _Py_Expr(e, EXTRA) }
69 | &'return' return_stmt
70 | &('import' | 'from') import_stmt
71 | &'raise' raise_stmt
72 | 'pass' { _Py_Pass(EXTRA) }
73 | &'del' del_stmt
74 | &'yield' yield_stmt
75 | &'assert' assert_stmt
76 | 'break' { _Py_Break(EXTRA) }
77 | 'continue' { _Py_Continue(EXTRA) }
78 | &'global' global_stmt
79 | &'nonlocal' nonlocal_stmt
80compound_stmt[stmt_ty]:
81 | &('def' | '@' | ASYNC) function_def
82 | &'if' if_stmt
83 | &('class' | '@') class_def
84 | &('with' | ASYNC) with_stmt
85 | &('for' | ASYNC) for_stmt
86 | &'try' try_stmt
87 | &'while' while_stmt
88
89# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030090assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010091 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030092 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030093 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030094 6,
95 "Variable annotation syntax is",
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030096 _Py_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030097 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030098 | a=('(' b=single_target ')' { b }
99 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300100 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100101 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Guido van Rossumc001c092020-04-30 12:12:19 -0700102 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300103 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100104 _Py_AugAssign(a, b->kind, c, EXTRA) }
105 | invalid_assignment
106
107augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300108 | '+=' { _PyPegen_augoperator(p, Add) }
109 | '-=' { _PyPegen_augoperator(p, Sub) }
110 | '*=' { _PyPegen_augoperator(p, Mult) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300111 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300112 | '/=' { _PyPegen_augoperator(p, Div) }
113 | '%=' { _PyPegen_augoperator(p, Mod) }
114 | '&=' { _PyPegen_augoperator(p, BitAnd) }
115 | '|=' { _PyPegen_augoperator(p, BitOr) }
116 | '^=' { _PyPegen_augoperator(p, BitXor) }
117 | '<<=' { _PyPegen_augoperator(p, LShift) }
118 | '>>=' { _PyPegen_augoperator(p, RShift) }
119 | '**=' { _PyPegen_augoperator(p, Pow) }
120 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100121
Pablo Galindoa5634c42020-09-16 19:42:00 +0100122global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300123 _Py_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100124nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300125 _Py_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100126
127yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
128
129assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
130
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300131del_stmt[stmt_ty]:
132 | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) }
133 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100134
135import_stmt[stmt_ty]: import_name | import_from
136import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
137# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
138import_from[stmt_ty]:
139 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
140 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
141 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
142 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100143import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100144 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300145 | import_from_as_names !','
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300146 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300147 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100148import_from_as_names[asdl_alias_seq*]:
149 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100150import_from_as_name[alias_ty]:
151 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
152 (b) ? ((expr_ty) b)->v.Name.id : NULL,
153 p->arena) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100154dotted_as_names[asdl_alias_seq*]:
155 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100156dotted_as_name[alias_ty]:
157 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
158 (b) ? ((expr_ty) b)->v.Name.id : NULL,
159 p->arena) }
160dotted_name[expr_ty]:
161 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
162 | NAME
163
164if_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000165 | 'if' a=named_expression &&':' b=block c=elif_stmt {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300166 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000167 | 'if' a=named_expression &&':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100168elif_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000169 | 'elif' a=named_expression &&':' b=block c=elif_stmt {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300170 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000171 | 'elif' a=named_expression &&':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
172else_block[asdl_stmt_seq*]: 'else' &&':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100173
174while_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000175 | 'while' a=named_expression &&':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100176
177for_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000178 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300179 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000180 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300181 CHECK_VERSION(stmt_ty, 5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300182 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100183
184with_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100185 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300186 _Py_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100187 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300188 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100189 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300190 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100191 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300192 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000193 | invalid_with_stmt
194
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100195with_item[withitem_ty]:
Batuhan Taskaya48f305f2020-10-09 12:56:48 +0300196 | e=expression 'as' t=star_target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300197 | invalid_with_item
198 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100199
200try_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000201 | 'try' &&':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
202 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100203except_block[excepthandler_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000204 | 'except' e=expression t=['as' z=NAME { z }] &&':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100205 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000206 | 'except' &&':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100207finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100208
209return_stmt[stmt_ty]:
210 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
211
212raise_stmt[stmt_ty]:
213 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
214 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
215
216function_def[stmt_ty]:
217 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
218 | function_def_raw
219
220function_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000221 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300222 _Py_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300223 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300224 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000225 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300226 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300227 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300228 5,
229 "Async functions are",
230 _Py_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300231 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300232 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
233 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100234func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700235 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
236 | invalid_double_type_comments
237 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100238
239params[arguments_ty]:
240 | invalid_parameters
241 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700242
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100243parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100244 | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100245 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700246 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100247 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100248 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100249 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700250 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100251 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700252
253# Some duplication here because we can't write (',' | &')'),
254# which is because we don't support empty alternatives (yet).
255#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100256slash_no_default[asdl_arg_seq*]:
257 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
258 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700259slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100260 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
261 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700262
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100263star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700264 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100265 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700266 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100267 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700268 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300269 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700270
Guido van Rossum3941d972020-05-01 09:42:03 -0700271kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700272
273# One parameter. This *includes* a following comma and type comment.
274#
275# There are three styles:
276# - No default
277# - With default
278# - Maybe with default
279#
280# There are two alternative forms of each, to deal with type comments:
281# - Ends in a comma followed by an optional type comment
282# - No comma, optional type comment, must be followed by close paren
283# The latter form is for a final parameter without trailing comma.
284#
285param_no_default[arg_ty]:
286 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
287 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
288param_with_default[NameDefaultPair*]:
289 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
290 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
291param_maybe_default[NameDefaultPair*]:
292 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
293 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
294param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
295
296annotation[expr_ty]: ':' a=expression { a }
297default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100298
Pablo Galindoa5634c42020-09-16 19:42:00 +0100299decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100300
301class_def[stmt_ty]:
302 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
303 | class_def_raw
304class_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000305 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100306 _Py_ClassDef(a->v.Name.id,
307 (b) ? ((expr_ty) b)->v.Call.args : NULL,
308 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
309 c, NULL, EXTRA) }
310
Pablo Galindoa5634c42020-09-16 19:42:00 +0100311block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100312 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000313 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100314 | invalid_block
315
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100316star_expressions[expr_ty]:
317 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300318 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
319 | a=star_expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100320 | star_expression
321star_expression[expr_ty] (memo):
322 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
323 | expression
324
Pablo Galindoa5634c42020-09-16 19:42:00 +0100325star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100326star_named_expression[expr_ty]:
327 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
328 | named_expression
329named_expression[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300330 | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100331 | expression !':='
332 | invalid_named_expression
333
334annotated_rhs[expr_ty]: yield_expr | star_expressions
335
336expressions[expr_ty]:
337 | a=expression b=(',' c=expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300338 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
339 | a=expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100340 | expression
341expression[expr_ty] (memo):
342 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
343 | disjunction
344 | lambdef
345
346lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300347 | 'lambda' a=[lambda_params] ':' b=expression {
348 _Py_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100349
350lambda_params[arguments_ty]:
351 | invalid_lambda_parameters
352 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700353
354# lambda_parameters etc. duplicates parameters but without annotations
355# or type comments, and if there's no comma after a parameter, we expect
356# a colon, not a close parenthesis. (For more, see parameters above.)
357#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100358lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100359 | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100360 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700361 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100362 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100363 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100364 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700365 | 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 +0100366 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700367
Pablo Galindoa5634c42020-09-16 19:42:00 +0100368lambda_slash_no_default[asdl_arg_seq*]:
369 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
370 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700371lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100372 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
373 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700374
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100375lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700376 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100377 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700378 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100379 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700380 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300381 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700382
383lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
384
385lambda_param_no_default[arg_ty]:
386 | a=lambda_param ',' { a }
387 | a=lambda_param &':' { a }
388lambda_param_with_default[NameDefaultPair*]:
389 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
390 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
391lambda_param_maybe_default[NameDefaultPair*]:
392 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
393 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
394lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100395
396disjunction[expr_ty] (memo):
397 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
398 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300399 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100400 EXTRA) }
401 | conjunction
402conjunction[expr_ty] (memo):
403 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
404 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300405 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100406 EXTRA) }
407 | inversion
408inversion[expr_ty] (memo):
409 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
410 | comparison
411comparison[expr_ty]:
412 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300413 _Py_Compare(
414 a,
415 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
416 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
417 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100418 | bitwise_or
419compare_op_bitwise_or_pair[CmpopExprPair*]:
420 | eq_bitwise_or
421 | noteq_bitwise_or
422 | lte_bitwise_or
423 | lt_bitwise_or
424 | gte_bitwise_or
425 | gt_bitwise_or
426 | notin_bitwise_or
427 | in_bitwise_or
428 | isnot_bitwise_or
429 | is_bitwise_or
430eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100431noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000432 | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100433lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
434lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
435gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
436gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
437notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
438in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
439isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
440is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
441
442bitwise_or[expr_ty]:
443 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
444 | bitwise_xor
445bitwise_xor[expr_ty]:
446 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
447 | bitwise_and
448bitwise_and[expr_ty]:
449 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
450 | shift_expr
451shift_expr[expr_ty]:
452 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
453 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
454 | sum
455
456sum[expr_ty]:
457 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
458 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
459 | term
460term[expr_ty]:
461 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
462 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
463 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
464 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300465 | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100466 | factor
467factor[expr_ty] (memo):
468 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
469 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
470 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
471 | power
472power[expr_ty]:
473 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
474 | await_primary
475await_primary[expr_ty] (memo):
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300476 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100477 | primary
478primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200479 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100480 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300481 | a=primary b=genexp { _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100482 | a=primary '(' b=[arguments] ')' {
483 _Py_Call(a,
484 (b) ? ((expr_ty) b)->v.Call.args : NULL,
485 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
486 EXTRA) }
487 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
488 | atom
489
490slices[expr_ty]:
491 | a=slice !',' { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100492 | a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100493slice[expr_ty]:
494 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200495 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100496atom[expr_ty]:
497 | NAME
498 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
499 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
500 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100501 | &STRING strings
502 | NUMBER
503 | &'(' (tuple | group | genexp)
504 | &'[' (list | listcomp)
505 | &'{' (dict | set | dictcomp | setcomp)
506 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
507
508strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
509list[expr_ty]:
510 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
511listcomp[expr_ty]:
Pablo Galindo835f14f2021-01-31 22:52:56 +0000512 | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100513 | invalid_comprehension
514tuple[expr_ty]:
515 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
516 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300517group[expr_ty]:
518 | '(' a=(yield_expr | named_expression) ')' { a }
519 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100520genexp[expr_ty]:
Pablo Galindo835f14f2021-01-31 22:52:56 +0000521 | '(' a=named_expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100522 | invalid_comprehension
Pablo Galindob0aba1f2020-11-17 01:17:12 +0000523set[expr_ty]: '{' a=star_named_expressions '}' { _Py_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100524setcomp[expr_ty]:
Pablo Galindo835f14f2021-01-31 22:52:56 +0000525 | '{' a=named_expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100526 | invalid_comprehension
527dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300528 | '{' a=[double_starred_kvpairs] '}' {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300529 _Py_Dict(
530 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
531 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
532 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100533dictcomp[expr_ty]:
534 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300535 | invalid_dict_comprehension
536double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
537double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100538 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300539 | kvpair
540kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100541for_if_clauses[asdl_comprehension_seq*]:
542 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300543for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100544 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300545 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100546 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300547 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300548 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100549
550yield_expr[expr_ty]:
551 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
552 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
553
554arguments[expr_ty] (memo):
555 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200556 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100557args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100558 | a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100559 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300560 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
561 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100562 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100563kwargs[asdl_seq*]:
564 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
565 | ','.kwarg_or_starred+
566 | ','.kwarg_or_double_starred+
567starred_expression[expr_ty]:
568 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
569kwarg_or_starred[KeywordOrStarred*]:
570 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300571 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100572 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300573 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100574kwarg_or_double_starred[KeywordOrStarred*]:
575 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300576 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
577 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300578 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100579
580# NOTE: star_targets may contain *bitwise_or, targets may not.
581star_targets[expr_ty]:
582 | a=star_target !',' { a }
583 | a=star_target b=(',' c=star_target { c })* [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300584 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200585star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
586star_targets_tuple_seq[asdl_expr_seq*]:
587 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
588 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100589star_target[expr_ty] (memo):
590 | '*' a=(!'*' star_target) {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300591 _Py_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200592 | target_with_star_atom
593target_with_star_atom[expr_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100594 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
595 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
596 | star_atom
597star_atom[expr_ty]:
598 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200599 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
600 | '(' a=[star_targets_tuple_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
601 | '[' a=[star_targets_list_seq] ']' { _Py_List(a, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100602
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300603single_target[expr_ty]:
604 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100605 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300606 | '(' a=single_target ')' { a }
607single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100608 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
609 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
610
Pablo Galindoa5634c42020-09-16 19:42:00 +0100611del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100612del_target[expr_ty] (memo):
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300613 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
614 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100615 | del_t_atom
616del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300617 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100618 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
619 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
620 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
621
Pablo Galindoa5634c42020-09-16 19:42:00 +0100622targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100623target[expr_ty] (memo):
624 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
625 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
626 | t_atom
627t_primary[expr_ty]:
628 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
629 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300630 | a=t_primary b=genexp &t_lookahead {
631 _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100632 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
633 _Py_Call(a,
634 (b) ? ((expr_ty) b)->v.Call.args : NULL,
635 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
636 EXTRA) }
637 | a=atom &t_lookahead { a }
638t_lookahead: '(' | '[' | '.'
639t_atom[expr_ty]:
640 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
641 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
642 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
643 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
644
645
646# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200647invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100648 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300649 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
650 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300651 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
652 | args ',' a=expression for_if_clauses {
653 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100654 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300655invalid_kwarg:
Pablo Galindo43c4fb62020-12-13 16:46:48 +0000656 | expression a='=' {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300657 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
658 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100659invalid_named_expression:
660 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300661 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
662 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100663invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300664 | a=invalid_ann_assign_target ':' expression {
665 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
666 a,
667 "only single target (not %s) can be annotated",
668 _PyPegen_get_expr_name(a)
669 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300670 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300671 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300672 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300673 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100674 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300675 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100676 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100677 | a=star_expressions augassign (yield_expr | star_expressions) {
678 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300679 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100680 "'%s' is an illegal expression for augmented assignment",
681 _PyPegen_get_expr_name(a)
682 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300683invalid_ann_assign_target[expr_ty]:
684 | list
685 | tuple
686 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300687invalid_del_stmt:
688 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300689 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100690invalid_block:
691 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200692invalid_primary:
693 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100694invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300695 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
696 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000697 | ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
Pablo Galindo835f14f2021-01-31 22:52:56 +0000698 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300699invalid_dict_comprehension:
700 | '{' a='**' bitwise_or for_if_clauses '}' {
701 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100702invalid_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200703 | param_no_default* invalid_parameters_helper param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100704 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200705invalid_parameters_helper: # This is only there to avoid type errors
706 | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
707 | param_with_default+
Pablo Galindoc6483c92020-06-10 14:07:06 +0100708invalid_lambda_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200709 | lambda_param_no_default* invalid_lambda_parameters_helper lambda_param_no_default {
Pablo Galindoc6483c92020-06-10 14:07:06 +0100710 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200711invalid_lambda_parameters_helper:
712 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
713 | lambda_param_with_default+
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300714invalid_star_etc:
715 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300716 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300717invalid_lambda_star_etc:
718 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700719invalid_double_type_comments:
720 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
721 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300722invalid_with_item:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000723 | expression 'as' a=expression &(',' | ')' | ':') {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300724 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300725
726invalid_for_target:
727 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300728 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300729
730invalid_group:
731 | '(' a=starred_expression ')' {
732 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300733invalid_import_from_targets:
734 | import_from_as_names ',' {
735 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000736
737invalid_with_stmt:
738 | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
739 | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'