1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
use std::sync::Arc;
use {
SyntaxKind, TextRange, TextUnit,
yellow::{SyntaxNode, GreenNode, SyntaxError},
parser::Sink
};
pub(crate) struct GreenBuilder {
text: String,
stack: Vec<GreenNode>,
pos: TextUnit,
root: Option<GreenNode>,
errors: Vec<SyntaxError>,
}
impl GreenBuilder {
}
impl Sink for GreenBuilder {
type Tree = SyntaxNode;
fn new(text: String) -> Self {
GreenBuilder {
text,
stack: Vec::new(),
pos: 0.into(),
root: None,
errors: Vec::new(),
}
}
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) {
let range = TextRange::offset_len(self.pos, len);
self.pos += len;
let text = self.text[range].to_owned();
let parent = self.stack.last_mut().unwrap();
if kind.is_trivia() {
parent.push_trivia(kind, text);
} else {
let node = GreenNode::new_leaf(kind, text);
parent.push_child(Arc::new(node));
}
}
fn start_internal(&mut self, kind: SyntaxKind) {
self.stack.push(GreenNode::new_branch(kind))
}
fn finish_internal(&mut self) {
let node = self.stack.pop().unwrap();
if let Some(parent) = self.stack.last_mut() {
parent.push_child(Arc::new(node))
} else {
self.root = Some(node);
}
}
fn error(&mut self, message: String) {
self.errors.push(SyntaxError { message, offset: self.pos })
}
fn finish(self) -> SyntaxNode {
SyntaxNode::new(Arc::new(self.root.unwrap()), self.errors)
}
}
|