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