aboutsummaryrefslogtreecommitdiff
path: root/src/parser/event_parser
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/event_parser')
-rw-r--r--src/parser/event_parser/grammar/items.rs22
-rw-r--r--src/parser/event_parser/grammar/mod.rs28
-rw-r--r--src/parser/event_parser/grammar/paths.rs2
-rw-r--r--src/parser/event_parser/parser.rs52
4 files changed, 30 insertions, 74 deletions
diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs
index 0638e3093..7fed5e83b 100644
--- a/src/parser/event_parser/grammar/items.rs
+++ b/src/parser/event_parser/grammar/items.rs
@@ -14,7 +14,7 @@ fn item(p: &mut Parser) {
14 let item = p.start(); 14 let item = p.start();
15 attributes::outer_attributes(p); 15 attributes::outer_attributes(p);
16 visibility(p); 16 visibility(p);
17 let la = p.raw_lookahead(1); 17 let la = p.nth(1);
18 let item_kind = match p.current() { 18 let item_kind = match p.current() {
19 EXTERN_KW if la == CRATE_KW => { 19 EXTERN_KW if la == CRATE_KW => {
20 extern_crate_item(p); 20 extern_crate_item(p);
@@ -171,7 +171,7 @@ fn use_item(p: &mut Parser) {
171 p.expect(SEMI); 171 p.expect(SEMI);
172 172
173 fn use_tree(p: &mut Parser) { 173 fn use_tree(p: &mut Parser) {
174 let la = p.raw_lookahead(1); 174 let la = p.nth(1);
175 let m = p.start(); 175 let m = p.start();
176 match (p.current(), la) { 176 match (p.current(), la) {
177 (STAR, _) => { 177 (STAR, _) => {
@@ -235,5 +235,21 @@ fn fn_item(p: &mut Parser) {
235 assert!(p.at(FN_KW)); 235 assert!(p.at(FN_KW));
236 p.bump(); 236 p.bump();
237 237
238 p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN) && p.curly_block(|_| ()); 238 p.expect(IDENT);
239 if p.at(L_PAREN) {
240 fn_value_parameters(p);
241 } else {
242 p.error().message("expected function arguments").emit();
243 }
244
245 if p.at(L_CURLY) {
246 p.expect(L_CURLY);
247 p.expect(R_CURLY);
248 }
249
250 fn fn_value_parameters(p: &mut Parser) {
251 assert!(p.at(L_PAREN));
252 p.bump();
253 p.expect(R_PAREN);
254 }
239} 255}
diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs
index b87f3ca8a..931193b5f 100644
--- a/src/parser/event_parser/grammar/mod.rs
+++ b/src/parser/event_parser/grammar/mod.rs
@@ -20,7 +20,7 @@ fn visibility(p: &mut Parser) {
20 let vis = p.start(); 20 let vis = p.start();
21 p.bump(); 21 p.bump();
22 if p.at(L_PAREN) { 22 if p.at(L_PAREN) {
23 match p.raw_lookahead(1) { 23 match p.nth(1) {
24 CRATE_KW | SELF_KW | SUPER_KW | IN_KW => { 24 CRATE_KW | SELF_KW | SUPER_KW | IN_KW => {
25 p.bump(); 25 p.bump();
26 if p.bump() == IN_KW { 26 if p.bump() == IN_KW {
@@ -57,7 +57,7 @@ impl<'p> Parser<'p> {
57 err.complete(self, ERROR); 57 err.complete(self, ERROR);
58 } 58 }
59 59
60 pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool { 60 fn expect(&mut self, kind: SyntaxKind) -> bool {
61 if self.at(kind) { 61 if self.at(kind) {
62 self.bump(); 62 self.bump();
63 true 63 true
@@ -77,39 +77,23 @@ impl<'p> Parser<'p> {
77 77
78trait Lookahead: Copy { 78trait Lookahead: Copy {
79 fn is_ahead(self, p: &Parser) -> bool; 79 fn is_ahead(self, p: &Parser) -> bool;
80 fn consume(p: &mut Parser);
81} 80}
82 81
83impl Lookahead for SyntaxKind { 82impl Lookahead for SyntaxKind {
84 fn is_ahead(self, p: &Parser) -> bool { 83 fn is_ahead(self, p: &Parser) -> bool {
85 p.current() == self 84 p.current() == self
86 } 85 }
87
88 fn consume(p: &mut Parser) {
89 p.bump();
90 }
91} 86}
92 87
93impl Lookahead for [SyntaxKind; 2] { 88impl Lookahead for [SyntaxKind; 2] {
94 fn is_ahead(self, p: &Parser) -> bool { 89 fn is_ahead(self, p: &Parser) -> bool {
95 p.current() == self[0] && p.raw_lookahead(1) == self[1] 90 p.current() == self[0] && p.nth(1) == self[1]
96 }
97
98 fn consume(p: &mut Parser) {
99 p.bump();
100 p.bump();
101 } 91 }
102} 92}
103 93
104impl Lookahead for [SyntaxKind; 3] { 94impl Lookahead for [SyntaxKind; 3] {
105 fn is_ahead(self, p: &Parser) -> bool { 95 fn is_ahead(self, p: &Parser) -> bool {
106 p.current() == self[0] && p.raw_lookahead(1) == self[1] && p.raw_lookahead(2) == self[2] 96 p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2]
107 }
108
109 fn consume(p: &mut Parser) {
110 p.bump();
111 p.bump();
112 p.bump();
113 } 97 }
114} 98}
115 99
@@ -121,8 +105,4 @@ impl<'a> Lookahead for AnyOf<'a> {
121 let curr = p.current(); 105 let curr = p.current();
122 self.0.iter().any(|&k| k == curr) 106 self.0.iter().any(|&k| k == curr)
123 } 107 }
124
125 fn consume(p: &mut Parser) {
126 p.bump();
127 }
128} 108}
diff --git a/src/parser/event_parser/grammar/paths.rs b/src/parser/event_parser/grammar/paths.rs
index 4e028073a..6c8a89f6c 100644
--- a/src/parser/event_parser/grammar/paths.rs
+++ b/src/parser/event_parser/grammar/paths.rs
@@ -12,7 +12,7 @@ pub(crate) fn use_path(p: &mut Parser) {
12 path_segment(p, true); 12 path_segment(p, true);
13 let mut qual = path.complete(p, PATH); 13 let mut qual = path.complete(p, PATH);
14 loop { 14 loop {
15 if p.at(COLONCOLON) && !items::is_use_tree_start(p.raw_lookahead(1)) { 15 if p.at(COLONCOLON) && !items::is_use_tree_start(p.nth(1)) {
16 let path = qual.precede(p); 16 let path = qual.precede(p);
17 p.bump(); 17 p.bump();
18 path_segment(p, false); 18 path_segment(p, false);
diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs
index 573e3d73a..a15d0b633 100644
--- a/src/parser/event_parser/parser.rs
+++ b/src/parser/event_parser/parser.rs
@@ -1,7 +1,7 @@
1use {SyntaxKind, TextUnit, Token}; 1use {SyntaxKind, TextUnit, Token};
2use super::Event; 2use super::Event;
3use super::super::is_insignificant; 3use super::super::is_insignificant;
4use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE}; 4use SyntaxKind::{EOF, TOMBSTONE};
5 5
6pub(crate) struct Marker { 6pub(crate) struct Marker {
7 pos: u32, 7 pos: u32,
@@ -106,9 +106,6 @@ pub(crate) struct Parser<'t> {
106 106
107 pos: usize, 107 pos: usize,
108 events: Vec<Event>, 108 events: Vec<Event>,
109
110 curly_level: i32,
111 curly_limit: Option<i32>,
112} 109}
113 110
114impl<'t> Parser<'t> { 111impl<'t> Parser<'t> {
@@ -131,30 +128,14 @@ impl<'t> Parser<'t> {
131 128
132 pos: 0, 129 pos: 0,
133 events: Vec::new(), 130 events: Vec::new(),
134 curly_level: 0,
135 curly_limit: None,
136 } 131 }
137 } 132 }
138 133
139 pub(crate) fn into_events(self) -> Vec<Event> { 134 pub(crate) fn into_events(self) -> Vec<Event> {
140 assert!(self.curly_limit.is_none());
141 assert_eq!(self.current(), EOF); 135 assert_eq!(self.current(), EOF);
142 self.events 136 self.events
143 } 137 }
144 138
145 pub(crate) fn current(&self) -> SyntaxKind {
146 if self.pos == self.tokens.len() {
147 return EOF;
148 }
149 let token = self.tokens[self.pos];
150 if let Some(limit) = self.curly_limit {
151 if limit == self.curly_level && token.kind == R_CURLY {
152 return EOF;
153 }
154 }
155 token.kind
156 }
157
158 pub(crate) fn start(&mut self) -> Marker { 139 pub(crate) fn start(&mut self) -> Marker {
159 let m = Marker { 140 let m = Marker {
160 pos: self.events.len() as u32, 141 pos: self.events.len() as u32,
@@ -172,11 +153,8 @@ impl<'t> Parser<'t> {
172 153
173 pub(crate) fn bump(&mut self) -> SyntaxKind { 154 pub(crate) fn bump(&mut self) -> SyntaxKind {
174 let kind = self.current(); 155 let kind = self.current();
175 match kind { 156 if kind == EOF {
176 L_CURLY => self.curly_level += 1, 157 return EOF;
177 R_CURLY => self.curly_level -= 1,
178 EOF => return EOF,
179 _ => (),
180 } 158 }
181 self.pos += 1; 159 self.pos += 1;
182 self.event(Event::Token { 160 self.event(Event::Token {
@@ -186,30 +164,12 @@ impl<'t> Parser<'t> {
186 kind 164 kind
187 } 165 }
188 166
189 pub(crate) fn raw_lookahead(&self, n: usize) -> SyntaxKind { 167 pub(crate) fn nth(&self, n: usize) -> SyntaxKind {
190 self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF) 168 self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF)
191 } 169 }
192 170
193 pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool { 171 pub(crate) fn current(&self) -> SyntaxKind {
194 let old_level = self.curly_level; 172 self.nth(0)
195 let old_limit = self.curly_limit;
196 if !self.expect(L_CURLY) {
197 return false;
198 }
199 self.curly_limit = Some(self.curly_level);
200 f(self);
201 assert!(self.curly_level > old_level);
202 self.curly_limit = old_limit;
203 if !self.expect(R_CURLY) {
204 let err = self.start();
205 while self.curly_level > old_level {
206 if self.bump() == EOF {
207 break;
208 }
209 }
210 err.complete(self, ERROR);
211 }
212 true
213 } 173 }
214 174
215 fn event(&mut self, event: Event) { 175 fn event(&mut self, event: Event) {