Skip to content

Commit d193baa

Browse files
authored
Separate token output from internal nodes in ParseStream (#19)
This data rearrangement gives a cleaner separation between tokens (which keep track of bytes in the source text) vs internal tree nodes (which keep track of which tokens they cover). As a result it reduces the size of the intermediate data structures. As part of rewriting build_tree to use the new data structures it's also become much faster and building the green tree no longer dominates the parsing time (probably due to fixing some type stability issues).
1 parent a25e259 commit d193baa

File tree

6 files changed

+264
-168
lines changed

6 files changed

+264
-168
lines changed

src/green_tree.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ struct GreenNode{Head}
4343
args::Union{Tuple{},Vector{GreenNode{Head}}}
4444
end
4545

46+
function GreenNode{Head}(head::Head, span::Integer) where {Head}
47+
GreenNode{Head}(head, span, ())
48+
end
49+
4650
function GreenNode(head::Head, span::Integer) where {Head}
4751
GreenNode{Head}(head, span, ())
4852
end
4953

50-
function GreenNode(head::Head, span::Integer, args::Vector{GreenNode{Head}}) where {Head}
51-
GreenNode{Head}(head, span, args)
54+
function GreenNode(head::Head, args) where {Head}
55+
children = collect(GreenNode{Head}, args)
56+
span = isempty(children) ? 0 : sum(x.span for x in children)
57+
GreenNode{Head}(head, span, children)
5258
end
5359

5460
function GreenNode(head::Head, args::GreenNode{Head}...) where {Head}
55-
span = sum(x.span for x in args)
56-
GreenNode{Head}(head, span, GreenNode{Head}[args...])
61+
GreenNode{Head}(head, GreenNode{Head}[args...])
5762
end
5863

5964

0 commit comments

Comments
 (0)