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
|
use std::sync::Arc;
use {
smol_str::SmolStr,
SyntaxKind::{self, *},
TextUnit,
};
#[derive(Clone, Debug)]
pub(crate) enum GreenNode {
Leaf(GreenLeaf),
Branch(Arc<GreenBranch>),
}
impl GreenNode {
pub fn kind(&self) -> SyntaxKind {
match self {
GreenNode::Leaf(l) => l.kind(),
GreenNode::Branch(b) => b.kind(),
}
}
pub fn text_len(&self) -> TextUnit {
match self {
GreenNode::Leaf(l) => l.text_len(),
GreenNode::Branch(b) => b.text_len(),
}
}
pub fn children(&self) -> &[GreenNode] {
match self {
GreenNode::Leaf(_) => &[],
GreenNode::Branch(b) => b.children(),
}
}
pub fn text(&self) -> String {
let mut buff = String::new();
go(self, &mut buff);
return buff;
fn go(node: &GreenNode, buff: &mut String) {
match node {
GreenNode::Leaf(l) => buff.push_str(&l.text()),
GreenNode::Branch(b) => b.children().iter().for_each(|child| go(child, buff)),
}
}
}
}
pub(crate) struct GreenNodeBuilder {
kind: SyntaxKind,
children: Vec<GreenNode>,
}
impl GreenNodeBuilder {
pub(crate) fn new_leaf(kind: SyntaxKind, text: &str) -> GreenNode {
GreenNode::Leaf(GreenLeaf::new(kind, text))
}
pub(crate) fn new_internal(kind: SyntaxKind) -> GreenNodeBuilder {
GreenNodeBuilder {
kind,
children: Vec::new(),
}
}
pub(crate) fn push_child(&mut self, node: GreenNode) {
self.children.push(node)
}
pub(crate) fn build(self) -> GreenNode {
let branch = GreenBranch::new(self.kind, self.children);
GreenNode::Branch(Arc::new(branch))
}
}
#[test]
fn assert_send_sync() {
fn f<T: Send + Sync>() {}
f::<GreenNode>();
}
#[derive(Clone, Debug)]
pub(crate) struct GreenBranch {
text_len: TextUnit,
kind: SyntaxKind,
children: Vec<GreenNode>,
}
impl GreenBranch {
fn new(kind: SyntaxKind, children: Vec<GreenNode>) -> GreenBranch {
let text_len = children.iter().map(|x| x.text_len()).sum::<TextUnit>();
GreenBranch {
text_len,
kind,
children,
}
}
pub fn kind(&self) -> SyntaxKind {
self.kind
}
pub fn text_len(&self) -> TextUnit {
self.text_len
}
pub fn children(&self) -> &[GreenNode] {
self.children.as_slice()
}
}
#[derive(Clone, Debug)]
pub(crate) enum GreenLeaf {
Whitespace {
newlines: u8,
spaces: u8,
},
Token {
kind: SyntaxKind,
text: Option<SmolStr>,
},
}
impl GreenLeaf {
fn new(kind: SyntaxKind, text: &str) -> Self {
if kind == WHITESPACE {
let newlines = text.bytes().take_while(|&b| b == b'\n').count();
let spaces = text[newlines..].bytes().take_while(|&b| b == b' ').count();
if newlines + spaces == text.len() && newlines <= N_NEWLINES && spaces <= N_SPACES {
return GreenLeaf::Whitespace {
newlines: newlines as u8,
spaces: spaces as u8,
};
}
}
let text = match SyntaxKind::static_text(kind) {
Some(t) => {
debug_assert_eq!(t, text);
None
}
None => Some(SmolStr::new(text)),
};
GreenLeaf::Token { kind, text }
}
pub(crate) fn kind(&self) -> SyntaxKind {
match self {
GreenLeaf::Whitespace { .. } => WHITESPACE,
GreenLeaf::Token { kind, .. } => *kind,
}
}
pub(crate) fn text(&self) -> &str {
match self {
&GreenLeaf::Whitespace { newlines, spaces } => {
let newlines = newlines as usize;
let spaces = spaces as usize;
assert!(newlines <= N_NEWLINES && spaces <= N_SPACES);
&WS[N_NEWLINES - newlines..N_NEWLINES + spaces]
}
GreenLeaf::Token { kind, text } => match text {
None => kind.static_text().unwrap(),
Some(t) => t.as_str(),
},
}
}
pub(crate) fn text_len(&self) -> TextUnit {
TextUnit::of_str(self.text())
}
}
const N_NEWLINES: usize = 16;
const N_SPACES: usize = 64;
const WS: &str =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ";
|