mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-07 21:25:31 +00:00
Refactor to avoid traversing the line unecessarily
This commit is contained in:
parent
8c81332205
commit
6050b5e4b7
1 changed files with 22 additions and 17 deletions
|
@ -96,26 +96,31 @@ where
|
|||
if !self.start_of_line {
|
||||
next = Some(Ok((soft_to_name(tok), *range)));
|
||||
} else {
|
||||
let mut nesting = 0;
|
||||
let mut first = true;
|
||||
let mut seen_name = false;
|
||||
let mut seen_equal = false;
|
||||
while let Some(Ok((tok, _))) = self.underlying.peek() {
|
||||
match tok {
|
||||
Tok::Newline => break,
|
||||
let mut is_type_alias = false;
|
||||
if let Some(Ok((tok, _))) = self.underlying.peek() {
|
||||
if matches!(
|
||||
tok,
|
||||
Tok::Name { .. } |
|
||||
// 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 first => 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,
|
||||
_ => {}
|
||||
// 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
|
||||
) {
|
||||
let mut nesting = 0;
|
||||
while let Some(Ok((tok, _))) = self.underlying.peek() {
|
||||
match tok {
|
||||
Tok::Newline => break,
|
||||
Tok::Equal if nesting == 0 => {
|
||||
is_type_alias = true;
|
||||
break;
|
||||
}
|
||||
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1,
|
||||
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
if !(seen_name && seen_equal) {
|
||||
if !is_type_alias {
|
||||
next = Some(Ok((soft_to_name(tok), *range)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue