diff options
Diffstat (limited to 'src/parser/event_parser')
-rw-r--r-- | src/parser/event_parser/grammar.rs | 9 | ||||
-rw-r--r-- | src/parser/event_parser/mod.rs | 20 | ||||
-rw-r--r-- | src/parser/event_parser/parser.rs | 52 |
3 files changed, 81 insertions, 0 deletions
diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs new file mode 100644 index 000000000..dd09061aa --- /dev/null +++ b/src/parser/event_parser/grammar.rs | |||
@@ -0,0 +1,9 @@ | |||
1 | use super::Event; | ||
2 | use super::parser::Parser; | ||
3 | |||
4 | use syntax_kinds::*; | ||
5 | |||
6 | pub fn parse_file(p: &mut Parser) { | ||
7 | p.start(FILE); | ||
8 | p.finish(); | ||
9 | } \ No newline at end of file | ||
diff --git a/src/parser/event_parser/mod.rs b/src/parser/event_parser/mod.rs new file mode 100644 index 000000000..14107720a --- /dev/null +++ b/src/parser/event_parser/mod.rs | |||
@@ -0,0 +1,20 @@ | |||
1 | use {Token, TextUnit, SyntaxKind}; | ||
2 | |||
3 | use syntax_kinds::*; | ||
4 | mod grammar; | ||
5 | mod parser; | ||
6 | |||
7 | pub(crate) enum Event { | ||
8 | Start { kind: SyntaxKind }, | ||
9 | Finish, | ||
10 | Token { | ||
11 | kind: SyntaxKind, | ||
12 | n_raw_tokens: u8, | ||
13 | } | ||
14 | } | ||
15 | |||
16 | pub(crate) fn parse<'t>(text: &'t str, raw_tokens: &'t [Token]) -> Vec<Event> { | ||
17 | let mut parser = parser::Parser::new(text, raw_tokens); | ||
18 | grammar::parse_file(&mut parser); | ||
19 | parser.into_events() | ||
20 | } \ No newline at end of file | ||
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 | ||