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
use downcast_rs::{Downcast, impl_downcast};
use std::{fmt::Debug, any::TypeId};
use crate::Renderer;
use crate::common::TypeKey;
use crate::common::sourcemap::SourcePos;
use crate::parser::extset::NodeExtSet;
use crate::parser::renderer::HTMLRenderer;
#[derive(Debug)]
#[readonly::make]
pub struct Node {
pub children: Vec<Node>,
pub srcmap: Option<SourcePos>,
pub ext: NodeExtSet,
pub attrs: Vec<(&'static str, String)>,
#[readonly]
pub node_type: TypeKey,
#[readonly]
pub node_value: Box<dyn NodeValue>,
}
impl Node {
pub fn new<T: NodeValue>(value: T) -> Self {
Self {
children: Vec::new(),
srcmap: None,
attrs: Vec::new(),
ext: NodeExtSet::new(),
node_type: TypeKey::of::<T>(),
node_value: Box::new(value),
}
}
pub fn name(&self) -> &'static str {
self.node_type.name
}
pub fn is<T: NodeValue>(&self) -> bool {
self.node_type.id == TypeId::of::<T>()
}
pub fn cast<T: NodeValue>(&self) -> Option<&T> {
if self.node_type.id == TypeId::of::<T>() {
Some(self.node_value.downcast_ref::<T>().unwrap())
} else {
None
}
}
pub fn cast_mut<T: NodeValue>(&mut self) -> Option<&mut T> {
if self.node_type.id == TypeId::of::<T>() {
Some(self.node_value.downcast_mut::<T>().unwrap())
} else {
None
}
}
pub fn render(&self) -> String {
let mut fmt = HTMLRenderer::<false>::new();
fmt.render(self);
fmt.into()
}
pub fn xrender(&self) -> String {
let mut fmt = HTMLRenderer::<true>::new();
fmt.render(self);
fmt.into()
}
pub fn replace<T: NodeValue>(&mut self, value: T) {
self.node_type = TypeKey::of::<T>();
self.node_value = Box::new(value);
}
pub fn walk(&self, mut f: impl FnMut(&Node, u32)) {
fn walk_recursive(node: &Node, depth: u32, f: &mut impl FnMut(&Node, u32)) {
f(node, depth);
for n in node.children.iter() {
stacker::maybe_grow(64*1024, 1024*1024, || {
walk_recursive(n, depth + 1, f);
});
}
}
walk_recursive(self, 0, &mut f);
}
pub fn walk_mut(&mut self, mut f: impl FnMut(&mut Node, u32)) {
fn walk_recursive(node: &mut Node, depth: u32, f: &mut impl FnMut(&mut Node, u32)) {
f(node, depth);
for n in node.children.iter_mut() {
stacker::maybe_grow(64*1024, 1024*1024, || {
walk_recursive(n, depth + 1, f);
});
}
}
walk_recursive(self, 0, &mut f);
}
pub fn walk_post(&self, mut f: impl FnMut(&Node, u32)) {
fn walk_recursive(node: &Node, depth: u32, f: &mut impl FnMut(&Node, u32)) {
for n in node.children.iter() {
stacker::maybe_grow(64*1024, 1024*1024, || {
walk_recursive(n, depth + 1, f);
});
}
f(node, depth);
}
walk_recursive(self, 0, &mut f);
}
pub fn walk_post_mut(&mut self, mut f: impl FnMut(&mut Node, u32)) {
fn walk_recursive(node: &mut Node, depth: u32, f: &mut impl FnMut(&mut Node, u32)) {
for n in node.children.iter_mut() {
stacker::maybe_grow(64*1024, 1024*1024, || {
walk_recursive(n, depth + 1, f);
});
}
f(node, depth);
}
walk_recursive(self, 0, &mut f);
}
}
impl Drop for Node {
fn drop(&mut self) {
self.walk_post_mut(|node, _| {
drop(std::mem::take(&mut node.children));
});
}
}
#[derive(Debug)]
#[doc(hidden)]
pub struct NodeEmpty;
impl NodeValue for NodeEmpty {}
impl Default for Node {
fn default() -> Self {
Node::new(NodeEmpty)
}
}
pub trait NodeValue : Debug + Downcast {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let _ = fmt;
unimplemented!("{} doesn't implement render", node.name());
}
}
impl_downcast!(NodeValue);