Skip to content

Commit 2edb531

Browse files
author
oscarddssmith
committed
support JuliaLowering
1 parent d3213a5 commit 2edb531

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/expr.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function _string_to_Expr(args)
140140
# """\n a\n b""" ==> "a\nb"
141141
return only(args2)
142142
else
143-
# This only happens when the kind is K"string" or when an error has occurred.
143+
# This only happens when the kind is K"string" or when an error has occurred.
144144
return Expr(:string, args2...)
145145
end
146146
end
@@ -159,7 +159,7 @@ function _fixup_Expr_children!(head, loc, args)
159159
arg = args[i]
160160
was_parens = @isexpr(arg, :parens)
161161
arg = _strip_parens(arg)
162-
if @isexpr(arg, :(=)) && eq_to_kw_in_call && i > 1
162+
if @isexpr(arg, :(=)) && eq_to_kw_in_call && i > 1
163163
arg = Expr(:kw, arg.args...)
164164
elseif k != K"parens" && @isexpr(arg, :., 1) && arg.args[1] isa Tuple
165165
h, a = arg.args[1]::Tuple{SyntaxHead,Any}
@@ -577,6 +577,13 @@ function build_tree(::Type{Expr}, stream::ParseStream;
577577
only(_fixup_Expr_children!(SyntaxHead(K"None",EMPTY_FLAGS), loc, Any[entry.ex]))
578578
end
579579

580+
581+
function build_tree(::Type{Tuple{Expr, SyntaxNode}}, stream::ParseStream;
582+
filename=nothing, first_line=1, kws...)
583+
syntaxtree = build_tree(SyntaxNode, stream, filename, first_line, kws...)
584+
Expr.(syntaxtree)
585+
end
586+
580587
function _to_expr(node)
581588
file = sourcefile(node)
582589
if is_leaf(node)

src/hooks.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ end
136136
# Debug log file for dumping parsed code
137137
const _debug_log = Ref{Union{Nothing,IO}}(nothing)
138138

139-
function core_parser_hook(code, filename::String, lineno::Int, offset::Int, options::Symbol)
139+
function core_parser_hook_for_lowering(code, filename::String, lineno::Int, offset::Int, options::Symbol)
140+
core_parser_hook(code, filename::String, lineno::Int, offset::Int, options::Symbol, true)
141+
end
142+
143+
function core_parser_hook(code, filename::String, lineno::Int, offset::Int, options::Symbol, for_lowering=false)
140144
try
141145
# TODO: Check that we do all this input wrangling without copying the
142146
# code buffer
@@ -233,7 +237,7 @@ function core_parser_hook(code, filename::String, lineno::Int, offset::Int, opti
233237
#
234238
# show_diagnostics(stdout, stream.diagnostics, code)
235239
#
236-
ex = build_tree(Expr, stream; filename=filename, first_line=lineno)
240+
ex = build_tree(for_lowering ? Tuple{Expr, SyntaxNode} : Expr, stream; filename=filename, first_line=lineno)
237241
end
238242

239243
# Note the next byte in 1-based indexing is `last_byte(stream) + 1` but
@@ -292,7 +296,7 @@ _default_system_parser = _has_v1_6_hooks ? Core._parse : nothing
292296

293297

294298
# hook into InteractiveUtils.@activate
295-
activate!() = enable_in_core!()
299+
activate!(enable=true) = enable_in_core!(enable)
296300

297301
"""
298302
enable_in_core!([enable=true; freeze_world_age=true, debug_filename=nothing])
@@ -319,7 +323,10 @@ function enable_in_core!(enable=true; freeze_world_age = true,
319323
close(_debug_log[])
320324
_debug_log[] = nothing
321325
end
322-
if enable
326+
if enable == :JuliaLowering
327+
world_age = freeze_world_age ? Base.get_world_counter() : typemax(UInt)
328+
_set_core_parse_hook(fix_world_age(core_parser_hook_for_lowering, world_age))
329+
elseif enable
323330
world_age = freeze_world_age ? Base.get_world_counter() : typemax(UInt)
324331
_set_core_parse_hook(fix_world_age(core_parser_hook, world_age))
325332
else

0 commit comments

Comments
 (0)