aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/builder.rs
blob: 70869952c3aeba70334dac0920a5ff421d6247be (plain)
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
use {
    parser_impl::Sink,
    yellow::{GreenNode, SyntaxError, SyntaxNode, SyntaxRoot},
    SyntaxKind, TextRange, TextUnit,
};

pub(crate) struct GreenBuilder<'a> {
    text: &'a str,
    parents: Vec<(SyntaxKind, usize)>,
    children: Vec<GreenNode>,
    pos: TextUnit,
    errors: Vec<SyntaxError>,
}

impl<'a> Sink<'a> for GreenBuilder<'a> {
    type Tree = SyntaxNode;

    fn new(text: &'a str) -> Self {
        GreenBuilder {
            text,
            parents: Vec::new(),
            children: Vec::new(),
            pos: 0.into(),
            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];
        self.children.push(
            GreenNode::new_leaf(kind, text)
        );
    }

    fn start_internal(&mut self, kind: SyntaxKind) {
        let len = self.children.len();
        self.parents.push((kind, len));
    }

    fn finish_internal(&mut self) {
        let (kind, first_child) = self.parents.pop().unwrap();
        let children = self.children
            .drain(first_child..)
            .collect();
        self.children.push(
            GreenNode::new_branch(kind, children)
        );
    }

    fn error(&mut self, message: String) {
        self.errors.push(SyntaxError {
            message,
            offset: self.pos,
        })
    }

    fn finish(mut self) -> SyntaxNode {
        assert_eq!(self.children.len(), 1);
        let root = self.children.pop().unwrap();
        let root = SyntaxRoot::new(root, self.errors);
        SyntaxNode::new_owned(root)
    }
}