Pablo Galindo | b4282dd | 2020-06-12 00:51:44 +0100 | [diff] [blame] | 1 | # PEG grammar for Python |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3 | @trailer ''' |
| 4 | void * |
| 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 Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 19 | } else if (p->start_rule == Py_func_type_input) { |
| 20 | result = func_type_rule(p); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 21 | } 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 Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 30 | file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 31 | interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) } |
| 32 | eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 33 | func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { FunctionType(a, b, p->arena) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 34 | fstring[expr_ty]: star_expressions |
| 35 | |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 36 | # type_expressions allow */** but ignore them |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 37 | type_expressions[asdl_expr_seq*]: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 38 | | a=','.expression+ ',' '*' b=expression ',' '**' c=expression { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 39 | (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 Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 43 | | 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) } |
Shantanu | 603d354 | 2020-05-03 22:08:14 -0700 | [diff] [blame] | 45 | | '*' a=expression ',' '**' b=expression { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 46 | (asdl_expr_seq*)_PyPegen_seq_append_to_end( |
| 47 | p, |
| 48 | CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)), |
| 49 | b) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 50 | | '*' 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 Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 53 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 54 | statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) } |
| 55 | statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmt { a } |
| 56 | statement_newline[asdl_stmt_seq*]: |
| 57 | | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 58 | | simple_stmt |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 59 | | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _Py_Pass(EXTRA))) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 60 | | ENDMARKER { _PyPegen_interactive_exit(p) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 61 | simple_stmt[asdl_stmt_seq*]: |
| 62 | | a=small_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup |
| 63 | | a[asdl_stmt_seq*]=';'.small_stmt+ [';'] NEWLINE { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 64 | # NOTE: assignment MUST precede expression, else parsing a simple assignment |
| 65 | # will throw a SyntaxError. |
| 66 | small_stmt[stmt_ty] (memo): |
| 67 | | assignment |
| 68 | | e=star_expressions { _Py_Expr(e, EXTRA) } |
| 69 | | &'return' return_stmt |
| 70 | | &('import' | 'from') import_stmt |
| 71 | | &'raise' raise_stmt |
| 72 | | 'pass' { _Py_Pass(EXTRA) } |
| 73 | | &'del' del_stmt |
| 74 | | &'yield' yield_stmt |
| 75 | | &'assert' assert_stmt |
| 76 | | 'break' { _Py_Break(EXTRA) } |
| 77 | | 'continue' { _Py_Continue(EXTRA) } |
| 78 | | &'global' global_stmt |
| 79 | | &'nonlocal' nonlocal_stmt |
| 80 | compound_stmt[stmt_ty]: |
| 81 | | &('def' | '@' | ASYNC) function_def |
| 82 | | &'if' if_stmt |
| 83 | | &('class' | '@') class_def |
| 84 | | &('with' | ASYNC) with_stmt |
| 85 | | &('for' | ASYNC) for_stmt |
| 86 | | &'try' try_stmt |
| 87 | | &'while' while_stmt |
| 88 | |
| 89 | # NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield' |
Lysandros Nikolaou | 999ec9a | 2020-05-06 21:11:04 +0300 | [diff] [blame] | 90 | assignment[stmt_ty]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 91 | | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 92 | CHECK_VERSION( |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 93 | stmt_ty, |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 94 | 6, |
| 95 | "Variable annotation syntax is", |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 96 | _Py_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA) |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 97 | ) } |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 98 | | a=('(' b=single_target ')' { b } |
| 99 | | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 100 | CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 101 | | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] { |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 102 | _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 103 | | a=single_target b=augassign ~ c=(yield_expr | star_expressions) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 104 | _Py_AugAssign(a, b->kind, c, EXTRA) } |
| 105 | | invalid_assignment |
| 106 | |
| 107 | augassign[AugOperator*]: |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 108 | | '+=' { _PyPegen_augoperator(p, Add) } |
| 109 | | '-=' { _PyPegen_augoperator(p, Sub) } |
| 110 | | '*=' { _PyPegen_augoperator(p, Mult) } |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 111 | | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) } |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 112 | | '/=' { _PyPegen_augoperator(p, Div) } |
| 113 | | '%=' { _PyPegen_augoperator(p, Mod) } |
| 114 | | '&=' { _PyPegen_augoperator(p, BitAnd) } |
| 115 | | '|=' { _PyPegen_augoperator(p, BitOr) } |
| 116 | | '^=' { _PyPegen_augoperator(p, BitXor) } |
| 117 | | '<<=' { _PyPegen_augoperator(p, LShift) } |
| 118 | | '>>=' { _PyPegen_augoperator(p, RShift) } |
| 119 | | '**=' { _PyPegen_augoperator(p, Pow) } |
| 120 | | '//=' { _PyPegen_augoperator(p, FloorDiv) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 121 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 122 | global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 123 | _Py_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 124 | nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 125 | _Py_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 126 | |
| 127 | yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) } |
| 128 | |
| 129 | assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) } |
| 130 | |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 131 | del_stmt[stmt_ty]: |
| 132 | | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) } |
| 133 | | invalid_del_stmt |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 134 | |
| 135 | import_stmt[stmt_ty]: import_name | import_from |
| 136 | import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) } |
| 137 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 138 | import_from[stmt_ty]: |
| 139 | | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets { |
| 140 | _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) } |
| 141 | | 'from' a=('.' | '...')+ 'import' b=import_from_targets { |
| 142 | _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 143 | import_from_targets[asdl_alias_seq*]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 144 | | '(' a=import_from_as_names [','] ')' { a } |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 145 | | import_from_as_names !',' |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 146 | | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p))) } |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 147 | | invalid_import_from_targets |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 148 | import_from_as_names[asdl_alias_seq*]: |
| 149 | | a[asdl_alias_seq*]=','.import_from_as_name+ { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 150 | import_from_as_name[alias_ty]: |
| 151 | | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id, |
| 152 | (b) ? ((expr_ty) b)->v.Name.id : NULL, |
| 153 | p->arena) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 154 | dotted_as_names[asdl_alias_seq*]: |
| 155 | | a[asdl_alias_seq*]=','.dotted_as_name+ { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 156 | dotted_as_name[alias_ty]: |
| 157 | | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id, |
| 158 | (b) ? ((expr_ty) b)->v.Name.id : NULL, |
| 159 | p->arena) } |
| 160 | dotted_name[expr_ty]: |
| 161 | | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) } |
| 162 | | NAME |
| 163 | |
| 164 | if_stmt[stmt_ty]: |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 165 | | 'if' a=named_expression ':' b=block c=elif_stmt { |
| 166 | _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 167 | | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) } |
| 168 | elif_stmt[stmt_ty]: |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 169 | | 'elif' a=named_expression ':' b=block c=elif_stmt { |
| 170 | _Py_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 171 | | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 172 | else_block[asdl_stmt_seq*]: 'else' ':' b=block { b } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 173 | |
| 174 | while_stmt[stmt_ty]: |
| 175 | | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) } |
| 176 | |
| 177 | for_stmt[stmt_ty]: |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 178 | | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 179 | _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 180 | | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 181 | CHECK_VERSION(stmt_ty, 5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 182 | | invalid_for_target |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 183 | |
| 184 | with_stmt[stmt_ty]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 185 | | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 186 | _Py_With(a, b, NULL, EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 187 | | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 188 | _Py_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 189 | | ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 190 | CHECK_VERSION(stmt_ty, 5, "Async with statements are", _Py_AsyncWith(a, b, NULL, EXTRA)) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 191 | | ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 192 | CHECK_VERSION(stmt_ty, 5, "Async with statements are", _Py_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 193 | with_item[withitem_ty]: |
Batuhan Taskaya | 48f305f | 2020-10-09 12:56:48 +0300 | [diff] [blame] | 194 | | e=expression 'as' t=star_target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 195 | | invalid_with_item |
| 196 | | e=expression { _Py_withitem(e, NULL, p->arena) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 197 | |
| 198 | try_stmt[stmt_ty]: |
| 199 | | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 200 | | 'try' ':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 201 | except_block[excepthandler_ty]: |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 202 | | 'except' e=expression t=['as' z=NAME { z }] ':' b=block { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 203 | _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) } |
| 204 | | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 205 | finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 206 | |
| 207 | return_stmt[stmt_ty]: |
| 208 | | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) } |
| 209 | |
| 210 | raise_stmt[stmt_ty]: |
| 211 | | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) } |
| 212 | | 'raise' { _Py_Raise(NULL, NULL, EXTRA) } |
| 213 | |
| 214 | function_def[stmt_ty]: |
| 215 | | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) } |
| 216 | | function_def_raw |
| 217 | |
| 218 | function_def_raw[stmt_ty]: |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 219 | | 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block { |
| 220 | _Py_FunctionDef(n->v.Name.id, |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 221 | (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 222 | b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) } |
| 223 | | ASYNC 'def' n=NAME '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block { |
| 224 | CHECK_VERSION( |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 225 | stmt_ty, |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 226 | 5, |
| 227 | "Async functions are", |
| 228 | _Py_AsyncFunctionDef(n->v.Name.id, |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 229 | (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 230 | b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) |
| 231 | ) } |
Pablo Galindo | d955241 | 2020-05-01 16:32:09 +0100 | [diff] [blame] | 232 | func_type_comment[Token*]: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 233 | | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block |
| 234 | | invalid_double_type_comments |
| 235 | | TYPE_COMMENT |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 236 | |
| 237 | params[arguments_ty]: |
| 238 | | invalid_parameters |
| 239 | | parameters |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 240 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 241 | parameters[arguments_ty]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 242 | | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 243 | _PyPegen_make_arguments(p, a, NULL, b, c, d) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 244 | | a=slash_with_default b=param_with_default* c=[star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 245 | _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 246 | | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 247 | _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 248 | | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 249 | | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 250 | |
| 251 | # Some duplication here because we can't write (',' | &')'), |
| 252 | # which is because we don't support empty alternatives (yet). |
| 253 | # |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 254 | slash_no_default[asdl_arg_seq*]: |
| 255 | | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a } |
| 256 | | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 257 | slash_with_default[SlashWithDefault*]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 258 | | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } |
| 259 | | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 260 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 261 | star_etc[StarEtc*]: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 262 | | '*' a=param_no_default b=param_maybe_default* c=[kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 263 | _PyPegen_star_etc(p, a, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 264 | | '*' ',' b=param_maybe_default+ c=[kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 265 | _PyPegen_star_etc(p, NULL, b, c) } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 266 | | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 267 | | invalid_star_etc |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 268 | |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 269 | kwds[arg_ty]: '**' a=param_no_default { a } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 270 | |
| 271 | # One parameter. This *includes* a following comma and type comment. |
| 272 | # |
| 273 | # There are three styles: |
| 274 | # - No default |
| 275 | # - With default |
| 276 | # - Maybe with default |
| 277 | # |
| 278 | # There are two alternative forms of each, to deal with type comments: |
| 279 | # - Ends in a comma followed by an optional type comment |
| 280 | # - No comma, optional type comment, must be followed by close paren |
| 281 | # The latter form is for a final parameter without trailing comma. |
| 282 | # |
| 283 | param_no_default[arg_ty]: |
| 284 | | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) } |
| 285 | | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) } |
| 286 | param_with_default[NameDefaultPair*]: |
| 287 | | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) } |
| 288 | | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) } |
| 289 | param_maybe_default[NameDefaultPair*]: |
| 290 | | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) } |
| 291 | | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) } |
| 292 | param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) } |
| 293 | |
| 294 | annotation[expr_ty]: ':' a=expression { a } |
| 295 | default[expr_ty]: '=' a=expression { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 296 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 297 | decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 298 | |
| 299 | class_def[stmt_ty]: |
| 300 | | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) } |
| 301 | | class_def_raw |
| 302 | class_def_raw[stmt_ty]: |
| 303 | | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block { |
| 304 | _Py_ClassDef(a->v.Name.id, |
| 305 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 306 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 307 | c, NULL, EXTRA) } |
| 308 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 309 | block[asdl_stmt_seq*] (memo): |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 310 | | NEWLINE INDENT a=statements DEDENT { a } |
| 311 | | simple_stmt |
| 312 | | invalid_block |
| 313 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 314 | expressions_list[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_expression+ [','] { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 315 | star_expressions[expr_ty]: |
| 316 | | a=star_expression b=(',' c=star_expression { c })+ [','] { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 317 | _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } |
| 318 | | a=star_expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 319 | | star_expression |
| 320 | star_expression[expr_ty] (memo): |
| 321 | | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } |
| 322 | | expression |
| 323 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 324 | star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 325 | star_named_expression[expr_ty]: |
| 326 | | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } |
| 327 | | named_expression |
| 328 | named_expression[expr_ty]: |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 329 | | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 330 | | expression !':=' |
| 331 | | invalid_named_expression |
| 332 | |
| 333 | annotated_rhs[expr_ty]: yield_expr | star_expressions |
| 334 | |
| 335 | expressions[expr_ty]: |
| 336 | | a=expression b=(',' c=expression { c })+ [','] { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 337 | _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } |
| 338 | | a=expression ',' { _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 339 | | expression |
| 340 | expression[expr_ty] (memo): |
| 341 | | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) } |
| 342 | | disjunction |
| 343 | | lambdef |
| 344 | |
| 345 | lambdef[expr_ty]: |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 346 | | 'lambda' a=[lambda_params] ':' b=expression { |
| 347 | _Py_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) } |
Pablo Galindo | c6483c9 | 2020-06-10 14:07:06 +0100 | [diff] [blame] | 348 | |
| 349 | lambda_params[arguments_ty]: |
| 350 | | invalid_lambda_parameters |
| 351 | | lambda_parameters |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 352 | |
| 353 | # lambda_parameters etc. duplicates parameters but without annotations |
| 354 | # or type comments, and if there's no comma after a parameter, we expect |
| 355 | # a colon, not a close parenthesis. (For more, see parameters above.) |
| 356 | # |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 357 | lambda_parameters[arguments_ty]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 358 | | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 359 | _PyPegen_make_arguments(p, a, NULL, b, c, d) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 360 | | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 361 | _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 362 | | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 363 | _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 364 | | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 365 | | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 366 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 367 | lambda_slash_no_default[asdl_arg_seq*]: |
| 368 | | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a } |
| 369 | | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 370 | lambda_slash_with_default[SlashWithDefault*]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 371 | | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } |
| 372 | | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 373 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 374 | lambda_star_etc[StarEtc*]: |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 375 | | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 376 | _PyPegen_star_etc(p, a, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 377 | | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 378 | _PyPegen_star_etc(p, NULL, b, c) } |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 379 | | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 380 | | invalid_lambda_star_etc |
Guido van Rossum | 3941d97 | 2020-05-01 09:42:03 -0700 | [diff] [blame] | 381 | |
| 382 | lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a } |
| 383 | |
| 384 | lambda_param_no_default[arg_ty]: |
| 385 | | a=lambda_param ',' { a } |
| 386 | | a=lambda_param &':' { a } |
| 387 | lambda_param_with_default[NameDefaultPair*]: |
| 388 | | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 389 | | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 390 | lambda_param_maybe_default[NameDefaultPair*]: |
| 391 | | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 392 | | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) } |
| 393 | lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 394 | |
| 395 | disjunction[expr_ty] (memo): |
| 396 | | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp( |
| 397 | Or, |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 398 | CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 399 | EXTRA) } |
| 400 | | conjunction |
| 401 | conjunction[expr_ty] (memo): |
| 402 | | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp( |
| 403 | And, |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 404 | CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 405 | EXTRA) } |
| 406 | | inversion |
| 407 | inversion[expr_ty] (memo): |
| 408 | | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) } |
| 409 | | comparison |
| 410 | comparison[expr_ty]: |
| 411 | | a=bitwise_or b=compare_op_bitwise_or_pair+ { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 412 | _Py_Compare( |
| 413 | a, |
| 414 | CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)), |
| 415 | CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)), |
| 416 | EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 417 | | bitwise_or |
| 418 | compare_op_bitwise_or_pair[CmpopExprPair*]: |
| 419 | | eq_bitwise_or |
| 420 | | noteq_bitwise_or |
| 421 | | lte_bitwise_or |
| 422 | | lt_bitwise_or |
| 423 | | gte_bitwise_or |
| 424 | | gt_bitwise_or |
| 425 | | notin_bitwise_or |
| 426 | | in_bitwise_or |
| 427 | | isnot_bitwise_or |
| 428 | | is_bitwise_or |
| 429 | eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) } |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 430 | noteq_bitwise_or[CmpopExprPair*]: |
Pablo Galindo | 06f8c33 | 2020-10-30 23:48:42 +0000 | [diff] [blame^] | 431 | | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 432 | lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) } |
| 433 | lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) } |
| 434 | gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) } |
| 435 | gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) } |
| 436 | notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) } |
| 437 | in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) } |
| 438 | isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) } |
| 439 | is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) } |
| 440 | |
| 441 | bitwise_or[expr_ty]: |
| 442 | | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) } |
| 443 | | bitwise_xor |
| 444 | bitwise_xor[expr_ty]: |
| 445 | | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) } |
| 446 | | bitwise_and |
| 447 | bitwise_and[expr_ty]: |
| 448 | | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) } |
| 449 | | shift_expr |
| 450 | shift_expr[expr_ty]: |
| 451 | | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) } |
| 452 | | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) } |
| 453 | | sum |
| 454 | |
| 455 | sum[expr_ty]: |
| 456 | | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) } |
| 457 | | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) } |
| 458 | | term |
| 459 | term[expr_ty]: |
| 460 | | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) } |
| 461 | | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) } |
| 462 | | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) } |
| 463 | | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) } |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 464 | | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 465 | | factor |
| 466 | factor[expr_ty] (memo): |
| 467 | | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) } |
| 468 | | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) } |
| 469 | | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) } |
| 470 | | power |
| 471 | power[expr_ty]: |
| 472 | | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) } |
| 473 | | await_primary |
| 474 | await_primary[expr_ty] (memo): |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 475 | | AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _Py_Await(a, EXTRA)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 476 | | primary |
| 477 | primary[expr_ty]: |
Lysandros Nikolaou | 15acc4e | 2020-10-27 20:54:20 +0200 | [diff] [blame] | 478 | | invalid_primary # must be before 'primay genexp' because of invalid_genexp |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 479 | | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 480 | | a=primary b=genexp { _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 481 | | a=primary '(' b=[arguments] ')' { |
| 482 | _Py_Call(a, |
| 483 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 484 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 485 | EXTRA) } |
| 486 | | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) } |
| 487 | | atom |
| 488 | |
| 489 | slices[expr_ty]: |
| 490 | | a=slice !',' { a } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 491 | | a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 492 | slice[expr_ty]: |
| 493 | | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) } |
| 494 | | a=expression { a } |
| 495 | atom[expr_ty]: |
| 496 | | NAME |
| 497 | | 'True' { _Py_Constant(Py_True, NULL, EXTRA) } |
| 498 | | 'False' { _Py_Constant(Py_False, NULL, EXTRA) } |
| 499 | | 'None' { _Py_Constant(Py_None, NULL, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 500 | | &STRING strings |
| 501 | | NUMBER |
| 502 | | &'(' (tuple | group | genexp) |
| 503 | | &'[' (list | listcomp) |
| 504 | | &'{' (dict | set | dictcomp | setcomp) |
| 505 | | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) } |
| 506 | |
| 507 | strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) } |
| 508 | list[expr_ty]: |
| 509 | | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) } |
| 510 | listcomp[expr_ty]: |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 511 | | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 512 | | invalid_comprehension |
| 513 | tuple[expr_ty]: |
| 514 | | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { |
| 515 | _Py_Tuple(a, Load, EXTRA) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 516 | group[expr_ty]: |
| 517 | | '(' a=(yield_expr | named_expression) ')' { a } |
| 518 | | invalid_group |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 519 | genexp[expr_ty]: |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 520 | | '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 521 | | invalid_comprehension |
| 522 | set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) } |
| 523 | setcomp[expr_ty]: |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 524 | | '{' a=expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 525 | | invalid_comprehension |
| 526 | dict[expr_ty]: |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 527 | | '{' a=[double_starred_kvpairs] '}' { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 528 | _Py_Dict( |
| 529 | CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)), |
| 530 | CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)), |
| 531 | EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 532 | dictcomp[expr_ty]: |
| 533 | | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) } |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 534 | | invalid_dict_comprehension |
| 535 | double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a } |
| 536 | double_starred_kvpair[KeyValuePair*]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 537 | | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) } |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 538 | | kvpair |
| 539 | kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 540 | for_if_clauses[asdl_comprehension_seq*]: |
| 541 | | a[asdl_comprehension_seq*]=for_if_clause+ { a } |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 542 | for_if_clause[comprehension_ty]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 543 | | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 544 | CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 545 | | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 546 | _Py_comprehension(a, b, c, 0, p->arena) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 547 | | invalid_for_target |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 548 | |
| 549 | yield_expr[expr_ty]: |
| 550 | | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) } |
| 551 | | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) } |
| 552 | |
| 553 | arguments[expr_ty] (memo): |
| 554 | | a=args [','] &')' { a } |
Lysandros Nikolaou | bca7014 | 2020-10-27 00:42:04 +0200 | [diff] [blame] | 555 | | invalid_arguments |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 556 | args[expr_ty]: |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 557 | | a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 558 | | a=kwargs { _Py_Call(_PyPegen_dummy_name(p), |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 559 | CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)), |
| 560 | CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)), |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 561 | EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 562 | kwargs[asdl_seq*]: |
| 563 | | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) } |
| 564 | | ','.kwarg_or_starred+ |
| 565 | | ','.kwarg_or_double_starred+ |
| 566 | starred_expression[expr_ty]: |
| 567 | | '*' a=expression { _Py_Starred(a, Load, EXTRA) } |
| 568 | kwarg_or_starred[KeywordOrStarred*]: |
| 569 | | a=NAME '=' b=expression { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 570 | _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 571 | | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) } |
Lysandros Nikolaou | 4638c64 | 2020-05-07 13:44:06 +0300 | [diff] [blame] | 572 | | invalid_kwarg |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 573 | kwarg_or_double_starred[KeywordOrStarred*]: |
| 574 | | a=NAME '=' b=expression { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 575 | _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(a->v.Name.id, b, EXTRA)), 1) } |
| 576 | | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _Py_keyword(NULL, a, EXTRA)), 1) } |
Lysandros Nikolaou | 4638c64 | 2020-05-07 13:44:06 +0300 | [diff] [blame] | 577 | | invalid_kwarg |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 578 | |
| 579 | # NOTE: star_targets may contain *bitwise_or, targets may not. |
| 580 | star_targets[expr_ty]: |
| 581 | | a=star_target !',' { a } |
| 582 | | a=star_target b=(',' c=star_target { c })* [','] { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 583 | _Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 584 | star_targets_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 585 | star_target[expr_ty] (memo): |
| 586 | | '*' a=(!'*' star_target) { |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 587 | _Py_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 588 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 589 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 590 | | star_atom |
| 591 | star_atom[expr_ty]: |
| 592 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 593 | | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) } |
| 594 | | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) } |
| 595 | | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) } |
| 596 | |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 597 | single_target[expr_ty]: |
| 598 | | single_subscript_attribute_target |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 599 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 600 | | '(' a=single_target ')' { a } |
| 601 | single_subscript_attribute_target[expr_ty]: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 602 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 603 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 604 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 605 | del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 606 | del_target[expr_ty] (memo): |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 607 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) } |
| 608 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 609 | | del_t_atom |
| 610 | del_t_atom[expr_ty]: |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 611 | | a=NAME { _PyPegen_set_expr_context(p, a, Del) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 612 | | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) } |
| 613 | | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) } |
| 614 | | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) } |
| 615 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 616 | targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.target+ [','] { a } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 617 | target[expr_ty] (memo): |
| 618 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 619 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 620 | | t_atom |
| 621 | t_primary[expr_ty]: |
| 622 | | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } |
| 623 | | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) } |
Lysandros Nikolaou | 2e5ca9e | 2020-10-21 22:53:14 +0300 | [diff] [blame] | 624 | | a=t_primary b=genexp &t_lookahead { |
| 625 | _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 626 | | a=t_primary '(' b=[arguments] ')' &t_lookahead { |
| 627 | _Py_Call(a, |
| 628 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 629 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 630 | EXTRA) } |
| 631 | | a=atom &t_lookahead { a } |
| 632 | t_lookahead: '(' | '[' | '.' |
| 633 | t_atom[expr_ty]: |
| 634 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 635 | | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) } |
| 636 | | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) } |
| 637 | | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) } |
| 638 | |
| 639 | |
| 640 | # From here on, there are rules for invalid syntax with specialised error messages |
Lysandros Nikolaou | bca7014 | 2020-10-27 00:42:04 +0200 | [diff] [blame] | 641 | invalid_arguments: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 642 | | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") } |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 643 | | a=expression for_if_clauses ',' [args | expression for_if_clauses] { |
| 644 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") } |
Lysandros Nikolaou | ae14583 | 2020-05-22 03:56:52 +0300 | [diff] [blame] | 645 | | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) } |
| 646 | | args ',' a=expression for_if_clauses { |
| 647 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 648 | | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) } |
Lysandros Nikolaou | 4638c64 | 2020-05-07 13:44:06 +0300 | [diff] [blame] | 649 | invalid_kwarg: |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 650 | | a=expression '=' { |
| 651 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 652 | a, "expression cannot contain assignment, perhaps you meant \"==\"?") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 653 | invalid_named_expression: |
| 654 | | a=expression ':=' expression { |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 655 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 656 | a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 657 | invalid_assignment: |
Batuhan Taskaya | c8f29ad | 2020-06-27 21:33:08 +0300 | [diff] [blame] | 658 | | a=invalid_ann_assign_target ':' expression { |
| 659 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 660 | a, |
| 661 | "only single target (not %s) can be annotated", |
| 662 | _PyPegen_get_expr_name(a) |
| 663 | )} |
Lysandros Nikolaou | 4b85e60 | 2020-06-26 02:22:36 +0300 | [diff] [blame] | 664 | | a=star_named_expression ',' star_named_expressions* ':' expression { |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 665 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") } |
Lysandros Nikolaou | 4b85e60 | 2020-06-26 02:22:36 +0300 | [diff] [blame] | 666 | | a=expression ':' expression { |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 667 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") } |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame] | 668 | | (star_targets '=')* a=star_expressions '=' { |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 669 | RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) } |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame] | 670 | | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") } |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 671 | | a=star_expressions augassign (yield_expr | star_expressions) { |
| 672 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
Lysandros Nikolaou | 4b85e60 | 2020-06-26 02:22:36 +0300 | [diff] [blame] | 673 | a, |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 674 | "'%s' is an illegal expression for augmented assignment", |
| 675 | _PyPegen_get_expr_name(a) |
| 676 | )} |
Batuhan Taskaya | c8f29ad | 2020-06-27 21:33:08 +0300 | [diff] [blame] | 677 | invalid_ann_assign_target[expr_ty]: |
| 678 | | list |
| 679 | | tuple |
| 680 | | '(' a=invalid_ann_assign_target ')' { a } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 681 | invalid_del_stmt: |
| 682 | | 'del' a=star_expressions { |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 683 | RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 684 | invalid_block: |
| 685 | | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") } |
Lysandros Nikolaou | 15acc4e | 2020-10-27 20:54:20 +0200 | [diff] [blame] | 686 | invalid_primary: |
| 687 | | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 688 | invalid_comprehension: |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 689 | | ('[' | '(' | '{') a=starred_expression for_if_clauses { |
| 690 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") } |
Batuhan Taskaya | b8a65ec | 2020-05-22 01:39:56 +0300 | [diff] [blame] | 691 | invalid_dict_comprehension: |
| 692 | | '{' a='**' bitwise_or for_if_clauses '}' { |
| 693 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 694 | invalid_parameters: |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 695 | | param_no_default* (slash_with_default | param_with_default+) param_no_default { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 696 | RAISE_SYNTAX_ERROR("non-default argument follows default argument") } |
Pablo Galindo | c6483c9 | 2020-06-10 14:07:06 +0100 | [diff] [blame] | 697 | invalid_lambda_parameters: |
| 698 | | lambda_param_no_default* (lambda_slash_with_default | lambda_param_with_default+) lambda_param_no_default { |
| 699 | RAISE_SYNTAX_ERROR("non-default argument follows default argument") } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 700 | invalid_star_etc: |
| 701 | | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } |
Lysandros Nikolaou | 75b863a | 2020-05-18 22:14:47 +0300 | [diff] [blame] | 702 | | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") } |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 703 | invalid_lambda_star_etc: |
| 704 | | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 705 | invalid_double_type_comments: |
| 706 | | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT { |
| 707 | RAISE_SYNTAX_ERROR("Cannot have two type comments on def") } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 708 | invalid_with_item: |
| 709 | | expression 'as' a=expression { |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 710 | RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 711 | |
| 712 | invalid_for_target: |
| 713 | | ASYNC? 'for' a=star_expressions { |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 714 | RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) } |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 715 | |
| 716 | invalid_group: |
| 717 | | '(' a=starred_expression ')' { |
| 718 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") } |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 719 | invalid_import_from_targets: |
| 720 | | import_from_as_names ',' { |
| 721 | RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") } |