bpo-45759: Better error messages for non-matching 'elif'/'else' statements (#29513)

This commit is contained in:
Crowthebird 2023-11-20 21:27:53 +08:00 committed by GitHub
parent 56e59a49ae
commit 1c8f912ebd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 598 additions and 437 deletions

View file

@ -124,6 +124,7 @@ simple_stmt[stmt_ty] (memo):
| &'nonlocal' nonlocal_stmt
compound_stmt[stmt_ty]:
| invalid_compound_stmt
| &('def' | '@' | 'async') function_def
| &'if' if_stmt
| &('class' | '@') class_def
@ -1298,6 +1299,10 @@ invalid_import_from_targets:
| import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
invalid_compound_stmt:
| a='elif' named_expression ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'elif' must match an if-statement here") }
| a='else' ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'else' must match a valid statement here") }
invalid_with_stmt:
| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }