diff options
author | Aleksey Kladov <[email protected]> | 2018-07-29 11:51:55 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-29 11:51:55 +0100 |
commit | c12450fb4e30c3418555e47d045bb9fd4318a10a (patch) | |
tree | e2dc508e1e415388392657cda3dfb00175cdabf2 /src/parser | |
parent | 8d9961b75377a7bd2656b5aa1451710de8c86f60 (diff) |
Introduce red-green syntax tree
Diffstat (limited to 'src/parser')
-rw-r--r-- | src/parser/event.rs | 8 | ||||
-rw-r--r-- | src/parser/mod.rs | 29 |
2 files changed, 31 insertions, 6 deletions
diff --git a/src/parser/event.rs b/src/parser/event.rs index ac8a55de9..0fbfaeb9f 100644 --- a/src/parser/event.rs +++ b/src/parser/event.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use { | 1 | use { |
2 | ErrorMsg, File, FileBuilder, Sink, SyntaxKind, Token, | 2 | ErrorMsg, File, FileBuilder, Sink, SyntaxKind, Token, GreenBuilder, |
3 | syntax_kinds::TOMBSTONE, | 3 | syntax_kinds::TOMBSTONE, |
4 | }; | 4 | }; |
5 | use super::is_insignificant; | 5 | use super::is_insignificant; |
@@ -69,6 +69,11 @@ pub(crate) enum Event { | |||
69 | 69 | ||
70 | pub(super) fn to_file(text: String, tokens: &[Token], events: Vec<Event>) -> File { | 70 | pub(super) fn to_file(text: String, tokens: &[Token], events: Vec<Event>) -> File { |
71 | let mut builder = FileBuilder::new(text); | 71 | let mut builder = FileBuilder::new(text); |
72 | process(&mut builder, tokens, events); | ||
73 | builder.finish() | ||
74 | } | ||
75 | |||
76 | pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>) { | ||
72 | let mut idx = 0; | 77 | let mut idx = 0; |
73 | 78 | ||
74 | let mut holes = Vec::new(); | 79 | let mut holes = Vec::new(); |
@@ -145,5 +150,4 @@ pub(super) fn to_file(text: String, tokens: &[Token], events: Vec<Event>) -> Fil | |||
145 | &Event::Error { ref msg } => builder.error(ErrorMsg { msg: msg.clone() }), | 150 | &Event::Error { ref msg } => builder.error(ErrorMsg { msg: msg.clone() }), |
146 | } | 151 | } |
147 | } | 152 | } |
148 | builder.finish() | ||
149 | } | 153 | } |
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 3814837e1..26fbb6e3d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs | |||
@@ -1,7 +1,3 @@ | |||
1 | use {File, SyntaxKind, Token}; | ||
2 | |||
3 | use syntax_kinds::*; | ||
4 | |||
5 | #[macro_use] | 1 | #[macro_use] |
6 | mod token_set; | 2 | mod token_set; |
7 | mod parser; | 3 | mod parser; |
@@ -9,6 +5,16 @@ mod input; | |||
9 | mod event; | 5 | mod event; |
10 | mod grammar; | 6 | mod grammar; |
11 | 7 | ||
8 | use std::sync::Arc; | ||
9 | use { | ||
10 | File, SyntaxKind, Token, | ||
11 | yellow::SyntaxNode, | ||
12 | syntax_kinds::* | ||
13 | }; | ||
14 | use GreenBuilder; | ||
15 | use parser::event::process; | ||
16 | |||
17 | |||
12 | /// Parse a sequence of tokens into the representative node tree | 18 | /// Parse a sequence of tokens into the representative node tree |
13 | pub fn parse(text: String, tokens: &[Token]) -> File { | 19 | pub fn parse(text: String, tokens: &[Token]) -> File { |
14 | let events = { | 20 | let events = { |
@@ -21,6 +27,21 @@ pub fn parse(text: String, tokens: &[Token]) -> File { | |||
21 | event::to_file(text, tokens, events) | 27 | event::to_file(text, tokens, events) |
22 | } | 28 | } |
23 | 29 | ||
30 | /// Parse a sequence of tokens into the representative node tree | ||
31 | pub fn parse_green(text: String, tokens: &[Token]) -> SyntaxNode { | ||
32 | let events = { | ||
33 | let input = input::ParserInput::new(&text, tokens); | ||
34 | let parser_impl = parser::imp::ParserImpl::new(&input); | ||
35 | let mut parser = parser::Parser(parser_impl); | ||
36 | grammar::file(&mut parser); | ||
37 | parser.0.into_events() | ||
38 | }; | ||
39 | let mut builder = GreenBuilder::new(text); | ||
40 | process(&mut builder, tokens, events); | ||
41 | let (green, errors) = builder.finish(); | ||
42 | SyntaxNode::new(Arc::new(green), errors) | ||
43 | } | ||
44 | |||
24 | fn is_insignificant(kind: SyntaxKind) -> bool { | 45 | fn is_insignificant(kind: SyntaxKind) -> bool { |
25 | match kind { | 46 | match kind { |
26 | WHITESPACE | COMMENT => true, | 47 | WHITESPACE | COMMENT => true, |