Skip to content

Commit 2c72d2f

Browse files
committed
Micro-optimize lookahead in composite tokens
1 parent ce06f8d commit 2c72d2f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

crates/ra_parser/src/parser.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,24 @@ impl<'t> Parser<'t> {
127127

128128
fn at_composite2(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind) -> bool {
129129
let t1 = self.token_source.lookahead_nth(n);
130+
if t1.kind != k1 || !t1.is_jointed_to_next {
131+
return false;
132+
}
130133
let t2 = self.token_source.lookahead_nth(n + 1);
131-
t1.kind == k1 && t1.is_jointed_to_next && t2.kind == k2
134+
t2.kind == k2
132135
}
133136

134137
fn at_composite3(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind, k3: SyntaxKind) -> bool {
135138
let t1 = self.token_source.lookahead_nth(n);
139+
if t1.kind != k1 || !t1.is_jointed_to_next {
140+
return false;
141+
}
136142
let t2 = self.token_source.lookahead_nth(n + 1);
143+
if t2.kind != k2 || !t2.is_jointed_to_next {
144+
return false;
145+
}
137146
let t3 = self.token_source.lookahead_nth(n + 2);
138-
(t1.kind == k1 && t1.is_jointed_to_next)
139-
&& (t2.kind == k2 && t2.is_jointed_to_next)
140-
&& t3.kind == k3
147+
t3.kind == k3
141148
}
142149

143150
/// Checks if the current token is in `kinds`.

0 commit comments

Comments
 (0)