aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow/builder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/yellow/builder.rs')
-rw-r--r--crates/ra_syntax/src/yellow/builder.rs32
1 files changed, 10 insertions, 22 deletions
diff --git a/crates/ra_syntax/src/yellow/builder.rs b/crates/ra_syntax/src/yellow/builder.rs
index e4ab37899..35dbaec05 100644
--- a/crates/ra_syntax/src/yellow/builder.rs
+++ b/crates/ra_syntax/src/yellow/builder.rs
@@ -1,15 +1,15 @@
1use rowan::GreenNodeBuilder;
1use { 2use {
2 parser_impl::Sink, 3 parser_impl::Sink,
3 yellow::{GreenNode, SyntaxError}, 4 yellow::{GreenNode, SyntaxError, RaTypes},
4 SyntaxKind, TextRange, TextUnit, 5 SyntaxKind, TextRange, TextUnit,
5}; 6};
6 7
7pub(crate) struct GreenBuilder<'a> { 8pub(crate) struct GreenBuilder<'a> {
8 text: &'a str, 9 text: &'a str,
9 parents: Vec<(SyntaxKind, usize)>,
10 children: Vec<GreenNode>,
11 pos: TextUnit, 10 pos: TextUnit,
12 errors: Vec<SyntaxError>, 11 errors: Vec<SyntaxError>,
12 inner: GreenNodeBuilder<RaTypes>,
13} 13}
14 14
15impl<'a> Sink<'a> for GreenBuilder<'a> { 15impl<'a> Sink<'a> for GreenBuilder<'a> {
@@ -18,35 +18,25 @@ impl<'a> Sink<'a> for GreenBuilder<'a> {
18 fn new(text: &'a str) -> Self { 18 fn new(text: &'a str) -> Self {
19 GreenBuilder { 19 GreenBuilder {
20 text, 20 text,
21 parents: Vec::new(),
22 children: Vec::new(),
23 pos: 0.into(), 21 pos: 0.into(),
24 errors: Vec::new(), 22 errors: Vec::new(),
23 inner: GreenNodeBuilder::new(),
25 } 24 }
26 } 25 }
27 26
28 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { 27 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) {
29 let range = TextRange::offset_len(self.pos, len); 28 let range = TextRange::offset_len(self.pos, len);
30 self.pos += len; 29 self.pos += len;
31 let text = &self.text[range]; 30 let text = self.text[range].into();
32 self.children.push( 31 self.inner.leaf(kind, text);
33 GreenNode::new_leaf(kind, text)
34 );
35 } 32 }
36 33
37 fn start_internal(&mut self, kind: SyntaxKind) { 34 fn start_internal(&mut self, kind: SyntaxKind) {
38 let len = self.children.len(); 35 self.inner.start_internal(kind)
39 self.parents.push((kind, len));
40 } 36 }
41 37
42 fn finish_internal(&mut self) { 38 fn finish_internal(&mut self) {
43 let (kind, first_child) = self.parents.pop().unwrap(); 39 self.inner.finish_internal();
44 let children: Vec<_> = self.children
45 .drain(first_child..)
46 .collect();
47 self.children.push(
48 GreenNode::new_branch(kind, children.into_boxed_slice())
49 );
50 } 40 }
51 41
52 fn error(&mut self, message: String) { 42 fn error(&mut self, message: String) {
@@ -56,9 +46,7 @@ impl<'a> Sink<'a> for GreenBuilder<'a> {
56 }) 46 })
57 } 47 }
58 48
59 fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) { 49 fn finish(self) -> (GreenNode, Vec<SyntaxError>) {
60 assert_eq!(self.children.len(), 1); 50 (self.inner.finish(), self.errors)
61 let root = self.children.pop().unwrap();
62 (root, self.errors)
63 } 51 }
64} 52}