Skip to content

Commit 6050b5e

Browse files
committed
Refactor to avoid traversing the line unecessarily
1 parent 8c81332 commit 6050b5e

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

Diff for: parser/src/soft_keywords.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,31 @@ where
9696
if !self.start_of_line {
9797
next = Some(Ok((soft_to_name(tok), *range)));
9898
} else {
99-
let mut nesting = 0;
100-
let mut first = true;
101-
let mut seen_name = false;
102-
let mut seen_equal = false;
103-
while let Some(Ok((tok, _))) = self.underlying.peek() {
104-
match tok {
105-
Tok::Newline => break,
99+
let mut is_type_alias = false;
100+
if let Some(Ok((tok, _))) = self.underlying.peek() {
101+
if matches!(
102+
tok,
106103
Tok::Name { .. } |
107-
// We treat a soft keyword token following a type token as a
108-
// name to support cases like `type type = int` or `type match = int`
109-
Tok::Type | Tok::Match | Tok::Case
110-
if first => seen_name = true,
111-
Tok::Equal if nesting == 0 && seen_name => seen_equal = true,
112-
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1,
113-
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,
114-
_ => {}
104+
// We treat a soft keyword token following a type token as a
105+
// name to support cases like `type type = int` or `type match = int`
106+
Tok::Type | Tok::Match | Tok::Case
107+
) {
108+
let mut nesting = 0;
109+
while let Some(Ok((tok, _))) = self.underlying.peek() {
110+
match tok {
111+
Tok::Newline => break,
112+
Tok::Equal if nesting == 0 => {
113+
is_type_alias = true;
114+
break;
115+
}
116+
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1,
117+
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,
118+
_ => {}
119+
}
120+
}
115121
}
116-
first = false;
117122
}
118-
if !(seen_name && seen_equal) {
123+
if !is_type_alias {
119124
next = Some(Ok((soft_to_name(tok), *range)));
120125
}
121126
}

0 commit comments

Comments
 (0)