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