mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Optimize Parser::is_composite a little
This commit is contained in:
parent
6f5ac06b47
commit
b118f7511c
1 changed files with 35 additions and 19 deletions
|
@ -6,7 +6,7 @@ use crate::{
|
||||||
event::Event,
|
event::Event,
|
||||||
ParseError,
|
ParseError,
|
||||||
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
|
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
|
||||||
TokenSet, TokenSource, T,
|
Token, TokenSet, TokenSource, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `Parser` struct provides the low-level API for
|
/// `Parser` struct provides the low-level API for
|
||||||
|
@ -87,8 +87,9 @@ impl<'t> Parser<'t> {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut kind = self.token_source.lookahead_nth(i).kind;
|
let token = self.token_source.lookahead_nth(i);
|
||||||
if let Some((composited, step)) = self.is_composite(kind, i) {
|
let mut kind = token.kind;
|
||||||
|
if let Some((composited, step)) = self.is_composite(token, i) {
|
||||||
kind = composited;
|
kind = composited;
|
||||||
i += step;
|
i += step;
|
||||||
} else {
|
} else {
|
||||||
|
@ -250,32 +251,47 @@ impl<'t> Parser<'t> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// helper function for check if it is composite.
|
/// helper function for check if it is composite.
|
||||||
fn is_composite(&self, kind: SyntaxKind, n: usize) -> Option<(SyntaxKind, usize)> {
|
fn is_composite(&self, first: Token, n: usize) -> Option<(SyntaxKind, usize)> {
|
||||||
// We assume the dollars will not occuried between
|
// We assume the dollars will not occuried between
|
||||||
// mult-byte tokens
|
// mult-byte tokens
|
||||||
|
|
||||||
let first = self.token_source.lookahead_nth(n);
|
let jn1 = first.is_jointed_to_next;
|
||||||
|
if !jn1 && first.kind != T![-] {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let second = self.token_source.lookahead_nth(n + 1);
|
let second = self.token_source.lookahead_nth(n + 1);
|
||||||
|
if first.kind == T![-] && second.kind == T![>] {
|
||||||
|
return Some((T![->], 2));
|
||||||
|
}
|
||||||
|
if !jn1 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
match (first.kind, second.kind) {
|
||||||
|
(T![:], T![:]) => return Some((T![::], 2)),
|
||||||
|
(T![=], T![=]) => return Some((T![==], 2)),
|
||||||
|
(T![=], T![>]) => return Some((T![=>], 2)),
|
||||||
|
(T![!], T![=]) => return Some((T![!=], 2)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if first.kind != T![.] || second.kind != T![.] {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let third = self.token_source.lookahead_nth(n + 2);
|
let third = self.token_source.lookahead_nth(n + 2);
|
||||||
|
|
||||||
let jn1 = first.is_jointed_to_next;
|
|
||||||
let la2 = second.kind;
|
|
||||||
let jn2 = second.is_jointed_to_next;
|
let jn2 = second.is_jointed_to_next;
|
||||||
let la3 = third.kind;
|
let la3 = third.kind;
|
||||||
|
|
||||||
match kind {
|
if jn2 && la3 == T![.] {
|
||||||
T![.] if jn1 && la2 == T![.] && jn2 && la3 == T![.] => Some((T![...], 3)),
|
return Some((T![...], 3));
|
||||||
T![.] if jn1 && la2 == T![.] && la3 == T![=] => Some((T![..=], 3)),
|
|
||||||
T![.] if jn1 && la2 == T![.] => Some((T![..], 2)),
|
|
||||||
|
|
||||||
T![:] if jn1 && la2 == T![:] => Some((T![::], 2)),
|
|
||||||
T![=] if jn1 && la2 == T![=] => Some((T![==], 2)),
|
|
||||||
T![=] if jn1 && la2 == T![>] => Some((T![=>], 2)),
|
|
||||||
|
|
||||||
T![!] if jn1 && la2 == T![=] => Some((T![!=], 2)),
|
|
||||||
T![-] if la2 == T![>] => Some((T![->], 2)),
|
|
||||||
_ => None,
|
|
||||||
}
|
}
|
||||||
|
if la3 == T![=] {
|
||||||
|
return Some((T![..=], 3));
|
||||||
|
}
|
||||||
|
return Some((T![..], 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eat_dollars(&mut self) {
|
fn eat_dollars(&mut self) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue