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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::rc::Rc;
use std::borrow::Cow;
use std::collections::{HashSet, VecDeque, LinkedList};
use std::iter::FromIterator;
use std::vec;
use serde::{Serialize, Deserialize};
use crate::backend;
use crate::backend::data::*;
#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
pub struct CharIndex {
pub byte_index: usize,
pub char_index: usize,
}
impl CharIndex {
pub fn zero() -> Self {
CharIndex{
byte_index: 0,
char_index: 0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
pub struct CharRange {
pub start: CharIndex,
pub end: CharIndex,
}
impl CharRange {
pub fn byte_index_range<'a>(&self, source: &'a str) -> Option<(usize, usize)> {
fn find_utf8_end(s: &str, i: usize) -> Option<usize> {
s.char_indices().nth(i).map(|(_, x)| x.len_utf8())
}
let start_byte = self.start.byte_index;
let end_byte = self.end.byte_index;
let real_end_byte = source
.get(start_byte..=end_byte)
.map(|_| end_byte)
.or_else(|| {
let corrected_end = find_utf8_end(source, end_byte)?;
source
.get(start_byte..=corrected_end)
.map(|_| corrected_end)
});
real_end_byte.map(|l| (start_byte, l))
}
pub fn substrng<'a>(&self, source: &'a str) -> Option<&'a str> {
if let Some((start, end)) = self.byte_index_range(source) {
let sub_str = source.get(start..end).unwrap();
Some(sub_str)
} else {
None
}
}
pub fn into_annotated_tree<T>(self, data: T) -> Ann<T> {
Ann::from_range(self, data)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Ann<T> {
pub start: CharIndex,
pub end: CharIndex,
pub data: T,
}
impl<T> Ann<T> {
pub fn from_range(range: CharRange, data: T) -> Self {
Ann {
start: range.start,
end: range.end,
data,
}
}
pub fn into_char_range(&self) -> CharRange {
let start = self.start;
let end = self.end;
CharRange{start, end}
}
}
#[derive(Debug, Clone)]
pub enum Node<'a> {
Ident(Ann<Atom<'a>>),
Enclosure(Ann<Enclosure<'a, Node<'a>>>),
String(Ann<Atom<'a>>),
}
impl<'a> Node<'a> {
pub fn is_ident(&self) -> bool {
match self {
Node::Ident(_) => true,
_ => false,
}
}
pub fn is_enclosure(&self) -> bool {
match self {
Node::Enclosure(_) => true,
_ => false,
}
}
pub fn is_string(&self) -> bool {
match self {
Node::String(_) => true,
_ => false,
}
}
pub fn unwrap_ident<'b>(&'b self) -> Option<&'b Ann<Atom<'a>>> {
match self {
Node::Ident(x) => Some(x),
_ => None,
}
}
pub fn unwrap_enclosure<'b>(&'b self) -> Option<&'b Ann<Enclosure<'a, Node<'a>>>> {
match self {
Node::Enclosure(x) => Some(x),
_ => None,
}
}
pub fn unwrap_string<'b>(&'b self) -> Option<&'b Ann<Atom<'a>>> {
match self {
Node::String(x) => Some(x),
_ => None,
}
}
pub fn is_whitespace(&self) -> bool {
match self {
Node::String(txt) => {
let x: &str = &txt.data;
x.trim().is_empty()
},
_ => false
}
}
pub fn into_highlight_ranges(
self,
binder: Option<Atom<'a>>,
) -> Vec<Highlight<'a>> {
match self {
Node::Enclosure(node) => {
let is_fragment = node.data.kind == EnclosureKind::Fragment;
let range = node.into_char_range();
let kind = match node.data.kind {
EnclosureKind::CurlyBrace => HighlightKind::CurlyBrace,
EnclosureKind::SquareParen => HighlightKind::SquareParen,
EnclosureKind::Parens => HighlightKind::Parens,
EnclosureKind::Fragment => HighlightKind::Fragment,
EnclosureKind::Error{open, close} => HighlightKind::Error{
open: Cow::Borrowed(open),
close: Cow::Borrowed(close),
},
};
let mut last_ident: Option<Atom> = None;
let children = node.data.children
.into_iter()
.flat_map(|x| {
if x.is_ident() {
let ident = x.unwrap_ident().unwrap().clone();
last_ident = Some(ident.data);
}
if x.is_string() && !x.is_whitespace() {
last_ident = None;
}
x.into_highlight_ranges(last_ident.clone())
})
.collect::<Vec<_>>();
let highlight = Highlight {
kind,
range,
binder: binder.clone(),
};
if is_fragment {
children
} else {
let mut xs = vec![highlight];
xs.extend(children);
xs
}
}
Node::Ident(value) => {
let range = value.into_char_range();
let highlight = Highlight {
kind: HighlightKind::Ident(value.data),
range,
binder: binder.clone(),
};
vec![highlight]
}
Node::String(value) => Vec::new(),
}
}
pub fn new_fragment(nodes: Vec<Self>) -> Self {
Node::Enclosure(Ann{
start: CharIndex::zero(),
end: CharIndex::zero(),
data: Enclosure {
kind: EnclosureKind::Fragment,
children: nodes,
}
})
}
pub fn into_fragment(self) -> Vec<Self> {
match self {
Node::Enclosure(Ann{
start,
end,
data: Enclosure{
kind: EnclosureKind::Fragment,
children
}
}) => children,
x => vec![x]
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Highlight<'a> {
pub range: CharRange,
pub kind: HighlightKind<'a>,
pub binder: Option<Atom<'a>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum HighlightKind<'a> {
CurlyBrace,
SquareParen,
Parens,
Fragment,
Error {
open: Atom<'a>,
close: Atom<'a>,
},
Ident(Atom<'a>),
}