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 p->soft_keywords = soft_keywords; 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_func_type_input) { 21 result = func_type_rule(p); 22 } 23 24 return result; 25} 26''' 27 28# ========================= START OF THE GRAMMAR ========================= 29 30# General grammatical elements and rules: 31# 32# * Strings with double quotes (") denote SOFT KEYWORDS 33# * Strings with single quotes (') denote KEYWORDS 34# * Upper case names (NAME) denote tokens in the Grammar/Tokens file 35# * Rule names starting with "invalid_" are used for specialized syntax errors 36# - These rules are NOT used in the first pass of the parser. 37# - Only if the first pass fails to parse, a second pass including the invalid 38# rules will be executed. 39# - If the parser fails in the second phase with a generic syntax error, the 40# location of the generic failure of the first pass will be used (this avoids 41# reporting incorrect locations due to the invalid rules). 42# - The order of the alternatives involving invalid rules matter 43# (like any rule in PEG). 44# 45# Grammar Syntax (see PEP 617 for more information): 46# 47# rule_name: expression 48# Optionally, a type can be included right after the rule name, which 49# specifies the return type of the C or Python function corresponding to the 50# rule: 51# rule_name[return_type]: expression 52# If the return type is omitted, then a void * is returned in C and an Any in 53# Python. 54# e1 e2 55# Match e1, then match e2. 56# e1 | e2 57# Match e1 or e2. 58# The first alternative can also appear on the line after the rule name for 59# formatting purposes. In that case, a | must be used before the first 60# alternative, like so: 61# rule_name[return_type]: 62# | first_alt 63# | second_alt 64# ( e ) 65# Match e (allows also to use other operators in the group like '(e)*') 66# [ e ] or e? 67# Optionally match e. 68# e* 69# Match zero or more occurrences of e. 70# e+ 71# Match one or more occurrences of e. 72# s.e+ 73# Match one or more occurrences of e, separated by s. The generated parse tree 74# does not include the separator. This is otherwise identical to (e (s e)*). 75# &e 76# Succeed if e can be parsed, without consuming any input. 77# !e 78# Fail if e can be parsed, without consuming any input. 79# ~ 80# Commit to the current alternative, even if it fails to parse. 81# &&e 82# Eager parse e. The parser will not backtrack and will immediately 83# fail with SyntaxError if e cannot be parsed. 84# 85 86# STARTING RULES 87# ============== 88 89file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) } 90interactive[mod_ty]: a=statement_newline { _PyAST_Interactive(a, p->arena) } 91eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _PyAST_Expression(a, p->arena) } 92func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _PyAST_FunctionType(a, b, p->arena) } 93 94# GENERAL STATEMENTS 95# ================== 96 97statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) } 98 99statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a } 100 101statement_newline[asdl_stmt_seq*]: 102 | a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } 103 | simple_stmts 104 | NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) } 105 | ENDMARKER { _PyPegen_interactive_exit(p) } 106 107simple_stmts[asdl_stmt_seq*]: 108 | a=simple_stmt !';' NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } # Not needed, there for speedup 109 | a[asdl_stmt_seq*]=';'.simple_stmt+ [';'] NEWLINE { a } 110 111# NOTE: assignment MUST precede expression, else parsing a simple assignment 112# will throw a SyntaxError. 113simple_stmt[stmt_ty] (memo): 114 | assignment 115 | &"type" type_alias 116 | e=star_expressions { _PyAST_Expr(e, EXTRA) } 117 | &'return' return_stmt 118 | &('import' | 'from') import_stmt 119 | &'raise' raise_stmt 120 | 'pass' { _PyAST_Pass(EXTRA) } 121 | &'del' del_stmt 122 | &'yield' yield_stmt 123 | &'assert' assert_stmt 124 | 'break' { _PyAST_Break(EXTRA) } 125 | 'continue' { _PyAST_Continue(EXTRA) } 126 | &'global' global_stmt 127 | &'nonlocal' nonlocal_stmt 128 129compound_stmt[stmt_ty]: 130 | &('def' | '@' | 'async') function_def 131 | &'if' if_stmt 132 | &('class' | '@') class_def 133 | &('with' | 'async') with_stmt 134 | &('for' | 'async') for_stmt 135 | &'try' try_stmt 136 | &'while' while_stmt 137 | match_stmt 138 139# SIMPLE STATEMENTS 140# ================= 141 142# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield' 143assignment[stmt_ty]: 144 | a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] { 145 CHECK_VERSION( 146 stmt_ty, 147 6, 148 "Variable annotation syntax is", 149 _PyAST_AnnAssign(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, c, 1, EXTRA) 150 ) } 151 | a=('(' b=single_target ')' { b } 152 | single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] { 153 CHECK_VERSION(stmt_ty, 6, "Variable annotations syntax is", _PyAST_AnnAssign(a, b, c, 0, EXTRA)) } 154 | a[asdl_expr_seq*]=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] { 155 _PyAST_Assign(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } 156 | a=single_target b=augassign ~ c=(yield_expr | star_expressions) { 157 _PyAST_AugAssign(a, b->kind, c, EXTRA) } 158 | invalid_assignment 159 160annotated_rhs[expr_ty]: yield_expr | star_expressions 161 162augassign[AugOperator*]: 163 | '+=' { _PyPegen_augoperator(p, Add) } 164 | '-=' { _PyPegen_augoperator(p, Sub) } 165 | '*=' { _PyPegen_augoperator(p, Mult) } 166 | '@=' { CHECK_VERSION(AugOperator*, 5, "The '@' operator is", _PyPegen_augoperator(p, MatMult)) } 167 | '/=' { _PyPegen_augoperator(p, Div) } 168 | '%=' { _PyPegen_augoperator(p, Mod) } 169 | '&=' { _PyPegen_augoperator(p, BitAnd) } 170 | '|=' { _PyPegen_augoperator(p, BitOr) } 171 | '^=' { _PyPegen_augoperator(p, BitXor) } 172 | '<<=' { _PyPegen_augoperator(p, LShift) } 173 | '>>=' { _PyPegen_augoperator(p, RShift) } 174 | '**=' { _PyPegen_augoperator(p, Pow) } 175 | '//=' { _PyPegen_augoperator(p, FloorDiv) } 176 177return_stmt[stmt_ty]: 178 | 'return' a=[star_expressions] { _PyAST_Return(a, EXTRA) } 179 180raise_stmt[stmt_ty]: 181 | 'raise' a=expression b=['from' z=expression { z }] { _PyAST_Raise(a, b, EXTRA) } 182 | 'raise' { _PyAST_Raise(NULL, NULL, EXTRA) } 183 184global_stmt[stmt_ty]: 'global' a[asdl_expr_seq*]=','.NAME+ { 185 _PyAST_Global(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) } 186 187nonlocal_stmt[stmt_ty]: 'nonlocal' a[asdl_expr_seq*]=','.NAME+ { 188 _PyAST_Nonlocal(CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, a)), EXTRA) } 189 190del_stmt[stmt_ty]: 191 | 'del' a=del_targets &(';' | NEWLINE) { _PyAST_Delete(a, EXTRA) } 192 | invalid_del_stmt 193 194yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) } 195 196assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) } 197 198import_stmt[stmt_ty]: 199 | invalid_import 200 | import_name 201 | import_from 202 203# Import statements 204# ----------------- 205 206import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) } 207# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS 208import_from[stmt_ty]: 209 | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets { 210 _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) } 211 | 'from' a=('.' | '...')+ 'import' b=import_from_targets { 212 _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) } 213import_from_targets[asdl_alias_seq*]: 214 | '(' a=import_from_as_names [','] ')' { a } 215 | import_from_as_names !',' 216 | '*' { (asdl_alias_seq*)_PyPegen_singleton_seq(p, CHECK(alias_ty, _PyPegen_alias_for_star(p, EXTRA))) } 217 | invalid_import_from_targets 218import_from_as_names[asdl_alias_seq*]: 219 | a[asdl_alias_seq*]=','.import_from_as_name+ { a } 220import_from_as_name[alias_ty]: 221 | a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id, 222 (b) ? ((expr_ty) b)->v.Name.id : NULL, 223 EXTRA) } 224dotted_as_names[asdl_alias_seq*]: 225 | a[asdl_alias_seq*]=','.dotted_as_name+ { a } 226dotted_as_name[alias_ty]: 227 | a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id, 228 (b) ? ((expr_ty) b)->v.Name.id : NULL, 229 EXTRA) } 230dotted_name[expr_ty]: 231 | a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) } 232 | NAME 233 234# COMPOUND STATEMENTS 235# =================== 236 237# Common elements 238# --------------- 239 240block[asdl_stmt_seq*] (memo): 241 | NEWLINE INDENT a=statements DEDENT { a } 242 | simple_stmts 243 | invalid_block 244 245decorators[asdl_expr_seq*]: a[asdl_expr_seq*]=('@' f=named_expression NEWLINE { f })+ { a } 246 247# Class definitions 248# ----------------- 249 250class_def[stmt_ty]: 251 | a=decorators b=class_def_raw { _PyPegen_class_def_decorators(p, a, b) } 252 | class_def_raw 253 254class_def_raw[stmt_ty]: 255 | invalid_class_def_raw 256 | 'class' a=NAME t=[type_params] b=['(' z=[arguments] ')' { z }] ':' c=block { 257 _PyAST_ClassDef(a->v.Name.id, 258 (b) ? ((expr_ty) b)->v.Call.args : NULL, 259 (b) ? ((expr_ty) b)->v.Call.keywords : NULL, 260 c, NULL, t, EXTRA) } 261 262# Function definitions 263# -------------------- 264 265function_def[stmt_ty]: 266 | d=decorators f=function_def_raw { _PyPegen_function_def_decorators(p, d, f) } 267 | function_def_raw 268 269function_def_raw[stmt_ty]: 270 | invalid_def_raw 271 | 'def' n=NAME t=[type_params] '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block { 272 _PyAST_FunctionDef(n->v.Name.id, 273 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), 274 b, NULL, a, NEW_TYPE_COMMENT(p, tc), t, EXTRA) } 275 | 'async' 'def' n=NAME t=[type_params] '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block { 276 CHECK_VERSION( 277 stmt_ty, 278 5, 279 "Async functions are", 280 _PyAST_AsyncFunctionDef(n->v.Name.id, 281 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), 282 b, NULL, a, NEW_TYPE_COMMENT(p, tc), t, EXTRA) 283 ) } 284 285# Function parameters 286# ------------------- 287 288params[arguments_ty]: 289 | invalid_parameters 290 | parameters 291 292parameters[arguments_ty]: 293 | a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] { 294 CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) } 295 | a=slash_with_default b=param_with_default* c=[star_etc] { 296 CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) } 297 | a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] { 298 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } 299 | a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} 300 | a=star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } 301 302# Some duplication here because we can't write (',' | &')'), 303# which is because we don't support empty alternatives (yet). 304 305slash_no_default[asdl_arg_seq*]: 306 | a[asdl_arg_seq*]=param_no_default+ '/' ',' { a } 307 | a[asdl_arg_seq*]=param_no_default+ '/' &')' { a } 308slash_with_default[SlashWithDefault*]: 309 | a=param_no_default* b=param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } 310 | a=param_no_default* b=param_with_default+ '/' &')' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } 311 312star_etc[StarEtc*]: 313 | invalid_star_etc 314 | '*' a=param_no_default b=param_maybe_default* c=[kwds] { 315 _PyPegen_star_etc(p, a, b, c) } 316 | '*' a=param_no_default_star_annotation b=param_maybe_default* c=[kwds] { 317 _PyPegen_star_etc(p, a, b, c) } 318 | '*' ',' b=param_maybe_default+ c=[kwds] { 319 _PyPegen_star_etc(p, NULL, b, c) } 320 | a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) } 321 322kwds[arg_ty]: 323 | invalid_kwds 324 | '**' a=param_no_default { a } 325 326# One parameter. This *includes* a following comma and type comment. 327# 328# There are three styles: 329# - No default 330# - With default 331# - Maybe with default 332# 333# There are two alternative forms of each, to deal with type comments: 334# - Ends in a comma followed by an optional type comment 335# - No comma, optional type comment, must be followed by close paren 336# The latter form is for a final parameter without trailing comma. 337# 338 339param_no_default[arg_ty]: 340 | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) } 341 | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) } 342param_no_default_star_annotation[arg_ty]: 343 | a=param_star_annotation ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) } 344 | a=param_star_annotation tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) } 345param_with_default[NameDefaultPair*]: 346 | a=param c=default ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) } 347 | a=param c=default tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) } 348param_maybe_default[NameDefaultPair*]: 349 | a=param c=default? ',' tc=TYPE_COMMENT? { _PyPegen_name_default_pair(p, a, c, tc) } 350 | a=param c=default? tc=TYPE_COMMENT? &')' { _PyPegen_name_default_pair(p, a, c, tc) } 351param[arg_ty]: a=NAME b=annotation? { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) } 352param_star_annotation[arg_ty]: a=NAME b=star_annotation { _PyAST_arg(a->v.Name.id, b, NULL, EXTRA) } 353annotation[expr_ty]: ':' a=expression { a } 354star_annotation[expr_ty]: ':' a=star_expression { a } 355default[expr_ty]: '=' a=expression { a } | invalid_default 356 357# If statement 358# ------------ 359 360if_stmt[stmt_ty]: 361 | invalid_if_stmt 362 | 'if' a=named_expression ':' b=block c=elif_stmt { 363 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) } 364 | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) } 365elif_stmt[stmt_ty]: 366 | invalid_elif_stmt 367 | 'elif' a=named_expression ':' b=block c=elif_stmt { 368 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) } 369 | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) } 370else_block[asdl_stmt_seq*]: 371 | invalid_else_stmt 372 | 'else' &&':' b=block { b } 373 374# While statement 375# --------------- 376 377while_stmt[stmt_ty]: 378 | invalid_while_stmt 379 | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) } 380 381# For statement 382# ------------- 383 384for_stmt[stmt_ty]: 385 | invalid_for_stmt 386 | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { 387 _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) } 388 | 'async' 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { 389 CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) } 390 | invalid_for_target 391 392# With statement 393# -------------- 394 395with_stmt[stmt_ty]: 396 | invalid_with_stmt_indent 397 | 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' tc=[TYPE_COMMENT] b=block { 398 _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } 399 | 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { 400 _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) } 401 | 'async' 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block { 402 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) } 403 | 'async' 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { 404 CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) } 405 | invalid_with_stmt 406 407with_item[withitem_ty]: 408 | e=expression 'as' t=star_target &(',' | ')' | ':') { _PyAST_withitem(e, t, p->arena) } 409 | invalid_with_item 410 | e=expression { _PyAST_withitem(e, NULL, p->arena) } 411 412# Try statement 413# ------------- 414 415try_stmt[stmt_ty]: 416 | invalid_try_stmt 417 | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) } 418 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) } 419 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] { 420 CHECK_VERSION(stmt_ty, 11, "Exception groups are", 421 _PyAST_TryStar(b, ex, el, f, EXTRA)) } 422 423 424# Except statement 425# ---------------- 426 427except_block[excepthandler_ty]: 428 | invalid_except_stmt_indent 429 | 'except' e=expression t=['as' z=NAME { z }] ':' b=block { 430 _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) } 431 | 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) } 432 | invalid_except_stmt 433except_star_block[excepthandler_ty]: 434 | invalid_except_star_stmt_indent 435 | 'except' '*' e=expression t=['as' z=NAME { z }] ':' b=block { 436 _PyAST_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) } 437 | invalid_except_stmt 438finally_block[asdl_stmt_seq*]: 439 | invalid_finally_stmt 440 | 'finally' &&':' a=block { a } 441 442# Match statement 443# --------------- 444 445match_stmt[stmt_ty]: 446 | "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT { 447 CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _PyAST_Match(subject, cases, EXTRA)) } 448 | invalid_match_stmt 449 450subject_expr[expr_ty]: 451 | value=star_named_expression ',' values=star_named_expressions? { 452 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) } 453 | named_expression 454 455case_block[match_case_ty]: 456 | invalid_case_block 457 | "case" pattern=patterns guard=guard? ':' body=block { 458 _PyAST_match_case(pattern, guard, body, p->arena) } 459 460guard[expr_ty]: 'if' guard=named_expression { guard } 461 462patterns[pattern_ty]: 463 | patterns[asdl_pattern_seq*]=open_sequence_pattern { 464 _PyAST_MatchSequence(patterns, EXTRA) } 465 | pattern 466 467pattern[pattern_ty]: 468 | as_pattern 469 | or_pattern 470 471as_pattern[pattern_ty]: 472 | pattern=or_pattern 'as' target=pattern_capture_target { 473 _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) } 474 | invalid_as_pattern 475 476or_pattern[pattern_ty]: 477 | patterns[asdl_pattern_seq*]='|'.closed_pattern+ { 478 asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) } 479 480closed_pattern[pattern_ty] (memo): 481 | literal_pattern 482 | capture_pattern 483 | wildcard_pattern 484 | value_pattern 485 | group_pattern 486 | sequence_pattern 487 | mapping_pattern 488 | class_pattern 489 490# Literal patterns are used for equality and identity constraints 491literal_pattern[pattern_ty]: 492 | value=signed_number !('+' | '-') { _PyAST_MatchValue(value, EXTRA) } 493 | value=complex_number { _PyAST_MatchValue(value, EXTRA) } 494 | value=strings { _PyAST_MatchValue(value, EXTRA) } 495 | 'None' { _PyAST_MatchSingleton(Py_None, EXTRA) } 496 | 'True' { _PyAST_MatchSingleton(Py_True, EXTRA) } 497 | 'False' { _PyAST_MatchSingleton(Py_False, EXTRA) } 498 499# Literal expressions are used to restrict permitted mapping pattern keys 500literal_expr[expr_ty]: 501 | signed_number !('+' | '-') 502 | complex_number 503 | strings 504 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) } 505 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) } 506 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) } 507 508complex_number[expr_ty]: 509 | real=signed_real_number '+' imag=imaginary_number { 510 _PyAST_BinOp(real, Add, imag, EXTRA) } 511 | real=signed_real_number '-' imag=imaginary_number { 512 _PyAST_BinOp(real, Sub, imag, EXTRA) } 513 514signed_number[expr_ty]: 515 | NUMBER 516 | '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) } 517 518signed_real_number[expr_ty]: 519 | real_number 520 | '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) } 521 522real_number[expr_ty]: 523 | real=NUMBER { _PyPegen_ensure_real(p, real) } 524 525imaginary_number[expr_ty]: 526 | imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) } 527 528capture_pattern[pattern_ty]: 529 | target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) } 530 531pattern_capture_target[expr_ty]: 532 | !"_" name=NAME !('.' | '(' | '=') { 533 _PyPegen_set_expr_context(p, name, Store) } 534 535wildcard_pattern[pattern_ty]: 536 | "_" { _PyAST_MatchAs(NULL, NULL, EXTRA) } 537 538value_pattern[pattern_ty]: 539 | attr=attr !('.' | '(' | '=') { _PyAST_MatchValue(attr, EXTRA) } 540 541attr[expr_ty]: 542 | value=name_or_attr '.' attr=NAME { 543 _PyAST_Attribute(value, attr->v.Name.id, Load, EXTRA) } 544 545name_or_attr[expr_ty]: 546 | attr 547 | NAME 548 549group_pattern[pattern_ty]: 550 | '(' pattern=pattern ')' { pattern } 551 552sequence_pattern[pattern_ty]: 553 | '[' patterns=maybe_sequence_pattern? ']' { _PyAST_MatchSequence(patterns, EXTRA) } 554 | '(' patterns=open_sequence_pattern? ')' { _PyAST_MatchSequence(patterns, EXTRA) } 555 556open_sequence_pattern[asdl_seq*]: 557 | pattern=maybe_star_pattern ',' patterns=maybe_sequence_pattern? { 558 _PyPegen_seq_insert_in_front(p, pattern, patterns) } 559 560maybe_sequence_pattern[asdl_seq*]: 561 | patterns=','.maybe_star_pattern+ ','? { patterns } 562 563maybe_star_pattern[pattern_ty]: 564 | star_pattern 565 | pattern 566 567star_pattern[pattern_ty] (memo): 568 | '*' target=pattern_capture_target { 569 _PyAST_MatchStar(target->v.Name.id, EXTRA) } 570 | '*' wildcard_pattern { 571 _PyAST_MatchStar(NULL, EXTRA) } 572 573mapping_pattern[pattern_ty]: 574 | '{' '}' { 575 _PyAST_MatchMapping(NULL, NULL, NULL, EXTRA) } 576 | '{' rest=double_star_pattern ','? '}' { 577 _PyAST_MatchMapping(NULL, NULL, rest->v.Name.id, EXTRA) } 578 | '{' items=items_pattern ',' rest=double_star_pattern ','? '}' { 579 _PyAST_MatchMapping( 580 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)), 581 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)), 582 rest->v.Name.id, 583 EXTRA) } 584 | '{' items=items_pattern ','? '}' { 585 _PyAST_MatchMapping( 586 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, items)), 587 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)), 588 NULL, 589 EXTRA) } 590 591items_pattern[asdl_seq*]: 592 | ','.key_value_pattern+ 593 594key_value_pattern[KeyPatternPair*]: 595 | key=(literal_expr | attr) ':' pattern=pattern { 596 _PyPegen_key_pattern_pair(p, key, pattern) } 597 598double_star_pattern[expr_ty]: 599 | '**' target=pattern_capture_target { target } 600 601class_pattern[pattern_ty]: 602 | cls=name_or_attr '(' ')' { 603 _PyAST_MatchClass(cls, NULL, NULL, NULL, EXTRA) } 604 | cls=name_or_attr '(' patterns=positional_patterns ','? ')' { 605 _PyAST_MatchClass(cls, patterns, NULL, NULL, EXTRA) } 606 | cls=name_or_attr '(' keywords=keyword_patterns ','? ')' { 607 _PyAST_MatchClass( 608 cls, NULL, 609 CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, 610 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))), 611 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)), 612 EXTRA) } 613 | cls=name_or_attr '(' patterns=positional_patterns ',' keywords=keyword_patterns ','? ')' { 614 _PyAST_MatchClass( 615 cls, 616 patterns, 617 CHECK(asdl_identifier_seq*, _PyPegen_map_names_to_ids(p, 618 CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))), 619 CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)), 620 EXTRA) } 621 | invalid_class_pattern 622 623positional_patterns[asdl_pattern_seq*]: 624 | args[asdl_pattern_seq*]=','.pattern+ { args } 625 626keyword_patterns[asdl_seq*]: 627 | ','.keyword_pattern+ 628 629keyword_pattern[KeyPatternPair*]: 630 | arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) } 631 632# Type statement 633# --------------- 634 635type_alias[stmt_ty]: 636 | "type" n=NAME t=[type_params] '=' b=expression { 637 CHECK_VERSION(stmt_ty, 12, "Type statement is", 638 _PyAST_TypeAlias(CHECK(expr_ty, _PyPegen_set_expr_context(p, n, Store)), t, b, EXTRA)) } 639 640# Type parameter declaration 641# -------------------------- 642 643type_params[asdl_type_param_seq*]: 644 | invalid_type_params 645 | '[' t=type_param_seq ']' { 646 CHECK_VERSION(asdl_type_param_seq *, 12, "Type parameter lists are", t) } 647 648type_param_seq[asdl_type_param_seq*]: a[asdl_type_param_seq*]=','.type_param+ [','] { a } 649 650type_param[type_param_ty] (memo): 651 | a=NAME b=[type_param_bound] c=[type_param_default] { _PyAST_TypeVar(a->v.Name.id, b, c, EXTRA) } 652 | '*' a=NAME colon=':' e=expression { 653 RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind 654 ? "cannot use constraints with TypeVarTuple" 655 : "cannot use bound with TypeVarTuple") 656 } 657 | '*' a=NAME b=[type_param_starred_default] { _PyAST_TypeVarTuple(a->v.Name.id, b, EXTRA) } 658 | '**' a=NAME colon=':' e=expression { 659 RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind 660 ? "cannot use constraints with ParamSpec" 661 : "cannot use bound with ParamSpec") 662 } 663 | '**' a=NAME b=[type_param_default] { _PyAST_ParamSpec(a->v.Name.id, b, EXTRA) } 664 665type_param_bound[expr_ty]: ':' e=expression { e } 666type_param_default[expr_ty]: '=' e=expression { 667 CHECK_VERSION(expr_ty, 13, "Type parameter defaults are", e) } 668type_param_starred_default[expr_ty]: '=' e=star_expression { 669 CHECK_VERSION(expr_ty, 13, "Type parameter defaults are", e) } 670 671# EXPRESSIONS 672# ----------- 673 674expressions[expr_ty]: 675 | a=expression b=(',' c=expression { c })+ [','] { 676 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } 677 | a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) } 678 | expression 679 680expression[expr_ty] (memo): 681 | invalid_expression 682 | invalid_legacy_expression 683 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) } 684 | disjunction 685 | lambdef 686 687yield_expr[expr_ty]: 688 | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) } 689 | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) } 690 691star_expressions[expr_ty]: 692 | a=star_expression b=(',' c=star_expression { c })+ [','] { 693 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Load, EXTRA) } 694 | a=star_expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) } 695 | star_expression 696 697star_expression[expr_ty] (memo): 698 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) } 699 | expression 700 701star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a } 702 703star_named_expression[expr_ty]: 704 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) } 705 | named_expression 706 707assignment_expression[expr_ty]: 708 | a=NAME ':=' ~ b=expression { 709 CHECK_VERSION(expr_ty, 8, "Assignment expressions are", 710 _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA)) } 711 712named_expression[expr_ty]: 713 | assignment_expression 714 | invalid_named_expression 715 | expression !':=' 716 717disjunction[expr_ty] (memo): 718 | a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp( 719 Or, 720 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), 721 EXTRA) } 722 | conjunction 723 724conjunction[expr_ty] (memo): 725 | a=inversion b=('and' c=inversion { c })+ { _PyAST_BoolOp( 726 And, 727 CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), 728 EXTRA) } 729 | inversion 730 731inversion[expr_ty] (memo): 732 | 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) } 733 | comparison 734 735# Comparison operators 736# -------------------- 737 738comparison[expr_ty]: 739 | a=bitwise_or b=compare_op_bitwise_or_pair+ { 740 _PyAST_Compare( 741 a, 742 CHECK(asdl_int_seq*, _PyPegen_get_cmpops(p, b)), 743 CHECK(asdl_expr_seq*, _PyPegen_get_exprs(p, b)), 744 EXTRA) } 745 | bitwise_or 746 747compare_op_bitwise_or_pair[CmpopExprPair*]: 748 | eq_bitwise_or 749 | noteq_bitwise_or 750 | lte_bitwise_or 751 | lt_bitwise_or 752 | gte_bitwise_or 753 | gt_bitwise_or 754 | notin_bitwise_or 755 | in_bitwise_or 756 | isnot_bitwise_or 757 | is_bitwise_or 758 759eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) } 760noteq_bitwise_or[CmpopExprPair*]: 761 | (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) } 762lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) } 763lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) } 764gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) } 765gt_bitwise_or[CmpopExprPair*]: '>' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Gt, a) } 766notin_bitwise_or[CmpopExprPair*]: 'not' 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, NotIn, a) } 767in_bitwise_or[CmpopExprPair*]: 'in' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, In, a) } 768isnot_bitwise_or[CmpopExprPair*]: 'is' 'not' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, IsNot, a) } 769is_bitwise_or[CmpopExprPair*]: 'is' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Is, a) } 770 771# Bitwise operators 772# ----------------- 773 774bitwise_or[expr_ty]: 775 | a=bitwise_or '|' b=bitwise_xor { _PyAST_BinOp(a, BitOr, b, EXTRA) } 776 | bitwise_xor 777 778bitwise_xor[expr_ty]: 779 | a=bitwise_xor '^' b=bitwise_and { _PyAST_BinOp(a, BitXor, b, EXTRA) } 780 | bitwise_and 781 782bitwise_and[expr_ty]: 783 | a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) } 784 | shift_expr 785 786shift_expr[expr_ty]: 787 | a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) } 788 | a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) } 789 | invalid_arithmetic 790 | sum 791 792# Arithmetic operators 793# -------------------- 794 795sum[expr_ty]: 796 | a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) } 797 | a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) } 798 | term 799 800term[expr_ty]: 801 | a=term '*' b=factor { _PyAST_BinOp(a, Mult, b, EXTRA) } 802 | a=term '/' b=factor { _PyAST_BinOp(a, Div, b, EXTRA) } 803 | a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) } 804 | a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) } 805 | a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) } 806 | invalid_factor 807 | factor 808 809factor[expr_ty] (memo): 810 | '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) } 811 | '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) } 812 | '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) } 813 | power 814 815power[expr_ty]: 816 | a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) } 817 | await_primary 818 819# Primary elements 820# ---------------- 821 822# Primary elements are things like "obj.something.something", "obj[something]", "obj(something)", "obj" ... 823 824await_primary[expr_ty] (memo): 825 | 'await' a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) } 826 | primary 827 828primary[expr_ty]: 829 | a=primary '.' b=NAME { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) } 830 | a=primary b=genexp { _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } 831 | a=primary '(' b=[arguments] ')' { 832 _PyAST_Call(a, 833 (b) ? ((expr_ty) b)->v.Call.args : NULL, 834 (b) ? ((expr_ty) b)->v.Call.keywords : NULL, 835 EXTRA) } 836 | a=primary '[' b=slices ']' { _PyAST_Subscript(a, b, Load, EXTRA) } 837 | atom 838 839slices[expr_ty]: 840 | a=slice !',' { a } 841 | a[asdl_expr_seq*]=','.(slice | starred_expression)+ [','] { _PyAST_Tuple(a, Load, EXTRA) } 842 843slice[expr_ty]: 844 | a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _PyAST_Slice(a, b, c, EXTRA) } 845 | a=named_expression { a } 846 847atom[expr_ty]: 848 | NAME 849 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) } 850 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) } 851 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) } 852 | &(STRING|FSTRING_START) strings 853 | NUMBER 854 | &'(' (tuple | group | genexp) 855 | &'[' (list | listcomp) 856 | &'{' (dict | set | dictcomp | setcomp) 857 | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) } 858 859group[expr_ty]: 860 | '(' a=(yield_expr | named_expression) ')' { a } 861 | invalid_group 862 863# Lambda functions 864# ---------------- 865 866lambdef[expr_ty]: 867 | 'lambda' a=[lambda_params] ':' b=expression { 868 _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) } 869 870lambda_params[arguments_ty]: 871 | invalid_lambda_parameters 872 | lambda_parameters 873 874# lambda_parameters etc. duplicates parameters but without annotations 875# or type comments, and if there's no comma after a parameter, we expect 876# a colon, not a close parenthesis. (For more, see parameters above.) 877# 878lambda_parameters[arguments_ty]: 879 | a=lambda_slash_no_default b[asdl_arg_seq*]=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] { 880 CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) } 881 | a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] { 882 CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) } 883 | a[asdl_arg_seq*]=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] { 884 _PyPegen_make_arguments(p, NULL, NULL, a, b, c) } 885 | a=lambda_param_with_default+ b=[lambda_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)} 886 | a=lambda_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) } 887 888lambda_slash_no_default[asdl_arg_seq*]: 889 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' ',' { a } 890 | a[asdl_arg_seq*]=lambda_param_no_default+ '/' &':' { a } 891 892lambda_slash_with_default[SlashWithDefault*]: 893 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } 894 | a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) } 895 896lambda_star_etc[StarEtc*]: 897 | invalid_lambda_star_etc 898 | '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] { 899 _PyPegen_star_etc(p, a, b, c) } 900 | '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] { 901 _PyPegen_star_etc(p, NULL, b, c) } 902 | a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) } 903 904lambda_kwds[arg_ty]: 905 | invalid_lambda_kwds 906 | '**' a=lambda_param_no_default { a } 907 908lambda_param_no_default[arg_ty]: 909 | a=lambda_param ',' { a } 910 | a=lambda_param &':' { a } 911lambda_param_with_default[NameDefaultPair*]: 912 | a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) } 913 | a=lambda_param c=default &':' { _PyPegen_name_default_pair(p, a, c, NULL) } 914lambda_param_maybe_default[NameDefaultPair*]: 915 | a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) } 916 | a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) } 917lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) } 918 919# LITERALS 920# ======== 921 922fstring_middle[expr_ty]: 923 | fstring_replacement_field 924 | t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) } 925fstring_replacement_field[expr_ty]: 926 | '{' a=annotated_rhs debug_expr='='? conversion=[fstring_conversion] format=[fstring_full_format_spec] rbrace='}' { 927 _PyPegen_formatted_value(p, a, debug_expr, conversion, format, rbrace, EXTRA) } 928 | invalid_replacement_field 929fstring_conversion[ResultTokenWithMetadata*]: 930 | conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) } 931fstring_full_format_spec[ResultTokenWithMetadata*]: 932 | colon=':' spec=fstring_format_spec* { _PyPegen_setup_full_format_spec(p, colon, (asdl_expr_seq *) spec, EXTRA) } 933fstring_format_spec[expr_ty]: 934 | t=FSTRING_MIDDLE { _PyPegen_decoded_constant_from_token(p, t) } 935 | fstring_replacement_field 936fstring[expr_ty]: 937 | a=FSTRING_START b=fstring_middle* c=FSTRING_END { _PyPegen_joined_str(p, a, (asdl_expr_seq*)b, c) } 938 939string[expr_ty]: s[Token*]=STRING { _PyPegen_constant_from_string(p, s) } 940strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string)+ { _PyPegen_concatenate_strings(p, a, EXTRA) } 941 942list[expr_ty]: 943 | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) } 944 945tuple[expr_ty]: 946 | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { 947 _PyAST_Tuple(a, Load, EXTRA) } 948 949set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) } 950 951# Dicts 952# ----- 953 954dict[expr_ty]: 955 | '{' a=[double_starred_kvpairs] '}' { 956 _PyAST_Dict( 957 CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)), 958 CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)), 959 EXTRA) } 960 | '{' invalid_double_starred_kvpairs '}' 961 962double_starred_kvpairs[asdl_seq*]: a=','.double_starred_kvpair+ [','] { a } 963 964double_starred_kvpair[KeyValuePair*]: 965 | '**' a=bitwise_or { _PyPegen_key_value_pair(p, NULL, a) } 966 | kvpair 967 968kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p, a, b) } 969 970# Comprehensions & Generators 971# --------------------------- 972 973for_if_clauses[asdl_comprehension_seq*]: 974 | a[asdl_comprehension_seq*]=for_if_clause+ { a } 975 976for_if_clause[comprehension_ty]: 977 | 'async' 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* { 978 CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) } 979 | 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* { 980 _PyAST_comprehension(a, b, c, 0, p->arena) } 981 | 'async'? 'for' (bitwise_or (',' bitwise_or)* [',']) !'in' { 982 RAISE_SYNTAX_ERROR("'in' expected after for-loop variables") } 983 | invalid_for_target 984 985listcomp[expr_ty]: 986 | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) } 987 | invalid_comprehension 988 989setcomp[expr_ty]: 990 | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) } 991 | invalid_comprehension 992 993genexp[expr_ty]: 994 | '(' a=( assignment_expression | expression !':=') b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) } 995 | invalid_comprehension 996 997dictcomp[expr_ty]: 998 | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) } 999 | invalid_dict_comprehension 1000 1001# FUNCTION CALL ARGUMENTS 1002# ======================= 1003 1004arguments[expr_ty] (memo): 1005 | a=args [','] &')' { a } 1006 | invalid_arguments 1007 1008args[expr_ty]: 1009 | a[asdl_expr_seq*]=','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ b=[',' k=kwargs {k}] { 1010 _PyPegen_collect_call_seqs(p, a, b, EXTRA) } 1011 | a=kwargs { _PyAST_Call(_PyPegen_dummy_name(p), 1012 CHECK_NULL_ALLOWED(asdl_expr_seq*, _PyPegen_seq_extract_starred_exprs(p, a)), 1013 CHECK_NULL_ALLOWED(asdl_keyword_seq*, _PyPegen_seq_delete_starred_exprs(p, a)), 1014 EXTRA) } 1015 1016kwargs[asdl_seq*]: 1017 | a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { _PyPegen_join_sequences(p, a, b) } 1018 | ','.kwarg_or_starred+ 1019 | ','.kwarg_or_double_starred+ 1020 1021starred_expression[expr_ty]: 1022 | invalid_starred_expression 1023 | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) } 1024 | '*' { RAISE_SYNTAX_ERROR("Invalid star expression") } 1025 1026kwarg_or_starred[KeywordOrStarred*]: 1027 | invalid_kwarg 1028 | a=NAME '=' b=expression { 1029 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) } 1030 | a=starred_expression { _PyPegen_keyword_or_starred(p, a, 0) } 1031 1032kwarg_or_double_starred[KeywordOrStarred*]: 1033 | invalid_kwarg 1034 | a=NAME '=' b=expression { 1035 _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(a->v.Name.id, b, EXTRA)), 1) } 1036 | '**' a=expression { _PyPegen_keyword_or_starred(p, CHECK(keyword_ty, _PyAST_keyword(NULL, a, EXTRA)), 1) } 1037 1038# ASSIGNMENT TARGETS 1039# ================== 1040 1041# Generic targets 1042# --------------- 1043 1044# NOTE: star_targets may contain *bitwise_or, targets may not. 1045star_targets[expr_ty]: 1046 | a=star_target !',' { a } 1047 | a=star_target b=(',' c=star_target { c })* [','] { 1048 _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)), Store, EXTRA) } 1049 1050star_targets_list_seq[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_target+ [','] { a } 1051 1052star_targets_tuple_seq[asdl_expr_seq*]: 1053 | a=star_target b=(',' c=star_target { c })+ [','] { (asdl_expr_seq*) _PyPegen_seq_insert_in_front(p, a, b) } 1054 | a=star_target ',' { (asdl_expr_seq*) _PyPegen_singleton_seq(p, a) } 1055 1056star_target[expr_ty] (memo): 1057 | '*' a=(!'*' star_target) { 1058 _PyAST_Starred(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), Store, EXTRA) } 1059 | target_with_star_atom 1060 1061target_with_star_atom[expr_ty] (memo): 1062 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) } 1063 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) } 1064 | star_atom 1065 1066star_atom[expr_ty]: 1067 | a=NAME { _PyPegen_set_expr_context(p, a, Store) } 1068 | '(' a=target_with_star_atom ')' { _PyPegen_set_expr_context(p, a, Store) } 1069 | '(' a=[star_targets_tuple_seq] ')' { _PyAST_Tuple(a, Store, EXTRA) } 1070 | '[' a=[star_targets_list_seq] ']' { _PyAST_List(a, Store, EXTRA) } 1071 1072single_target[expr_ty]: 1073 | single_subscript_attribute_target 1074 | a=NAME { _PyPegen_set_expr_context(p, a, Store) } 1075 | '(' a=single_target ')' { a } 1076 1077single_subscript_attribute_target[expr_ty]: 1078 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Store, EXTRA) } 1079 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Store, EXTRA) } 1080 1081t_primary[expr_ty]: 1082 | a=t_primary '.' b=NAME &t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Load, EXTRA) } 1083 | a=t_primary '[' b=slices ']' &t_lookahead { _PyAST_Subscript(a, b, Load, EXTRA) } 1084 | a=t_primary b=genexp &t_lookahead { 1085 _PyAST_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) } 1086 | a=t_primary '(' b=[arguments] ')' &t_lookahead { 1087 _PyAST_Call(a, 1088 (b) ? ((expr_ty) b)->v.Call.args : NULL, 1089 (b) ? ((expr_ty) b)->v.Call.keywords : NULL, 1090 EXTRA) } 1091 | a=atom &t_lookahead { a } 1092 1093t_lookahead: '(' | '[' | '.' 1094 1095# Targets for del statements 1096# -------------------------- 1097 1098del_targets[asdl_expr_seq*]: a[asdl_expr_seq*]=','.del_target+ [','] { a } 1099 1100del_target[expr_ty] (memo): 1101 | a=t_primary '.' b=NAME !t_lookahead { _PyAST_Attribute(a, b->v.Name.id, Del, EXTRA) } 1102 | a=t_primary '[' b=slices ']' !t_lookahead { _PyAST_Subscript(a, b, Del, EXTRA) } 1103 | del_t_atom 1104 1105del_t_atom[expr_ty]: 1106 | a=NAME { _PyPegen_set_expr_context(p, a, Del) } 1107 | '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) } 1108 | '(' a=[del_targets] ')' { _PyAST_Tuple(a, Del, EXTRA) } 1109 | '[' a=[del_targets] ']' { _PyAST_List(a, Del, EXTRA) } 1110 1111# TYPING ELEMENTS 1112# --------------- 1113 1114# type_expressions allow */** but ignore them 1115type_expressions[asdl_expr_seq*]: 1116 | a=','.expression+ ',' '*' b=expression ',' '**' c=expression { 1117 (asdl_expr_seq*)_PyPegen_seq_append_to_end( 1118 p, 1119 CHECK(asdl_seq*, _PyPegen_seq_append_to_end(p, a, b)), 1120 c) } 1121 | a=','.expression+ ',' '*' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) } 1122 | a=','.expression+ ',' '**' b=expression { (asdl_expr_seq*)_PyPegen_seq_append_to_end(p, a, b) } 1123 | '*' a=expression ',' '**' b=expression { 1124 (asdl_expr_seq*)_PyPegen_seq_append_to_end( 1125 p, 1126 CHECK(asdl_seq*, _PyPegen_singleton_seq(p, a)), 1127 b) } 1128 | '*' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) } 1129 | '**' a=expression { (asdl_expr_seq*)_PyPegen_singleton_seq(p, a) } 1130 | a[asdl_expr_seq*]=','.expression+ {a} 1131 1132func_type_comment[Token*]: 1133 | NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block 1134 | invalid_double_type_comments 1135 | TYPE_COMMENT 1136 1137# ========================= END OF THE GRAMMAR =========================== 1138 1139 1140 1141# ========================= START OF INVALID RULES ======================= 1142 1143# From here on, there are rules for invalid syntax with specialised error messages 1144invalid_arguments: 1145 | ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) a=',' ','.(starred_expression !'=')+ { 1146 RAISE_SYNTAX_ERROR_STARTING_FROM(a, "iterable argument unpacking follows keyword argument unpacking") } 1147 | a=expression b=for_if_clauses ',' [args | expression for_if_clauses] { 1148 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") } 1149 | a=NAME b='=' expression for_if_clauses { 1150 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")} 1151 | (args ',')? a=NAME b='=' &(',' | ')') { 1152 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected argument value expression")} 1153 | a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) } 1154 | args ',' a=expression b=for_if_clauses { 1155 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") } 1156 | a=args ',' args { _PyPegen_arguments_parsing_error(p, a) } 1157invalid_kwarg: 1158 | a[Token*]=('True'|'False'|'None') b='=' { 1159 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to %s", PyBytes_AS_STRING(a->bytes)) } 1160 | a=NAME b='=' expression for_if_clauses { 1161 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")} 1162 | !(NAME '=') a=expression b='=' { 1163 RAISE_SYNTAX_ERROR_KNOWN_RANGE( 1164 a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") } 1165 | a='**' expression '=' b=expression { 1166 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to keyword argument unpacking") } 1167 1168# IMPORTANT: Note that the "_without_invalid" suffix causes the rule to not call invalid rules under it 1169expression_without_invalid[expr_ty]: 1170 | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) } 1171 | disjunction 1172 | lambdef 1173invalid_legacy_expression: 1174 | a=NAME !'(' b=star_expressions { 1175 _PyPegen_check_legacy_stmt(p, a) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, 1176 "Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL} 1177 1178invalid_expression: 1179 # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf" 1180 # Soft keywords need to also be ignored because they can be parsed as NAME NAME 1181 | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid { 1182 _PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL : 1183 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") } 1184 | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") } 1185 | a='lambda' [lambda_params] b=':' &FSTRING_MIDDLE { 1186 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "f-string: lambda expressions are not allowed without parentheses") } 1187 1188invalid_named_expression(memo): 1189 | a=expression ':=' expression { 1190 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 1191 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } 1192 | a=NAME '=' b=bitwise_or !('='|':=') { 1193 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") } 1194 | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') { 1195 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?", 1196 _PyPegen_get_expr_name(a)) } 1197 1198invalid_assignment: 1199 | a=invalid_ann_assign_target ':' expression { 1200 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 1201 a, 1202 "only single target (not %s) can be annotated", 1203 _PyPegen_get_expr_name(a) 1204 )} 1205 | a=star_named_expression ',' star_named_expressions* ':' expression { 1206 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") } 1207 | a=expression ':' expression { 1208 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") } 1209 | (star_targets '=')* a=star_expressions '=' { 1210 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) } 1211 | (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") } 1212 | a=star_expressions augassign annotated_rhs { 1213 RAISE_SYNTAX_ERROR_KNOWN_LOCATION( 1214 a, 1215 "'%s' is an illegal expression for augmented assignment", 1216 _PyPegen_get_expr_name(a) 1217 )} 1218invalid_ann_assign_target[expr_ty]: 1219 | list 1220 | tuple 1221 | '(' a=invalid_ann_assign_target ')' { a } 1222invalid_del_stmt: 1223 | 'del' a=star_expressions { 1224 RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) } 1225invalid_block: 1226 | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") } 1227invalid_comprehension: 1228 | ('[' | '(' | '{') a=starred_expression for_if_clauses { 1229 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") } 1230 | ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses { 1231 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, expr_ty), 1232 "did you forget parentheses around the comprehension target?") } 1233 | ('[' | '{') a=star_named_expression b=',' for_if_clauses { 1234 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget parentheses around the comprehension target?") } 1235invalid_dict_comprehension: 1236 | '{' a='**' bitwise_or for_if_clauses '}' { 1237 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") } 1238invalid_parameters: 1239 | a="/" ',' { 1240 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "at least one argument must precede /") } 1241 | (slash_no_default | slash_with_default) param_maybe_default* a='/' { 1242 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ may appear only once") } 1243 | slash_no_default? param_no_default* invalid_parameters_helper a=param_no_default { 1244 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameter without a default follows parameter with a default") } 1245 | param_no_default* a='(' param_no_default+ ','? b=')' { 1246 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Function parameters cannot be parenthesized") } 1247 | (slash_no_default | slash_with_default)? param_maybe_default* '*' (',' | param_no_default) param_maybe_default* a='/' { 1248 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ must be ahead of *") } 1249 | param_maybe_default+ '/' a='*' { 1250 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected comma between / and *") } 1251invalid_default: 1252 | a='=' &(')'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected default value expression") } 1253invalid_star_etc: 1254 | a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named arguments must follow bare *") } 1255 | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") } 1256 | '*' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional argument cannot have default value") } 1257 | '*' (param_no_default | ',') param_maybe_default* a='*' (param_no_default | ',') { 1258 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* argument may appear only once") } 1259invalid_kwds: 1260 | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword argument cannot have default value") } 1261 | '**' param ',' a=param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") } 1262 | '**' param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") } 1263invalid_parameters_helper: # This is only there to avoid type errors 1264 | a=slash_with_default { _PyPegen_singleton_seq(p, a) } 1265 | param_with_default+ 1266invalid_lambda_parameters: 1267 | a="/" ',' { 1268 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "at least one argument must precede /") } 1269 | (lambda_slash_no_default | lambda_slash_with_default) lambda_param_maybe_default* a='/' { 1270 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ may appear only once") } 1271 | lambda_slash_no_default? lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default { 1272 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameter without a default follows parameter with a default") } 1273 | lambda_param_no_default* a='(' ','.lambda_param+ ','? b=')' { 1274 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Lambda expression parameters cannot be parenthesized") } 1275 | (lambda_slash_no_default | lambda_slash_with_default)? lambda_param_maybe_default* '*' (',' | lambda_param_no_default) lambda_param_maybe_default* a='/' { 1276 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ must be ahead of *") } 1277 | lambda_param_maybe_default+ '/' a='*' { 1278 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected comma between / and *") } 1279invalid_lambda_parameters_helper: 1280 | a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) } 1281 | lambda_param_with_default+ 1282invalid_lambda_star_etc: 1283 | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") } 1284 | '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional argument cannot have default value") } 1285 | '*' (lambda_param_no_default | ',') lambda_param_maybe_default* a='*' (lambda_param_no_default | ',') { 1286 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* argument may appear only once") } 1287invalid_lambda_kwds: 1288 | '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword argument cannot have default value") } 1289 | '**' lambda_param ',' a=lambda_param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") } 1290 | '**' lambda_param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") } 1291invalid_double_type_comments: 1292 | TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT { 1293 RAISE_SYNTAX_ERROR("Cannot have two type comments on def") } 1294invalid_with_item: 1295 | expression 'as' a=expression &(',' | ')' | ':') { 1296 RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) } 1297 1298invalid_for_target: 1299 | 'async'? 'for' a=star_expressions { 1300 RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) } 1301 1302invalid_group: 1303 | '(' a=starred_expression ')' { 1304 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") } 1305 | '(' a='**' expression ')' { 1306 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") } 1307invalid_import: 1308 | a='import' ','.dotted_name+ 'from' dotted_name { 1309 RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") } 1310 | 'import' token=NEWLINE { 1311 RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") } 1312 1313invalid_import_from_targets: 1314 | import_from_as_names ',' NEWLINE { 1315 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") } 1316 | token=NEWLINE { 1317 RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") } 1318 1319invalid_with_stmt: 1320 | ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1321 | ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1322invalid_with_stmt_indent: 1323 | ['async'] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT { 1324 RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) } 1325 | ['async'] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT { 1326 RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) } 1327 1328invalid_try_stmt: 1329 | a='try' ':' NEWLINE !INDENT { 1330 RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) } 1331 | 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") } 1332 | 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' { 1333 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot have both 'except' and 'except*' on the same 'try'") } 1334 | 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' { 1335 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") } 1336invalid_except_stmt: 1337 | 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' { 1338 RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") } 1339 | a='except' '*'? expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1340 | a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1341 | a='except' '*' (NEWLINE | ':') { RAISE_SYNTAX_ERROR("expected one or more exception types") } 1342invalid_finally_stmt: 1343 | a='finally' ':' NEWLINE !INDENT { 1344 RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) } 1345invalid_except_stmt_indent: 1346 | a='except' expression ['as' NAME ] ':' NEWLINE !INDENT { 1347 RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) } 1348 | a='except' ':' NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) } 1349invalid_except_star_stmt_indent: 1350 | a='except' '*' expression ['as' NAME ] ':' NEWLINE !INDENT { 1351 RAISE_INDENTATION_ERROR("expected an indented block after 'except*' statement on line %d", a->lineno) } 1352invalid_match_stmt: 1353 | "match" subject_expr NEWLINE { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) } 1354 | a="match" subject=subject_expr ':' NEWLINE !INDENT { 1355 RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) } 1356invalid_case_block: 1357 | "case" patterns guard? NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1358 | a="case" patterns guard? ':' NEWLINE !INDENT { 1359 RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) } 1360invalid_as_pattern: 1361 | or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") } 1362 | or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") } 1363invalid_class_pattern: 1364 | name_or_attr '(' a=invalid_class_argument_pattern { RAISE_SYNTAX_ERROR_KNOWN_RANGE( 1365 PyPegen_first_item(a, pattern_ty), 1366 PyPegen_last_item(a, pattern_ty), 1367 "positional patterns follow keyword patterns") } 1368invalid_class_argument_pattern[asdl_pattern_seq*]: 1369 | [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a } 1370invalid_if_stmt: 1371 | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1372 | a='if' a=named_expression ':' NEWLINE !INDENT { 1373 RAISE_INDENTATION_ERROR("expected an indented block after 'if' statement on line %d", a->lineno) } 1374invalid_elif_stmt: 1375 | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1376 | a='elif' named_expression ':' NEWLINE !INDENT { 1377 RAISE_INDENTATION_ERROR("expected an indented block after 'elif' statement on line %d", a->lineno) } 1378invalid_else_stmt: 1379 | a='else' ':' NEWLINE !INDENT { 1380 RAISE_INDENTATION_ERROR("expected an indented block after 'else' statement on line %d", a->lineno) } 1381invalid_while_stmt: 1382 | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1383 | a='while' named_expression ':' NEWLINE !INDENT { 1384 RAISE_INDENTATION_ERROR("expected an indented block after 'while' statement on line %d", a->lineno) } 1385invalid_for_stmt: 1386 | ['async'] 'for' star_targets 'in' star_expressions NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1387 | ['async'] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT { 1388 RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) } 1389invalid_def_raw: 1390 | ['async'] a='def' NAME [type_params] '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT { 1391 RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) } 1392 | ['async'] 'def' NAME [type_params] &&'(' [params] ')' ['->' expression] &&':' [func_type_comment] block 1393invalid_class_def_raw: 1394 | 'class' NAME [type_params] ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } 1395 | a='class' NAME [type_params] ['(' [arguments] ')'] ':' NEWLINE !INDENT { 1396 RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) } 1397 1398invalid_double_starred_kvpairs: 1399 | ','.double_starred_kvpair+ ',' invalid_kvpair 1400 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") } 1401 | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } 1402invalid_kvpair: 1403 | a=expression !(':') { 1404 RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") } 1405 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") } 1406 | expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } 1407invalid_starred_expression: 1408 | a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") } 1409 1410invalid_replacement_field: 1411 | '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") } 1412 | '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") } 1413 | '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before ':'") } 1414 | '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '}'") } 1415 | '{' !annotated_rhs { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting a valid expression after '{'")} 1416 | '{' annotated_rhs !('=' | '!' | ':' | '}') { 1417 PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '=', or '!', or ':', or '}'") } 1418 | '{' annotated_rhs '=' !('!' | ':' | '}') { 1419 PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '!', or ':', or '}'") } 1420 | '{' annotated_rhs '='? invalid_conversion_character 1421 | '{' annotated_rhs '='? ['!' NAME] !(':' | '}') { 1422 PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting ':' or '}'") } 1423 | '{' annotated_rhs '='? ['!' NAME] ':' fstring_format_spec* !'}' { 1424 PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '}', or format specs") } 1425 | '{' annotated_rhs '='? ['!' NAME] !'}' { 1426 PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '}'") } 1427 1428invalid_conversion_character: 1429 | '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: missing conversion character") } 1430 | '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: invalid conversion character") } 1431 1432invalid_arithmetic: 1433 | sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") } 1434invalid_factor: 1435 | ('+' | '-' | '~') a='not' b=factor { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") } 1436 1437invalid_type_params: 1438 | '[' token=']' { 1439 RAISE_SYNTAX_ERROR_STARTING_FROM( 1440 token, 1441 "Type parameter list cannot be empty")} 1442