Skip to content

Commit c029996

Browse files
authored
Tweaks to expr-conversion for JuliaLowering (#569)
* 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. * Remove source's type annotation too A JuliaLowering.SyntaxTree may have a line number node instead of a SourceFile as its location. Pushing the linenode through `node_to_expr` is strange and temporary, according to comments.
1 parent 2deac15 commit c029996

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/integration/expr.jl

Lines changed: 19 additions & 11 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, 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, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32)
201201
args = retexpr.args
202202
firstchildhead = head(cursor)
203203
firstchildrange::UnitRange{UInt32} = byte_range(cursor)
@@ -215,8 +215,14 @@ 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+
# `source` is a SourceFile, or if node was an Expr originally, a LineNumberNode
225+
function node_to_expr(cursor, source, txtbuf::Vector{UInt8}, txtbuf_offset::UInt32=UInt32(0))
220226
if !should_include_node(cursor)
221227
return nothing
222228
end
@@ -225,14 +231,12 @@ function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFi
225231
k = kind(cursor)
226232
srcrange::UnitRange{UInt32} = byte_range(cursor)
227233
if is_leaf(cursor)
228-
if k == K"MacroName" && view(source, srcrange) == "."
229-
return Symbol("@__dot__")
230-
elseif is_error(k)
234+
if is_error(k)
231235
return k == K"error" ?
232236
Expr(:error) :
233237
Expr(:error, "$(_token_error_descriptions[k]): `$(source[srcrange])`")
234238
else
235-
val = parse_julia_literal(txtbuf, head(cursor), srcrange .+ txtbuf_offset)
239+
val = _expr_leaf_val(cursor, txtbuf, txtbuf_offset)
236240
if val isa Union{Int128,UInt128,BigInt}
237241
# Ignore the values of large integers and convert them back to
238242
# symbolic/textural form for compatibility with the Expr
@@ -242,6 +246,8 @@ function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFi
242246
val isa UInt128 ? Symbol("@uint128_str") :
243247
Symbol("@big_str")
244248
return Expr(:macrocall, GlobalRef(Core, macname), nothing, str)
249+
elseif k == K"MacroName" && val === Symbol("@.")
250+
return Symbol("@__dot__")
245251
else
246252
return val
247253
end
@@ -297,7 +303,7 @@ end
297303
firstchildhead::SyntaxHead,
298304
firstchildrange::UnitRange{UInt32},
299305
nodehead::SyntaxHead,
300-
source::SourceFile)
306+
source)
301307
args = retexpr.args
302308
k = kind(nodehead)
303309
endloc = source_location(LineNumberNode, source, last(srcrange))
@@ -635,9 +641,11 @@ function build_tree(::Type{Expr}, stream::ParseStream, source::SourceFile)
635641
return entry
636642
end
637643

638-
function Base.Expr(node::SyntaxNode)
644+
function to_expr(node)
639645
source = sourcefile(node)
640646
txtbuf_offset, txtbuf = _unsafe_wrap_substring(sourcetext(source))
641647
wrapper_head = SyntaxHead(K"wrapper",EMPTY_FLAGS)
642648
return fixup_Expr_child(wrapper_head, node_to_expr(node, source, txtbuf, UInt32(txtbuf_offset)), false)
643649
end
650+
651+
Base.Expr(node::SyntaxNode) = to_expr(node)

0 commit comments

Comments
 (0)