Skip to content

Commit b01837a

Browse files
committed
Some more tweaks
1 parent 24c8936 commit b01837a

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/green_node.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,22 @@ function Base.show(io::IO, ::MIME"text/plain", node::GreenNode, str::AbstractStr
119119
end
120120

121121
function GreenNode(cursor::GreenTreeCursor)
122+
chead = head(cursor)
123+
T = typeof(chead)
122124
if is_leaf(cursor)
123-
children = nothing
125+
return GreenNode{T}(head(cursor), span(cursor), nothing)
124126
else
125-
children = GreenNode{typeof(head(cursor))}[]
127+
children = GreenNode{T}[]
126128
for child in reverse(cursor)
127129
pushfirst!(children, GreenNode(child))
128130
end
131+
return GreenNode{T}(head(cursor), span(cursor), children)
129132
end
130-
return GreenNode{typeof(head(cursor))}(head(cursor), span(cursor), children)
131133
end
132134

133135
function build_tree(T::Type{GreenNode}, stream::ParseStream; kws...)
134136
cursor = GreenTreeCursor(stream)
135-
if treesize(cursor)+1 != length(stream.output)-1 # First output is a tombstone =
137+
if has_toplevel_siblings(cursor)
136138
# There are multiple toplevel nodes, e.g. because we're using this
137139
# to test a partial parse. Wrap everything in K"wrapper"
138140
all_processed = 0

src/parse_stream.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ end
360360

361361
function Base.getproperty(rgn::RawGreenNode, name::Symbol)
362362
if name === :node_span
363-
has_flags(rgn.head, NON_TERMINAL_FLAG) || return UInt32(0) # Leaf nodes have no children
363+
has_flags(getfield(rgn, :head), NON_TERMINAL_FLAG) || return UInt32(0) # Leaf nodes have no children
364364
return getfield(rgn, :node_span_or_orig_kind)
365365
elseif name === :orig_kind
366-
has_flags(rgn.head, NON_TERMINAL_FLAG) && error("Cannot access orig_kind for non-terminal node")
366+
has_flags(getfield(rgn, :head), NON_TERMINAL_FLAG) && error("Cannot access orig_kind for non-terminal node")
367367
return Kind(getfield(rgn, :node_span_or_orig_kind))
368368
end
369369
getfield(rgn, name)

src/tree_cursors.jl

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@ end
4040
Base.reverse(node::GreenTreeCursor) = Base.Iterators.Reverse(node)
4141
Base.IteratorSize(::Type{Reverse{GreenTreeCursor}}) = Base.SizeUnknown()
4242
@inline function Base.iterate(node::Reverse{GreenTreeCursor},
43-
next_idx::UInt32 = node.itr.position-UInt32(1))::Union{Nothing, Tuple{GreenTreeCursor, UInt32}}
43+
(next_idx, final)::NTuple{2, UInt32} =
44+
(node.itr.position-UInt32(1), node.itr.position - this(node.itr).node_span - UInt32(1)))::Union{Nothing, Tuple{GreenTreeCursor, NTuple{2, UInt32}}}
4445
node = node.itr
4546
while true
46-
next_idx == node.position - this(node).node_span - UInt32(1) && return nothing
47+
next_idx == final && return nothing
4748
next_node = GreenTreeCursor(node.parser_output, next_idx)
48-
if kind(next_node) == K"TOMBSTONE"
49+
nrgn = this(next_node)
50+
if getfield(nrgn, :head).kind == K"TOMBSTONE"
4951
# TOMBSTONED nodes are counted as part of the size of the tree, but
5052
# do not contribute either byte ranges or children.
5153
next_idx -= UInt32(1)
5254
continue
5355
end
5456
# Inlined prev_sibling_assumed
55-
new_next_idx = next_idx - this(next_node).node_span - UInt32(1)
56-
return (next_node, new_next_idx)
57+
new_next_idx = next_idx - nrgn.node_span - UInt32(1)
58+
return (next_node, (new_next_idx, final))
5759
end
5860
end
5961

@@ -95,15 +97,21 @@ end
9597

9698
Base.reverse(node::RedTreeCursor) = Base.Iterators.Reverse(node)
9799
Base.IteratorSize(::Type{Reverse{RedTreeCursor}}) = Base.SizeUnknown()
98-
@inline function Base.iterate(
99-
node::Reverse{RedTreeCursor},
100-
(next_byte_end, next_idx)::NTuple{2, UInt32} =
101-
(node.itr.byte_end, node.itr.green.position-UInt32(1)))::Union{Nothing, Tuple{RedTreeCursor, NTuple{2, UInt32}}}
102-
r = iterate(Reverse(node.itr.green), next_idx)
100+
@inline function Base.iterate(node::Reverse{RedTreeCursor})::Union{Nothing, Tuple{RedTreeCursor, NTuple{3, UInt32}}}
101+
r = iterate(Reverse(node.itr.green))
102+
return _iterate_red_cursor(r, node.itr.byte_end)
103+
end
104+
105+
@inline function Base.iterate(node::Reverse{RedTreeCursor}, state::NTuple{3, UInt32})::Union{Nothing, Tuple{RedTreeCursor, NTuple{3, UInt32}}}
106+
r = iterate(Reverse(node.itr.green), Base.tail(state))
107+
return _iterate_red_cursor(r, first(state))
108+
end
109+
110+
@inline function _iterate_red_cursor(r, byte_end)
103111
r === nothing && return nothing
104112
next_node, next_idx = r
105-
return RedTreeCursor(next_node, next_byte_end),
106-
(next_byte_end - span(next_node), next_idx)
113+
return RedTreeCursor(next_node, byte_end),
114+
(byte_end - span(next_node), next_idx...)
107115
end
108116

109117
is_leaf(node::RedTreeCursor) = is_leaf(node.green)

0 commit comments

Comments
 (0)