blob: eb10fc211051cf2d804cbabaea477e51e52aabc3 [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) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +020031interactive[mod_ty]: a=statement_newline { _PyAST_Interactive(a, p->arena) }
32eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _PyAST_Expression(a, p->arena) }
33func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _PyAST_FunctionType(a, b, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010034fstring[expr_ty]: star_expressions
35
Guido van Rossumc001c092020-04-30 12:12:19 -070036# type_expressions allow */** but ignore them
Pablo Galindoa5634c42020-09-16 19:42:00 +010037type_expressions[asdl_expr_seq*]:
Guido van Rossumc001c092020-04-30 12:12:19 -070038 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030039 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
40 p,
41 CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)),
42 c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010043 | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
44 | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) }
Shantanu603d3542020-05-03 22:08:14 -070045 | '*' a=expression ',' '**' b=expression {
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030046 (asdl_expr_seq*)_PyPegen_seq_append_to_end(
47 p,
48 CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)),
49 b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +010050 | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
51 | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) }
52 | a[asdl_expr_seq*]=','.expression+ {a}
Guido van Rossumc001c092020-04-30 12:12:19 -070053
Pablo Galindoa5634c42020-09-16 19:42:00 +010054statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000055statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a }
Pablo Galindoa5634c42020-09-16 19:42:00 +010056statement_newline[asdl_stmt_seq*]:
57 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000058 | simple_stmts
Victor Stinnerd27f8d22021-04-07 21:34:22 +020059 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010060 | ENDMARKER { _PyPegen_interactive_exit(p) }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000061simple_stmts[asdl_stmt_seq*]:
62 | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup
63 | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010064# NOTE: assignment MUST precede expression, else parsing a simple assignment
65# will throw a SyntaxError.
Pablo Galindo9bdc40e2020-11-30 19:42:38 +000066simple_stmt[stmt_ty] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +010067 | assignment
Victor Stinnerd27f8d22021-04-07 21:34:22 +020068 | e=star_expressions { _PyAST_Expr(e, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010069 | &'return' return_stmt
70 | &('import' | 'from') import_stmt
71 | &'raise' raise_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020072 | 'pass' { _PyAST_Pass(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010073 | &'del' del_stmt
74 | &'yield' yield_stmt
75 | &'assert' assert_stmt
Victor Stinnerd27f8d22021-04-07 21:34:22 +020076 | 'break' { _PyAST_Break(EXTRA) }
77 | 'continue' { _PyAST_Continue(EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010078 | &'global' global_stmt
79 | &'nonlocal' nonlocal_stmt
80compound_stmt[stmt_ty]:
81 | &('def' | '@' | ASYNC) function_def
82 | &'if' if_stmt
83 | &('class' | '@') class_def
84 | &('with' | ASYNC) with_stmt
85 | &('for' | ASYNC) for_stmt
86 | &'try' try_stmt
87 | &'while' while_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -080088 | match_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +010089
90# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
Lysandros Nikolaou999ec9a2020-05-06 21:11:04 +030091assignment[stmt_ty]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +010092 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030093 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +030094 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030095 6,
96 "Variable annotation syntax is",
Victor Stinnerd27f8d22021-04-07 21:34:22 +020097 _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA)
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030098 ) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +030099 | a=('(' b=single_target ')' { b }
100 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200101 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100102 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200103 _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300104 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200105 _PyAST_AugAssign(a, b->kind, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100106 | invalid_assignment
107
108augassign[AugOperator*]:
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300109 | '+=' { _PyPegen_augoperator(p, Add) }
110 | '-=' { _PyPegen_augoperator(p, Sub) }
111 | '*=' { _PyPegen_augoperator(p, Mult) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300112 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300113 | '/=' { _PyPegen_augoperator(p, Div) }
114 | '%=' { _PyPegen_augoperator(p, Mod) }
115 | '&=' { _PyPegen_augoperator(p, BitAnd) }
116 | '|=' { _PyPegen_augoperator(p, BitOr) }
117 | '^=' { _PyPegen_augoperator(p, BitXor) }
118 | '<<=' { _PyPegen_augoperator(p, LShift) }
119 | '>>=' { _PyPegen_augoperator(p, RShift) }
120 | '**=' { _PyPegen_augoperator(p, Pow) }
121 | '//=' { _PyPegen_augoperator(p, FloorDiv) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100122
Pablo Galindoa5634c42020-09-16 19:42:00 +0100123global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200124 _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100125nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200126 _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100127
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200128yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100129
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200130assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100131
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300132del_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200133 | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300134 | invalid_del_stmt
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100135
136import_stmt[stmt_ty]: import_name | import_from
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200137import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100138# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
139import_from[stmt_ty]:
140 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200141 _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100142 | 'from' a=('.' | '...')+ 'import' b=import_from_targets {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200143 _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100144import_from_targets[asdl_alias_seq*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100145 | '(' a=import_from_as_names [','] ')' { a }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300146 | import_from_as_names !','
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400147 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300148 | invalid_import_from_targets
Pablo Galindoa5634c42020-09-16 19:42:00 +0100149import_from_as_names[asdl_alias_seq*]:
150 | a[asdl_alias_seq*]=','.import_from_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100151import_from_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200152 | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100153 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400154 EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100155dotted_as_names[asdl_alias_seq*]:
156 | a[asdl_alias_seq*]=','.dotted_as_name+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100157dotted_as_name[alias_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200158 | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100159 (b) ? ((expr_ty) b)->v.Name.id : NULL,
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400160 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100161dotted_name[expr_ty]:
162 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
163 | NAME
164
165if_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000166 | 'if' a=named_expression &&':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200167 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
168 | 'if' a=named_expression &&':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100169elif_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000170 | 'elif' a=named_expression &&':' b=block c=elif_stmt {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200171 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
172 | 'elif' a=named_expression &&':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000173else_block[asdl_stmt_seq*]: 'else' &&':' b=block { b }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100174
175while_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200176 | 'while' a=named_expression &&':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100177
178for_stmt[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000179 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200180 _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000181 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200182 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 +0300183 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100184
185with_stmt[stmt_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100186 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200187 _PyAST_With(a, b, NULL, EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100188 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200189 _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100190 | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200191 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100192 | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200193 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 +0000194 | invalid_with_stmt
195
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100196with_item[withitem_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200197 | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300198 | invalid_with_item
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200199 | e=expression { _PyAST_withitem(e, NULL, p->arena) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100200
201try_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200202 | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
203 | '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 +0100204except_block[excepthandler_ty]:
Pablo Galindo206cbda2021-02-07 18:42:21 +0000205 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200206 _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
207 | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
Pablo Galindo206cbda2021-02-07 18:42:21 +0000208 | invalid_except_block
Pablo Galindoa5634c42020-09-16 19:42:00 +0100209finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100210
Brandt Bucher145bf262021-02-26 14:51:55 -0800211match_stmt[stmt_ty]:
212 | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200213 CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000214 | invalid_match_stmt
Brandt Bucher145bf262021-02-26 14:51:55 -0800215subject_expr[expr_ty]:
216 | value=star_named_expression ',' values=star_named_expressions? {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200217 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800218 | named_expression
219case_block[match_case_ty]:
220 | "case" pattern=patterns guard=guard? ':' body=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200221 _PyAST_match_case(pattern, guard, body, p->arena) }
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000222 | invalid_case_block
Brandt Bucher145bf262021-02-26 14:51:55 -0800223guard[expr_ty]: 'if' guard=named_expression { guard }
224
225patterns[expr_ty]:
226 | values[asdl_expr_seq*]=open_sequence_pattern {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200227 _PyAST_Tuple(values, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800228 | pattern
229pattern[expr_ty]:
230 | as_pattern
231 | or_pattern
232as_pattern[expr_ty]:
233 | pattern=or_pattern 'as' target=capture_pattern {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200234 _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800235or_pattern[expr_ty]:
236 | patterns[asdl_expr_seq*]='|'.closed_pattern+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200237 asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800238closed_pattern[expr_ty]:
239 | literal_pattern
240 | capture_pattern
241 | wildcard_pattern
242 | value_pattern
243 | group_pattern
244 | sequence_pattern
245 | mapping_pattern
246 | class_pattern
247
248literal_pattern[expr_ty]:
249 | signed_number !('+' | '-')
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200250 | real=signed_number '+' imag=NUMBER { _PyAST_BinOp(real, Add, imag, EXTRA) }
251 | real=signed_number '-' imag=NUMBER { _PyAST_BinOp(real, Sub, imag, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800252 | strings
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200253 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
254 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
255 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800256signed_number[expr_ty]:
257 | NUMBER
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200258 | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800259
260capture_pattern[expr_ty]:
261 | !"_" name=NAME !('.' | '(' | '=') {
262 _PyPegen_set_expr_context(p, name, Store) }
263
264wildcard_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200265 | "_" { _PyAST_Name(CHECK(PyObject*, _PyPegen_new_identifier(p, "_")), Store, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800266
267value_pattern[expr_ty]:
268 | attr=attr !('.' | '(' | '=') { attr }
269attr[expr_ty]:
270 | value=name_or_attr '.' attr=NAME {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200271 _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800272name_or_attr[expr_ty]:
273 | attr
274 | NAME
275
276group_pattern[expr_ty]:
277 | '(' pattern=pattern ')' { pattern }
278
279sequence_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200280 | '[' values=maybe_sequence_pattern? ']' { _PyAST_List(values, Load, EXTRA) }
281 | '(' values=open_sequence_pattern? ')' { _PyAST_Tuple(values, Load, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800282open_sequence_pattern[asdl_seq*]:
283 | value=maybe_star_pattern ',' values=maybe_sequence_pattern? {
284 _PyPegen_seq_insert_in_front(p, value, values) }
285maybe_sequence_pattern[asdl_seq*]:
286 | values=','.maybe_star_pattern+ ','? { values }
287maybe_star_pattern[expr_ty]:
288 | star_pattern
289 | pattern
290star_pattern[expr_ty]:
291 | '*' value=(capture_pattern | wildcard_pattern) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200292 _PyAST_Starred(value, Store, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800293
294mapping_pattern[expr_ty]:
295 | '{' items=items_pattern? '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200296 _PyAST_Dict(CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, items)), CHECK(asdl_expr_seq*, _PyPegen_get_values(p, items)), EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800297items_pattern[asdl_seq*]:
298 | items=','.key_value_pattern+ ','? { items }
299key_value_pattern[KeyValuePair*]:
300 | key=(literal_pattern | value_pattern) ':' value=pattern {
301 _PyPegen_key_value_pair(p, key, value) }
302 | double_star_pattern
303double_star_pattern[KeyValuePair*]:
304 | '**' value=capture_pattern { _PyPegen_key_value_pair(p, NULL, value) }
305
306class_pattern[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200307 | func=name_or_attr '(' ')' { _PyAST_Call(func, NULL, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800308 | func=name_or_attr '(' args=positional_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200309 _PyAST_Call(func, args, NULL, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800310 | func=name_or_attr '(' keywords=keyword_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200311 _PyAST_Call(func, NULL, keywords, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800312 | func=name_or_attr '(' args=positional_patterns ',' keywords=keyword_patterns ','? ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200313 _PyAST_Call(func, args, keywords, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800314positional_patterns[asdl_expr_seq*]:
315 | args[asdl_expr_seq*]=','.pattern+ { args }
316keyword_patterns[asdl_keyword_seq*]:
317 | keywords[asdl_keyword_seq*]=','.keyword_pattern+ { keywords }
318keyword_pattern[keyword_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200319 | arg=NAME '=' value=pattern { _PyAST_keyword(arg->v.Name.id, value, EXTRA) }
Brandt Bucher145bf262021-02-26 14:51:55 -0800320
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100321return_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200322 | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100323
324raise_stmt[stmt_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200325 | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) }
326 | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100327
328function_def[stmt_ty]:
329 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) }
330 | function_def_raw
331
332function_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000333 | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200334 _PyAST_FunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300335 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300336 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000337 | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300338 CHECK_VERSION(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300339 stmt_ty,
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300340 5,
341 "Async functions are",
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200342 _PyAST_AsyncFunctionDef(n->v.Name.id,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300343 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300344 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
345 ) }
Pablo Galindod9552412020-05-01 16:32:09 +0100346func_type_comment[Token*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700347 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block
348 | invalid_double_type_comments
349 | TYPE_COMMENT
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100350
351params[arguments_ty]:
352 | invalid_parameters
353 | parameters
Guido van Rossumc001c092020-04-30 12:12:19 -0700354
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100355parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100356 | 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 +0100357 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700358 | a=slash_with_default b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100359 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100360 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100361 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700362 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100363 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700364
365# Some duplication here because we can't write (',' | &')'),
366# which is because we don't support empty alternatives (yet).
367#
Pablo Galindoa5634c42020-09-16 19:42:00 +0100368slash_no_default[asdl_arg_seq*]:
369 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a }
370 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700371slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100372 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
373 | 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 -0700374
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100375star_etc[StarEtc*]:
Guido van Rossumc001c092020-04-30 12:12:19 -0700376 | '*' a=param_no_default b=param_maybe_default* c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100377 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700378 | '*' ',' b=param_maybe_default+ c=[kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100379 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700380 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300381 | invalid_star_etc
Guido van Rossumc001c092020-04-30 12:12:19 -0700382
Guido van Rossum3941d972020-05-01 09:42:03 -0700383kwds[arg_ty]: '**' a=param_no_default { a }
Guido van Rossumc001c092020-04-30 12:12:19 -0700384
385# One parameter. This *includes* a following comma and type comment.
386#
387# There are three styles:
388# - No default
389# - With default
390# - Maybe with default
391#
392# There are two alternative forms of each, to deal with type comments:
393# - Ends in a comma followed by an optional type comment
394# - No comma, optional type comment, must be followed by close paren
395# The latter form is for a final parameter without trailing comma.
396#
397param_no_default[arg_ty]:
398 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) }
399 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) }
400param_with_default[NameDefaultPair*]:
401 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
402 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
403param_maybe_default[NameDefaultPair*]:
404 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) }
405 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200406param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) }
Guido van Rossumc001c092020-04-30 12:12:19 -0700407
408annotation[expr_ty]: ':' a=expression { a }
409default[expr_ty]: '=' a=expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100410
Pablo Galindoa5634c42020-09-16 19:42:00 +0100411decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100412
413class_def[stmt_ty]:
414 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) }
415 | class_def_raw
416class_def_raw[stmt_ty]:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000417 | 'class' a=NAME b=['(' z=[arguments] ')' { z }] &&':' c=block {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200418 _PyAST_ClassDef(a->v.Name.id,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100419 (b) ? ((expr_ty) b)->v.Call.args : NULL,
420 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
421 c, NULL, EXTRA) }
422
Pablo Galindoa5634c42020-09-16 19:42:00 +0100423block[asdl_stmt_seq*] (memo):
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100424 | NEWLINE INDENT a=statements DEDENT { a }
Pablo Galindo9bdc40e2020-11-30 19:42:38 +0000425 | simple_stmts
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100426 | invalid_block
427
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100428star_expressions[expr_ty]:
429 | a=star_expression b=(',' c=star_expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200430 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
431 | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100432 | star_expression
433star_expression[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200434 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100435 | expression
436
Pablo Galindoa5634c42020-09-16 19:42:00 +0100437star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100438star_named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200439 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100440 | named_expression
441named_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200442 | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100443 | expression !':='
444 | invalid_named_expression
445
446annotated_rhs[expr_ty]: yield_expr | star_expressions
447
448expressions[expr_ty]:
449 | a=expression b=(',' c=expression { c })+ [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200450 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) }
451 | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100452 | expression
453expression[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200454 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100455 | disjunction
456 | lambdef
457
458lambdef[expr_ty]:
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300459 | 'lambda' a=[lambda_params] ':' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200460 _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
Pablo Galindoc6483c92020-06-10 14:07:06 +0100461
462lambda_params[arguments_ty]:
463 | invalid_lambda_parameters
464 | lambda_parameters
Guido van Rossum3941d972020-05-01 09:42:03 -0700465
466# lambda_parameters etc. duplicates parameters but without annotations
467# or type comments, and if there's no comma after a parameter, we expect
468# a colon, not a close parenthesis. (For more, see parameters above.)
469#
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100470lambda_parameters[arguments_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100471 | 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 +0100472 _PyPegen_make_arguments(p, a, NULL, b, c, d) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700473 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100474 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100475 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100476 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700477 | 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 +0100478 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700479
Pablo Galindoa5634c42020-09-16 19:42:00 +0100480lambda_slash_no_default[asdl_arg_seq*]:
481 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a }
482 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a }
Guido van Rossum3941d972020-05-01 09:42:03 -0700483lambda_slash_with_default[SlashWithDefault*]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100484 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
485 | 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 -0700486
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100487lambda_star_etc[StarEtc*]:
Guido van Rossum3941d972020-05-01 09:42:03 -0700488 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100489 _PyPegen_star_etc(p, a, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700490 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100491 _PyPegen_star_etc(p, NULL, b, c) }
Guido van Rossum3941d972020-05-01 09:42:03 -0700492 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300493 | invalid_lambda_star_etc
Guido van Rossum3941d972020-05-01 09:42:03 -0700494
495lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
496
497lambda_param_no_default[arg_ty]:
498 | a=lambda_param ',' { a }
499 | a=lambda_param &':' { a }
500lambda_param_with_default[NameDefaultPair*]:
501 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
502 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
503lambda_param_maybe_default[NameDefaultPair*]:
504 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
505 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200506lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100507
508disjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200509 | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100510 Or,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300511 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100512 EXTRA) }
513 | conjunction
514conjunction[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200515 | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp(
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100516 And,
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300517 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100518 EXTRA) }
519 | inversion
520inversion[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200521 | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100522 | comparison
523comparison[expr_ty]:
524 | a=bitwise_or b=compare_op_bitwise_or_pair+ {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200525 _PyAST_Compare(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300526 a,
527 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)),
528 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)),
529 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100530 | bitwise_or
531compare_op_bitwise_or_pair[CmpopExprPair*]:
532 | eq_bitwise_or
533 | noteq_bitwise_or
534 | lte_bitwise_or
535 | lt_bitwise_or
536 | gte_bitwise_or
537 | gt_bitwise_or
538 | notin_bitwise_or
539 | in_bitwise_or
540 | isnot_bitwise_or
541 | is_bitwise_or
542eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
Pablo Galindo2b74c832020-04-27 18:02:07 +0100543noteq_bitwise_or[CmpopExprPair*]:
Pablo Galindo06f8c332020-10-30 23:48:42 +0000544 | (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 +0100545lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
546lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
547gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
548gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) }
549notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) }
550in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) }
551isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) }
552is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) }
553
554bitwise_or[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200555 | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100556 | bitwise_xor
557bitwise_xor[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200558 | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100559 | bitwise_and
560bitwise_and[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200561 | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100562 | shift_expr
563shift_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200564 | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
565 | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100566 | sum
567
568sum[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200569 | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
570 | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100571 | term
572term[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200573 | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) }
574 | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) }
575 | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
576 | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
577 | 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 +0100578 | factor
579factor[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200580 | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
581 | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
582 | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100583 | power
584power[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200585 | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100586 | await_primary
587await_primary[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200588 | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100589 | primary
590primary[expr_ty]:
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200591 | invalid_primary # must be before 'primay genexp' because of invalid_genexp
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200592 | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
593 | 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 +0100594 | a=primary '(' b=[arguments] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200595 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100596 (b) ? ((expr_ty) b)->v.Call.args : NULL,
597 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
598 EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200599 | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100600 | atom
601
602slices[expr_ty]:
603 | a=slice !',' { a }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200604 | a[asdl_expr_seq*]=','.slice+ [','] { _PyAST_Tuple(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100605slice[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200606 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) }
Lysandros Nikolaoucae60182020-11-17 01:09:35 +0200607 | a=named_expression { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100608atom[expr_ty]:
609 | NAME
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200610 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
611 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
612 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100613 | &STRING strings
614 | NUMBER
615 | &'(' (tuple | group | genexp)
616 | &'[' (list | listcomp)
617 | &'{' (dict | set | dictcomp | setcomp)
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200618 | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100619
620strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
621list[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200622 | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100623listcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200624 | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100625 | invalid_comprehension
626tuple[expr_ty]:
627 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200628 _PyAST_Tuple(a, Load, EXTRA) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300629group[expr_ty]:
630 | '(' a=(yield_expr | named_expression) ')' { a }
631 | invalid_group
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100632genexp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200633 | '(' a=named_expression b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100634 | invalid_comprehension
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200635set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100636setcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200637 | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100638 | invalid_comprehension
639dict[expr_ty]:
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300640 | '{' a=[double_starred_kvpairs] '}' {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200641 _PyAST_Dict(
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300642 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
643 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
644 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100645dictcomp[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200646 | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300647 | invalid_dict_comprehension
648double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a }
649double_starred_kvpair[KeyValuePair*]:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100650 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300651 | kvpair
652kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100653for_if_clauses[asdl_comprehension_seq*]:
654 | a[asdl_comprehension_seq*]=for_if_clause+ { a }
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300655for_if_clause[comprehension_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100656 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200657 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
Pablo Galindoa5634c42020-09-16 19:42:00 +0100658 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200659 _PyAST_comprehension(a, b, c, 0, p->arena) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300660 | invalid_for_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100661
662yield_expr[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200663 | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
664 | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100665
666arguments[expr_ty] (memo):
667 | a=args [','] &')' { a }
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200668 | invalid_arguments
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100669args[expr_ty]:
Pablo Galindoa5634c42020-09-16 19:42:00 +0100670 | a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200671 | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p),
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300672 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)),
673 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)),
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100674 EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100675kwargs[asdl_seq*]:
676 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) }
677 | ','.kwarg_or_starred+
678 | ','.kwarg_or_double_starred+
679starred_expression[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200680 | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100681kwarg_or_starred[KeywordOrStarred*]:
682 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200683 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100684 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300685 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100686kwarg_or_double_starred[KeywordOrStarred*]:
687 | a=NAME '=' b=expression {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200688 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) }
689 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300690 | invalid_kwarg
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100691
692# NOTE: star_targets may contain *bitwise_or, targets may not.
693star_targets[expr_ty]:
694 | a=star_target !',' { a }
695 | a=star_target b=(',' c=star_target { c })* [','] {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200696 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200697star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a }
698star_targets_tuple_seq[asdl_expr_seq*]:
699 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) }
700 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100701star_target[expr_ty] (memo):
702 | '*' a=(!'*' star_target) {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200703 _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200704 | target_with_star_atom
705target_with_star_atom[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200706 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
707 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100708 | star_atom
709star_atom[expr_ty]:
710 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaou2ea320d2021-01-03 01:14:21 +0200711 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200712 | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) }
713 | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100714
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300715single_target[expr_ty]:
716 | single_subscript_attribute_target
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100717 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300718 | '(' a=single_target ')' { a }
719single_subscript_attribute_target[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200720 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
721 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100722
Pablo Galindoa5634c42020-09-16 19:42:00 +0100723del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100724del_target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200725 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) }
726 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100727 | del_t_atom
728del_t_atom[expr_ty]:
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300729 | a=NAME { _PyPegen_set_expr_context(p, a, Del) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100730 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200731 | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) }
732 | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100733
Pablo Galindoa5634c42020-09-16 19:42:00 +0100734targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100735target[expr_ty] (memo):
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200736 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) }
737 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100738 | t_atom
739t_primary[expr_ty]:
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200740 | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) }
741 | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) }
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300742 | a=t_primary b=genexp &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200743 _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100744 | a=t_primary '(' b=[arguments] ')' &t_lookahead {
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200745 _PyAST_Call(a,
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100746 (b) ? ((expr_ty) b)->v.Call.args : NULL,
747 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
748 EXTRA) }
749 | a=atom &t_lookahead { a }
750t_lookahead: '(' | '[' | '.'
751t_atom[expr_ty]:
752 | a=NAME { _PyPegen_set_expr_context(p, a, Store) }
753 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) }
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200754 | '(' b=[targets] ')' { _PyAST_Tuple(b, Store, EXTRA) }
755 | '[' b=[targets] ']' { _PyAST_List(b, Store, EXTRA) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100756
757
758# From here on, there are rules for invalid syntax with specialised error messages
Lysandros Nikolaoubca70142020-10-27 00:42:04 +0200759invalid_arguments:
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100760 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300761 | a=expression for_if_clauses ',' [args | expression for_if_clauses] {
762 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300763 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
764 | args ',' a=expression for_if_clauses {
765 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100766 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
Lysandros Nikolaou4638c642020-05-07 13:44:06 +0300767invalid_kwarg:
Pablo Galindo43c4fb62020-12-13 16:46:48 +0000768 | expression a='=' {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300769 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
770 a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100771invalid_named_expression:
772 | a=expression ':=' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300773 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
774 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100775invalid_assignment:
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300776 | a=invalid_ann_assign_target ':' expression {
777 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
778 a,
779 "only single target (not %s) can be annotated",
780 _PyPegen_get_expr_name(a)
781 )}
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300782 | a=star_named_expression ',' star_named_expressions* ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300783 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300784 | a=expression ':' expression {
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300785 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
Pablo Galindo9f495902020-06-08 02:57:00 +0100786 | (star_targets '=')* a=star_expressions '=' {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300787 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Pablo Galindo9f495902020-06-08 02:57:00 +0100788 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
Pablo Galindo16ab0702020-05-15 02:04:52 +0100789 | a=star_expressions augassign (yield_expr | star_expressions) {
Brandt Bucher145bf262021-02-26 14:51:55 -0800790 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Lysandros Nikolaou4b85e602020-06-26 02:22:36 +0300791 a,
Pablo Galindo16ab0702020-05-15 02:04:52 +0100792 "'%s' is an illegal expression for augmented assignment",
793 _PyPegen_get_expr_name(a)
794 )}
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300795invalid_ann_assign_target[expr_ty]:
796 | list
797 | tuple
798 | '(' a=invalid_ann_assign_target ')' { a }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300799invalid_del_stmt:
800 | 'del' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300801 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100802invalid_block:
803 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200804invalid_primary:
805 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100806invalid_comprehension:
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300807 | ('[' | '(' | '{') a=starred_expression for_if_clauses {
808 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000809 | ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
Pablo Galindo835f14f2021-01-31 22:52:56 +0000810 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
Batuhan Taskayab8a65ec2020-05-22 01:39:56 +0300811invalid_dict_comprehension:
812 | '{' a='**' bitwise_or for_if_clauses '}' {
813 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100814invalid_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200815 | param_no_default* invalid_parameters_helper param_no_default {
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100816 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200817invalid_parameters_helper: # This is only there to avoid type errors
818 | a=slash_with_default { _PyPegen_singleton_seq(p, a) }
819 | param_with_default+
Pablo Galindoc6483c92020-06-10 14:07:06 +0100820invalid_lambda_parameters:
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200821 | lambda_param_no_default* invalid_lambda_parameters_helper lambda_param_no_default {
Pablo Galindoc6483c92020-06-10 14:07:06 +0100822 RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
Lysandros Nikolaou07dcd862021-01-08 00:31:25 +0200823invalid_lambda_parameters_helper:
824 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
825 | lambda_param_with_default+
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300826invalid_star_etc:
827 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300828 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300829invalid_lambda_star_etc:
830 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
Guido van Rossumc001c092020-04-30 12:12:19 -0700831invalid_double_type_comments:
832 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
833 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300834invalid_with_item:
Pablo Galindo58fb1562021-02-02 19:54:22 +0000835 | expression 'as' a=expression &(',' | ')' | ':') {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300836 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300837
838invalid_for_target:
839 | ASYNC? 'for' a=star_expressions {
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300840 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300841
842invalid_group:
843 | '(' a=starred_expression ')' {
844 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
Pablo Galindo8efad612021-03-24 19:34:17 +0000845 | '(' a='**' expression ')' {
846 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use double starred expression here") }
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300847invalid_import_from_targets:
848 | import_from_as_names ',' {
849 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Pablo Galindo58fb1562021-02-02 19:54:22 +0000850
851invalid_with_stmt:
852 | [ASYNC] 'with' ','.(expression ['as' star_target])+ &&':'
853 | [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' &&':'
Pablo Galindo206cbda2021-02-07 18:42:21 +0000854
855invalid_except_block:
856 | 'except' a=expression ',' expressions ['as' NAME ] ':' {
857 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
858 | 'except' expression ['as' NAME ] &&':'
Victor Stinnerd27f8d22021-04-07 21:34:22 +0200859 | 'except' &&':'
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000860
861invalid_match_stmt:
862 | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
863
864invalid_case_block:
865 | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }