1# PEG grammar for Python 2 3@trailer ''' 4void * 5_PyPegen_parse(Parser *p) 6{ 7 // Initialize keywords 8 p->keywords = reserved_keywords; 9 p->n_keyword_lists = n_keyword_lists; 10 11 // Run parser 12 void *result = NULL; 13 if (p->start_rule == Py_file_input) { 14 result = file_rule(p); 15 } else if (p->start_rule == Py_single_input) { 16 result = interactive_rule(p); 17 } else if (p->start_rule == Py_eval_input) { 18 result = eval_rule(p); 19 } else if (p->start_rule == Py_func_type_input) { 20 result = func_type_rule(p); 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''' 30file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) } 31interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) } 32eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) } 33func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { FunctionType(a, b, p->arena) } 34fstring[expr_ty]: star_expressions 35 36# type_expressions allow */** but ignore them 37type_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) } 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) } 46 | ','.expression+ 47 48statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) } 49statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) } | simple_stmt 50statement_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) } 55simple_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. 60small_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 74compound_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' 84assignment[stmt_ty]: 85 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] { 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 ) } 91 | a=('(' b=single_target ')' { b } 92 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] { 93 CHECK_VERSION(6, "Variable annotations syntax is", _Py_AnnAssign(a, b, c, 0, EXTRA)) } 94 | a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] { 95 _Py_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } 96 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) { 97 _Py_AugAssign(a, b->kind, c, EXTRA) } 98 | invalid_assignment 99 100augassign[AugOperator*]: 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) } 114 115global_stmt[stmt_ty]: 'global' a=','.NAME+ { 116 _Py_Global(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) } 117nonlocal_stmt[stmt_ty]: 'nonlocal' a=','.NAME+ { 118 _Py_Nonlocal(CHECK(_PyPegen_map_names_to_ids(p, a)), EXTRA) } 119 120yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) } 121 122assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) } 123 124del_stmt[stmt_ty]: 125 | 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) } 126 | invalid_del_stmt 127 128import_stmt[stmt_ty]: import_name | import_from 129import_name[stmt_ty]: 'import' a=dotted_as_names { _Py_Import(a, EXTRA) } 130# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS 131import_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) } 136import_from_targets[asdl_seq*]: 137 | '(' a=import_from_as_names [','] ')' { a } 138 | import_from_as_names !',' 139 | '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) } 140 | invalid_import_from_targets 141import_from_as_names[asdl_seq*]: 142 | a=','.import_from_as_name+ { a } 143import_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) } 147dotted_as_names[asdl_seq*]: 148 | a=','.dotted_as_name+ { a } 149dotted_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) } 153dotted_name[expr_ty]: 154 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) } 155 | NAME 156 157if_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) } 160elif_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) } 163else_block[asdl_seq*]: 'else' ':' b=block { b } 164 165while_stmt[stmt_ty]: 166 | 'while' a=named_expression ':' b=block c=[else_block] { _Py_While(a, b, c, EXTRA) } 167 168for_stmt[stmt_ty]: 169 | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { 170 _Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) } 171 | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { 172 CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) } 173 | invalid_for_target 174 175with_stmt[stmt_ty]: 176 | 'with' '(' a=','.with_item+ ','? ')' ':' b=block { 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) } 180 | ASYNC 'with' '(' a=','.with_item+ ','? ')' ':' b=block { 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)) } 184with_item[withitem_ty]: 185 | e=expression 'as' t=star_target &(',' | ')' | ':') { _Py_withitem(e, t, p->arena) } 186 | invalid_with_item 187 | e=expression { _Py_withitem(e, NULL, p->arena) } 188 189try_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) } 192except_block[excepthandler_ty]: 193 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block { 194 _Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) } 195 | 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) } 196finally_block[asdl_seq*]: 'finally' ':' a=block { a } 197 198return_stmt[stmt_ty]: 199 | 'return' a=[star_expressions] { _Py_Return(a, EXTRA) } 200 201raise_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 205function_def[stmt_ty]: 206 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) } 207 | function_def_raw 208 209function_def_raw[stmt_ty]: 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 ) } 222func_type_comment[Token*]: 223 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block 224 | invalid_double_type_comments 225 | TYPE_COMMENT 226 227params[arguments_ty]: 228 | invalid_parameters 229 | parameters 230 231parameters[arguments_ty]: 232 | a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] { 233 _PyPegen_make_arguments(p, a, NULL, b, c, d) } 234 | a=slash_with_default b=param_with_default* c=[star_etc] { 235 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } 236 | a=param_no_default+ b=param_with_default* c=[star_etc] { 237 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } 238 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} 239 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } 240 241# Some duplication here because we can't write (',' | &')'), 242# which is because we don't support empty alternatives (yet). 243# 244slash_no_default[asdl_seq*]: 245 | a=param_no_default+ '/' ',' { a } 246 | a=param_no_default+ '/' &')' { a } 247slash_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 251star_etc[StarEtc*]: 252 | '*' a=param_no_default b=param_maybe_default* c=[kwds] { 253 _PyPegen_star_etc(p, a, b, c) } 254 | '*' ',' b=param_maybe_default+ c=[kwds] { 255 _PyPegen_star_etc(p, NULL, b, c) } 256 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) } 257 | invalid_star_etc 258 259kwds[arg_ty]: '**' a=param_no_default { a } 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# 273param_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) } 276param_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) } 279param_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) } 282param[arg_ty]: a=NAME b=annotation? { _Py_arg(a->v.Name.id, b, NULL, EXTRA) } 283 284annotation[expr_ty]: ':' a=expression { a } 285default[expr_ty]: '=' a=expression { a } 286 287decorators[asdl_seq*]: a=('@' f=named_expression NEWLINE { f })+ { a } 288 289class_def[stmt_ty]: 290 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) } 291 | class_def_raw 292class_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 299block[asdl_seq*] (memo): 300 | NEWLINE INDENT a=statements DEDENT { a } 301 | simple_stmt 302 | invalid_block 303 304star_expressions[expr_ty]: 305 | a=star_expression b=(',' c=star_expression { c })+ [','] { 306 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } 307 | a=star_expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) } 308 | star_expression 309star_expression[expr_ty] (memo): 310 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } 311 | expression 312 313star_named_expressions[asdl_seq*]: a=','.star_named_expression+ [','] { a } 314star_named_expression[expr_ty]: 315 | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } 316 | named_expression 317named_expression[expr_ty]: 318 | a=NAME ':=' ~ b=expression { _Py_NamedExpr(CHECK(_PyPegen_set_expr_context(p, a, Store)), b, EXTRA) } 319 | expression !':=' 320 | invalid_named_expression 321 322annotated_rhs[expr_ty]: yield_expr | star_expressions 323 324expressions[expr_ty]: 325 | a=expression b=(',' c=expression { c })+ [','] { 326 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } 327 | a=expression ',' { _Py_Tuple(CHECK(_PyPegen_singleton_seq(p, a)), Load, EXTRA) } 328 | expression 329expression[expr_ty] (memo): 330 | a=disjunction 'if' b=disjunction 'else' c=expression { _Py_IfExp(b, a, c, EXTRA) } 331 | disjunction 332 | lambdef 333 334lambdef[expr_ty]: 335 | 'lambda' a=[lambda_params] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) } 336 337lambda_params[arguments_ty]: 338 | invalid_lambda_parameters 339 | lambda_parameters 340 341# lambda_parameters etc. duplicates parameters but without annotations 342# or type comments, and if there's no comma after a parameter, we expect 343# a colon, not a close parenthesis. (For more, see parameters above.) 344# 345lambda_parameters[arguments_ty]: 346 | a=lambda_slash_no_default b=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] { 347 _PyPegen_make_arguments(p, a, NULL, b, c, d) } 348 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] { 349 _PyPegen_make_arguments(p, NULL, a, NULL, b, c) } 350 | a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] { 351 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } 352 | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} 353 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } 354 355lambda_slash_no_default[asdl_seq*]: 356 | a=lambda_param_no_default+ '/' ',' { a } 357 | a=lambda_param_no_default+ '/' &':' { a } 358lambda_slash_with_default[SlashWithDefault*]: 359 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, a, b) } 360 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, a, b) } 361 362lambda_star_etc[StarEtc*]: 363 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] { 364 _PyPegen_star_etc(p, a, b, c) } 365 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] { 366 _PyPegen_star_etc(p, NULL, b, c) } 367 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) } 368 | invalid_lambda_star_etc 369 370lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a } 371 372lambda_param_no_default[arg_ty]: 373 | a=lambda_param ',' { a } 374 | a=lambda_param &':' { a } 375lambda_param_with_default[NameDefaultPair*]: 376 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) } 377 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) } 378lambda_param_maybe_default[NameDefaultPair*]: 379 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) } 380 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) } 381lambda_param[arg_ty]: a=NAME { _Py_arg(a->v.Name.id, NULL, NULL, EXTRA) } 382 383disjunction[expr_ty] (memo): 384 | a=conjunction b=('or' c=conjunction { c })+ { _Py_BoolOp( 385 Or, 386 CHECK(_PyPegen_seq_insert_in_front(p, a, b)), 387 EXTRA) } 388 | conjunction 389conjunction[expr_ty] (memo): 390 | a=inversion b=('and' c=inversion { c })+ { _Py_BoolOp( 391 And, 392 CHECK(_PyPegen_seq_insert_in_front(p, a, b)), 393 EXTRA) } 394 | inversion 395inversion[expr_ty] (memo): 396 | 'not' a=inversion { _Py_UnaryOp(Not, a, EXTRA) } 397 | comparison 398comparison[expr_ty]: 399 | a=bitwise_or b=compare_op_bitwise_or_pair+ { 400 _Py_Compare(a, CHECK(_PyPegen_get_cmpops(p, b)), CHECK(_PyPegen_get_exprs(p, b)), EXTRA) } 401 | bitwise_or 402compare_op_bitwise_or_pair[CmpopExprPair*]: 403 | eq_bitwise_or 404 | noteq_bitwise_or 405 | lte_bitwise_or 406 | lt_bitwise_or 407 | gte_bitwise_or 408 | gt_bitwise_or 409 | notin_bitwise_or 410 | in_bitwise_or 411 | isnot_bitwise_or 412 | is_bitwise_or 413eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) } 414noteq_bitwise_or[CmpopExprPair*]: 415 | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) } 416lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) } 417lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) } 418gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) } 419gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) } 420notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) } 421in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) } 422isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) } 423is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) } 424 425bitwise_or[expr_ty]: 426 | a=bitwise_or '|' b=bitwise_xor { _Py_BinOp(a, BitOr, b, EXTRA) } 427 | bitwise_xor 428bitwise_xor[expr_ty]: 429 | a=bitwise_xor '^' b=bitwise_and { _Py_BinOp(a, BitXor, b, EXTRA) } 430 | bitwise_and 431bitwise_and[expr_ty]: 432 | a=bitwise_and '&' b=shift_expr { _Py_BinOp(a, BitAnd, b, EXTRA) } 433 | shift_expr 434shift_expr[expr_ty]: 435 | a=shift_expr '<<' b=sum { _Py_BinOp(a, LShift, b, EXTRA) } 436 | a=shift_expr '>>' b=sum { _Py_BinOp(a, RShift, b, EXTRA) } 437 | sum 438 439sum[expr_ty]: 440 | a=sum '+' b=term { _Py_BinOp(a, Add, b, EXTRA) } 441 | a=sum '-' b=term { _Py_BinOp(a, Sub, b, EXTRA) } 442 | term 443term[expr_ty]: 444 | a=term '*' b=factor { _Py_BinOp(a, Mult, b, EXTRA) } 445 | a=term '/' b=factor { _Py_BinOp(a, Div, b, EXTRA) } 446 | a=term '//' b=factor { _Py_BinOp(a, FloorDiv, b, EXTRA) } 447 | a=term '%' b=factor { _Py_BinOp(a, Mod, b, EXTRA) } 448 | a=term '@' b=factor { CHECK_VERSION(5, "The '@' operator is", _Py_BinOp(a, MatMult, b, EXTRA)) } 449 | factor 450factor[expr_ty] (memo): 451 | '+' a=factor { _Py_UnaryOp(UAdd, a, EXTRA) } 452 | '-' a=factor { _Py_UnaryOp(USub, a, EXTRA) } 453 | '~' a=factor { _Py_UnaryOp(Invert, a, EXTRA) } 454 | power 455power[expr_ty]: 456 | a=await_primary '**' b=factor { _Py_BinOp(a, Pow, b, EXTRA) } 457 | await_primary 458await_primary[expr_ty] (memo): 459 | AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) } 460 | primary 461primary[expr_ty]: 462 | invalid_primary # must be before 'primay genexp' because of invalid_genexp 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 473slices[expr_ty]: 474 | a=slice !',' { a } 475 | a=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) } 476slice[expr_ty]: 477 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) } 478 | a=expression { a } 479atom[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) } 484 | '__peg_parser__' { RAISE_SYNTAX_ERROR("You found it!") } 485 | &STRING strings 486 | NUMBER 487 | &'(' (tuple | group | genexp) 488 | &'[' (list | listcomp) 489 | &'{' (dict | set | dictcomp | setcomp) 490 | '...' { _Py_Constant(Py_Ellipsis, NULL, EXTRA) } 491 492strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) } 493list[expr_ty]: 494 | '[' a=[star_named_expressions] ']' { _Py_List(a, Load, EXTRA) } 495listcomp[expr_ty]: 496 | '[' a=named_expression ~ b=for_if_clauses ']' { _Py_ListComp(a, b, EXTRA) } 497 | invalid_comprehension 498tuple[expr_ty]: 499 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { 500 _Py_Tuple(a, Load, EXTRA) } 501group[expr_ty]: 502 | '(' a=(yield_expr | named_expression) ')' { a } 503 | invalid_group 504genexp[expr_ty]: 505 | '(' a=named_expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) } 506 | invalid_comprehension 507set[expr_ty]: '{' a=star_named_expressions '}' { _Py_Set(a, EXTRA) } 508setcomp[expr_ty]: 509 | '{' a=named_expression ~ b=for_if_clauses '}' { _Py_SetComp(a, b, EXTRA) } 510 | invalid_comprehension 511dict[expr_ty]: 512 | '{' a=[double_starred_kvpairs] '}' { 513 _Py_Dict(CHECK(_PyPegen_get_keys(p, a)), CHECK(_PyPegen_get_values(p, a)), EXTRA) } 514dictcomp[expr_ty]: 515 | '{' a=kvpair b=for_if_clauses '}' { _Py_DictComp(a->key, a->value, b, EXTRA) } 516 | invalid_dict_comprehension 517double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a } 518double_starred_kvpair[KeyValuePair*]: 519 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) } 520 | kvpair 521kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) } 522for_if_clauses[asdl_seq*]: 523 | for_if_clause+ 524for_if_clause[comprehension_ty]: 525 | ASYNC 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* { 526 CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) } 527 | 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* { 528 _Py_comprehension(a, b, c, 0, p->arena) } 529 | invalid_for_target 530 531yield_expr[expr_ty]: 532 | 'yield' 'from' a=expression { _Py_YieldFrom(a, EXTRA) } 533 | 'yield' a=[star_expressions] { _Py_Yield(a, EXTRA) } 534 535arguments[expr_ty] (memo): 536 | a=args [','] &')' { a } 537 | invalid_arguments 538args[expr_ty]: 539 | a=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) } 540 | a=kwargs { _Py_Call(_PyPegen_dummy_name(p), 541 CHECK_NULL_ALLOWED(_PyPegen_seq_extract_starred_exprs(p, a)), 542 CHECK_NULL_ALLOWED(_PyPegen_seq_delete_starred_exprs(p, a)), 543 EXTRA) } 544kwargs[asdl_seq*]: 545 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) } 546 | ','.kwarg_or_starred+ 547 | ','.kwarg_or_double_starred+ 548starred_expression[expr_ty]: 549 | '*' a=expression { _Py_Starred(a, Load, EXTRA) } 550kwarg_or_starred[KeywordOrStarred*]: 551 | a=NAME '=' b=expression { 552 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) } 553 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) } 554 | invalid_kwarg 555kwarg_or_double_starred[KeywordOrStarred*]: 556 | a=NAME '=' b=expression { 557 _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(a->v.Name.id, b, EXTRA)), 1) } 558 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(_Py_keyword(NULL, a, EXTRA)), 1) } 559 | invalid_kwarg 560 561# NOTE: star_targets may contain *bitwise_or, targets may not. 562star_targets[expr_ty]: 563 | a=star_target !',' { a } 564 | a=star_target b=(',' c=star_target { c })* [','] { 565 _Py_Tuple(CHECK(_PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) } 566star_targets_seq[asdl_seq*]: a=','.star_target+ [','] { a } 567star_target[expr_ty] (memo): 568 | '*' a=(!'*' star_target) { 569 _Py_Starred(CHECK(_PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) } 570 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } 571 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } 572 | star_atom 573star_atom[expr_ty]: 574 | a=NAME { _PyPegen_set_expr_context(p, a, Store) } 575 | '(' a=star_target ')' { _PyPegen_set_expr_context(p, a, Store) } 576 | '(' a=[star_targets_seq] ')' { _Py_Tuple(a, Store, EXTRA) } 577 | '[' a=[star_targets_seq] ']' { _Py_List(a, Store, EXTRA) } 578 579single_target[expr_ty]: 580 | single_subscript_attribute_target 581 | a=NAME { _PyPegen_set_expr_context(p, a, Store) } 582 | '(' a=single_target ')' { a } 583single_subscript_attribute_target[expr_ty]: 584 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } 585 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } 586 587del_targets[asdl_seq*]: a=','.del_target+ [','] { a } 588del_target[expr_ty] (memo): 589 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) } 590 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) } 591 | del_t_atom 592del_t_atom[expr_ty]: 593 | a=NAME { _PyPegen_set_expr_context(p, a, Del) } 594 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) } 595 | '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) } 596 | '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) } 597 598targets[asdl_seq*]: a=','.target+ [','] { a } 599target[expr_ty] (memo): 600 | a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Store, EXTRA) } 601 | a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) } 602 | t_atom 603t_primary[expr_ty]: 604 | a=t_primary '.' b=NAME &t_lookahead { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) } 605 | a=t_primary '[' b=slices ']' &t_lookahead { _Py_Subscript(a, b, Load, EXTRA) } 606 | a=t_primary b=genexp &t_lookahead { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } 607 | a=t_primary '(' b=[arguments] ')' &t_lookahead { 608 _Py_Call(a, 609 (b) ? ((expr_ty) b)->v.Call.args : NULL, 610 (b) ? ((expr_ty) b)->v.Call.keywords : NULL, 611 EXTRA) } 612 | a=atom &t_lookahead { a } 613t_lookahead: '(' | '[' | '.' 614t_atom[expr_ty]: 615 | a=NAME { _PyPegen_set_expr_context(p, a, Store) } 616 | '(' a=target ')' { _PyPegen_set_expr_context(p, a, Store) } 617 | '(' b=[targets] ')' { _Py_Tuple(b, Store, EXTRA) } 618 | '[' b=[targets] ']' { _Py_List(b, Store, EXTRA) } 619 620 621# From here on, there are rules for invalid syntax with specialised error messages 622invalid_arguments: 623 | args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") } 624 | a=expression for_if_clauses ',' [args | expression for_if_clauses] { 625 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") } 626 | a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) } 627 | args ',' a=expression for_if_clauses { 628 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") } 629 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) } 630invalid_kwarg: 631 | a=expression '=' { 632 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 633 a, "expression cannot contain assignment, perhaps you meant \"==\"?") } 634invalid_named_expression: 635 | a=expression ':=' expression { 636 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 637 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } 638invalid_assignment: 639 | a=invalid_ann_assign_target ':' expression { 640 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 641 a, 642 "only single target (not %s) can be annotated", 643 _PyPegen_get_expr_name(a) 644 )} 645 | a=star_named_expression ',' star_named_expressions* ':' expression { 646 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") } 647 | a=expression ':' expression { 648 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") } 649 | (star_targets '=')* a=star_expressions '=' { 650 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) } 651 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") } 652 | a=star_expressions augassign (yield_expr | star_expressions) { 653 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 654 a, 655 "'%s' is an illegal expression for augmented assignment", 656 _PyPegen_get_expr_name(a) 657 )} 658invalid_ann_assign_target[expr_ty]: 659 | list 660 | tuple 661 | '(' a=invalid_ann_assign_target ')' { a } 662invalid_del_stmt: 663 | 'del' a=star_expressions { 664 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) } 665invalid_block: 666 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") } 667invalid_primary: 668 | primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") } 669invalid_comprehension: 670 | ('[' | '(' | '{') a=starred_expression for_if_clauses { 671 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") } 672invalid_dict_comprehension: 673 | '{' a='**' bitwise_or for_if_clauses '}' { 674 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") } 675invalid_parameters: 676 | param_no_default* (slash_with_default | param_with_default+) param_no_default { 677 RAISE_SYNTAX_ERROR("non-default argument follows default argument") } 678invalid_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") } 681invalid_star_etc: 682 | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } 683 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") } 684invalid_lambda_star_etc: 685 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } 686invalid_double_type_comments: 687 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT { 688 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") } 689invalid_with_item: 690 | expression 'as' a=expression { 691 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) } 692 693invalid_for_target: 694 | ASYNC? 'for' a=star_expressions { 695 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) } 696 697invalid_group: 698 | '(' a=starred_expression ')' { 699 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") } 700invalid_import_from_targets: 701 | import_from_as_names ',' { 702 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") } 703