From 2c72d2f438fe0f8b1cbc41ac12e578a5817bafc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Fri, 26 Jun 2020 21:47:17 +0300 Subject: Micro-optimize lookahead in composite tokens --- crates/ra_parser/src/parser.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/parser.rs b/crates/ra_parser/src/parser.rs index 4f59b0a23..d797f2cc9 100644 --- a/crates/ra_parser/src/parser.rs +++ b/crates/ra_parser/src/parser.rs @@ -127,17 +127,24 @@ impl<'t> Parser<'t> { fn at_composite2(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind) -> bool { let t1 = self.token_source.lookahead_nth(n); + if t1.kind != k1 || !t1.is_jointed_to_next { + return false; + } let t2 = self.token_source.lookahead_nth(n + 1); - t1.kind == k1 && t1.is_jointed_to_next && t2.kind == k2 + t2.kind == k2 } fn at_composite3(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind, k3: SyntaxKind) -> bool { let t1 = self.token_source.lookahead_nth(n); + if t1.kind != k1 || !t1.is_jointed_to_next { + return false; + } let t2 = self.token_source.lookahead_nth(n + 1); + if t2.kind != k2 || !t2.is_jointed_to_next { + return false; + } let t3 = self.token_source.lookahead_nth(n + 2); - (t1.kind == k1 && t1.is_jointed_to_next) - && (t2.kind == k2 && t2.is_jointed_to_next) - && t3.kind == k3 + t3.kind == k3 } /// Checks if the current token is in `kinds`. -- cgit v1.2.3