aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/builder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/yellow/builder.rs')
-rw-r--r--src/yellow/builder.rs24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/yellow/builder.rs b/src/yellow/builder.rs
index 346d561cd..73b4b5ff9 100644
--- a/src/yellow/builder.rs
+++ b/src/yellow/builder.rs
@@ -1,13 +1,12 @@
1use std::sync::Arc;
2use { 1use {
3 SyntaxKind, TextRange, TextUnit, 2 SyntaxKind, TextRange, TextUnit,
4 yellow::{SyntaxNode, GreenNode, SyntaxError}, 3 yellow::{SyntaxNode, GreenNode, GreenNodeBuilder, SyntaxError},
5 parser::Sink 4 parser::Sink
6}; 5};
7 6
8pub(crate) struct GreenBuilder { 7pub(crate) struct GreenBuilder {
9 text: String, 8 text: String,
10 stack: Vec<GreenNode>, 9 stack: Vec<GreenNodeBuilder>,
11 pos: TextUnit, 10 pos: TextUnit,
12 root: Option<GreenNode>, 11 root: Option<GreenNode>,
13 errors: Vec<SyntaxError>, 12 errors: Vec<SyntaxError>,
@@ -33,24 +32,21 @@ impl Sink for GreenBuilder {
33 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { 32 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) {
34 let range = TextRange::offset_len(self.pos, len); 33 let range = TextRange::offset_len(self.pos, len);
35 self.pos += len; 34 self.pos += len;
36 let text = self.text[range].to_owned(); 35 let text = &self.text[range];
36 let leaf = GreenNodeBuilder::new_leaf(kind, text);
37 let parent = self.stack.last_mut().unwrap(); 37 let parent = self.stack.last_mut().unwrap();
38 if kind.is_trivia() { 38 parent.push_child(leaf)
39 parent.push_trivia(kind, text);
40 } else {
41 let node = GreenNode::new_leaf(kind, text);
42 parent.push_child(Arc::new(node));
43 }
44 } 39 }
45 40
46 fn start_internal(&mut self, kind: SyntaxKind) { 41 fn start_internal(&mut self, kind: SyntaxKind) {
47 self.stack.push(GreenNode::new_branch(kind)) 42 self.stack.push(GreenNodeBuilder::new_internal(kind))
48 } 43 }
49 44
50 fn finish_internal(&mut self) { 45 fn finish_internal(&mut self) {
51 let node = self.stack.pop().unwrap(); 46 let builder = self.stack.pop().unwrap();
47 let node = builder.build();
52 if let Some(parent) = self.stack.last_mut() { 48 if let Some(parent) = self.stack.last_mut() {
53 parent.push_child(Arc::new(node)) 49 parent.push_child(node);
54 } else { 50 } else {
55 self.root = Some(node); 51 self.root = Some(node);
56 } 52 }
@@ -61,7 +57,7 @@ impl Sink for GreenBuilder {
61 } 57 }
62 58
63 fn finish(self) -> SyntaxNode { 59 fn finish(self) -> SyntaxNode {
64 SyntaxNode::new(Arc::new(self.root.unwrap()), self.errors) 60 SyntaxNode::new(self.root.unwrap(), self.errors)
65 } 61 }
66} 62}
67 63