65
65
66
66
67
67
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)))
69
69
70
70
# Julia string literals in a `K"string"` node may be split into several chunks
71
71
# interspersed with trivia in two situations:
@@ -74,7 +74,7 @@ reverse_nontrivia_children(cursor::SyntaxNode) = Iterators.filter(should_include
74
74
#
75
75
# This function concatenating adjacent string chunks together as done in the
76
76
# 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 )
78
78
ret = Expr (:string )
79
79
args2 = Any[]
80
80
i = 1
@@ -197,7 +197,7 @@ function _append_iterspec!(args::Vector{Any}, @nospecialize(ex))
197
197
return args
198
198
end
199
199
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 )
201
201
args = retexpr. args
202
202
firstchildhead = head (cursor)
203
203
firstchildrange:: UnitRange{UInt32} = byte_range (cursor)
@@ -215,8 +215,14 @@ function parseargs!(retexpr::Expr, loc::LineNumberNode, cursor::Union{RedTreeCur
215
215
return (firstchildhead, firstchildrange)
216
216
end
217
217
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 ))
220
226
if ! should_include_node (cursor)
221
227
return nothing
222
228
end
@@ -225,14 +231,12 @@ function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFi
225
231
k = kind (cursor)
226
232
srcrange:: UnitRange{UInt32} = byte_range (cursor)
227
233
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)
231
235
return k == K " error" ?
232
236
Expr (:error ) :
233
237
Expr (:error , " $(_token_error_descriptions[k]) : `$(source[srcrange]) `" )
234
238
else
235
- val = parse_julia_literal (txtbuf, head ( cursor), srcrange .+ txtbuf_offset)
239
+ val = _expr_leaf_val ( cursor, txtbuf, txtbuf_offset)
236
240
if val isa Union{Int128,UInt128,BigInt}
237
241
# Ignore the values of large integers and convert them back to
238
242
# symbolic/textural form for compatibility with the Expr
@@ -242,6 +246,8 @@ function node_to_expr(cursor::Union{RedTreeCursor, SyntaxNode}, source::SourceFi
242
246
val isa UInt128 ? Symbol (" @uint128_str" ) :
243
247
Symbol (" @big_str" )
244
248
return Expr (:macrocall , GlobalRef (Core, macname), nothing , str)
249
+ elseif k == K " MacroName" && val === Symbol (" @." )
250
+ return Symbol (" @__dot__" )
245
251
else
246
252
return val
247
253
end
297
303
firstchildhead:: SyntaxHead ,
298
304
firstchildrange:: UnitRange{UInt32} ,
299
305
nodehead:: SyntaxHead ,
300
- source:: SourceFile )
306
+ source)
301
307
args = retexpr. args
302
308
k = kind (nodehead)
303
309
endloc = source_location (LineNumberNode, source, last (srcrange))
@@ -635,9 +641,11 @@ function build_tree(::Type{Expr}, stream::ParseStream, source::SourceFile)
635
641
return entry
636
642
end
637
643
638
- function Base . Expr (node:: SyntaxNode )
644
+ function to_expr (node)
639
645
source = sourcefile (node)
640
646
txtbuf_offset, txtbuf = _unsafe_wrap_substring (sourcetext (source))
641
647
wrapper_head = SyntaxHead (K " wrapper" ,EMPTY_FLAGS)
642
648
return fixup_Expr_child (wrapper_head, node_to_expr (node, source, txtbuf, UInt32 (txtbuf_offset)), false )
643
649
end
650
+
651
+ Base. Expr (node:: SyntaxNode ) = to_expr (node)
0 commit comments