Skip to content

Commit a55ac20

Browse files
committed
Tweaks to expr-conversion for JuliaLowering
Two small and related changes: 1. The node->expr conversion shouldn't peek at provenance from SyntaxNode or SyntaxTree, so fix the couple of places we do this. We've always had provenance available, but we won't once there are nodes constructed from exprs for compatibility. 2. JuliaLowering currently relies on putting its own data structures through the JuliaSyntax node->expr machine. Remove a couple of type annotations and reinstate `_expr_leaf_val` so that this is possible.
1 parent 71320ea commit a55ac20

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/integration/expr.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565

6666

6767
reverse_nontrivia_children(cursor::RedTreeCursor) = Iterators.filter(should_include_node, Iterators.reverse(cursor))
68-
reverse_nontrivia_children(cursor::SyntaxNode) = Iterators.filter(should_include_node, Iterators.reverse(children(cursor)))
68+
reverse_nontrivia_children(cursor) = Iterators.filter(should_include_node, Iterators.reverse(children(cursor)))
6969

7070
# Julia string literals in a `K"string"` node may be split into several chunks
7171
# interspersed with trivia in two situations:
@@ -74,7 +74,7 @@ reverse_nontrivia_children(cursor::SyntaxNode) = Iterators.filter(should_include
7474
#
7575
# This function concatenating adjacent string chunks together as done in the
7676
# reference parser.
77-
function _string_to_Expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFile, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32)
77+
function _string_to_Expr(cursor, source::SourceFile, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32)
7878
ret = Expr(:string)
7979
args2 = Any[]
8080
i = 1
@@ -197,7 +197,7 @@ function _append_iterspec!(args::Vector{Any}, @nospecialize(ex))
197197
return args
198198
end
199199

200-
function parseargs!(retexpr::Expr, loc::LineNumberNode, cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFile, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32)
200+
function parseargs!(retexpr::Expr, loc::LineNumberNode, cursor, source::SourceFile, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32)
201201
args = retexpr.args
202202
firstchildhead = head(cursor)
203203
firstchildrange::UnitRange{UInt32} = byte_range(cursor)
@@ -215,8 +215,13 @@ function parseargs!(retexpr::Expr, loc::LineNumberNode, cursor::Union{RedTreeCur
215215
return (firstchildhead, firstchildrange)
216216
end
217217

218-
# Convert internal node of the JuliaSyntax parse tree to an Expr
219-
function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFile, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32=UInt32(0))
218+
_expr_leaf_val(node::SyntaxNode, _...) = node.val
219+
_expr_leaf_val(cursor::RedTreeCursor, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32) =
220+
parse_julia_literal(txtbuf, head(cursor), byte_range(cursor) .+ txtbuf_offset)
221+
# Extended in JuliaLowering to support `node_to_expr(::SyntaxTree, ...)`
222+
223+
# Convert `cursor` (SyntaxNode or RedTreeCursor) to an Expr
224+
function node_to_expr(cursor, source::SourceFile, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32=UInt32(0))
220225
if !should_include_node(cursor)
221226
return nothing
222227
end
@@ -225,14 +230,12 @@ function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFi
225230
k = kind(cursor)
226231
srcrange::UnitRange{UInt32} = byte_range(cursor)
227232
if is_leaf(cursor)
228-
if k == K"MacroName" && view(source, srcrange) == "."
229-
return Symbol("@__dot__")
230-
elseif is_error(k)
233+
if is_error(k)
231234
return k == K"error" ?
232235
Expr(:error) :
233236
Expr(:error, "$(_token_error_descriptions[k]): `$(source[srcrange])`")
234237
else
235-
val = parse_julia_literal(txtbuf, head(cursor), srcrange .+ txtbuf_offset)
238+
val = _expr_leaf_val(cursor, txtbuf, txtbuf_offset)
236239
if val isa Union{Int128,UInt128,BigInt}
237240
# Ignore the values of large integers and convert them back to
238241
# symbolic/textural form for compatibility with the Expr
@@ -242,6 +245,8 @@ function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFi
242245
val isa UInt128 ? Symbol("@uint128_str") :
243246
Symbol("@big_str")
244247
return Expr(:macrocall, GlobalRef(Core, macname), nothing, str)
248+
elseif k == K"MacroName" && val === Symbol("@.")
249+
return Symbol("@__dot__")
245250
else
246251
return val
247252
end
@@ -631,9 +636,11 @@ function build_tree(::Type{Expr}, stream::ParseStream, source::SourceFile)
631636
return entry
632637
end
633638

634-
function Base.Expr(node::SyntaxNode)
639+
function to_expr(node)
635640
source = sourcefile(node)
636641
txtbuf_offset, txtbuf = _unsafe_wrap_substring(sourcetext(source))
637642
wrapper_head = SyntaxHead(K"wrapper",EMPTY_FLAGS)
638643
return fixup_Expr_child(wrapper_head, node_to_expr(node, source, txtbuf, UInt32(txtbuf_offset)), false)
639644
end
645+
646+
Base.Expr(node::SyntaxNode) = to_expr(node)

0 commit comments

Comments
 (0)