@@ -709,12 +709,17 @@ expressions[expr_ty]:
709709 | expression
710710
711711expression[expr_ty] (memo):
712+ | invalid_if_expression
712713 | invalid_expression
713714 | invalid_legacy_expression
714- | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
715+ | if_expression
715716 | disjunction
716717 | lambdef
717718
719+ if_expression[expr_ty]:
720+ | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
721+ | invalid_if_expression
722+
718723yield_expr[expr_ty]:
719724 | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) }
720725 | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) }
@@ -731,10 +736,16 @@ star_expression[expr_ty] (memo):
731736
732737star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a }
733738
739+ star_named_expressions_sequence[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression_sequence+ [','] { a }
740+
734741star_named_expression[expr_ty]:
735742 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
736743 | named_expression
737744
745+ star_named_expression_sequence[expr_ty]:
746+ | invalid_starred_expression_unpacking_sequence
747+ | star_named_expression
748+
738749assignment_expression[expr_ty]:
739750 | a=NAME ':=' ~ b=expression {
740751 CHECK_VERSION(expr_ty, 8, "Assignment expressions are",
@@ -998,13 +1009,13 @@ strings[expr_ty] (memo):
9981009 | a[asdl_expr_seq*]=tstring+ { _PyPegen_concatenate_tstrings(p, a, EXTRA) }
9991010
10001011list[expr_ty]:
1001- | '[' a=[star_named_expressions ] ']' { _PyAST_List(a, Load, EXTRA) }
1012+ | '[' a=[star_named_expressions_sequence ] ']' { _PyAST_List(a, Load, EXTRA) }
10021013
10031014tuple[expr_ty]:
1004- | '(' a=[y=star_named_expression ',' z=[star_named_expressions ] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
1015+ | '(' a=[y=star_named_expression_sequence ',' z=[star_named_expressions_sequence ] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' {
10051016 _PyAST_Tuple(a, Load, EXTRA) }
10061017
1007- set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) }
1018+ set[expr_ty]: '{' a=star_named_expressions_sequence '}' { _PyAST_Set(a, EXTRA) }
10081019
10091020# Dicts
10101021# -----
@@ -1262,6 +1273,12 @@ invalid_expression:
12621273 | a='lambda' [lambda_params] b=':' &TSTRING_MIDDLE {
12631274 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "t-string: lambda expressions are not allowed without parentheses") }
12641275
1276+ invalid_if_expression:
1277+ | disjunction 'if' b=disjunction 'else' a='*' {
1278+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot unpack only part of a conditional expression") }
1279+ | disjunction 'if' b=disjunction 'else' a='**' {
1280+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use dict unpacking on only part of a conditional expression") }
1281+
12651282invalid_named_expression(memo):
12661283 | a=expression ':=' expression {
12671284 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
@@ -1529,21 +1546,32 @@ invalid_class_def_raw:
15291546 RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) }
15301547
15311548invalid_double_starred_kvpairs:
1532- | ','.double_starred_kvpair+ ',' invalid_kvpair
1533- | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
1534- | expression ':' a='**' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use dict unpacking in a dictionary value") }
1549+ | invalid_kvpair_unpacking [',']
1550+ | ','.double_starred_kvpair+ ',' (invalid_kvpair | invalid_kvpair_unpacking)
15351551 | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1552+ invalid_kvpair_unpacking:
1553+ | a='**' b=if_expression {
1554+ RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid double starred expression. Did you forget to wrap the conditional expression in parentheses?") }
1555+ | a='*' b=bitwise_or ':' expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use a starred expression in a dictionary key") }
1556+ | a='**' b=bitwise_or ':' expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use dict unpacking in a dictionary key") }
1557+ | expression ':' a='*' b=bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use a starred expression in a dictionary value") }
1558+ | expression ':' a='**' b=bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use dict unpacking in a dictionary value") }
15361559invalid_kvpair:
15371560 | a=expression !(':') {
15381561 RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
15391562 | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
15401563 | expression ':' a='**' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use dict unpacking in a dictionary value") }
15411564 | expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
15421565invalid_starred_expression_unpacking:
1566+ | a='*' b=if_expression {
1567+ RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid starred expression. Did you forget to wrap the conditional expression in parentheses?") }
15431568 | a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }
1569+ invalid_starred_expression_unpacking_sequence:
1570+ | a='**' bitwise_or {
1571+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use dict unpacking here") }
1572+ | invalid_starred_expression_unpacking
15441573invalid_starred_expression:
15451574 | '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }
1546-
15471575invalid_fstring_replacement_field:
15481576 | '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
15491577 | '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") }
0 commit comments