Skip to content

Commit

Permalink
Fix SyntaxNode->Expr conversion with SubString source code
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Jan 15, 2025
1 parent a72d68c commit 82f7419
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function _expr_leaf_val(node::SyntaxNode)
node.val
end

function _leaf_to_Expr(source, txtbuf, head, srcrange, node)
function _leaf_to_Expr(source, txtbuf, txtbuf_offset, head, srcrange, node)
k = kind(head)
if k == K"MacroName" && view(source, srcrange) == "."
return Symbol("@__dot__")
Expand All @@ -77,7 +77,9 @@ function _leaf_to_Expr(source, txtbuf, head, srcrange, node)
Expr(:error) :
Expr(:error, "$(_token_error_descriptions[k]): `$(source[srcrange])`")
else
val = isnothing(node) ? parse_julia_literal(txtbuf, head, srcrange) : _expr_leaf_val(node)
val = isnothing(node) ?
parse_julia_literal(txtbuf, head, srcrange .+ txtbuf_offset) :
_expr_leaf_val(node)
if val isa Union{Int128,UInt128,BigInt}
# Ignore the values of large integers and convert them back to
# symbolic/textural form for compatibility with the Expr
Expand Down Expand Up @@ -547,7 +549,7 @@ function build_tree(::Type{Expr}, stream::ParseStream;
end
k = kind(head)
if isnothing(nodechildren)
ex = _leaf_to_Expr(source, txtbuf, head, srcrange, nothing)
ex = _leaf_to_Expr(source, txtbuf, 0, head, srcrange, nothing)
else
resize!(childranges, length(nodechildren))
resize!(childheads, length(nodechildren))
Expand All @@ -568,8 +570,8 @@ end
function _to_expr(node)
file = sourcefile(node)
if is_leaf(node)
offset, txtbuf = _unsafe_wrap_substring(sourcetext(file))
return _leaf_to_Expr(file, txtbuf, head(node), byte_range(node) .+ offset, node)
txtbuf_offset, txtbuf = _unsafe_wrap_substring(sourcetext(file))
return _leaf_to_Expr(file, txtbuf, txtbuf_offset, head(node), byte_range(node), node)
end
cs = children(node)
args = Any[_to_expr(c) for c in cs]
Expand Down
6 changes: 6 additions & 0 deletions test/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,9 @@
Expr(:import, Expr(:as, Expr(:., :A, :+), :y))
end
end

@testset "SyntaxNode->Expr conversion" begin
src = repeat('a', 1000) * '\n' * "@hi"
@test Expr(parsestmt(SyntaxNode, SubString(src, 1001:lastindex(src)))) ==
Expr(:macrocall, Symbol("@hi"), LineNumberNode(2))
end

0 comments on commit 82f7419

Please sign in to comment.