diff options
Diffstat (limited to 'src/parser/event_parser/parser.rs')
-rw-r--r-- | src/parser/event_parser/parser.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs new file mode 100644 index 000000000..9592b90c9 --- /dev/null +++ b/src/parser/event_parser/parser.rs | |||
@@ -0,0 +1,52 @@ | |||
1 | use {Token, SyntaxKind, TextUnit}; | ||
2 | use super::Event; | ||
3 | use syntax_kinds::{WHITESPACE, COMMENT}; | ||
4 | |||
5 | pub struct Parser<'t> { | ||
6 | text: &'t str, | ||
7 | raw_tokens: &'t [Token], | ||
8 | non_ws_tokens: Vec<(usize, TextUnit)>, | ||
9 | |||
10 | pos: usize, | ||
11 | events: Vec<Event>, | ||
12 | } | ||
13 | |||
14 | impl<'t> Parser<'t> { | ||
15 | pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { | ||
16 | let mut non_ws_tokens = Vec::new(); | ||
17 | let mut len = TextUnit::new(0); | ||
18 | for (idx, &token) in raw_tokens.iter().enumerate() { | ||
19 | match token.kind { | ||
20 | WHITESPACE | COMMENT => (), | ||
21 | _ => non_ws_tokens.push((idx, len)), | ||
22 | } | ||
23 | len += token.len; | ||
24 | } | ||
25 | |||
26 | Parser { | ||
27 | text, | ||
28 | raw_tokens, | ||
29 | non_ws_tokens, | ||
30 | |||
31 | pos: 0, | ||
32 | events: Vec::new(), | ||
33 | } | ||
34 | } | ||
35 | |||
36 | pub(crate) fn into_events(self) -> Vec<Event> { | ||
37 | assert!(self.pos == self.non_ws_tokens.len()); | ||
38 | self.events | ||
39 | } | ||
40 | |||
41 | pub(crate) fn start(&mut self, kind: SyntaxKind) { | ||
42 | self.event(Event::Start { kind }); | ||
43 | } | ||
44 | |||
45 | pub(crate) fn finish(&mut self) { | ||
46 | self.event(Event::Finish); | ||
47 | } | ||
48 | |||
49 | fn event(&mut self, event: Event) { | ||
50 | self.events.push(event) | ||
51 | } | ||
52 | } \ No newline at end of file | ||