mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Simplify
This commit is contained in:
parent
dd64a155e9
commit
cf7d4a2a24
5 changed files with 63 additions and 39 deletions
|
@ -41,6 +41,11 @@ Grammar(
|
|||
[">=", "GTEQ"],
|
||||
["+=", "PLUSEQ"],
|
||||
["-=", "MINUSEQ"],
|
||||
["|=", "PIPEEQ"],
|
||||
["&=", "AMPEQ"],
|
||||
["^=", "CARETEQ"],
|
||||
["/=", "SLASHEQ"],
|
||||
["*=", "STAREQ"],
|
||||
["&&", "AMPAMP"],
|
||||
["||", "PIPEPIPE"],
|
||||
["<<", "SHL"],
|
||||
|
|
|
@ -45,35 +45,33 @@ enum Op {
|
|||
}
|
||||
|
||||
fn current_op(p: &Parser) -> (u8, Op) {
|
||||
if p.at_compound2(PLUS, EQ) {
|
||||
return (1, Op::Composite(PLUSEQ, 2));
|
||||
if let Some(t) = p.next3() {
|
||||
match t {
|
||||
(L_ANGLE, L_ANGLE, EQ) =>
|
||||
return (1, Op::Composite(SHLEQ, 3)),
|
||||
(R_ANGLE, R_ANGLE, EQ) =>
|
||||
return (1, Op::Composite(SHREQ, 3)),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
if p.at_compound2(MINUS, EQ) {
|
||||
return (1, Op::Composite(MINUSEQ, 2));
|
||||
}
|
||||
if p.at_compound3(L_ANGLE, L_ANGLE, EQ) {
|
||||
return (1, Op::Composite(SHLEQ, 3));
|
||||
}
|
||||
if p.at_compound3(R_ANGLE, R_ANGLE, EQ) {
|
||||
return (1, Op::Composite(SHREQ, 3));
|
||||
}
|
||||
if p.at_compound2(PIPE, PIPE) {
|
||||
return (3, Op::Composite(PIPEPIPE, 2));
|
||||
}
|
||||
if p.at_compound2(AMP, AMP) {
|
||||
return (4, Op::Composite(AMPAMP, 2));
|
||||
}
|
||||
if p.at_compound2(L_ANGLE, EQ) {
|
||||
return (5, Op::Composite(LTEQ, 2));
|
||||
}
|
||||
if p.at_compound2(R_ANGLE, EQ) {
|
||||
return (5, Op::Composite(GTEQ, 2));
|
||||
}
|
||||
if p.at_compound2(L_ANGLE, L_ANGLE) {
|
||||
return (9, Op::Composite(SHL, 2));
|
||||
}
|
||||
if p.at_compound2(R_ANGLE, R_ANGLE) {
|
||||
return (9, Op::Composite(SHR, 2));
|
||||
|
||||
if let Some(t) = p.next2() {
|
||||
match t {
|
||||
(PLUS, EQ) => return (1, Op::Composite(PLUSEQ, 2)),
|
||||
(MINUS, EQ) => return (1, Op::Composite(MINUSEQ, 2)),
|
||||
(STAR, EQ) => return (1, Op::Composite(STAREQ, 2)),
|
||||
(SLASH, EQ) => return (1, Op::Composite(SLASHEQ, 2)),
|
||||
(PIPE, EQ) => return (1, Op::Composite(PIPEEQ, 2)),
|
||||
(AMP, EQ) => return (1, Op::Composite(AMPEQ, 2)),
|
||||
(CARET, EQ) => return (1, Op::Composite(CARETEQ, 2)),
|
||||
(PIPE, PIPE) => return (3, Op::Composite(PIPEPIPE, 2)),
|
||||
(AMP, AMP) => return (4, Op::Composite(AMPAMP, 2)),
|
||||
(L_ANGLE, EQ) => return (5, Op::Composite(LTEQ, 2)),
|
||||
(R_ANGLE, EQ) => return (5, Op::Composite(GTEQ, 2)),
|
||||
(L_ANGLE, L_ANGLE) => return (9, Op::Composite(SHL, 2)),
|
||||
(R_ANGLE, R_ANGLE) => return (9, Op::Composite(SHR, 2)),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
let bp = match p.current() {
|
||||
|
|
|
@ -68,12 +68,12 @@ impl<'t> Parser<'t> {
|
|||
self.current() == kind
|
||||
}
|
||||
|
||||
pub(crate) fn at_compound2(&self, c1: SyntaxKind, c2: SyntaxKind) -> bool {
|
||||
self.0.at_compound2(c1, c2)
|
||||
pub(crate) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> {
|
||||
self.0.next2()
|
||||
}
|
||||
|
||||
pub(crate) fn at_compound3(&self, c1: SyntaxKind, c2: SyntaxKind, c3: SyntaxKind) -> bool {
|
||||
self.0.at_compound3(c1, c2, c3)
|
||||
pub(crate) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> {
|
||||
self.0.next3()
|
||||
}
|
||||
|
||||
/// Checks if the current token is contextual keyword with text `t`.
|
||||
|
|
|
@ -65,15 +65,26 @@ impl<'t> ParserImpl<'t> {
|
|||
self.events
|
||||
}
|
||||
|
||||
pub(super) fn at_compound2(&self, c1: SyntaxKind, c2: SyntaxKind) -> bool {
|
||||
self.inp.kind(self.pos) == c1 && self.inp.kind(self.pos + 1) == c2
|
||||
&& self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos)
|
||||
pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> {
|
||||
let c1 = self.inp.kind(self.pos);
|
||||
let c2 = self.inp.kind(self.pos + 1);
|
||||
if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) {
|
||||
Some((c1, c2))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn at_compound3(&self, c1: SyntaxKind, c2: SyntaxKind, c3: SyntaxKind) -> bool {
|
||||
self.inp.kind(self.pos) == c1 && self.inp.kind(self.pos + 1) == c2 && self.inp.kind(self.pos + 2) == c3
|
||||
&& self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos)
|
||||
&& self.inp.start(self.pos + 2) == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1)
|
||||
pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> {
|
||||
let c1 = self.inp.kind(self.pos);
|
||||
let c2 = self.inp.kind(self.pos + 1);
|
||||
let c3 = self.inp.kind(self.pos + 2);
|
||||
if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos)
|
||||
&& self.inp.start(self.pos + 2) == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1){
|
||||
Some((c1, c2, c3))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn nth(&self, n: u32) -> SyntaxKind {
|
||||
|
|
|
@ -50,6 +50,11 @@ pub enum SyntaxKind {
|
|||
GTEQ,
|
||||
PLUSEQ,
|
||||
MINUSEQ,
|
||||
PIPEEQ,
|
||||
AMPEQ,
|
||||
CARETEQ,
|
||||
SLASHEQ,
|
||||
STAREQ,
|
||||
AMPAMP,
|
||||
PIPEPIPE,
|
||||
SHL,
|
||||
|
@ -288,6 +293,11 @@ impl SyntaxKind {
|
|||
GTEQ => &SyntaxInfo { name: "GTEQ" },
|
||||
PLUSEQ => &SyntaxInfo { name: "PLUSEQ" },
|
||||
MINUSEQ => &SyntaxInfo { name: "MINUSEQ" },
|
||||
PIPEEQ => &SyntaxInfo { name: "PIPEEQ" },
|
||||
AMPEQ => &SyntaxInfo { name: "AMPEQ" },
|
||||
CARETEQ => &SyntaxInfo { name: "CARETEQ" },
|
||||
SLASHEQ => &SyntaxInfo { name: "SLASHEQ" },
|
||||
STAREQ => &SyntaxInfo { name: "STAREQ" },
|
||||
AMPAMP => &SyntaxInfo { name: "AMPAMP" },
|
||||
PIPEPIPE => &SyntaxInfo { name: "PIPEPIPE" },
|
||||
SHL => &SyntaxInfo { name: "SHL" },
|
||||
|
|
Loading…
Reference in a new issue