aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/green.rs
blob: dafe1bb220760a8823969800fd917fa6a21f6684 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::sync::Arc;
use text_unit::TextUnit;
use SyntaxKind;

type TokenText = String;

#[derive(Debug)]
pub(crate) struct GreenNode {
    kind: SyntaxKind,
    data: GreenNodeData,
}

impl GreenNode {
    pub(crate) fn new_leaf(kind: SyntaxKind, text: TokenText) -> GreenNode {
        GreenNode {
            kind,
            data: GreenNodeData::Leaf(GreenLeaf { text }),
        }
    }

    pub(crate) fn new_branch(
        kind: SyntaxKind,
    ) -> GreenNode {
        let branch = GreenBranch {
            text_len: 0.into(),
            leading_trivia: Trivias::default(),
            children: Vec::new(),
        };
        GreenNode {
            kind,
            data: GreenNodeData::Branch(branch),
        }
    }

    pub(crate) fn push_trivia(&mut self, kind: SyntaxKind, text: TokenText) {
        let branch = match &mut self.data {
            GreenNodeData::Branch(branch) => branch,
            _ => panic!()
        };
        branch.text_len += TextUnit::of_str(&text);
        let leading = &mut branch.leading_trivia;
        branch.children.last_mut().map(|(_, t)| t).unwrap_or(leading)
            .push(Arc::new(GreenTrivia { kind, text }));
    }

    pub(crate) fn push_child(&mut self, node: Arc<GreenNode>) {
        let branch = match &mut self.data {
            GreenNodeData::Branch(branch) => branch,
            _ => panic!()
        };
        branch.text_len += node.text_len();
        branch.children.push((node, Trivias::default()));
    }

    pub(crate) fn kind(&self) -> SyntaxKind {
        self.kind
    }

    pub(crate) fn text_len(&self) -> TextUnit {
        match &self.data {
            GreenNodeData::Leaf(l) => l.text_len(),
            GreenNodeData::Branch(b) => b.text_len(),
        }
    }

    pub(crate) fn text(&self) -> String {
        let mut buff = String::new();
        go(self, &mut buff);
        return buff;
        fn go(node: &GreenNode, buff: &mut String) {
            match &node.data {
                GreenNodeData::Leaf(l) => buff.push_str(&l.text),
                GreenNodeData::Branch(branch) => {
                    add_trivia(&branch.leading_trivia, buff);
                    branch.children.iter().for_each(|(child, trivias)| {
                        go(child, buff);
                        add_trivia(trivias, buff);
                    })
                }
            }
        }

        fn add_trivia(trivias: &Trivias, buff: &mut String) {
            trivias.iter().for_each(|t| buff.push_str(&t.text))
        }
    }

    pub(crate) fn n_children(&self) -> usize {
        match &self.data {
            GreenNodeData::Leaf(_) => 0,
            GreenNodeData::Branch(branch) => branch.children.len(),
        }
    }

    pub(crate) fn nth_child(&self, idx: usize) -> &Arc<GreenNode> {
        match &self.data {
            GreenNodeData::Leaf(_) => panic!("leaf nodes have no children"),
            GreenNodeData::Branch(branch) => &branch.children[idx].0,
        }
    }

    pub(crate) fn nth_trivias(&self, idx: usize) -> &Trivias {
        match &self.data {
            GreenNodeData::Leaf(_) => panic!("leaf nodes have no children"),
            GreenNodeData::Branch(branch) => if idx == 0 {
                &branch.leading_trivia
            } else {
                &branch.children[idx - 1].1
            },
        }
    }

    pub(crate) fn is_leaf(&self) -> bool {
        match self.data {
            GreenNodeData::Leaf(_) => true,
            GreenNodeData::Branch(_) => false
        }
    }
}

#[derive(Debug)]
enum GreenNodeData {
    Leaf(GreenLeaf),
    Branch(GreenBranch),
}

#[derive(Debug)]
struct GreenLeaf {
    text: TokenText
}

#[derive(Debug)]
struct GreenBranch {
    text_len: TextUnit,
    leading_trivia: Trivias,
    children: Vec<(Arc<GreenNode>, Trivias)>,
}

#[derive(Debug)]
pub(crate) struct GreenTrivia {
    pub(crate) kind: SyntaxKind,
    pub(crate) text: TokenText,
}

type Trivias = Vec<Arc<GreenTrivia>>;


pub(crate) trait TextLen {
    fn text_len(&self) -> TextUnit;
}

impl TextLen for GreenTrivia {
    fn text_len(&self) -> TextUnit {
        TextUnit::of_str(&self.text)
    }
}

impl<T: TextLen> TextLen for Arc<T> {
    fn text_len(&self) -> TextUnit {
        let this: &T = self;
        this.text_len()
    }
}

impl TextLen for GreenNode {
    fn text_len(&self) -> TextUnit {
        self.text_len()
    }
}

impl TextLen for GreenLeaf {
    fn text_len(&self) -> TextUnit {
        TextUnit::of_str(&self.text)
    }
}

impl TextLen for GreenBranch {
    fn text_len(&self) -> TextUnit {
        self.text_len
    }
}

impl<T: TextLen> TextLen for [T] {
    fn text_len(&self) -> TextUnit {
        self.iter().map(TextLen::text_len).sum()
    }
}