blob: ae5e4b5d4ca64d1ad2c8b79c576512c2ae10586d [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) }
55statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmt { a }
56statement_newline[asdl_stmt_seq*]:
57 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010058 | simple_stmt
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 Galindoa5634c42020-09-16 19:42:00 +010061simple_stmt[asdl_stmt_seq*]:
62 | a=small_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
63 | a[asdl_stmt_seq*]=';'.small_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.
66small_stmt[stmt_ty] (memo):
67 | 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]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300165 | 'if' a=named_expression ':' b=block c=elif_stmt {
166 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100167 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
168elif_stmt[stmt_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300169 | 'elif' a=named_expression ':' b=block c=elif_stmt {
170 _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100171 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100172else_block[asdl_stmt_seq*]: 'else' ':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100173
174while_stmt[stmt_ty]:
175 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
176
177for_stmt[stmt_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300178 | '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) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300180 | 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 Galindoc5fc1562020-04-22 23:29:27 +0100193with_item[withitem_ty]:
Batuhan Taskaya48f305f2020-10-09 12:56:48 +0300194 | e=expression 'as' t=star_target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300195 | invalid_with_item
196 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100197
198try_stmt[stmt_ty]:
199 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100200 | '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 +0100201except_block[excepthandler_ty]:
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300202 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100203 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
204 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100205finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100206
207return_stmt[stmt_ty]:
208 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
209
210raise_stmt[stmt_ty]:
211 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
212 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
213
214function_def[stmt_ty]:
215 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
216 | function_def_raw
217
218function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300219 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
220 _Py_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300221 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300222 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
223 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
224 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300225 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300226 5,
227 "Async functions are",
228 _Py_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300229 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300230 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
231 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100232func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700233 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
234 | invalid_double_type_comments
235 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100236
237params[arguments_ty]:
238 | invalid_parameters
239 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700240
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100241parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100242 | 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 +0100243 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700244 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100245 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100246 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100247 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700248 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100249 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700250
251# Some duplication here because we can't write (',' | &')'),
252# which is because we don't support empty alternatives (yet).
253#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100254slash_no_default[asdl_arg_seq*]:
255 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
256 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700257slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100258 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
259 | 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 -0700260
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100261star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700262 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100263 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700264 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100265 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700266 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300267 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700268
Guido van Rossum3941d972020-05-01 09:42:03 -0700269kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700270
271# One parameter. This *includes* a following comma and type comment.
272#
273# There are three styles:
274# - No default
275# - With default
276# - Maybe with default
277#
278# There are two alternative forms of each, to deal with type comments:
279# - Ends in a comma followed by an optional type comment
280# - No comma, optional type comment, must be followed by close paren
281# The latter form is for a final parameter without trailing comma.
282#
283param_no_default[arg_ty]:
284 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
285 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
286param_with_default[NameDefaultPair*]:
287 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
288 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
289param_maybe_default[NameDefaultPair*]:
290 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
291 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
292param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
293
294annotation[expr_ty]: ':' a=expression { a }
295default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100296
Pablo Galindoa5634c42020-09-16 19:42:00 +0100297decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100298
299class_def[stmt_ty]:
300 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
301 | class_def_raw
302class_def_raw[stmt_ty]:
303 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
304 _Py_ClassDef(a->v.Name.id,
305 (b) ? ((expr_ty) b)->v.Call.args : NULL,
306 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
307 c, NULL, EXTRA) }
308
Pablo Galindoa5634c42020-09-16 19:42:00 +0100309block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100310 | NEWLINE INDENT a=statements DEDENT { a }
311 | simple_stmt
312 | invalid_block
313
Pablo Galindoa5634c42020-09-16 19:42:00 +0100314expressions_list[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100315star_expressions[expr_ty]:
316 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300317 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
318 | a=star_expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100319 | star_expression
320star_expression[expr_ty] (memo):
321 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
322 | expression
323
Pablo Galindoa5634c42020-09-16 19:42:00 +0100324star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100325star_named_expression[expr_ty]:
326 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
327 | named_expression
328named_expression[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300329 | 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 +0100330 | expression !':='
331 | invalid_named_expression
332
333annotated_rhs[expr_ty]: yield_expr | star_expressions
334
335expressions[expr_ty]:
336 | a=expression b=(',' c=expression { c })+ [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300337 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
338 | a=expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100339 | expression
340expression[expr_ty] (memo):
341 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
342 | disjunction
343 | lambdef
344
345lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300346 | 'lambda' a=[lambda_params] ':' b=expression {
347 _Py_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100348
349lambda_params[arguments_ty]:
350 | invalid_lambda_parameters
351 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700352
353# lambda_parameters etc. duplicates parameters but without annotations
354# or type comments, and if there's no comma after a parameter, we expect
355# a colon, not a close parenthesis. (For more, see parameters above.)
356#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100357lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100358 | 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 +0100359 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700360 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100361 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100362 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100363 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700364 | 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 +0100365 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700366
Pablo Galindoa5634c42020-09-16 19:42:00 +0100367lambda_slash_no_default[asdl_arg_seq*]:
368 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
369 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700370lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100371 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
372 | 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 -0700373
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100374lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700375 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100376 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700377 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100378 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700379 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300380 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700381
382lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
383
384lambda_param_no_default[arg_ty]:
385 | a=lambda_param ',' { a }
386 | a=lambda_param &':' { a }
387lambda_param_with_default[NameDefaultPair*]:
388 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
389 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
390lambda_param_maybe_default[NameDefaultPair*]:
391 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
392 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
393lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100394
395disjunction[expr_ty] (memo):
396 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
397 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300398 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100399 EXTRA) }
400 | conjunction
401conjunction[expr_ty] (memo):
402 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
403 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300404 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100405 EXTRA) }
406 | inversion
407inversion[expr_ty] (memo):
408 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
409 | comparison
410comparison[expr_ty]:
411 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300412 _Py_Compare(
413 a,
414 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
415 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
416 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100417 | bitwise_or
418compare_op_bitwise_or_pair[CmpopExprPair*]:
419 | eq_bitwise_or
420 | noteq_bitwise_or
421 | lte_bitwise_or
422 | lt_bitwise_or
423 | gte_bitwise_or
424 | gt_bitwise_or
425 | notin_bitwise_or
426 | in_bitwise_or
427 | isnot_bitwise_or
428 | is_bitwise_or
429eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100430noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000431 | (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 +0100432lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
433lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
434gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
435gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
436notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
437in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
438isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
439is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
440
441bitwise_or[expr_ty]:
442 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
443 | bitwise_xor
444bitwise_xor[expr_ty]:
445 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
446 | bitwise_and
447bitwise_and[expr_ty]:
448 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
449 | shift_expr
450shift_expr[expr_ty]:
451 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
452 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
453 | sum
454
455sum[expr_ty]:
456 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
457 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
458 | term
459term[expr_ty]:
460 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
461 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
462 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
463 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300464 | 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 +0100465 | factor
466factor[expr_ty] (memo):
467 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
468 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
469 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
470 | power
471power[expr_ty]:
472 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
473 | await_primary
474await_primary[expr_ty] (memo):
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300475 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100476 | primary
477primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200478 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100479 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300480 | 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 +0100481 | a=primary '(' b=[arguments] ')' {
482 _Py_Call(a,
483 (b) ? ((expr_ty) b)->v.Call.args : NULL,
484 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
485 EXTRA) }
486 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
487 | atom
488
489slices[expr_ty]:
490 | a=slice !',' { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100491 | a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100492slice[expr_ty]:
493 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
494 | a=expression { a }
495atom[expr_ty]:
496 | NAME
497 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
498 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
499 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100500 | &STRING strings
501 | NUMBER
502 | &'(' (tuple | group | genexp)
503 | &'[' (list | listcomp)
504 | &'{' (dict | set | dictcomp | setcomp)
505 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
506
507strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
508list[expr_ty]:
509 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
510listcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300511 | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100512 | invalid_comprehension
513tuple[expr_ty]:
514 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
515 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300516group[expr_ty]:
517 | '(' a=(yield_expr | named_expression) ')' { a }
518 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100519genexp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300520 | '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100521 | invalid_comprehension
522set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
523setcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300524 | '{' a=expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100525 | invalid_comprehension
526dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300527 | '{' a=[double_starred_kvpairs] '}' {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300528 _Py_Dict(
529 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
530 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
531 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100532dictcomp[expr_ty]:
533 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300534 | invalid_dict_comprehension
535double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
536double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100537 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300538 | kvpair
539kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100540for_if_clauses[asdl_comprehension_seq*]:
541 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300542for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100543 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300544 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100545 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300546 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300547 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100548
549yield_expr[expr_ty]:
550 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
551 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
552
553arguments[expr_ty] (memo):
554 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200555 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100556args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100557 | 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 +0100558 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300559 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
560 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100561 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100562kwargs[asdl_seq*]:
563 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
564 | ','.kwarg_or_starred+
565 | ','.kwarg_or_double_starred+
566starred_expression[expr_ty]:
567 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
568kwarg_or_starred[KeywordOrStarred*]:
569 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300570 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100571 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300572 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100573kwarg_or_double_starred[KeywordOrStarred*]:
574 | a=NAME '=' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300575 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
576 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300577 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100578
579# NOTE: star_targets may contain *bitwise_or, targets may not.
580star_targets[expr_ty]:
581 | a=star_target !',' { a }
582 | a=star_target b=(',' c=star_target { c })* [','] {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300583 _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100584star_targets_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100585star_target[expr_ty] (memo):
586 | '*' a=(!'*' star_target) {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300587 _Py_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100588 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
589 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
590 | star_atom
591star_atom[expr_ty]:
592 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
593 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
594 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
595 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
596
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300597single_target[expr_ty]:
598 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100599 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300600 | '(' a=single_target ')' { a }
601single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100602 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
603 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
604
Pablo Galindoa5634c42020-09-16 19:42:00 +0100605del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100606del_target[expr_ty] (memo):
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300607 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
608 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100609 | del_t_atom
610del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300611 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100612 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
613 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
614 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
615
Pablo Galindoa5634c42020-09-16 19:42:00 +0100616targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100617target[expr_ty] (memo):
618 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
619 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
620 | t_atom
621t_primary[expr_ty]:
622 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
623 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300624 | a=t_primary b=genexp &t_lookahead {
625 _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100626 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
627 _Py_Call(a,
628 (b) ? ((expr_ty) b)->v.Call.args : NULL,
629 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
630 EXTRA) }
631 | a=atom &t_lookahead { a }
632t_lookahead: '(' | '[' | '.'
633t_atom[expr_ty]:
634 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
635 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
636 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
637 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
638
639
640# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200641invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100642 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300643 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
644 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300645 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
646 | args ',' a=expression for_if_clauses {
647 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100648 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300649invalid_kwarg:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300650 | a=expression '=' {
651 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
652 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100653invalid_named_expression:
654 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300655 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
656 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100657invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300658 | a=invalid_ann_assign_target ':' expression {
659 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
660 a,
661 "only single target (not %s) can be annotated",
662 _PyPegen_get_expr_name(a)
663 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300664 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300665 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300666 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300667 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100668 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300669 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100670 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100671 | a=star_expressions augassign (yield_expr | star_expressions) {
672 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300673 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100674 "'%s' is an illegal expression for augmented assignment",
675 _PyPegen_get_expr_name(a)
676 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300677invalid_ann_assign_target[expr_ty]:
678 | list
679 | tuple
680 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300681invalid_del_stmt:
682 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300683 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100684invalid_block:
685 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200686invalid_primary:
687 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100688invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300689 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
690 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300691invalid_dict_comprehension:
692 | '{' a='**' bitwise_or for_if_clauses '}' {
693 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100694invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700695 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100696 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100697invalid_lambda_parameters:
698 | lambda_param_no_default* (lambda_slash_with_default | lambda_param_with_default+) lambda_param_no_default {
699 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300700invalid_star_etc:
701 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300702 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300703invalid_lambda_star_etc:
704 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700705invalid_double_type_comments:
706 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
707 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300708invalid_with_item:
709 | expression 'as' a=expression {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300710 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300711
712invalid_for_target:
713 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300714 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300715
716invalid_group:
717 | '(' a=starred_expression ')' {
718 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300719invalid_import_from_targets:
720 | import_from_as_names ',' {
721 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }