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
mod item;
pub use item::Item;
mod visit_gen;
pub use visit_gen::Observer;
use crate::Node;
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalPtr;
#[cfg(feature = "compile-with-external-structures")]
type Ptr<T> = ExternalPtr<T>;
#[cfg(not(feature = "compile-with-external-structures"))]
type Ptr<T> = Box<T>;
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalMaybePtr;
#[cfg(feature = "compile-with-external-structures")]
type MaybePtr<T> = ExternalMaybePtr<T>;
#[cfg(not(feature = "compile-with-external-structures"))]
type MaybePtr<T> = Option<Box<T>>;
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalList;
#[cfg(feature = "compile-with-external-structures")]
type List<T> = ExternalList<T>;
#[cfg(not(feature = "compile-with-external-structures"))]
type List<T> = Vec<T>;
#[derive(Debug)]
pub struct Visitor<T>
where
T: Observer,
{
pub observer: T,
}
pub(crate) trait Visit<TItem> {
fn visit(&mut self, item: TItem, visit_as: Item);
}
impl<TObserver: Observer> Visit<&[Node]> for Visitor<TObserver> {
fn visit(&mut self, nodes: &[Node], visit_as: Item) {
self.observer.on_subitem(visit_as);
self.observer.on_node_list(nodes);
for (idx, node) in nodes.iter().enumerate() {
self.visit(node, Item::Idx(idx));
}
self.observer.on_subitem_moving_up(visit_as);
}
}
impl<TObserver: Observer> Visit<&List<Node>> for Visitor<TObserver> {
fn visit(&mut self, nodes: &List<Node>, visit_as: Item) {
let nodes: &[Node] = nodes;
self.visit(nodes, visit_as);
}
}
impl<TObserver: Observer> Visit<&Ptr<Node>> for Visitor<TObserver> {
fn visit(&mut self, node: &Ptr<Node>, visit_as: Item) {
let node: &Node = &*node;
self.visit(node, visit_as);
}
}
impl<TObserver: Observer> Visit<&MaybePtr<Node>> for Visitor<TObserver> {
fn visit(&mut self, node: &MaybePtr<Node>, visit_as: Item) {
if let Some(node) = node.as_ref() {
self.visit(node, visit_as);
}
}
}
impl<T> Visitor<T>
where
T: Observer,
{
pub fn visit_root(&mut self, node: &Node) {
self.visit(node, Item::Root);
}
}