aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-29 11:51:55 +0100
committerAleksey Kladov <[email protected]>2018-07-29 11:51:55 +0100
commitc12450fb4e30c3418555e47d045bb9fd4318a10a (patch)
treee2dc508e1e415388392657cda3dfb00175cdabf2 /src/parser
parent8d9961b75377a7bd2656b5aa1451710de8c86f60 (diff)
Introduce red-green syntax tree
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event.rs8
-rw-r--r--src/parser/mod.rs29
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 @@
1use { 1use {
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};
5use super::is_insignificant; 5use super::is_insignificant;
@@ -69,6 +69,11 @@ pub(crate) enum Event {
69 69
70pub(super) fn to_file(text: String, tokens: &[Token], events: Vec<Event>) -> File { 70pub(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
76pub(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 @@
1use {File, SyntaxKind, Token};
2
3use syntax_kinds::*;
4
5#[macro_use] 1#[macro_use]
6mod token_set; 2mod token_set;
7mod parser; 3mod parser;
@@ -9,6 +5,16 @@ mod input;
9mod event; 5mod event;
10mod grammar; 6mod grammar;
11 7
8use std::sync::Arc;
9use {
10 File, SyntaxKind, Token,
11 yellow::SyntaxNode,
12 syntax_kinds::*
13};
14use GreenBuilder;
15use 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
13pub fn parse(text: String, tokens: &[Token]) -> File { 19pub 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
31pub 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
24fn is_insignificant(kind: SyntaxKind) -> bool { 45fn is_insignificant(kind: SyntaxKind) -> bool {
25 match kind { 46 match kind {
26 WHITESPACE | COMMENT => true, 47 WHITESPACE | COMMENT => true,