Skip to content

Commit aaaae29

Browse files
authored
AbstractSyntaxNode generalization & convenience (#208)
These add a couple more convenience methods useful for TypedSyntax.
1 parent 20b3b3e commit aaaae29

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/syntax_tree.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ haschildren(node::TreeNode) = node.children !== nothing
152152
children(node::TreeNode) = (c = node.children; return c === nothing ? () : c)
153153

154154

155-
head(node::SyntaxNode) = head(node.raw)
155+
head(node::AbstractSyntaxNode) = head(node.raw)
156156

157-
span(node::SyntaxNode) = span(node.raw)
157+
span(node::AbstractSyntaxNode) = span(node.raw)
158158

159159
first_byte(node::AbstractSyntaxNode) = node.position
160160
last_byte(node::AbstractSyntaxNode) = node.position + span(node) - 1
@@ -169,6 +169,9 @@ function sourcetext(node::AbstractSyntaxNode)
169169
view(node.source, val_range)
170170
end
171171

172+
source_line(node::AbstractSyntaxNode) = source_line(node.source, node.position)
173+
source_location(node::AbstractSyntaxNode) = source_location(node.source, node.position)
174+
172175
function interpolate_literal(node::SyntaxNode, val)
173176
@assert kind(node) == K"$"
174177
SyntaxNode(node.source, node.raw, node.position, node.parent, true, val)

test/syntax_tree.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
@test sourcetext(child(t, 3)) == "c"
1212

1313
@test JuliaSyntax.first_byte(child(t, 2)) == findfirst(==('+'), tt)
14+
@test JuliaSyntax.source_line(child(t, 3)) == 1
15+
@test source_location(child(t, 3)) == (1, 7)
1416

1517
# Child indexing
1618
@test t[1] === child(t, 1)

0 commit comments

Comments
 (0)