blob: 56daca054c8b5482c89f23abf29b23044efbe7bc [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;
Pablo Galindob2802482021-04-15 21:38:45 +010010 p->soft_keywords = soft_keywords;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010011
12 // Run parser
13 void *result = NULL;
14 if (p->start_rule == Py_file_input) {
15 result = file_rule(p);
16 } else if (p->start_rule == Py_single_input) {
17 result = interactive_rule(p);
18 } else if (p->start_rule == Py_eval_input) {
19 result = eval_rule(p);
Guido van Rossumc001c092020-04-30 12:12:19 -070020 } else if (p->start_rule == Py_func_type_input) {
21 result = func_type_rule(p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +010022 } else if (p->start_rule == Py_fstring_input) {
23 result = fstring_rule(p);
24 }
25
26 return result;
27}
28
29// The end
30'''
Guido van Rossumc001c092020-04-30 12:12:19 -070031file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +020032interactive[mod_ty]: a=statement_newline { _PyAST_Interactive(a, p->arena) }
33eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _PyAST_Expression(a, p->arena) }
34func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _PyAST_FunctionType(a, b, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010035fstring[expr_ty]: star_expressions
36
Guido van Rossumc001c092020-04-30 12:12:19 -070037# type_expressions allow */** but ignore them
Pablo Galindoa5634c42020-09-16 19:42:00 +010038type_expressions[asdl_expr_seq*]:
Guido van Rossumc001c092020-04-30 12:12:19 -070039 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030040 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
41 p,
42 CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
43 c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010044 | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
45 | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070046 | '*' a=expression ',' '**' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030047 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
48 p,
49 CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
50 b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010051 | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
52 | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
53 | a[asdl_expr_seq*]=','.expression+ {a}
Guido van Rossumc001c092020-04-30 12:12:19 -070054
Pablo Galindoa5634c42020-09-16 19:42:00 +010055statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000056statement[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 +010057statement_newline[asdl_stmt_seq*]:
58 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000059 | simple_stmts
Victor Stinnerd27f8d22021-04-07 21:34:22 +020060 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010061 | ENDMARKER { _PyPegen_interactive_exit(p) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000062simple_stmts[asdl_stmt_seq*]:
63 | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
64 | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010065# NOTE: assignment MUST precede expression, else parsing a simple assignment
66# will throw a SyntaxError.
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000067simple_stmt[stmt_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +010068 | assignment
Victor Stinnerd27f8d22021-04-07 21:34:22 +020069 | e=star_expressions { _PyAST_Expr(e, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010070 | &'return' return_stmt
71 | &('import' | 'from') import_stmt
72 | &'raise' raise_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020073 | 'pass' { _PyAST_Pass(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010074 | &'del' del_stmt
75 | &'yield' yield_stmt
76 | &'assert' assert_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020077 | 'break' { _PyAST_Break(EXTRA) }
78 | 'continue' { _PyAST_Continue(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010079 | &'global' global_stmt
80 | &'nonlocal' nonlocal_stmt
81compound_stmt[stmt_ty]:
82 | &('def' | '@' | ASYNC) function_def
83 | &'if' if_stmt
84 | &('class' | '@') class_def
85 | &('with' | ASYNC) with_stmt
86 | &('for' | ASYNC) for_stmt
87 | &'try' try_stmt
88 | &'while' while_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -080089 | match_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +010090
91# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030092assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010093 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030094 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030095 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030096 6,
97 "Variable annotation syntax is",
Victor Stinnerd27f8d22021-04-07 21:34:22 +020098 _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030099 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300100 | a=('(' b=single_target ')' { b }
101 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200102 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100103 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200104 _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300105 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200106 _PyAST_AugAssign(a, b->kind, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100107 | invalid_assignment
108
109augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300110 | '+=' { _PyPegen_augoperator(p, Add) }
111 | '-=' { _PyPegen_augoperator(p, Sub) }
112 | '*=' { _PyPegen_augoperator(p, Mult) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300113 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300114 | '/=' { _PyPegen_augoperator(p, Div) }
115 | '%=' { _PyPegen_augoperator(p, Mod) }
116 | '&=' { _PyPegen_augoperator(p, BitAnd) }
117 | '|=' { _PyPegen_augoperator(p, BitOr) }
118 | '^=' { _PyPegen_augoperator(p, BitXor) }
119 | '<<=' { _PyPegen_augoperator(p, LShift) }
120 | '>>=' { _PyPegen_augoperator(p, RShift) }
121 | '**=' { _PyPegen_augoperator(p, Pow) }
122 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100123
Pablo Galindoa5634c42020-09-16 19:42:00 +0100124global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200125 _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100126nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200127 _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100128
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200129yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100130
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200131assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100132
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300133del_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200134 | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300135 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100136
137import_stmt[stmt_ty]: import_name | import_from
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200138import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100139# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
140import_from[stmt_ty]:
141 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200142 _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100143 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200144 _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100145import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100146 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300147 | import_from_as_names !','
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400148 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300149 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100150import_from_as_names[asdl_alias_seq*]:
151 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100152import_from_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200153 | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100154 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400155 EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100156dotted_as_names[asdl_alias_seq*]:
157 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100158dotted_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200159 | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100160 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400161 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100162dotted_name[expr_ty]:
163 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
164 | NAME
165
166if_stmt[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100167 | invalid_if_stmt
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100168 | 'if' a=named_expression ':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200169 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100170 | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100171elif_stmt[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100172 | invalid_elif_stmt
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100173 | 'elif' a=named_expression ':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200174 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100175 | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100176else_block[asdl_stmt_seq*]:
177 | invalid_else_stmt
178 | 'else' &&':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100179
180while_stmt[stmt_ty]:
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100181 | invalid_while_stmt
Pablo Galindo56c95df2021-04-21 15:28:21 +0100182 | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100183
184for_stmt[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100185 | invalid_for_stmt
Pablo Galindo58fb1562021-02-02 19:54:22 +0000186 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200187 _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000188 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200189 CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300190 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100191
192with_stmt[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100193 | invalid_with_stmt_indent
Pablo Galindoa5634c42020-09-16 19:42:00 +0100194 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200195 _PyAST_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100196 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200197 _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100198 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200199 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100200 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200201 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000202 | invalid_with_stmt
203
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100204with_item[withitem_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200205 | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300206 | invalid_with_item
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200207 | e=expression { _PyAST_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100208
209try_stmt[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100210 | invalid_try_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200211 | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
212 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100213except_block[excepthandler_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100214 | invalid_except_stmt_indent
Pablo Galindo206cbda2021-02-07 18:42:21 +0000215 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200216 _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
217 | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100218 | invalid_except_stmt
219finally_block[asdl_stmt_seq*]:
220 | invalid_finally_stmt
221 | 'finally' &&':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100222
Brandt Bucher145bf262021-02-26 14:51:55 -0800223match_stmt[stmt_ty]:
224 | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200225 CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000226 | invalid_match_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -0800227subject_expr[expr_ty]:
228 | value=star_named_expression ',' values=star_named_expressions? {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200229 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800230 | named_expression
231case_block[match_case_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100232 | invalid_case_block
Brandt Bucher145bf262021-02-26 14:51:55 -0800233 | "case" pattern=patterns guard=guard? ':' body=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200234 _PyAST_match_case(pattern, guard, body, p->arena) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800235guard[expr_ty]: 'if' guard=named_expression { guard }
236
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000237patterns[pattern_ty]:
238 | patterns[asdl_pattern_seq*]=open_sequence_pattern {
239 _PyAST_MatchSequence(patterns, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800240 | pattern
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000241pattern[pattern_ty]:
Brandt Bucher145bf262021-02-26 14:51:55 -0800242 | as_pattern
243 | or_pattern
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000244as_pattern[pattern_ty]:
245 | pattern=or_pattern 'as' target=pattern_capture_target {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200246 _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
Pablo Galindoa8c418d2021-06-18 22:15:57 +0100247 | invalid_as_pattern
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000248or_pattern[pattern_ty]:
249 | patterns[asdl_pattern_seq*]='|'.closed_pattern+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200250 asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) }
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000251closed_pattern[pattern_ty]:
Brandt Bucher145bf262021-02-26 14:51:55 -0800252 | literal_pattern
253 | capture_pattern
254 | wildcard_pattern
255 | value_pattern
256 | group_pattern
257 | sequence_pattern
258 | mapping_pattern
259 | class_pattern
260
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000261# Literal patterns are used for equality and identity constraints
262literal_pattern[pattern_ty]:
263 | value=signed_number !('+' | '-') { _PyAST_MatchValue(value, EXTRA) }
264 | value=complex_number { _PyAST_MatchValue(value, EXTRA) }
265 | value=strings { _PyAST_MatchValue(value, EXTRA) }
266 | 'None' { _PyAST_MatchSingleton(Py_None, EXTRA) }
267 | 'True' { _PyAST_MatchSingleton(Py_True, EXTRA) }
268 | 'False' { _PyAST_MatchSingleton(Py_False, EXTRA) }
269
270# Literal expressions are used to restrict permitted mapping pattern keys
271literal_expr[expr_ty]:
Brandt Bucher145bf262021-02-26 14:51:55 -0800272 | signed_number !('+' | '-')
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000273 | complex_number
Brandt Bucher145bf262021-02-26 14:51:55 -0800274 | strings
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200275 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
276 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
277 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000278
279complex_number[expr_ty]:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -0700280 | real=signed_real_number '+' imag=imaginary_number {
281 _PyAST_BinOp(real, Add, imag, EXTRA) }
282 | real=signed_real_number '-' imag=imaginary_number {
283 _PyAST_BinOp(real, Sub, imag, EXTRA) }
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000284
Brandt Bucher145bf262021-02-26 14:51:55 -0800285signed_number[expr_ty]:
286 | NUMBER
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200287 | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800288
Brandt Bucherdbe60ee2021-04-29 17:19:28 -0700289signed_real_number[expr_ty]:
290 | real_number
291 | '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }
292
293real_number[expr_ty]:
294 | real=NUMBER { _PyPegen_ensure_real(p, real) }
295
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000296imaginary_number[expr_ty]:
297 | imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
298
299capture_pattern[pattern_ty]:
300 | target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }
301
302pattern_capture_target[expr_ty]:
Brandt Bucher145bf262021-02-26 14:51:55 -0800303 | !"_" name=NAME !('.' | '(' | '=') {
304 _PyPegen_set_expr_context(p, name, Store) }
305
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000306wildcard_pattern[pattern_ty]:
307 | "_" { _PyAST_MatchAs(NULL, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800308
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000309value_pattern[pattern_ty]:
310 | attr=attr !('.' | '(' | '=') { _PyAST_MatchValue(attr, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800311attr[expr_ty]:
312 | value=name_or_attr '.' attr=NAME {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200313 _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800314name_or_attr[expr_ty]:
315 | attr
316 | NAME
317
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000318group_pattern[pattern_ty]:
Brandt Bucher145bf262021-02-26 14:51:55 -0800319 | '(' pattern=pattern ')' { pattern }
320
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000321sequence_pattern[pattern_ty]:
322 | '[' patterns=maybe_sequence_pattern? ']' { _PyAST_MatchSequence(patterns, EXTRA) }
323 | '(' patterns=open_sequence_pattern? ')' { _PyAST_MatchSequence(patterns, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800324open_sequence_pattern[asdl_seq*]:
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000325 | pattern=maybe_star_pattern ',' patterns=maybe_sequence_pattern? {
326 _PyPegen_seq_insert_in_front(p, pattern, patterns) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800327maybe_sequence_pattern[asdl_seq*]:
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000328 | patterns=','.maybe_star_pattern+ ','? { patterns }
329maybe_star_pattern[pattern_ty]:
Brandt Bucher145bf262021-02-26 14:51:55 -0800330 | star_pattern
331 | pattern
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000332star_pattern[pattern_ty]:
333 | '*' target=pattern_capture_target {
334 _PyAST_MatchStar(target->v.Name.id, EXTRA) }
335 | '*' wildcard_pattern {
336 _PyAST_MatchStar(NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800337
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000338mapping_pattern[pattern_ty]:
339 | '{' '}' {
340 _PyAST_MatchMapping(NULL, NULL, NULL, EXTRA) }
341 | '{' rest=double_star_pattern ','? '}' {
342 _PyAST_MatchMapping(NULL, NULL, rest->v.Name.id, EXTRA) }
343 | '{' items=items_pattern ',' rest=double_star_pattern ','? '}' {
344 _PyAST_MatchMapping(
345 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)),
346 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
347 rest->v.Name.id,
348 EXTRA) }
349 | '{' items=items_pattern ','? '}' {
350 _PyAST_MatchMapping(
351 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)),
352 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
353 NULL,
354 EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800355items_pattern[asdl_seq*]:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -0700356 | ','.key_value_pattern+
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000357key_value_pattern[KeyPatternPair*]:
358 | key=(literal_expr | attr) ':' pattern=pattern {
359 _PyPegen_key_pattern_pair(p, key, pattern) }
360double_star_pattern[expr_ty]:
361 | '**' target=pattern_capture_target { target }
Brandt Bucher145bf262021-02-26 14:51:55 -0800362
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000363class_pattern[pattern_ty]:
364 | cls=name_or_attr '(' ')' {
365 _PyAST_MatchClass(cls, NULL, NULL, NULL, EXTRA) }
366 | cls=name_or_attr '(' patterns=positional_patterns ','? ')' {
367 _PyAST_MatchClass(cls, patterns, NULL, NULL, EXTRA) }
368 | cls=name_or_attr '(' keywords=keyword_patterns ','? ')' {
369 _PyAST_MatchClass(
370 cls, NULL,
371 CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p,
372 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
373 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
374 EXTRA) }
375 | cls=name_or_attr '(' patterns=positional_patterns ',' keywords=keyword_patterns ','? ')' {
376 _PyAST_MatchClass(
377 cls,
378 patterns,
379 CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p,
380 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
381 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
382 EXTRA) }
383positional_patterns[asdl_pattern_seq*]:
384 | args[asdl_pattern_seq*]=','.pattern+ { args }
385keyword_patterns[asdl_seq*]:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -0700386 | ','.keyword_pattern+
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000387keyword_pattern[KeyPatternPair*]:
388 | arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800389
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100390return_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200391 | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100392
393raise_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200394 | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
395 | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100396
397function_def[stmt_ty]:
398 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
399 | function_def_raw
400
401function_def_raw[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100402 | invalid_def_raw
Pablo Galindo58fb1562021-02-02 19:54:22 +0000403 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200404 _PyAST_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300405 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300406 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000407 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300408 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300409 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300410 5,
411 "Async functions are",
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200412 _PyAST_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300413 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300414 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
415 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100416func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700417 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
418 | invalid_double_type_comments
419 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100420
421params[arguments_ty]:
422 | invalid_parameters
423 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700424
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100425parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100426 | 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 +0100427 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700428 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100429 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100430 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100431 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700432 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100433 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700434
435# Some duplication here because we can't write (',' | &')'),
436# which is because we don't support empty alternatives (yet).
437#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100438slash_no_default[asdl_arg_seq*]:
439 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
440 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700441slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100442 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
443 | 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 -0700444
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100445star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700446 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100447 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700448 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100449 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700450 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300451 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700452
Guido van Rossum3941d972020-05-01 09:42:03 -0700453kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700454
455# One parameter. This *includes* a following comma and type comment.
456#
457# There are three styles:
458# - No default
459# - With default
460# - Maybe with default
461#
462# There are two alternative forms of each, to deal with type comments:
463# - Ends in a comma followed by an optional type comment
464# - No comma, optional type comment, must be followed by close paren
465# The latter form is for a final parameter without trailing comma.
466#
467param_no_default[arg_ty]:
468 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
469 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
470param_with_default[NameDefaultPair*]:
471 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
472 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
473param_maybe_default[NameDefaultPair*]:
474 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
475 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200476param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700477
478annotation[expr_ty]: ':' a=expression { a }
479default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100480
Pablo Galindoa5634c42020-09-16 19:42:00 +0100481decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100482
483class_def[stmt_ty]:
484 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
485 | class_def_raw
486class_def_raw[stmt_ty]:
Pablo Galindo56c95df2021-04-21 15:28:21 +0100487 | invalid_class_def_raw
Pablo Galindo58fb1562021-02-02 19:54:22 +0000488 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200489 _PyAST_ClassDef(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100490 (b) ? ((expr_ty) b)->v.Call.args : NULL,
491 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
492 c, NULL, EXTRA) }
493
Pablo Galindoa5634c42020-09-16 19:42:00 +0100494block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100495 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000496 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100497 | invalid_block
498
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100499star_expressions[expr_ty]:
500 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200501 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
502 | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100503 | star_expression
504star_expression[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200505 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100506 | expression
507
Pablo Galindoa5634c42020-09-16 19:42:00 +0100508star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100509star_named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200510 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100511 | named_expression
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100512
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100513
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700514assigment_expression[expr_ty]:
Pablo Galindod9151cb2021-04-13 02:32:33 +0100515 | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700516
517named_expression[expr_ty]:
518 | assigment_expression
519 | invalid_named_expression
Pablo Galindod9151cb2021-04-13 02:32:33 +0100520 | expression !':='
521
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100522annotated_rhs[expr_ty]: yield_expr | star_expressions
523
524expressions[expr_ty]:
525 | a=expression b=(',' c=expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200526 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
527 | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100528 | expression
529expression[expr_ty] (memo):
Pablo Galindob2802482021-04-15 21:38:45 +0100530 | invalid_expression
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200531 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100532 | disjunction
533 | lambdef
534
535lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300536 | 'lambda' a=[lambda_params] ':' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200537 _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100538
539lambda_params[arguments_ty]:
540 | invalid_lambda_parameters
541 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700542
543# lambda_parameters etc. duplicates parameters but without annotations
544# or type comments, and if there's no comma after a parameter, we expect
545# a colon, not a close parenthesis. (For more, see parameters above.)
546#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100547lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100548 | 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 +0100549 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700550 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100551 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100552 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100553 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700554 | 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 +0100555 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700556
Pablo Galindoa5634c42020-09-16 19:42:00 +0100557lambda_slash_no_default[asdl_arg_seq*]:
558 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
559 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700560lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100561 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
562 | 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 -0700563
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100564lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700565 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100566 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700567 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100568 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700569 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300570 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700571
572lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
573
574lambda_param_no_default[arg_ty]:
575 | a=lambda_param ',' { a }
576 | a=lambda_param &':' { a }
577lambda_param_with_default[NameDefaultPair*]:
578 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
579 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
580lambda_param_maybe_default[NameDefaultPair*]:
581 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
582 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200583lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100584
585disjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200586 | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100587 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300588 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100589 EXTRA) }
590 | conjunction
591conjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200592 | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100593 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300594 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100595 EXTRA) }
596 | inversion
597inversion[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200598 | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100599 | comparison
600comparison[expr_ty]:
601 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200602 _PyAST_Compare(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300603 a,
604 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
605 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
606 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100607 | bitwise_or
608compare_op_bitwise_or_pair[CmpopExprPair*]:
609 | eq_bitwise_or
610 | noteq_bitwise_or
611 | lte_bitwise_or
612 | lt_bitwise_or
613 | gte_bitwise_or
614 | gt_bitwise_or
615 | notin_bitwise_or
616 | in_bitwise_or
617 | isnot_bitwise_or
618 | is_bitwise_or
619eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100620noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000621 | (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 +0100622lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
623lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
624gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
625gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
626notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
627in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
628isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
629is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
630
631bitwise_or[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200632 | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100633 | bitwise_xor
634bitwise_xor[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200635 | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100636 | bitwise_and
637bitwise_and[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200638 | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100639 | shift_expr
640shift_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200641 | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
642 | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100643 | sum
644
645sum[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200646 | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
647 | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100648 | term
649term[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200650 | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) }
651 | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) }
652 | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
653 | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
654 | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100655 | factor
656factor[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200657 | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
658 | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
659 | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100660 | power
661power[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200662 | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100663 | await_primary
664await_primary[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200665 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100666 | primary
667primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200668 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200669 | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
670 | a=primary b=genexp { _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100671 | a=primary '(' b=[arguments] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200672 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100673 (b) ? ((expr_ty) b)->v.Call.args : NULL,
674 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
675 EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200676 | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100677 | atom
678
679slices[expr_ty]:
680 | a=slice !',' { a }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200681 | a[asdl_expr_seq*]=','.slice+ [','] { _PyAST_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100682slice[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200683 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200684 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100685atom[expr_ty]:
686 | NAME
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200687 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
688 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
689 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100690 | &STRING strings
691 | NUMBER
692 | &'(' (tuple | group | genexp)
693 | &'[' (list | listcomp)
694 | &'{' (dict | set | dictcomp | setcomp)
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200695 | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100696
697strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
698list[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200699 | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100700listcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200701 | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100702 | invalid_comprehension
703tuple[expr_ty]:
704 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200705 _PyAST_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300706group[expr_ty]:
707 | '(' a=(yield_expr | named_expression) ')' { a }
708 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100709genexp[expr_ty]:
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700710 | '(' a=( assigment_expression | expression !':=') b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100711 | invalid_comprehension
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200712set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100713setcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200714 | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100715 | invalid_comprehension
716dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300717 | '{' a=[double_starred_kvpairs] '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200718 _PyAST_Dict(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300719 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
720 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
721 EXTRA) }
Pablo Galindoda743502021-04-15 14:06:39 +0100722 | '{' invalid_double_starred_kvpairs '}'
723
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100724dictcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200725 | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300726 | invalid_dict_comprehension
727double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
728double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100729 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300730 | kvpair
731kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100732for_if_clauses[asdl_comprehension_seq*]:
733 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300734for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100735 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200736 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100737 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200738 _PyAST_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300739 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100740
741yield_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200742 | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
743 | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100744
745arguments[expr_ty] (memo):
746 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200747 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100748args[expr_ty]:
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700749 | a[asdl_expr_seq*]=','.(starred_expression | ( assigment_expression | expression !':=') !'=')+ b=[',' k=kwargs {k}] {
750 _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200751 | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300752 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
753 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100754 EXTRA) }
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700755
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100756kwargs[asdl_seq*]:
757 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
758 | ','.kwarg_or_starred+
759 | ','.kwarg_or_double_starred+
760starred_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200761 | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100762kwarg_or_starred[KeywordOrStarred*]:
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700763 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100764 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200765 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100766 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
767kwarg_or_double_starred[KeywordOrStarred*]:
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700768 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100769 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200770 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
771 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100772
773# NOTE: star_targets may contain *bitwise_or, targets may not.
774star_targets[expr_ty]:
775 | a=star_target !',' { a }
776 | a=star_target b=(',' c=star_target { c })* [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200777 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200778star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
779star_targets_tuple_seq[asdl_expr_seq*]:
780 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
781 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100782star_target[expr_ty] (memo):
783 | '*' a=(!'*' star_target) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200784 _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200785 | target_with_star_atom
786target_with_star_atom[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200787 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
788 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100789 | star_atom
790star_atom[expr_ty]:
791 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200792 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200793 | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) }
794 | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100795
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300796single_target[expr_ty]:
797 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100798 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300799 | '(' a=single_target ')' { a }
800single_subscript_attribute_target[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200801 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
802 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100803
Pablo Galindoa5634c42020-09-16 19:42:00 +0100804del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100805del_target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200806 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) }
807 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100808 | del_t_atom
809del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300810 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100811 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200812 | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) }
813 | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100814
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100815t_primary[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200816 | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
817 | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300818 | a=t_primary b=genexp &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200819 _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100820 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200821 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100822 (b) ? ((expr_ty) b)->v.Call.args : NULL,
823 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
824 EXTRA) }
825 | a=atom &t_lookahead { a }
826t_lookahead: '(' | '[' | '.'
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100827
828# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200829invalid_arguments:
Pablo Galindoa77aac42021-04-23 14:27:05 +0100830 | a=args ',' '*' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable argument unpacking follows keyword argument unpacking") }
831 | a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
832 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, comprehension_ty)->target, "Generator expression must be parenthesized") }
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700833 | a=NAME b='=' expression for_if_clauses {
834 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300835 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
Pablo Galindoa77aac42021-04-23 14:27:05 +0100836 | args ',' a=expression b=for_if_clauses {
837 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100838 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300839invalid_kwarg:
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700840 | a=NAME b='=' expression for_if_clauses {
841 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700842 | !(NAME '=') a=expression b='=' {
Pablo Galindoa77aac42021-04-23 14:27:05 +0100843 RAISE_SYNTAX_ERROR_KNOWN_RANGE(
844 a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindob2802482021-04-15 21:38:45 +0100845
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700846expression_without_invalid[expr_ty]:
847 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
848 | disjunction
849 | lambdef
Pablo Galindob2802482021-04-15 21:38:45 +0100850invalid_expression:
851 # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
852 # Soft keywords need to also be ignored because they can be parsed as NAME NAME
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700853 | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
Pablo Galindoa77aac42021-04-23 14:27:05 +0100854 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
Pablo Galindob2802482021-04-15 21:38:45 +0100855
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100856invalid_named_expression:
857 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300858 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
859 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -0700860 | a=NAME '=' b=bitwise_or !('='|':=') {
861 p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
862 | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {
863 p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100864 _PyPegen_get_expr_name(a)) }
865
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100866invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300867 | a=invalid_ann_assign_target ':' expression {
868 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
869 a,
870 "only single target (not %s) can be annotated",
871 _PyPegen_get_expr_name(a)
872 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300873 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300874 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300875 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300876 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100877 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300878 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100879 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100880 | a=star_expressions augassign (yield_expr | star_expressions) {
Brandt Bucher145bf262021-02-26 14:51:55 -0800881 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300882 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100883 "'%s' is an illegal expression for augmented assignment",
884 _PyPegen_get_expr_name(a)
885 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300886invalid_ann_assign_target[expr_ty]:
887 | list
888 | tuple
889 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300890invalid_del_stmt:
891 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300892 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100893invalid_block:
894 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200895invalid_primary:
896 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100897invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300898 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
899 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindoa77aac42021-04-23 14:27:05 +0100900 | ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses {
901 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, expr_ty),
902 "did you forget parentheses around the comprehension target?") }
903 | ('[' | '{') a=star_named_expression b=',' for_if_clauses {
904 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget parentheses around the comprehension target?") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300905invalid_dict_comprehension:
906 | '{' a='**' bitwise_or for_if_clauses '}' {
907 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100908invalid_parameters:
Pablo Galindoa77aac42021-04-23 14:27:05 +0100909 | param_no_default* invalid_parameters_helper a=param_no_default {
910 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200911invalid_parameters_helper: # This is only there to avoid type errors
912 | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
913 | param_with_default+
Pablo Galindoc6483c92020-06-10 14:07:06 +0100914invalid_lambda_parameters:
Pablo Galindoa77aac42021-04-23 14:27:05 +0100915 | lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default {
916 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200917invalid_lambda_parameters_helper:
918 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
919 | lambda_param_with_default+
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300920invalid_star_etc:
Pablo Galindoa77aac42021-04-23 14:27:05 +0100921 | a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300922 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300923invalid_lambda_star_etc:
924 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700925invalid_double_type_comments:
926 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
927 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300928invalid_with_item:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000929 | expression 'as' a=expression &(',' | ')' | ':') {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300930 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300931
932invalid_for_target:
933 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300934 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300935
936invalid_group:
937 | '(' a=starred_expression ')' {
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100938 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
Pablo Galindo8efad612021-03-24 19:34:17 +0000939 | '(' a='**' expression ')' {
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100940 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300941invalid_import_from_targets:
942 | import_from_as_names ',' {
943 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000944
945invalid_with_stmt:
946 | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
947 | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
Pablo Galindo56c95df2021-04-21 15:28:21 +0100948invalid_with_stmt_indent:
949 | [ASYNC] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT {
950 RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
951 | [ASYNC] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT {
952 RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
Pablo Galindo206cbda2021-02-07 18:42:21 +0000953
Pablo Galindo56c95df2021-04-21 15:28:21 +0100954invalid_try_stmt:
955 | a='try' ':' NEWLINE !INDENT {
956 RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
Pablo Galindoe53f72a2021-06-04 00:11:43 +0100957 | 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100958invalid_except_stmt:
Pablo Galindo206cbda2021-02-07 18:42:21 +0000959 | 'except' a=expression ',' expressions ['as' NAME ] ':' {
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -0700960 RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100961 | a='except' expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
962 | a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
963invalid_finally_stmt:
964 | a='finally' ':' NEWLINE !INDENT {
965 RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) }
966invalid_except_stmt_indent:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -0700967 | a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
Pablo Galindo56c95df2021-04-21 15:28:21 +0100968 RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
969 | a='except' ':' NEWLINE !INDENT { RAISE_SYNTAX_ERROR("expected an indented block after except statement on line %d", a->lineno) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000970invalid_match_stmt:
971 | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
Brandt Bucherdbe60ee2021-04-29 17:19:28 -0700972 | a="match" subject=subject_expr ':' NEWLINE !INDENT {
Pablo Galindo56c95df2021-04-21 15:28:21 +0100973 RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000974invalid_case_block:
975 | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100976 | a="case" patterns guard? ':' NEWLINE !INDENT {
977 RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
Pablo Galindoa8c418d2021-06-18 22:15:57 +0100978invalid_as_pattern:
979 | or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
980 | or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100981invalid_if_stmt:
982 | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100983 | a='if' a=named_expression ':' NEWLINE !INDENT {
984 RAISE_INDENTATION_ERROR("expected an indented block after 'if' statement on line %d", a->lineno) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100985invalid_elif_stmt:
986 | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100987 | a='elif' named_expression ':' NEWLINE !INDENT {
988 RAISE_INDENTATION_ERROR("expected an indented block after 'elif' statement on line %d", a->lineno) }
989invalid_else_stmt:
990 | a='else' ':' NEWLINE !INDENT {
991 RAISE_INDENTATION_ERROR("expected an indented block after 'else' statement on line %d", a->lineno) }
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100992invalid_while_stmt:
993 | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
Pablo Galindo56c95df2021-04-21 15:28:21 +0100994 | a='while' named_expression ':' NEWLINE !INDENT {
995 RAISE_INDENTATION_ERROR("expected an indented block after 'while' statement on line %d", a->lineno) }
996invalid_for_stmt:
997 | [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {
998 RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) }
999invalid_def_raw:
1000 | [ASYNC] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
1001 RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) }
1002invalid_class_def_raw:
1003 | a='class' NAME ['('[arguments] ')'] ':' NEWLINE !INDENT {
1004 RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) }
Pablo Galindoda743502021-04-15 14:06:39 +01001005
1006invalid_double_starred_kvpairs:
1007 | ','.double_starred_kvpair+ ',' invalid_kvpair
Pablo Galindoa77aac42021-04-23 14:27:05 +01001008 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
Pablo Galindoda743502021-04-15 14:06:39 +01001009 | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1010invalid_kvpair:
Pablo Galindob5b98bd2021-04-16 00:45:42 +01001011 | a=expression !(':') {
Pablo Galindoa77aac42021-04-23 14:27:05 +01001012 RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
1013 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
Pablo Galindoda743502021-04-15 14:06:39 +01001014 | expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }