Skip to content

Commit 0b2c422

Browse files
authored
Allow SyntaxNode node[end] to get the last child (#189)
Also add start of more targeted tests for SyntaxNode.
1 parent e57b800 commit 0b2c422

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/syntax_tree.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ end
254254
function Base.getindex(node::Union{SyntaxNode,GreenNode}, path::Int...)
255255
child(node, path...)
256256
end
257+
function Base.lastindex(node::Union{SyntaxNode,GreenNode})
258+
length(children(node))
259+
end
260+
257261
function Base.setindex!(node::SyntaxNode, x::SyntaxNode, path::Int...)
258262
setchild!(node, path, x)
259263
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ end
4848

4949
include("parse_stream.jl")
5050
include("parser.jl")
51+
include("syntax_tree.jl")
5152
include("diagnostics.jl")
5253
include("parser_api.jl")
5354
include("expr.jl")

test/syntax_tree.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@testset "SyntaxNode" begin
2+
# Child access
3+
t = parse(SyntaxNode, "a*b + c")
4+
5+
@test sourcetext(child(t, 1)) == "a*b"
6+
@test sourcetext(child(t, 1, 1)) == "a"
7+
@test sourcetext(child(t, 1, 2)) == "*"
8+
@test sourcetext(child(t, 1, 3)) == "b"
9+
@test sourcetext(child(t, 2)) == "+"
10+
@test sourcetext(child(t, 3)) == "c"
11+
12+
# Child indexing
13+
@test t[1] === child(t, 1)
14+
@test t[1, 1] === child(t, 1, 1)
15+
@test t[end] === child(t, 3)
16+
# Unfortunately, can't make t[1, end] work
17+
# as `lastindex(t, 2)` isn't well defined
18+
end

0 commit comments

Comments
 (0)