aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/parser/grammar/attributes.rs2
-rw-r--r--src/parser/grammar/mod.rs62
-rw-r--r--src/parser/grammar/paths.rs5
-rw-r--r--src/parser/mod.rs30
4 files changed, 35 insertions, 64 deletions
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 @@
1use super::*; 1use super::*;
2 2
3pub(super) fn inner_attributes(p: &mut Parser) { 3pub(super) fn inner_attributes(p: &mut Parser) {
4 while p.at([POUND, EXCL]) { 4 while p.current() == POUND && p.nth(1) == EXCL {
5 attribute(p, true) 5 attribute(p, true)
6 } 6 }
7} 7}
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) {
110 } 110 }
111 err.complete(p, ERROR); 111 err.complete(p, ERROR);
112} 112}
113
114impl<'p> Parser<'p> {
115 fn at<L: Lookahead>(&self, l: L) -> bool {
116 l.is_ahead(self)
117 }
118
119 fn err_and_bump(&mut self, message: &str) {
120 let err = self.start();
121 self.error(message);
122 self.bump();
123 err.complete(self, ERROR);
124 }
125
126 fn expect(&mut self, kind: SyntaxKind) -> bool {
127 if self.at(kind) {
128 self.bump();
129 true
130 } else {
131 self.error(format!("expected {:?}", kind));
132 false
133 }
134 }
135
136 fn eat(&mut self, kind: SyntaxKind) -> bool {
137 self.current() == kind && {
138 self.bump();
139 true
140 }
141 }
142}
143
144trait Lookahead: Copy {
145 fn is_ahead(self, p: &Parser) -> bool;
146}
147
148impl Lookahead for SyntaxKind {
149 fn is_ahead(self, p: &Parser) -> bool {
150 p.current() == self
151 }
152}
153
154impl Lookahead for [SyntaxKind; 2] {
155 fn is_ahead(self, p: &Parser) -> bool {
156 p.current() == self[0] && p.nth(1) == self[1]
157 }
158}
159
160impl Lookahead for [SyntaxKind; 3] {
161 fn is_ahead(self, p: &Parser) -> bool {
162 p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2]
163 }
164}
165
166#[derive(Clone, Copy)]
167struct AnyOf<'a>(&'a [SyntaxKind]);
168
169impl<'a> Lookahead for AnyOf<'a> {
170 fn is_ahead(self, p: &Parser) -> bool {
171 let curr = p.current();
172 self.0.iter().any(|&k| k == curr)
173 }
174}
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 @@
1use super::*; 1use super::*;
2 2
3pub(super) fn is_path_start(p: &Parser) -> bool { 3pub(super) fn is_path_start(p: &Parser) -> bool {
4 AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p) 4 match p.current() {
5 IDENT | SELF_KW | SUPER_KW | COLONCOLON => true,
6 _ => false,
7 }
5} 8}
6 9
7pub(super) fn use_path(p: &mut Parser) { 10pub(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 {
26 _ => false, 26 _ => false,
27 } 27 }
28} 28}
29
30impl<'p> parser::Parser<'p> {
31 fn at(&self, kind: SyntaxKind) -> bool {
32 self.current() == kind
33 }
34
35 fn err_and_bump(&mut self, message: &str) {
36 let err = self.start();
37 self.error(message);
38 self.bump();
39 err.complete(self, ERROR);
40 }
41
42 fn expect(&mut self, kind: SyntaxKind) -> bool {
43 if self.at(kind) {
44 self.bump();
45 true
46 } else {
47 self.error(format!("expected {:?}", kind));
48 false
49 }
50 }
51
52 fn eat(&mut self, kind: SyntaxKind) -> bool {
53 self.at(kind) && {
54 self.bump();
55 true
56 }
57 }
58}