mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
bpo-44368: Improve syntax errors with invalid as pattern targets (GH-26632)
This commit is contained in:
parent
e7b4644607
commit
05073036dc
4 changed files with 332 additions and 213 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue