diff options
author | Laurențiu Nicola <[email protected]> | 2019-08-04 12:06:07 +0100 |
---|---|---|
committer | Laurențiu Nicola <[email protected]> | 2019-08-05 14:53:54 +0100 |
commit | b118f7511ca522e1d5199105a856f18b4dee51f8 (patch) | |
tree | a65c68c8ebf131d90d30e871395e61e1f1e96c4a | |
parent | 6f5ac06b47b040936c6e0a1fbac9cdba13e26197 (diff) |
Optimize Parser::is_composite a little
-rw-r--r-- | crates/ra_parser/src/parser.rs | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/crates/ra_parser/src/parser.rs b/crates/ra_parser/src/parser.rs index 159ed50df..393586561 100644 --- a/crates/ra_parser/src/parser.rs +++ b/crates/ra_parser/src/parser.rs | |||
@@ -6,7 +6,7 @@ use crate::{ | |||
6 | event::Event, | 6 | event::Event, |
7 | ParseError, | 7 | ParseError, |
8 | SyntaxKind::{self, EOF, ERROR, TOMBSTONE}, | 8 | SyntaxKind::{self, EOF, ERROR, TOMBSTONE}, |
9 | TokenSet, TokenSource, T, | 9 | Token, TokenSet, TokenSource, T, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | /// `Parser` struct provides the low-level API for | 12 | /// `Parser` struct provides the low-level API for |
@@ -87,8 +87,9 @@ impl<'t> Parser<'t> { | |||
87 | let mut i = 0; | 87 | let mut i = 0; |
88 | 88 | ||
89 | loop { | 89 | loop { |
90 | let mut kind = self.token_source.lookahead_nth(i).kind; | 90 | let token = self.token_source.lookahead_nth(i); |
91 | if let Some((composited, step)) = self.is_composite(kind, i) { | 91 | let mut kind = token.kind; |
92 | if let Some((composited, step)) = self.is_composite(token, i) { | ||
92 | kind = composited; | 93 | kind = composited; |
93 | i += step; | 94 | i += step; |
94 | } else { | 95 | } else { |
@@ -250,32 +251,47 @@ impl<'t> Parser<'t> { | |||
250 | } | 251 | } |
251 | 252 | ||
252 | /// helper function for check if it is composite. | 253 | /// helper function for check if it is composite. |
253 | fn is_composite(&self, kind: SyntaxKind, n: usize) -> Option<(SyntaxKind, usize)> { | 254 | fn is_composite(&self, first: Token, n: usize) -> Option<(SyntaxKind, usize)> { |
254 | // We assume the dollars will not occuried between | 255 | // We assume the dollars will not occuried between |
255 | // mult-byte tokens | 256 | // mult-byte tokens |
256 | 257 | ||
257 | let first = self.token_source.lookahead_nth(n); | 258 | let jn1 = first.is_jointed_to_next; |
259 | if !jn1 && first.kind != T![-] { | ||
260 | return None; | ||
261 | } | ||
262 | |||
258 | let second = self.token_source.lookahead_nth(n + 1); | 263 | let second = self.token_source.lookahead_nth(n + 1); |
264 | if first.kind == T![-] && second.kind == T![>] { | ||
265 | return Some((T![->], 2)); | ||
266 | } | ||
267 | if !jn1 { | ||
268 | return None; | ||
269 | } | ||
270 | |||
271 | match (first.kind, second.kind) { | ||
272 | (T![:], T![:]) => return Some((T![::], 2)), | ||
273 | (T![=], T![=]) => return Some((T![==], 2)), | ||
274 | (T![=], T![>]) => return Some((T![=>], 2)), | ||
275 | (T![!], T![=]) => return Some((T![!=], 2)), | ||
276 | _ => {} | ||
277 | } | ||
278 | |||
279 | if first.kind != T![.] || second.kind != T![.] { | ||
280 | return None; | ||
281 | } | ||
282 | |||
259 | let third = self.token_source.lookahead_nth(n + 2); | 283 | let third = self.token_source.lookahead_nth(n + 2); |
260 | 284 | ||
261 | let jn1 = first.is_jointed_to_next; | ||
262 | let la2 = second.kind; | ||
263 | let jn2 = second.is_jointed_to_next; | 285 | let jn2 = second.is_jointed_to_next; |
264 | let la3 = third.kind; | 286 | let la3 = third.kind; |
265 | 287 | ||
266 | match kind { | 288 | if jn2 && la3 == T![.] { |
267 | T![.] if jn1 && la2 == T![.] && jn2 && la3 == T![.] => Some((T![...], 3)), | 289 | return Some((T![...], 3)); |
268 | T![.] if jn1 && la2 == T![.] && la3 == T![=] => Some((T![..=], 3)), | 290 | } |
269 | T![.] if jn1 && la2 == T![.] => Some((T![..], 2)), | 291 | if la3 == T![=] { |
270 | 292 | return Some((T![..=], 3)); | |
271 | T![:] if jn1 && la2 == T![:] => Some((T![::], 2)), | ||
272 | T![=] if jn1 && la2 == T![=] => Some((T![==], 2)), | ||
273 | T![=] if jn1 && la2 == T![>] => Some((T![=>], 2)), | ||
274 | |||
275 | T![!] if jn1 && la2 == T![=] => Some((T![!=], 2)), | ||
276 | T![-] if la2 == T![>] => Some((T![->], 2)), | ||
277 | _ => None, | ||
278 | } | 293 | } |
294 | return Some((T![..], 2)); | ||
279 | } | 295 | } |
280 | 296 | ||
281 | fn eat_dollars(&mut self) { | 297 | fn eat_dollars(&mut self) { |