mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-04 02:39:22 +00:00
Use match
statement in soft keyword token handler
This commit is contained in:
parent
bbedb61607
commit
d1c319ba15
1 changed files with 62 additions and 57 deletions
|
@ -50,14 +50,15 @@ where
|
|||
// used as an identifier. We assume every soft keyword use is an identifier unless
|
||||
// a heuristic is met.
|
||||
|
||||
match tok {
|
||||
// For `match` and `case`, all of the following conditions must be met:
|
||||
// 1. The token is at the start of a logical line.
|
||||
// 2. The logical line contains a top-level colon (that is, a colon that is not nested
|
||||
// inside a parenthesized expression, list, or dictionary).
|
||||
// 3. The top-level colon is not the immediate sibling of a `match` or `case` token.
|
||||
// (This is to avoid treating `match` or `case` as identifiers when annotated with
|
||||
// type hints.)
|
||||
if matches!(tok, Tok::Match | Tok::Case) {
|
||||
// type hints.) type hints.)
|
||||
Tok::Match | Tok::Case => {
|
||||
if !self.start_of_line {
|
||||
next = Some(Ok((soft_to_name(tok), *range)));
|
||||
} else {
|
||||
|
@ -91,7 +92,7 @@ where
|
|||
// 1. The token is at the start of a logical line.
|
||||
// 2. The type token is followed by a name token.
|
||||
// 3. The name token is followed by an equality token.
|
||||
else if matches!(tok, Tok::Type) {
|
||||
Tok::Type => {
|
||||
if !self.start_of_line {
|
||||
next = Some(Ok((soft_to_name(tok), *range)));
|
||||
} else {
|
||||
|
@ -104,7 +105,9 @@ where
|
|||
Tok::Name { .. } if nesting == 0 => seen_name = true,
|
||||
// We treat a soft keyword token following a type token as a
|
||||
// name to support cases like `type type = int` or `type match = int`
|
||||
Tok::Type | Tok::Match | Tok::Case if nesting == 0 => seen_name = true,
|
||||
Tok::Type | Tok::Match | Tok::Case if nesting == 0 => {
|
||||
seen_name = true
|
||||
}
|
||||
Tok::Equal if nesting == 0 && seen_name => seen_equal = true,
|
||||
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1,
|
||||
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,
|
||||
|
@ -116,6 +119,8 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
_ => (), // Not a soft keyword token
|
||||
}
|
||||
}
|
||||
|
||||
self.start_of_line = next.as_ref().map_or(false, |lex_result| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue