Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1 | # Simplified grammar for Python |
| 2 | |
| 3 | @bytecode True |
| 4 | @trailer ''' |
| 5 | void * |
| 6 | _PyPegen_parse(Parser *p) |
| 7 | { |
| 8 | // Initialize keywords |
| 9 | p->keywords = reserved_keywords; |
| 10 | p->n_keyword_lists = n_keyword_lists; |
| 11 | |
| 12 | // Run parser |
| 13 | void *result = NULL; |
| 14 | if (p->start_rule == Py_file_input) { |
| 15 | result = file_rule(p); |
| 16 | } else if (p->start_rule == Py_single_input) { |
| 17 | result = interactive_rule(p); |
| 18 | } else if (p->start_rule == Py_eval_input) { |
| 19 | result = eval_rule(p); |
| 20 | } else if (p->start_rule == Py_fstring_input) { |
| 21 | result = fstring_rule(p); |
| 22 | } |
| 23 | |
| 24 | return result; |
| 25 | } |
| 26 | |
| 27 | // The end |
| 28 | ''' |
| 29 | file[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) } |
| 30 | interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) } |
| 31 | eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) } |
| 32 | fstring[expr_ty]: star_expressions |
| 33 | |
| 34 | statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) } |
| 35 | statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt |
| 36 | statement_newline[asdl_seq*]: |
| 37 | | a=compound_stmt NEWLINE { _PyPegen_singleton_seq(p, a) } |
| 38 | | simple_stmt |
| 39 | | NEWLINE { _PyPegen_singleton_seq(p, CHECK(_Py_Pass(EXTRA))) } |
| 40 | | ENDMARKER { _PyPegen_interactive_exit(p) } |
| 41 | simple_stmt[asdl_seq*]: |
| 42 | | a=small_stmt !';' NEWLINE { _PyPegen_singleton_seq(p, a) } # Not needed, there for speedup |
| 43 | | a=';'.small_stmt+ [';'] NEWLINE { a } |
| 44 | # NOTE: assignment MUST precede expression, else parsing a simple assignment |
| 45 | # will throw a SyntaxError. |
| 46 | small_stmt[stmt_ty] (memo): |
| 47 | | assignment |
| 48 | | e=star_expressions { _Py_Expr(e, EXTRA) } |
| 49 | | &'return' return_stmt |
| 50 | | &('import' | 'from') import_stmt |
| 51 | | &'raise' raise_stmt |
| 52 | | 'pass' { _Py_Pass(EXTRA) } |
| 53 | | &'del' del_stmt |
| 54 | | &'yield' yield_stmt |
| 55 | | &'assert' assert_stmt |
| 56 | | 'break' { _Py_Break(EXTRA) } |
| 57 | | 'continue' { _Py_Continue(EXTRA) } |
| 58 | | &'global' global_stmt |
| 59 | | &'nonlocal' nonlocal_stmt |
| 60 | compound_stmt[stmt_ty]: |
| 61 | | &('def' | '@' | ASYNC) function_def |
| 62 | | &'if' if_stmt |
| 63 | | &('class' | '@') class_def |
| 64 | | &('with' | ASYNC) with_stmt |
| 65 | | &('for' | ASYNC) for_stmt |
| 66 | | &'try' try_stmt |
| 67 | | &'while' while_stmt |
| 68 | |
| 69 | # NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield' |
| 70 | assignment: |
| 71 | | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] { |
| 72 | _Py_AnnAssign(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA) } |
| 73 | | a=('(' b=inside_paren_ann_assign_target ')' { b } |
| 74 | | ann_assign_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] { |
| 75 | _Py_AnnAssign(a, b, c, 0, EXTRA)} |
| 76 | | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) { |
| 77 | _Py_Assign(a, b, NULL, EXTRA) } |
| 78 | | a=target b=augassign c=(yield_expr | star_expressions) { |
| 79 | _Py_AugAssign(a, b->kind, c, EXTRA) } |
| 80 | | invalid_assignment |
| 81 | |
| 82 | augassign[AugOperator*]: |
| 83 | | '+=' {_PyPegen_augoperator(p, Add)} |
| 84 | | '-=' {_PyPegen_augoperator(p, Sub)} |
| 85 | | '*=' {_PyPegen_augoperator(p, Mult)} |
| 86 | | '@=' {_PyPegen_augoperator(p, MatMult)} |
| 87 | | '/=' {_PyPegen_augoperator(p, Div)} |
| 88 | | '%=' {_PyPegen_augoperator(p, Mod)} |
| 89 | | '&=' {_PyPegen_augoperator(p, BitAnd)} |
| 90 | | '|=' {_PyPegen_augoperator(p, BitOr)} |
| 91 | | '^=' {_PyPegen_augoperator(p, BitXor)} |
| 92 | | '<<=' {_PyPegen_augoperator(p, LShift)} |
| 93 | | '>>=' {_PyPegen_augoperator(p, RShift)} |
| 94 | | '**=' {_PyPegen_augoperator(p, Pow)} |
| 95 | | '//=' {_PyPegen_augoperator(p, FloorDiv)} |
| 96 | |
| 97 | global_stmt[stmt_ty]: 'global' a=','.NAME+ { |
| 98 | _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) } |
| 99 | nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ { |
| 100 | _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) } |
| 101 | |
| 102 | yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) } |
| 103 | |
| 104 | assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) } |
| 105 | |
| 106 | del_stmt[stmt_ty]: 'del' a=del_targets { _Py_Delete(a, EXTRA) } |
| 107 | |
| 108 | import_stmt[stmt_ty]: import_name | import_from |
| 109 | import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) } |
| 110 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 111 | import_from[stmt_ty]: |
| 112 | | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets { |
| 113 | _Py_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) } |
| 114 | | 'from' a=('.' | '...')+ 'import' b=import_from_targets { |
| 115 | _Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) } |
| 116 | import_from_targets[asdl_seq*]: |
| 117 | | '(' a=import_from_as_names [','] ')' { a } |
| 118 | | import_from_as_names |
| 119 | | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) } |
| 120 | import_from_as_names[asdl_seq*]: |
| 121 | | a=','.import_from_as_name+ { a } |
| 122 | import_from_as_name[alias_ty]: |
| 123 | | a=NAME b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id, |
| 124 | (b) ? ((expr_ty) b)->v.Name.id : NULL, |
| 125 | p->arena) } |
| 126 | dotted_as_names[asdl_seq*]: |
| 127 | | a=','.dotted_as_name+ { a } |
| 128 | dotted_as_name[alias_ty]: |
| 129 | | a=dotted_name b=['as' z=NAME { z }] { _Py_alias(a->v.Name.id, |
| 130 | (b) ? ((expr_ty) b)->v.Name.id : NULL, |
| 131 | p->arena) } |
| 132 | dotted_name[expr_ty]: |
| 133 | | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) } |
| 134 | | NAME |
| 135 | |
| 136 | if_stmt[stmt_ty]: |
| 137 | | 'if' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) } |
| 138 | | 'if' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) } |
| 139 | elif_stmt[stmt_ty]: |
| 140 | | 'elif' a=named_expression ':' b=block c=elif_stmt { _Py_If(a, b, CHECK(_PyPegen_singleton_seq(p, c)), EXTRA) } |
| 141 | | 'elif' a=named_expression ':' b=block c=[else_block] { _Py_If(a, b, c, EXTRA) } |
| 142 | else_block[asdl_seq*]: 'else' ':' b=block { b } |
| 143 | |
| 144 | while_stmt[stmt_ty]: |
| 145 | | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) } |
| 146 | |
| 147 | for_stmt[stmt_ty]: |
| 148 | | is_async=[ASYNC] 'for' t=star_targets 'in' ex=star_expressions ':' b=block el=[else_block] { |
| 149 | (is_async ? _Py_AsyncFor : _Py_For)(t, ex, b, el, NULL, EXTRA) } |
| 150 | |
| 151 | with_stmt[stmt_ty]: |
| 152 | | is_async=[ASYNC] 'with' '(' a=','.with_item+ ')' ':' b=block { |
| 153 | (is_async ? _Py_AsyncWith : _Py_With)(a, b, NULL, EXTRA) } |
| 154 | | is_async=[ASYNC] 'with' a=','.with_item+ ':' b=block { |
| 155 | (is_async ? _Py_AsyncWith : _Py_With)(a, b, NULL, EXTRA) } |
| 156 | with_item[withitem_ty]: |
| 157 | | e=expression o=['as' t=target { t }] { _Py_withitem(e, o, p->arena) } |
| 158 | |
| 159 | try_stmt[stmt_ty]: |
| 160 | | 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) } |
| 161 | | 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) } |
| 162 | except_block[excepthandler_ty]: |
| 163 | | 'except' e=expression t=['as' z=target { z }] ':' b=block { |
| 164 | _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) } |
| 165 | | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) } |
| 166 | finally_block[asdl_seq*]: 'finally' ':' a=block { a } |
| 167 | |
| 168 | return_stmt[stmt_ty]: |
| 169 | | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) } |
| 170 | |
| 171 | raise_stmt[stmt_ty]: |
| 172 | | 'raise' a=expression b=['from' z=expression { z }] { _Py_Raise(a, b, EXTRA) } |
| 173 | | 'raise' { _Py_Raise(NULL, NULL, EXTRA) } |
| 174 | |
| 175 | function_def[stmt_ty]: |
| 176 | | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) } |
| 177 | | function_def_raw |
| 178 | |
| 179 | function_def_raw[stmt_ty]: |
| 180 | | is_async=[ASYNC] 'def' n=NAME '(' params=[params] ')' a=['->' z=annotation { z }] ':' b=block { |
| 181 | (is_async ? _Py_AsyncFunctionDef : _Py_FunctionDef)(n->v.Name.id, |
| 182 | (params) ? params : CHECK(_PyPegen_empty_arguments(p)), |
| 183 | b, NULL, a, NULL, EXTRA) } |
| 184 | |
| 185 | params[arguments_ty]: |
| 186 | | invalid_parameters |
| 187 | | parameters |
| 188 | parameters[arguments_ty]: |
| 189 | | a=slash_without_default b=[',' x=plain_names { x }] c=[',' y=names_with_default { y }] d=[',' z=[star_etc] { z }] { |
| 190 | _PyPegen_make_arguments(p, a, NULL, b, c, d) } |
| 191 | | a=slash_with_default b=[',' y=names_with_default { y }] c=[',' z=[star_etc] { z }] { |
| 192 | _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } |
| 193 | | a=plain_names b=[',' y=names_with_default { y }] c=[',' z=[star_etc] { z }] { |
| 194 | _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } |
| 195 | | a=names_with_default b=[',' z=[star_etc] { z }] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} |
| 196 | | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } |
| 197 | slash_without_default[asdl_seq*]: a=plain_names ',' '/' { a } |
| 198 | slash_with_default[SlashWithDefault*]: a=[n=plain_names ',' { n }] b=names_with_default ',' '/' { |
| 199 | _PyPegen_slash_with_default(p, a, b) } |
| 200 | star_etc[StarEtc*]: |
| 201 | | '*' a=plain_name b=name_with_optional_default* c=[',' d=kwds { d }] [','] { |
| 202 | _PyPegen_star_etc(p, a, b, c) } |
| 203 | | '*' b=name_with_optional_default+ c=[',' d=kwds { d }] [','] { |
| 204 | _PyPegen_star_etc(p, NULL, b, c) } |
| 205 | | a=kwds [','] { _PyPegen_star_etc(p, NULL, NULL, a) } |
| 206 | name_with_optional_default[NameDefaultPair*]: |
| 207 | | ',' a=plain_name b=['=' e=expression { e }] { _PyPegen_name_default_pair(p, a, b) } |
| 208 | names_with_default[asdl_seq*]: a=','.name_with_default+ { a } |
| 209 | name_with_default[NameDefaultPair*]: |
| 210 | | n=plain_name '=' e=expression { _PyPegen_name_default_pair(p, n, e) } |
| 211 | plain_names[asdl_seq*] (memo): a=','.(plain_name !'=')+ { a } |
| 212 | plain_name[arg_ty]: |
| 213 | | a=NAME b=[':' z=annotation { z }] { _Py_arg(a->v.Name.id, b, NULL, EXTRA) } |
| 214 | kwds[arg_ty]: |
| 215 | | '**' a=plain_name { a } |
| 216 | annotation[expr_ty]: expression |
| 217 | |
| 218 | decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a } |
| 219 | |
| 220 | class_def[stmt_ty]: |
| 221 | | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) } |
| 222 | | class_def_raw |
| 223 | class_def_raw[stmt_ty]: |
| 224 | | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block { |
| 225 | _Py_ClassDef(a->v.Name.id, |
| 226 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 227 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 228 | c, NULL, EXTRA) } |
| 229 | |
| 230 | block[asdl_seq*] (memo): |
| 231 | | NEWLINE INDENT a=statements DEDENT { a } |
| 232 | | simple_stmt |
| 233 | | invalid_block |
| 234 | |
| 235 | expressions_list[asdl_seq*]: a=','.star_expression+ [','] { a } |
| 236 | star_expressions[expr_ty]: |
| 237 | | a=star_expression b=(',' c=star_expression { c })+ [','] { |
| 238 | _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } |
| 239 | | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) } |
| 240 | | star_expression |
| 241 | star_expression[expr_ty] (memo): |
| 242 | | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } |
| 243 | | expression |
| 244 | |
| 245 | star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a } |
| 246 | star_named_expression[expr_ty]: |
| 247 | | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } |
| 248 | | named_expression |
| 249 | named_expression[expr_ty]: |
| 250 | | a=NAME ':=' b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) } |
| 251 | | expression !':=' |
| 252 | | invalid_named_expression |
| 253 | |
| 254 | annotated_rhs[expr_ty]: yield_expr | star_expressions |
| 255 | |
| 256 | expressions[expr_ty]: |
| 257 | | a=expression b=(',' c=expression { c })+ [','] { |
| 258 | _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } |
| 259 | | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) } |
| 260 | | expression |
| 261 | expression[expr_ty] (memo): |
| 262 | | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) } |
| 263 | | disjunction |
| 264 | | lambdef |
| 265 | |
| 266 | lambdef[expr_ty]: |
| 267 | | 'lambda' a=[lambda_parameters] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) } |
| 268 | lambda_parameters[arguments_ty]: |
| 269 | | a=lambda_slash_without_default b=[',' x=lambda_plain_names { x }] c=[',' y=lambda_names_with_default { y }] d=[',' z=[lambda_star_etc] { z }] { |
| 270 | _PyPegen_make_arguments(p, a, NULL, b, c, d) } |
| 271 | | a=lambda_slash_with_default b=[',' y=lambda_names_with_default { y }] c=[',' z=[lambda_star_etc] { z }] { |
| 272 | _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } |
| 273 | | a=lambda_plain_names b=[',' y=lambda_names_with_default { y }] c=[',' z=[lambda_star_etc] { z }] { |
| 274 | _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } |
| 275 | | a=lambda_names_with_default b=[',' z=[lambda_star_etc] { z }] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} |
| 276 | | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } |
| 277 | lambda_slash_without_default[asdl_seq*]: a=lambda_plain_names ',' '/' { a } |
| 278 | lambda_slash_with_default[SlashWithDefault*]: a=[n=lambda_plain_names ',' { n }] b=lambda_names_with_default ',' '/' { |
| 279 | _PyPegen_slash_with_default(p, a, b) } |
| 280 | lambda_star_etc[StarEtc*]: |
| 281 | | '*' a=lambda_plain_name b=lambda_name_with_optional_default* c=[',' d=lambda_kwds { d }] [','] { |
| 282 | _PyPegen_star_etc(p, a, b, c) } |
| 283 | | '*' b=lambda_name_with_optional_default+ c=[',' d=lambda_kwds { d }] [','] { |
| 284 | _PyPegen_star_etc(p, NULL, b, c) } |
| 285 | | a=lambda_kwds [','] { _PyPegen_star_etc(p, NULL, NULL, a) } |
| 286 | lambda_name_with_optional_default[NameDefaultPair*]: |
| 287 | | ',' a=lambda_plain_name b=['=' e=expression { e }] { _PyPegen_name_default_pair(p, a, b) } |
| 288 | lambda_names_with_default[asdl_seq*]: a=','.lambda_name_with_default+ { a } |
| 289 | lambda_name_with_default[NameDefaultPair*]: |
| 290 | | n=lambda_plain_name '=' e=expression { _PyPegen_name_default_pair(p, n, e) } |
| 291 | lambda_plain_names[asdl_seq*]: a=','.(lambda_plain_name !'=')+ { a } |
| 292 | lambda_plain_name[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) } |
| 293 | lambda_kwds[arg_ty]: '**' a=lambda_plain_name { a } |
| 294 | |
| 295 | disjunction[expr_ty] (memo): |
| 296 | | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp( |
| 297 | Or, |
| 298 | CHECK(_PyPegen_seq_insert_in_front(p, a, b)), |
| 299 | EXTRA) } |
| 300 | | conjunction |
| 301 | conjunction[expr_ty] (memo): |
| 302 | | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp( |
| 303 | And, |
| 304 | CHECK(_PyPegen_seq_insert_in_front(p, a, b)), |
| 305 | EXTRA) } |
| 306 | | inversion |
| 307 | inversion[expr_ty] (memo): |
| 308 | | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) } |
| 309 | | comparison |
| 310 | comparison[expr_ty]: |
| 311 | | a=bitwise_or b=compare_op_bitwise_or_pair+ { |
| 312 | _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) } |
| 313 | | bitwise_or |
| 314 | compare_op_bitwise_or_pair[CmpopExprPair*]: |
| 315 | | eq_bitwise_or |
| 316 | | noteq_bitwise_or |
| 317 | | lte_bitwise_or |
| 318 | | lt_bitwise_or |
| 319 | | gte_bitwise_or |
| 320 | | gt_bitwise_or |
| 321 | | notin_bitwise_or |
| 322 | | in_bitwise_or |
| 323 | | isnot_bitwise_or |
| 324 | | is_bitwise_or |
| 325 | 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^] | 326 | noteq_bitwise_or[CmpopExprPair*]: |
| 327 | | (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] | 328 | lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) } |
| 329 | lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) } |
| 330 | gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) } |
| 331 | gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) } |
| 332 | notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) } |
| 333 | in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) } |
| 334 | isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) } |
| 335 | is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) } |
| 336 | |
| 337 | bitwise_or[expr_ty]: |
| 338 | | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) } |
| 339 | | bitwise_xor |
| 340 | bitwise_xor[expr_ty]: |
| 341 | | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) } |
| 342 | | bitwise_and |
| 343 | bitwise_and[expr_ty]: |
| 344 | | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) } |
| 345 | | shift_expr |
| 346 | shift_expr[expr_ty]: |
| 347 | | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) } |
| 348 | | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) } |
| 349 | | sum |
| 350 | |
| 351 | sum[expr_ty]: |
| 352 | | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) } |
| 353 | | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) } |
| 354 | | term |
| 355 | term[expr_ty]: |
| 356 | | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) } |
| 357 | | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) } |
| 358 | | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) } |
| 359 | | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) } |
| 360 | | a=term '@' b=factor { _Py_BinOp(a, MatMult, b, EXTRA) } |
| 361 | | factor |
| 362 | factor[expr_ty] (memo): |
| 363 | | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) } |
| 364 | | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) } |
| 365 | | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) } |
| 366 | | power |
| 367 | power[expr_ty]: |
| 368 | | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) } |
| 369 | | await_primary |
| 370 | await_primary[expr_ty] (memo): |
| 371 | | AWAIT a=primary { _Py_Await(a, EXTRA) } |
| 372 | | primary |
| 373 | primary[expr_ty]: |
| 374 | | a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } |
| 375 | | a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } |
| 376 | | a=primary '(' b=[arguments] ')' { |
| 377 | _Py_Call(a, |
| 378 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 379 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 380 | EXTRA) } |
| 381 | | a=primary '[' b=slices ']' { _Py_Subscript(a, b, Load, EXTRA) } |
| 382 | | atom |
| 383 | |
| 384 | slices[expr_ty]: |
| 385 | | a=slice !',' { a } |
| 386 | | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) } |
| 387 | slice[expr_ty]: |
| 388 | | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) } |
| 389 | | a=expression { a } |
| 390 | atom[expr_ty]: |
| 391 | | NAME |
| 392 | | 'True' { _Py_Constant(Py_True, NULL, EXTRA) } |
| 393 | | 'False' { _Py_Constant(Py_False, NULL, EXTRA) } |
| 394 | | 'None' { _Py_Constant(Py_None, NULL, EXTRA) } |
| 395 | | '__new_parser__' { RAISE_SYNTAX_ERROR("You found it!") } |
| 396 | | &STRING strings |
| 397 | | NUMBER |
| 398 | | &'(' (tuple | group | genexp) |
| 399 | | &'[' (list | listcomp) |
| 400 | | &'{' (dict | set | dictcomp | setcomp) |
| 401 | | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) } |
| 402 | |
| 403 | strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) } |
| 404 | list[expr_ty]: |
| 405 | | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) } |
| 406 | listcomp[expr_ty]: |
| 407 | | '[' a=named_expression b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) } |
| 408 | | invalid_comprehension |
| 409 | tuple[expr_ty]: |
| 410 | | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { |
| 411 | _Py_Tuple(a, Load, EXTRA) } |
| 412 | group[expr_ty]: '(' a=(yield_expr | named_expression) ')' { a } |
| 413 | genexp[expr_ty]: |
| 414 | | '(' a=expression b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) } |
| 415 | | invalid_comprehension |
| 416 | set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) } |
| 417 | setcomp[expr_ty]: |
| 418 | | '{' a=expression b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) } |
| 419 | | invalid_comprehension |
| 420 | dict[expr_ty]: |
| 421 | | '{' a=[kvpairs] '}' { _Py_Dict(CHECK(_PyPegen_get_keys(p, a)), |
| 422 | CHECK(_PyPegen_get_values(p, a)), EXTRA) } |
| 423 | dictcomp[expr_ty]: |
| 424 | | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) } |
| 425 | kvpairs[asdl_seq*]: a=','.kvpair+ [','] { a } |
| 426 | kvpair[KeyValuePair*]: |
| 427 | | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) } |
| 428 | | a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) } |
| 429 | for_if_clauses[asdl_seq*]: |
| 430 | | a=(y=[ASYNC] 'for' a=star_targets 'in' b=disjunction c=('if' z=disjunction { z })* |
| 431 | { _Py_comprehension(a, b, c, y != NULL, p->arena) })+ { a } |
| 432 | |
| 433 | yield_expr[expr_ty]: |
| 434 | | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) } |
| 435 | | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) } |
| 436 | |
| 437 | arguments[expr_ty] (memo): |
| 438 | | a=args [','] &')' { a } |
| 439 | | incorrect_arguments |
| 440 | args[expr_ty]: |
| 441 | | a=starred_expression b=[',' c=args { c }] { |
| 442 | _Py_Call(_PyPegen_dummy_name(p), |
| 443 | (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args)) |
| 444 | : CHECK(_PyPegen_singleton_seq(p, a)), |
| 445 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 446 | EXTRA) } |
| 447 | | a=kwargs { _Py_Call(_PyPegen_dummy_name(p), |
| 448 | CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)), |
| 449 | CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)), |
| 450 | EXTRA) } |
| 451 | | a=named_expression b=[',' c=args { c }] { |
| 452 | _Py_Call(_PyPegen_dummy_name(p), |
| 453 | (b) ? CHECK(_PyPegen_seq_insert_in_front(p, a, ((expr_ty) b)->v.Call.args)) |
| 454 | : CHECK(_PyPegen_singleton_seq(p, a)), |
| 455 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 456 | EXTRA) } |
| 457 | kwargs[asdl_seq*]: |
| 458 | | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) } |
| 459 | | ','.kwarg_or_starred+ |
| 460 | | ','.kwarg_or_double_starred+ |
| 461 | starred_expression[expr_ty]: |
| 462 | | '*' a=expression { _Py_Starred(a, Load, EXTRA) } |
| 463 | kwarg_or_starred[KeywordOrStarred*]: |
| 464 | | a=NAME '=' b=expression { |
| 465 | _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) } |
| 466 | | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) } |
| 467 | kwarg_or_double_starred[KeywordOrStarred*]: |
| 468 | | a=NAME '=' b=expression { |
| 469 | _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) } |
| 470 | | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) } |
| 471 | |
| 472 | # NOTE: star_targets may contain *bitwise_or, targets may not. |
| 473 | star_targets[expr_ty]: |
| 474 | | a=star_target !',' { a } |
| 475 | | a=star_target b=(',' c=star_target { c })* [','] { |
| 476 | _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) } |
| 477 | star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a } |
| 478 | star_target[expr_ty] (memo): |
| 479 | | '*' a=(!'*' star_target) { |
| 480 | _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) } |
| 481 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 482 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 483 | | star_atom |
| 484 | star_atom[expr_ty]: |
| 485 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 486 | | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) } |
| 487 | | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) } |
| 488 | | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) } |
| 489 | |
| 490 | inside_paren_ann_assign_target[expr_ty]: |
| 491 | | ann_assign_subscript_attribute_target |
| 492 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 493 | | '(' a=inside_paren_ann_assign_target ')' { a } |
| 494 | |
| 495 | ann_assign_subscript_attribute_target[expr_ty]: |
| 496 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 497 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 498 | |
| 499 | del_targets[asdl_seq*]: a=','.del_target+ [','] { a } |
| 500 | del_target[expr_ty] (memo): |
| 501 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) } |
| 502 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) } |
| 503 | | del_t_atom |
| 504 | del_t_atom[expr_ty]: |
| 505 | | a=NAME { _PyPegen_set_expr_context(p, a, Del) } |
| 506 | | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) } |
| 507 | | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) } |
| 508 | | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) } |
| 509 | |
| 510 | targets[asdl_seq*]: a=','.target+ [','] { a } |
| 511 | target[expr_ty] (memo): |
| 512 | | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } |
| 513 | | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } |
| 514 | | t_atom |
| 515 | t_primary[expr_ty]: |
| 516 | | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } |
| 517 | | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) } |
| 518 | | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } |
| 519 | | a=t_primary '(' b=[arguments] ')' &t_lookahead { |
| 520 | _Py_Call(a, |
| 521 | (b) ? ((expr_ty) b)->v.Call.args : NULL, |
| 522 | (b) ? ((expr_ty) b)->v.Call.keywords : NULL, |
| 523 | EXTRA) } |
| 524 | | a=atom &t_lookahead { a } |
| 525 | t_lookahead: '(' | '[' | '.' |
| 526 | t_atom[expr_ty]: |
| 527 | | a=NAME { _PyPegen_set_expr_context(p, a, Store) } |
| 528 | | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) } |
| 529 | | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) } |
| 530 | | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) } |
| 531 | |
| 532 | |
| 533 | # From here on, there are rules for invalid syntax with specialised error messages |
| 534 | incorrect_arguments: |
| 535 | | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") } |
| 536 | | expression for_if_clauses ',' [args | expression for_if_clauses] { |
| 537 | RAISE_SYNTAX_ERROR("Generator expression must be parenthesized") } |
| 538 | | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) } |
| 539 | invalid_named_expression: |
| 540 | | a=expression ':=' expression { |
| 541 | RAISE_SYNTAX_ERROR("cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } |
| 542 | invalid_assignment: |
| 543 | | list ':' { RAISE_SYNTAX_ERROR("only single target (not list) can be annotated") } |
| 544 | | tuple ':' { RAISE_SYNTAX_ERROR("only single target (not tuple) can be annotated") } |
| 545 | | expression ':' expression ['=' annotated_rhs] { |
| 546 | RAISE_SYNTAX_ERROR("illegal target for annotation") } |
| 547 | | a=expression ('=' | augassign) (yield_expr | star_expressions) { |
| 548 | RAISE_SYNTAX_ERROR("cannot assign to %s", _PyPegen_get_expr_name(a)) } |
| 549 | invalid_block: |
| 550 | | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") } |
| 551 | invalid_comprehension: |
| 552 | | ('[' | '(' | '{') '*' expression for_if_clauses { |
| 553 | RAISE_SYNTAX_ERROR("iterable unpacking cannot be used in comprehension") } |
| 554 | invalid_parameters: |
| 555 | | [plain_names ','] (slash_with_default | names_with_default) ',' plain_names { |
| 556 | RAISE_SYNTAX_ERROR("non-default argument follows default argument") } |