From cf7d4a2a243cac1975b9b28d47ed91a6bd01b34f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Aug 2018 00:48:10 +0300 Subject: Simplify --- crates/libsyntax2/src/grammar.ron | 5 +++ crates/libsyntax2/src/grammar/expressions/mod.rs | 54 ++++++++++++------------ crates/libsyntax2/src/parser_api.rs | 8 ++-- crates/libsyntax2/src/parser_impl/mod.rs | 25 ++++++++--- crates/libsyntax2/src/syntax_kinds/generated.rs | 10 +++++ 5 files changed, 63 insertions(+), 39 deletions(-) (limited to 'crates') diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index daf80dde3..2cec5b3e3 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -41,6 +41,11 @@ Grammar( [">=", "GTEQ"], ["+=", "PLUSEQ"], ["-=", "MINUSEQ"], + ["|=", "PIPEEQ"], + ["&=", "AMPEQ"], + ["^=", "CARETEQ"], + ["/=", "SLASHEQ"], + ["*=", "STAREQ"], ["&&", "AMPAMP"], ["||", "PIPEPIPE"], ["<<", "SHL"], diff --git a/crates/libsyntax2/src/grammar/expressions/mod.rs b/crates/libsyntax2/src/grammar/expressions/mod.rs index feef5f1d6..3da539699 100644 --- a/crates/libsyntax2/src/grammar/expressions/mod.rs +++ b/crates/libsyntax2/src/grammar/expressions/mod.rs @@ -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 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 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(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() { diff --git a/crates/libsyntax2/src/parser_api.rs b/crates/libsyntax2/src/parser_api.rs index c78c6e43a..bb34fe973 100644 --- a/crates/libsyntax2/src/parser_api.rs +++ b/crates/libsyntax2/src/parser_api.rs @@ -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`. diff --git a/crates/libsyntax2/src/parser_impl/mod.rs b/crates/libsyntax2/src/parser_impl/mod.rs index 06c16cdb4..14cceced5 100644 --- a/crates/libsyntax2/src/parser_impl/mod.rs +++ b/crates/libsyntax2/src/parser_impl/mod.rs @@ -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 { diff --git a/crates/libsyntax2/src/syntax_kinds/generated.rs b/crates/libsyntax2/src/syntax_kinds/generated.rs index b04dfe55c..fc387c93d 100644 --- a/crates/libsyntax2/src/syntax_kinds/generated.rs +++ b/crates/libsyntax2/src/syntax_kinds/generated.rs @@ -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" }, -- cgit v1.2.3