blob: e4533b1a1b8797178b9ededaf7c872f8e20e85ff [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 {
Pablo Galindoa5634c42020-09-16 19:42:00 +010039 (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, CHECK(_PyPegen_seq_append_to_end(p, a, b)), c) }
40 | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
41 | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070042 | '*' a=expression ',' '**' b=expression {
Pablo Galindoa5634c42020-09-16 19:42:00 +010043 (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, CHECK(_PyPegen_singleton_seq(p, a)), b) }
44 | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
45 | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
46 | a[asdl_expr_seq*]=','.expression+ {a}
Guido van Rossumc001c092020-04-30 12:12:19 -070047
Pablo Galindoa5634c42020-09-16 19:42:00 +010048statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
49statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmt { a }
50statement_newline[asdl_stmt_seq*]:
51 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010052 | simple_stmt
Pablo Galindoa5634c42020-09-16 19:42:00 +010053 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010054 | ENDMARKER { _PyPegen_interactive_exit(p) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010055simple_stmt[asdl_stmt_seq*]:
56 | a=small_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
57 | a[asdl_stmt_seq*]=';'.small_stmt+ [';'] NEWLINE { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010058# NOTE: assignment MUST precede expression, else parsing a simple assignment
59# will throw a SyntaxError.
60small_stmt[stmt_ty] (memo):
61 | assignment
62 | e=star_expressions { _Py_Expr(e, EXTRA) }
63 | &'return' return_stmt
64 | &('import' | 'from') import_stmt
65 | &'raise' raise_stmt
66 | 'pass' { _Py_Pass(EXTRA) }
67 | &'del' del_stmt
68 | &'yield' yield_stmt
69 | &'assert' assert_stmt
70 | 'break' { _Py_Break(EXTRA) }
71 | 'continue' { _Py_Continue(EXTRA) }
72 | &'global' global_stmt
73 | &'nonlocal' nonlocal_stmt
74compound_stmt[stmt_ty]:
75 | &('def' | '@' | ASYNC) function_def
76 | &'if' if_stmt
77 | &('class' | '@') class_def
78 | &('with' | ASYNC) with_stmt
79 | &('for' | ASYNC) for_stmt
80 | &'try' try_stmt
81 | &'while' while_stmt
82
83# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030084assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010085 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030086 CHECK_VERSION(
87 6,
88 "Variable annotation syntax is",
89 _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
90 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030091 | a=('(' b=single_target ')' { b }
92 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030093 CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010094 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Guido van Rossumc001c092020-04-30 12:12:19 -070095 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +030096 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +010097 _Py_AugAssign(a, b->kind, c, EXTRA) }
98 | invalid_assignment
99
100augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300101 | '+=' { _PyPegen_augoperator(p, Add) }
102 | '-=' { _PyPegen_augoperator(p, Sub) }
103 | '*=' { _PyPegen_augoperator(p, Mult) }
104 | '@=' { CHECK_VERSION(5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
105 | '/=' { _PyPegen_augoperator(p, Div) }
106 | '%=' { _PyPegen_augoperator(p, Mod) }
107 | '&=' { _PyPegen_augoperator(p, BitAnd) }
108 | '|=' { _PyPegen_augoperator(p, BitOr) }
109 | '^=' { _PyPegen_augoperator(p, BitXor) }
110 | '<<=' { _PyPegen_augoperator(p, LShift) }
111 | '>>=' { _PyPegen_augoperator(p, RShift) }
112 | '**=' { _PyPegen_augoperator(p, Pow) }
113 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100114
Pablo Galindoa5634c42020-09-16 19:42:00 +0100115global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100116 _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100117nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100118 _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) }
119
120yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
121
122assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
123
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300124del_stmt[stmt_ty]:
125 | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) }
126 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100127
128import_stmt[stmt_ty]: import_name | import_from
129import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) }
130# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
131import_from[stmt_ty]:
132 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
133 _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
134 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
135 _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100136import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100137 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300138 | import_from_as_names !','
Pablo Galindoa5634c42020-09-16 19:42:00 +0100139 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300140 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100141import_from_as_names[asdl_alias_seq*]:
142 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100143import_from_as_name[alias_ty]:
144 | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
145 (b) ? ((expr_ty) b)->v.Name.id : NULL,
146 p->arena) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100147dotted_as_names[asdl_alias_seq*]:
148 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100149dotted_as_name[alias_ty]:
150 | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id,
151 (b) ? ((expr_ty) b)->v.Name.id : NULL,
152 p->arena) }
153dotted_name[expr_ty]:
154 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
155 | NAME
156
157if_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100158 | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK((asdl_stmt_seq*)_PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100159 | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
160elif_stmt[stmt_ty]:
161 | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) }
162 | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100163else_block[asdl_stmt_seq*]: 'else' ':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100164
165while_stmt[stmt_ty]:
166 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) }
167
168for_stmt[stmt_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300169 | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300170 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300171 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300172 CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300173 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100174
175with_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100176 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300177 _Py_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100178 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300179 _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100180 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300181 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100182 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300183 CHECK_VERSION(5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100184with_item[withitem_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300185 | e=expression 'as' t=target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) }
186 | invalid_with_item
187 | e=expression { _Py_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100188
189try_stmt[stmt_ty]:
190 | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100191 | '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 +0100192except_block[excepthandler_ty]:
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300193 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100194 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
195 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100196finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100197
198return_stmt[stmt_ty]:
199 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }
200
201raise_stmt[stmt_ty]:
202 | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) }
203 | 'raise' { _Py_Raise(NULL, NULL, EXTRA) }
204
205function_def[stmt_ty]:
206 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
207 | function_def_raw
208
209function_def_raw[stmt_ty]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300210 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
211 _Py_FunctionDef(n->v.Name.id,
212 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
213 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
214 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
215 CHECK_VERSION(
216 5,
217 "Async functions are",
218 _Py_AsyncFunctionDef(n->v.Name.id,
219 (params) ? params : CHECK(_PyPegen_empty_arguments(p)),
220 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
221 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100222func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700223 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
224 | invalid_double_type_comments
225 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100226
227params[arguments_ty]:
228 | invalid_parameters
229 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700230
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100231parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100232 | 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 +0100233 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700234 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100235 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100236 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100237 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700238 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100239 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700240
241# Some duplication here because we can't write (',' | &')'),
242# which is because we don't support empty alternatives (yet).
243#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100244slash_no_default[asdl_arg_seq*]:
245 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
246 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700247slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100248 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
249 | 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 -0700250
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100251star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700252 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100253 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700254 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100255 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700256 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300257 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700258
Guido van Rossum3941d972020-05-01 09:42:03 -0700259kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700260
261# One parameter. This *includes* a following comma and type comment.
262#
263# There are three styles:
264# - No default
265# - With default
266# - Maybe with default
267#
268# There are two alternative forms of each, to deal with type comments:
269# - Ends in a comma followed by an optional type comment
270# - No comma, optional type comment, must be followed by close paren
271# The latter form is for a final parameter without trailing comma.
272#
273param_no_default[arg_ty]:
274 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
275 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
276param_with_default[NameDefaultPair*]:
277 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
278 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
279param_maybe_default[NameDefaultPair*]:
280 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
281 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
282param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) }
283
284annotation[expr_ty]: ':' a=expression { a }
285default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100286
Pablo Galindoa5634c42020-09-16 19:42:00 +0100287decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100288
289class_def[stmt_ty]:
290 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
291 | class_def_raw
292class_def_raw[stmt_ty]:
293 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
294 _Py_ClassDef(a->v.Name.id,
295 (b) ? ((expr_ty) b)->v.Call.args : NULL,
296 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
297 c, NULL, EXTRA) }
298
Pablo Galindoa5634c42020-09-16 19:42:00 +0100299block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100300 | NEWLINE INDENT a=statements DEDENT { a }
301 | simple_stmt
302 | invalid_block
303
Pablo Galindoa5634c42020-09-16 19:42:00 +0100304expressions_list[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100305star_expressions[expr_ty]:
306 | a=star_expression b=(',' c=star_expression { c })+ [','] {
307 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
308 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
309 | star_expression
310star_expression[expr_ty] (memo):
311 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
312 | expression
313
Pablo Galindoa5634c42020-09-16 19:42:00 +0100314star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100315star_named_expression[expr_ty]:
316 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
317 | named_expression
318named_expression[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300319 | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100320 | expression !':='
321 | invalid_named_expression
322
323annotated_rhs[expr_ty]: yield_expr | star_expressions
324
325expressions[expr_ty]:
326 | a=expression b=(',' c=expression { c })+ [','] {
327 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
328 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) }
329 | expression
330expression[expr_ty] (memo):
331 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) }
332 | disjunction
333 | lambdef
334
335lambdef[expr_ty]:
Pablo Galindoc6483c92020-06-10 14:07:06 +0100336 | 'lambda' a=[lambda_params] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }
337
338lambda_params[arguments_ty]:
339 | invalid_lambda_parameters
340 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700341
342# lambda_parameters etc. duplicates parameters but without annotations
343# or type comments, and if there's no comma after a parameter, we expect
344# a colon, not a close parenthesis. (For more, see parameters above.)
345#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100346lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100347 | 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 +0100348 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700349 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100350 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100351 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100352 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700353 | 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 +0100354 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700355
Pablo Galindoa5634c42020-09-16 19:42:00 +0100356lambda_slash_no_default[asdl_arg_seq*]:
357 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
358 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700359lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100360 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
361 | 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 -0700362
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100363lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700364 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100365 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700366 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100367 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700368 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300369 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700370
371lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
372
373lambda_param_no_default[arg_ty]:
374 | a=lambda_param ',' { a }
375 | a=lambda_param &':' { a }
376lambda_param_with_default[NameDefaultPair*]:
377 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
378 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
379lambda_param_maybe_default[NameDefaultPair*]:
380 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
381 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
382lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100383
384disjunction[expr_ty] (memo):
385 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp(
386 Or,
387 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
388 EXTRA) }
389 | conjunction
390conjunction[expr_ty] (memo):
391 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp(
392 And,
393 CHECK(_PyPegen_seq_insert_in_front(p, a, b)),
394 EXTRA) }
395 | inversion
396inversion[expr_ty] (memo):
397 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) }
398 | comparison
399comparison[expr_ty]:
400 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
401 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) }
402 | bitwise_or
403compare_op_bitwise_or_pair[CmpopExprPair*]:
404 | eq_bitwise_or
405 | noteq_bitwise_or
406 | lte_bitwise_or
407 | lt_bitwise_or
408 | gte_bitwise_or
409 | gt_bitwise_or
410 | notin_bitwise_or
411 | in_bitwise_or
412 | isnot_bitwise_or
413 | is_bitwise_or
414eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100415noteq_bitwise_or[CmpopExprPair*]:
416 | (tok='!=' {_PyPegen_check_barry_as_flufl(p) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100417lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
418lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
419gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
420gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
421notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
422in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
423isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
424is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
425
426bitwise_or[expr_ty]:
427 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) }
428 | bitwise_xor
429bitwise_xor[expr_ty]:
430 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) }
431 | bitwise_and
432bitwise_and[expr_ty]:
433 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) }
434 | shift_expr
435shift_expr[expr_ty]:
436 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) }
437 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) }
438 | sum
439
440sum[expr_ty]:
441 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) }
442 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) }
443 | term
444term[expr_ty]:
445 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) }
446 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) }
447 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) }
448 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300449 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100450 | factor
451factor[expr_ty] (memo):
452 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) }
453 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) }
454 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) }
455 | power
456power[expr_ty]:
457 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) }
458 | await_primary
459await_primary[expr_ty] (memo):
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300460 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100461 | primary
462primary[expr_ty]:
463 | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
464 | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
465 | a=primary '(' b=[arguments] ')' {
466 _Py_Call(a,
467 (b) ? ((expr_ty) b)->v.Call.args : NULL,
468 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
469 EXTRA) }
470 | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) }
471 | atom
472
473slices[expr_ty]:
474 | a=slice !',' { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100475 | a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100476slice[expr_ty]:
477 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
478 | a=expression { a }
479atom[expr_ty]:
480 | NAME
481 | 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
482 | 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
483 | 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100484 | &STRING strings
485 | NUMBER
486 | &'(' (tuple | group | genexp)
487 | &'[' (list | listcomp)
488 | &'{' (dict | set | dictcomp | setcomp)
489 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) }
490
491strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
492list[expr_ty]:
493 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) }
494listcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300495 | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100496 | invalid_comprehension
497tuple[expr_ty]:
498 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
499 _Py_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300500group[expr_ty]:
501 | '(' a=(yield_expr | named_expression) ')' { a }
502 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100503genexp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300504 | '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100505 | invalid_comprehension
506set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
507setcomp[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300508 | '{' a=expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100509 | invalid_comprehension
510dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300511 | '{' a=[double_starred_kvpairs] '}' {
512 _Py_Dict(CHECK(_PyPegen_get_keys(p, a)), CHECK(_PyPegen_get_values(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100513dictcomp[expr_ty]:
514 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300515 | invalid_dict_comprehension
516double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
517double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100518 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300519 | kvpair
520kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100521for_if_clauses[asdl_comprehension_seq*]:
522 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300523for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100524 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300525 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100526 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300527 _Py_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300528 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100529
530yield_expr[expr_ty]:
531 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) }
532 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) }
533
534arguments[expr_ty] (memo):
535 | a=args [','] &')' { a }
536 | incorrect_arguments
537args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100538 | 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 +0100539 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
540 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)),
541 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)),
542 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100543kwargs[asdl_seq*]:
544 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
545 | ','.kwarg_or_starred+
546 | ','.kwarg_or_double_starred+
547starred_expression[expr_ty]:
548 | '*' a=expression { _Py_Starred(a, Load, EXTRA) }
549kwarg_or_starred[KeywordOrStarred*]:
550 | a=NAME '=' b=expression {
551 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
552 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300553 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100554kwarg_or_double_starred[KeywordOrStarred*]:
555 | a=NAME '=' b=expression {
556 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) }
557 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300558 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100559
560# NOTE: star_targets may contain *bitwise_or, targets may not.
561star_targets[expr_ty]:
562 | a=star_target !',' { a }
563 | a=star_target b=(',' c=star_target { c })* [','] {
564 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100565star_targets_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100566star_target[expr_ty] (memo):
567 | '*' a=(!'*' star_target) {
568 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
569 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
570 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
571 | star_atom
572star_atom[expr_ty]:
573 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
574 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) }
575 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) }
576 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) }
577
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300578single_target[expr_ty]:
579 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100580 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300581 | '(' a=single_target ')' { a }
582single_subscript_attribute_target[expr_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100583 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
584 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
585
Pablo Galindoa5634c42020-09-16 19:42:00 +0100586del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100587del_target[expr_ty] (memo):
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300588 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
589 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100590 | del_t_atom
591del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300592 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100593 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
594 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
595 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
596
Pablo Galindoa5634c42020-09-16 19:42:00 +0100597targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100598target[expr_ty] (memo):
599 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) }
600 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }
601 | t_atom
602t_primary[expr_ty]:
603 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
604 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) }
605 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
606 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
607 _Py_Call(a,
608 (b) ? ((expr_ty) b)->v.Call.args : NULL,
609 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
610 EXTRA) }
611 | a=atom &t_lookahead { a }
612t_lookahead: '(' | '[' | '.'
613t_atom[expr_ty]:
614 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
615 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
616 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) }
617 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) }
618
619
620# From here on, there are rules for invalid syntax with specialised error messages
621incorrect_arguments:
622 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300623 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
624 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300625 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
626 | args ',' a=expression for_if_clauses {
627 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100628 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300629invalid_kwarg:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300630 | a=expression '=' {
631 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
632 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100633invalid_named_expression:
634 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300635 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
636 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100637invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300638 | a=invalid_ann_assign_target ':' expression {
639 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
640 a,
641 "only single target (not %s) can be annotated",
642 _PyPegen_get_expr_name(a)
643 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300644 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300645 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300646 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300647 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100648 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300649 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100650 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100651 | a=star_expressions augassign (yield_expr | star_expressions) {
652 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300653 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100654 "'%s' is an illegal expression for augmented assignment",
655 _PyPegen_get_expr_name(a)
656 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300657invalid_ann_assign_target[expr_ty]:
658 | list
659 | tuple
660 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300661invalid_del_stmt:
662 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300663 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100664invalid_block:
665 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
666invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300667 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
668 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300669invalid_dict_comprehension:
670 | '{' a='**' bitwise_or for_if_clauses '}' {
671 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100672invalid_parameters:
Guido van Rossumc001c092020-04-30 12:12:19 -0700673 | param_no_default* (slash_with_default | param_with_default+) param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100674 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100675invalid_lambda_parameters:
676 | lambda_param_no_default* (lambda_slash_with_default | lambda_param_with_default+) lambda_param_no_default {
677 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300678invalid_star_etc:
679 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300680 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300681invalid_lambda_star_etc:
682 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700683invalid_double_type_comments:
684 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
685 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300686invalid_with_item:
687 | expression 'as' a=expression {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300688 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300689
690invalid_for_target:
691 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300692 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300693
694invalid_group:
695 | '(' a=starred_expression ')' {
696 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300697invalid_import_from_targets:
698 | import_from_as_names ',' {
699 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }