diff options
author | Aleksey Kladov <[email protected]> | 2018-01-01 13:05:46 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-01-01 13:05:46 +0000 |
commit | 4cda3255309e7ed9c9d9094e12a8d91bdc1554ab (patch) | |
tree | e82fd275d4b019397c4f8d559c4036fbd80e01d9 | |
parent | e24cadb490ee3d912a500d8ae4f1455ece0d5e68 (diff) |
Parser: even more groundwork
-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 (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 @@ | |||
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.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 @@ | |||
1 | use {Token, TextUnit, SyntaxKind}; | 1 | use {Token, SyntaxKind, TextUnit}; |
2 | use super::Event; | ||
3 | use syntax_kinds::{WHITESPACE, COMMENT}; | ||
2 | 4 | ||
3 | use syntax_kinds::*; | 5 | pub struct Parser<'t> { |
4 | |||
5 | |||
6 | pub(crate) enum Event { | ||
7 | Start { kind: SyntaxKind }, | ||
8 | Finish, | ||
9 | Token { | ||
10 | kind: SyntaxKind, | ||
11 | n_raw_tokens: u8, | ||
12 | } | ||
13 | } | ||
14 | |||
15 | pub(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 | |||
21 | struct 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 | ||
30 | impl<'t> Parser<'t> { | 14 | impl<'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 | |||
63 | fn 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 |