aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-01 12:10:56 +0000
committerAleksey Kladov <[email protected]>2018-01-01 12:10:56 +0000
commite24cadb490ee3d912a500d8ae4f1455ece0d5e68 (patch)
treeb54ae4b2aacd58597df43de60ef4c30029a6e54b /src/parser
parent9ed73378ce3343237bae39f6f9837aa18acbc9e7 (diff)
Parser: more groundwork
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event_parser.rs66
-rw-r--r--src/parser/mod.rs21
2 files changed, 83 insertions, 4 deletions
diff --git a/src/parser/event_parser.rs b/src/parser/event_parser.rs
new file mode 100644
index 000000000..c6aacfefb
--- /dev/null
+++ b/src/parser/event_parser.rs
@@ -0,0 +1,66 @@
1use {Token, TextUnit, SyntaxKind};
2
3use syntax_kinds::*;
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,
23 raw_tokens: &'t [Token],
24 non_ws_tokens: Vec<(usize, TextUnit)>,
25
26 pos: usize,
27 events: Vec<Event>,
28}
29
30impl<'t> Parser<'t> {
31 fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> {
32 let mut non_ws_tokens = Vec::new();
33 let mut len = TextUnit::new(0);
34 for (idx, &token) in raw_tokens.iter().enumerate() {
35 match token.kind {
36 WHITESPACE | COMMENT => (),
37 _ => non_ws_tokens.push((idx, len)),
38 }
39 len += token.len;
40 }
41
42 Parser {
43 text,
44 raw_tokens,
45 non_ws_tokens,
46
47 pos: 0,
48 events: Vec::new(),
49 }
50 }
51
52 fn start(&mut self, kind: SyntaxKind) {
53 self.event(Event::Start { kind });
54 }
55 fn finish(&mut self) {
56 self.event(Event::Finish);
57 }
58 fn event(&mut self, event: Event) {
59 self.events.push(event)
60 }
61}
62
63fn parse_file(p: &mut Parser) {
64 p.start(FILE);
65 p.finish();
66} \ No newline at end of file
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index 0a136266c..ccccd78f9 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -1,11 +1,24 @@
1use {Token, File, FileBuilder, Sink}; 1use {Token, File, FileBuilder, Sink, SyntaxKind};
2 2
3use syntax_kinds::*; 3use syntax_kinds::*;
4 4
5mod event_parser;
6use self::event_parser::Event;
7
5 8
6pub fn parse(text: String, tokens: &[Token]) -> File { 9pub fn parse(text: String, tokens: &[Token]) -> File {
10 let events = event_parser::parse(&text, tokens);
11 from_events_to_file(text, events)
12}
13
14fn from_events_to_file(text: String, events: Vec<Event>) -> File {
7 let mut builder = FileBuilder::new(text); 15 let mut builder = FileBuilder::new(text);
8 builder.start_internal(FILE); 16 for event in events {
9 builder.finish_internal(); 17 match event {
18 Event::Start { kind } => builder.start_internal(kind),
19 Event::Finish => builder.finish_internal(),
20 Event::Token { .. } => unimplemented!(),
21 }
22 }
10 builder.finish() 23 builder.finish()
11} \ No newline at end of file 24}