@@ -3,9 +3,9 @@ module XML
3
3
using OrderedCollections: OrderedDict
4
4
using Mmap
5
5
using Tables
6
- using AbstractTrees: AbstractTrees
6
+ using AbstractTrees: AbstractTrees, children
7
7
8
- export Node, NodeType
8
+ export Node, NodeType, children
9
9
10
10
# -----------------------------------------------------------------------------# escape/unescape
11
11
escape_chars = [' &' => " &" , ' "' => " "" , ' '' => " '" , ' <' => " <" , ' >' => " >" ]
@@ -189,46 +189,51 @@ end
189
189
190
190
# -----------------------------------------------------------------------------# Lazy
191
191
struct LazyNode
192
- tokens:: Tokens
193
192
data:: TokenData
194
193
end
195
- LazyNode (t:: Tokens ) = LazyNode (t, init (t))
194
+ LazyNode (t:: Tokens ) = LazyNode (init (t))
196
195
LazyNode (filename:: AbstractString ) = LazyNode (Tokens (filename))
197
196
198
- function Base. get (o:: LazyNode )
199
- iszero (o. data. pos) ? RowNode (0 , DOCUMENT_NODE, nothing , nothing , nothing ) : RowNode (o. data)
200
- end
197
+ Base. get (o:: LazyNode ) = RowNode (o. data)
201
198
202
- next (o:: LazyNode ) = LazyNode (o. tokens, next (o. data))
203
- prev (o:: LazyNode ) = LazyNode (o. tokens, prev (o. data))
199
+ function next (o:: LazyNode )
200
+ x = next (o. data)
201
+ isnothing (x) ? nothing : LazyNode (x)
202
+ end
203
+ function prev (o:: LazyNode )
204
+ x = prev (o. data)
205
+ isnothing (x) ? nothing : LazyNode (x)
206
+ end
204
207
205
208
function Base. show (io:: IO , o:: LazyNode )
206
- print (io, " Lazy : " )
209
+ print (io, " LazyNode : " )
207
210
show (io, get (o))
208
211
end
209
212
function AbstractTrees. children (o:: LazyNode )
210
- i = o. i
211
- depth = iszero (i) ? 0 : o. tokens[i]. depth
213
+ depth = o. data. depth
212
214
out = LazyNode[]
213
- for j in (i+ 1 ): length (o. tokens)
214
- tok = o. tokens[j]
215
- tok. depth == depth && break
216
- tok. depth == depth + 1 && push! (out, LazyNode (o. tokens, j))
215
+ x = o
216
+ while ! isnothing (x)
217
+ x = next (x)
218
+ isnothing (x) && break
219
+ x. data. tok === TOK_END_ELEMENT && continue
220
+ x. data. depth == depth && break
221
+ x. data. depth == depth + 1 && push! (out, x)
217
222
end
218
223
return out
219
224
end
220
225
AbstractTrees. nodevalue (o:: LazyNode ) = get (o)
226
+
221
227
function AbstractTrees. parent (o:: LazyNode )
222
- iszero (i) && return nothing
223
- i = findprev (x -> x. depth < o. tokens[o. i]. depth, o. tokens, o. i - 1 )
224
- isnothing (i) ? LazyNode (o. tokens, 0 ) : LazyNode (o. tokens, i)
228
+ depth = o. data. depth
229
+ x = prev (o)
230
+ while ! isnothing (x)
231
+ x. data. depth == depth - 1 && return x
232
+ x = prev (x)
233
+ end
234
+ return nothing
225
235
end
226
236
227
- function lazy (t:: Tokens )
228
- tokens = filter! (x -> x. tok != = TOK_END_ELEMENT, collect (t))
229
- LazyNode (tokens, 0 )
230
- end
231
- lazy (filename:: AbstractString ) = lazy (Tokens (filename))
232
237
233
238
# -----------------------------------------------------------------------------# Rows
234
239
struct Rows
@@ -247,6 +252,7 @@ struct RowNode
247
252
end
248
253
function RowNode (t:: TokenData )
249
254
(; tok, pos, len, depth) = t
255
+ pos === 0 && return RowNode (0 , DOCUMENT_NODE, nothing , nothing , nothing )
250
256
data = view (t. data, pos: pos+ len)
251
257
@views if tok === TOK_TEXT # text
252
258
return RowNode (depth, TEXT_NODE, nothing , nothing , unescape (String (data)))
0 commit comments