aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow/builder.rs
blob: e4ab37899d575c14f2649ad279ccdb795417d6ed (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
use {
    parser_impl::Sink,
    yellow::{GreenNode, SyntaxError},
    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 = (GreenNode, Vec<SyntaxError>);

    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: Vec<_> = self.children
            .drain(first_child..)
            .collect();
        self.children.push(
            GreenNode::new_branch(kind, children.into_boxed_slice())
        );
    }

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

    fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) {
        assert_eq!(self.children.len(), 1);
        let root = self.children.pop().unwrap();
        (root, self.errors)
    }
}