aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/parser/event_parser/grammar.rs9
-rw-r--r--src/parser/event_parser/mod.rs20
-rw-r--r--src/parser/event_parser/parser.rs (renamed from src/parser/event_parser.rs)42
3 files changed, 43 insertions, 28 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 @@
1use super::Event;
2use super::parser::Parser;
3
4use syntax_kinds::*;
5
6pub 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 @@
1use {Token, TextUnit, SyntaxKind};
2
3use syntax_kinds::*;
4mod grammar;
5mod parser;
6
7pub(crate) enum Event {
8 Start { kind: SyntaxKind },
9 Finish,
10 Token {
11 kind: SyntaxKind,
12 n_raw_tokens: u8,
13 }
14}
15
16pub(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.rs b/src/parser/event_parser/parser.rs
index c6aacfefb..9592b90c9 100644
--- a/src/parser/event_parser.rs
+++ b/src/parser/event_parser/parser.rs
@@ -1,24 +1,8 @@
1use {Token, TextUnit, SyntaxKind}; 1use {Token, SyntaxKind, TextUnit};
2use super::Event;
3use syntax_kinds::{WHITESPACE, COMMENT};
2 4
3use syntax_kinds::*; 5pub struct Parser<'t> {
4
5
6pub(crate) enum Event {
7 Start { kind: SyntaxKind },
8 Finish,
9 Token {
10 kind: SyntaxKind,
11 n_raw_tokens: u8,
12 }
13}
14
15pub(crate) fn parse<'t>(text: &'t str, raw_tokens: &'t [Token]) -> Vec<Event> {
16 let mut parser = Parser::new(text, raw_tokens);
17 parse_file(&mut parser);
18 parser.events
19}
20
21struct Parser<'t> {
22 text: &'t str, 6 text: &'t str,
23 raw_tokens: &'t [Token], 7 raw_tokens: &'t [Token],
24 non_ws_tokens: Vec<(usize, TextUnit)>, 8 non_ws_tokens: Vec<(usize, TextUnit)>,
@@ -28,7 +12,7 @@ struct Parser<'t> {
28} 12}
29 13
30impl<'t> Parser<'t> { 14impl<'t> Parser<'t> {
31 fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { 15 pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> {
32 let mut non_ws_tokens = Vec::new(); 16 let mut non_ws_tokens = Vec::new();
33 let mut len = TextUnit::new(0); 17 let mut len = TextUnit::new(0);
34 for (idx, &token) in raw_tokens.iter().enumerate() { 18 for (idx, &token) in raw_tokens.iter().enumerate() {
@@ -49,18 +33,20 @@ impl<'t> Parser<'t> {
49 } 33 }
50 } 34 }
51 35
52 fn start(&mut self, kind: SyntaxKind) { 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) {
53 self.event(Event::Start { kind }); 42 self.event(Event::Start { kind });
54 } 43 }
55 fn finish(&mut self) { 44
45 pub(crate) fn finish(&mut self) {
56 self.event(Event::Finish); 46 self.event(Event::Finish);
57 } 47 }
48
58 fn event(&mut self, event: Event) { 49 fn event(&mut self, event: Event) {
59 self.events.push(event) 50 self.events.push(event)
60 } 51 }
61}
62
63fn parse_file(p: &mut Parser) {
64 p.start(FILE);
65 p.finish();
66} \ No newline at end of file 52} \ No newline at end of file