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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use super::parser;
use super::parser::Visibility;
use super::AccIndentation;
use super::Accumulator;
use super::GlobalData;
use super::Parents;
use super::TotalBytesGlobalData;
use super::WithByteRange;
use super::P;
use super::parser::Node as _;
use super::parser::TreeCursor as _;
use super::Accumulator as _;
use super::GlobalData as _;
/// Define a zipped visitor, where you mostly have to implement,
/// [`ZippedTraversal::pre`] going down,
/// and [`ZippedTraversal::post`] going up in the traversal.
pub trait ZippedTraversal
where
Self::Global: TotalBytesGlobalData,
{
type Global: TotalBytesGlobalData + GlobalData;
type Acc: AccIndentation + WithByteRange;
// # results
// type Node1;
type Stores;
// # source
type Text: ?Sized;
type Node<'a>: parser::Node<'a>;
type TreeCursor<'a>: parser::TreeCursor<'a, Self::Node<'a>> + std::fmt::Debug;
fn init_val(&mut self, text: &Self::Text, node: &Self::Node<'_>) -> Self::Acc;
/// Can be implemented if you want to skip certain nodes,
/// note that skipping only act on the "overlay" tree structure,
/// meaning that the content of a skipped node is fed to its parents
///
/// The default implementation skips nothing.
///
/// see also also the following example use:
/// [`hyperast_gen_ts_cpp::legion::CppTreeGen::pre_skippable`](../../hyperast_gen_ts_cpp/legion/struct.CppTreeGen.html#method.pre_skippable)
fn pre_skippable(
&mut self,
text: &Self::Text,
cursor: &Self::TreeCursor<'_>,
stack: &Parents<Self::Acc>,
global: &mut Self::Global,
) -> PreResult<Self::Acc> {
PreResult::Ok(self.pre(text, &cursor.node(), stack, global))
}
/// Called when going up
fn pre(
&mut self,
text: &Self::Text,
node: &Self::Node<'_>,
stack: &Parents<Self::Acc>,
global: &mut Self::Global,
) -> Self::Acc;
/// Called when going up
fn post(
&mut self,
parent: &mut Self::Acc,
global: &mut Self::Global,
text: &Self::Text,
acc: Self::Acc,
) -> <Self::Acc as Accumulator>::Node;
fn stores(&mut self) -> &mut Self::Stores;
fn gen(
&mut self,
text: &Self::Text,
stack: &mut Parents<Self::Acc>,
cursor: &mut Self::TreeCursor<'_>,
global: &mut Self::Global,
) {
let mut has = Has::Down;
loop {
if has != Has::Up
&& let Some(visibility) = cursor.goto_first_child_extended()
{
has = Has::Down;
global.down();
let n = self.pre_skippable(text, cursor, &stack, global);
match n {
PreResult::Skip => {
has = Has::Up;
global.up();
}
PreResult::Ignore => {
if let Visibility::Visible = visibility {
stack.push(P::ManualyHidden);
} else {
stack.push(P::BothHidden);
}
}
PreResult::SkipChildren(acc) => {
has = Has::Up;
if let Visibility::Visible = visibility {
stack.push(P::Visible(acc));
} else {
unimplemented!("Only concrete nodes should be leafs")
}
}
PreResult::Ok(acc) => {
global.set_sum_byte_length(acc.begin_byte());
if let Visibility::Visible = visibility {
stack.push(P::Visible(acc));
} else {
stack.push(P::Hidden(acc));
}
}
}
} else {
let is_visible;
let is_parent_hidden;
let full_node: Option<_> = match (stack.pop().unwrap(), stack.parent_mut_with_vis())
{
(P::Visible(acc), None) => {
global.up();
is_visible = true;
is_parent_hidden = false;
//global.set_sum_byte_length(acc.end_byte());
stack.push(P::Visible(acc));
None
}
(_, None) => {
panic!();
}
(P::ManualyHidden, Some((v, _))) => {
is_visible = false;
is_parent_hidden = v == Visibility::Hidden;
None
}
(P::BothHidden, Some((v, _))) => {
is_visible = false;
is_parent_hidden = v == Visibility::Hidden;
None
}
(P::Visible(acc), Some((v, parent))) => {
is_visible = true;
is_parent_hidden = v == Visibility::Hidden;
if !acc.has_children() {
global.set_sum_byte_length(acc.end_byte());
}
if is_parent_hidden && parent.end_byte() <= acc.begin_byte() {
panic!()
}
global.up();
let full_node = self.post(parent, global, text, acc);
Some(full_node)
}
(P::Hidden(acc), Some((v, parent))) => {
is_visible = false;
is_parent_hidden = v == Visibility::Hidden;
if !acc.has_children() {
global.set_sum_byte_length(acc.end_byte());
}
if is_parent_hidden && parent.end_byte() < acc.begin_byte() {
panic!("{} {}", parent.end_byte(), acc.begin_byte());
} else if is_parent_hidden && parent.end_byte() == acc.begin_byte() {
log::error!("{} {}", parent.end_byte(), acc.begin_byte());
assert!(!acc.has_children());
global.up();
None
} else {
global.up();
let full_node = self.post(parent, global, text, acc);
Some(full_node)
}
}
};
// TODO opt out of using end_byte other than on leafs,
// it should help with trailing spaces,
// something like `cursor.node().child_count().ne(0).then(||cursor.node().end_byte())` then just call set_sum_byte_length if some
if let Some(visibility) = cursor.goto_next_sibling_extended() {
has = Has::Right;
let parent = stack.parent_mut().unwrap();
if let Some(full_node) = full_node {
parent.push(full_node);
}
loop {
let parent = stack.parent_mut().unwrap();
if parent.end_byte() <= cursor.node().start_byte() {
loop {
let p = stack.pop().unwrap();
match p {
P::ManualyHidden => (),
P::BothHidden => (),
P::Hidden(acc) => {
let parent = stack.parent_mut().unwrap();
let full_node = self.post(parent, global, text, acc);
parent.push(full_node);
break;
}
P::Visible(acc) => {
let parent = stack.parent_mut().unwrap();
let full_node = self.post(parent, global, text, acc);
parent.push(full_node);
break;
}
}
}
} else {
break;
}
}
global.down();
let n = self.pre_skippable(text, cursor, &stack, global);
match n {
PreResult::Skip => {
has = Has::Up;
global.up();
}
PreResult::Ignore => {
if let Visibility::Visible = visibility {
stack.push(P::ManualyHidden);
} else {
stack.push(P::BothHidden);
}
}
PreResult::SkipChildren(acc) => {
has = Has::Up;
if let Visibility::Visible = visibility {
stack.push(P::Visible(acc));
} else {
unimplemented!("Only concrete nodes should be leafs")
}
}
PreResult::Ok(acc) => {
global.set_sum_byte_length(acc.begin_byte());
if let Visibility::Visible = visibility {
stack.push(P::Visible(acc));
} else {
stack.push(P::Hidden(acc));
}
}
}
} else {
has = Has::Up;
if is_parent_hidden || stack.0.last().map_or(false, P::is_both_hidden) {
if let Some(full_node) = full_node {
let parent = stack.parent_mut().unwrap();
parent.push(full_node);
}
} else if cursor.goto_parent() {
if let Some(full_node) = full_node {
let parent = stack.parent_mut().unwrap();
parent.push(full_node);
} else if is_visible {
if has == Has::Down {}
return;
}
} else {
assert!(full_node.is_none());
if has == Has::Down {}
return;
}
}
}
}
}
}
#[derive(PartialEq, Eq)]
pub(crate) enum Has {
Down,
Up,
Right,
}
pub enum PreResult<Acc> {
/// Do not process node and its children
Skip,
/// Do not process node (but process children)
Ignore,
/// Do not process children
SkipChildren(Acc),
Ok(Acc),
}