diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2021-06-10 22:50:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-10 22:50:32 (GMT) |
commit | 05073036dcecefc00b0c3e7397601809da41e2f1 (patch) | |
tree | e6db253475a3b4bf1b446a2d12e90a0ea4a76043 /Grammar | |
parent | e7b4644607789848f9752a3bd20ff216e25b4156 (diff) | |
download | cpython-05073036dcecefc00b0c3e7397601809da41e2f1.zip cpython-05073036dcecefc00b0c3e7397601809da41e2f1.tar.gz cpython-05073036dcecefc00b0c3e7397601809da41e2f1.tar.bz2 |
bpo-44368: Improve syntax errors with invalid as pattern targets (GH-26632)
Diffstat (limited to 'Grammar')
-rw-r--r-- | Grammar/python.gram | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/Grammar/python.gram b/Grammar/python.gram index 0ccdc3e..56daca0 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -244,6 +244,7 @@ pattern[pattern_ty]: as_pattern[pattern_ty]: | pattern=or_pattern 'as' target=pattern_capture_target { _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) } + | invalid_as_pattern or_pattern[pattern_ty]: | patterns[asdl_pattern_seq*]='|'.closed_pattern+ { asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _PyAST_MatchOr(patterns, EXTRA) } @@ -974,6 +975,9 @@ invalid_case_block: | "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") } | a="case" patterns guard? ':' NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) } +invalid_as_pattern: + | or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") } + | or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") } invalid_if_stmt: | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") } | a='if' a=named_expression ':' NEWLINE !INDENT { |