@@ -163,17 +163,20 @@ dotted_name[expr_ty]:
163163 | NAME
164164
165165if_stmt[stmt_ty]:
166- | 'if' a=named_expression && ':' b=block c=elif_stmt {
166+ | 'if' a=named_expression ':' b=block c=elif_stmt {
167167 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
168- | 'if' a=named_expression &&':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
168+ | 'if' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
169+ | invalid_if_stmt
169170elif_stmt[stmt_ty]:
170- | 'elif' a=named_expression && ':' b=block c=elif_stmt {
171+ | 'elif' a=named_expression ':' b=block c=elif_stmt {
171172 _PyAST_If(a, b, CHECK(asdl_stmt_seq*, _PyPegen_singleton_seq(p, c)), EXTRA) }
172- | 'elif' a=named_expression &&':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
173+ | 'elif' a=named_expression ':' b=block c=[else_block] { _PyAST_If(a, b, c, EXTRA) }
174+ | invalid_elif_stmt
173175else_block[asdl_stmt_seq*]: 'else' &&':' b=block { b }
174176
175177while_stmt[stmt_ty]:
176- | 'while' a=named_expression &&':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
178+ | 'while' a=named_expression ':' b=block c=[else_block] { _PyAST_While(a, b, c, EXTRA) }
179+ | invalid_while_stmt
177180
178181for_stmt[stmt_ty]:
179182 | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {
@@ -438,10 +441,11 @@ star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_express
438441star_named_expression[expr_ty]:
439442 | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) }
440443 | named_expression
444+
441445named_expression[expr_ty]:
442446 | a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
443- | expression !':='
444447 | invalid_named_expression
448+ | expression !':='
445449
446450annotated_rhs[expr_ty]: yield_expr | star_expressions
447451
@@ -772,6 +776,12 @@ invalid_named_expression:
772776 | a=expression ':=' expression {
773777 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
774778 a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
779+ | a=NAME b='=' bitwise_or !('='|':='|',') {
780+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
781+ | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':='|',') {
782+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
783+ _PyPegen_get_expr_name(a)) }
784+
775785invalid_assignment:
776786 | a=invalid_ann_assign_target ':' expression {
777787 RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
@@ -841,9 +851,9 @@ invalid_for_target:
841851
842852invalid_group:
843853 | '(' a=starred_expression ')' {
844- RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
854+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
845855 | '(' a='**' expression ')' {
846- RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use double starred expression here") }
856+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
847857invalid_import_from_targets:
848858 | import_from_as_names ',' {
849859 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
@@ -860,6 +870,11 @@ invalid_except_block:
860870
861871invalid_match_stmt:
862872 | "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
863-
864873invalid_case_block:
865874 | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }
875+ invalid_if_stmt:
876+ | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
877+ invalid_elif_stmt:
878+ | 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
879+ invalid_while_stmt:
880+ | 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
0 commit comments