From 65ebfd9a34554b8139d5c4673bc3b5daa5ab0df5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Feb 2018 13:13:06 +0300 Subject: Simplify --- src/parser/grammar/attributes.rs | 2 +- src/parser/grammar/mod.rs | 62 ---------------------------------------- src/parser/grammar/paths.rs | 5 +++- src/parser/mod.rs | 30 +++++++++++++++++++ 4 files changed, 35 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/parser/grammar/attributes.rs b/src/parser/grammar/attributes.rs index 92dfb99ef..7f1c0401a 100644 --- a/src/parser/grammar/attributes.rs +++ b/src/parser/grammar/attributes.rs @@ -1,7 +1,7 @@ use super::*; pub(super) fn inner_attributes(p: &mut Parser) { - while p.at([POUND, EXCL]) { + while p.current() == POUND && p.nth(1) == EXCL { attribute(p, true) } } diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index 5266354c1..f5b63aaab 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -110,65 +110,3 @@ fn error_block(p: &mut Parser, message: &str) { } err.complete(p, ERROR); } - -impl<'p> Parser<'p> { - fn at(&self, l: L) -> bool { - l.is_ahead(self) - } - - fn err_and_bump(&mut self, message: &str) { - let err = self.start(); - self.error(message); - self.bump(); - err.complete(self, ERROR); - } - - fn expect(&mut self, kind: SyntaxKind) -> bool { - if self.at(kind) { - self.bump(); - true - } else { - self.error(format!("expected {:?}", kind)); - false - } - } - - fn eat(&mut self, kind: SyntaxKind) -> bool { - self.current() == kind && { - self.bump(); - true - } - } -} - -trait Lookahead: Copy { - fn is_ahead(self, p: &Parser) -> bool; -} - -impl Lookahead for SyntaxKind { - fn is_ahead(self, p: &Parser) -> bool { - p.current() == self - } -} - -impl Lookahead for [SyntaxKind; 2] { - fn is_ahead(self, p: &Parser) -> bool { - p.current() == self[0] && p.nth(1) == self[1] - } -} - -impl Lookahead for [SyntaxKind; 3] { - fn is_ahead(self, p: &Parser) -> bool { - p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2] - } -} - -#[derive(Clone, Copy)] -struct AnyOf<'a>(&'a [SyntaxKind]); - -impl<'a> Lookahead for AnyOf<'a> { - fn is_ahead(self, p: &Parser) -> bool { - let curr = p.current(); - self.0.iter().any(|&k| k == curr) - } -} diff --git a/src/parser/grammar/paths.rs b/src/parser/grammar/paths.rs index 6ed315c3d..d3eb20ea1 100644 --- a/src/parser/grammar/paths.rs +++ b/src/parser/grammar/paths.rs @@ -1,7 +1,10 @@ use super::*; pub(super) fn is_path_start(p: &Parser) -> bool { - AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p) + match p.current() { + IDENT | SELF_KW | SUPER_KW | COLONCOLON => true, + _ => false, + } } pub(super) fn use_path(p: &mut Parser) { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 49a69900f..c23ed3349 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -26,3 +26,33 @@ fn is_insignificant(kind: SyntaxKind) -> bool { _ => false, } } + +impl<'p> parser::Parser<'p> { + fn at(&self, kind: SyntaxKind) -> bool { + self.current() == kind + } + + fn err_and_bump(&mut self, message: &str) { + let err = self.start(); + self.error(message); + self.bump(); + err.complete(self, ERROR); + } + + fn expect(&mut self, kind: SyntaxKind) -> bool { + if self.at(kind) { + self.bump(); + true + } else { + self.error(format!("expected {:?}", kind)); + false + } + } + + fn eat(&mut self, kind: SyntaxKind) -> bool { + self.at(kind) && { + self.bump(); + true + } + } +} -- cgit v1.2.3