从 Julia AST 中剥离行号
Stripping line numbers from Julia AST
我正在使用以下函数从 Julia AST 中删除行号:
function filter_lineno(ex::Expr)
filter!(ex.args) do e
isa(e, LineNumberNode) && return false
if isa(e, Expr)
(e::Expr).head === :line && return false
filter_lineno(e::Expr)
end
return true
end
return ex
end
但是当代码中有宏时,这似乎不能正常工作。这是一个失败的例子:
expr = Meta.parse("begin run(``) end")
filter_lineno(expr)
我收到以下错误:
BoundsError: attempt to access 2-element Array{Any,1} at index [3]
处理文档字符串的另一个例子:
expr = Meta.parse("begin \"Here is the doc\"\nmodule X end end")
filter_lineno(expr)
产生以下结果:
quote
Core.@doc module X
end
end
这个函数有什么问题,我该如何解决?
只需使用 MacroTools
包:
julia> using MacroTools
julia> cc = Meta.parse("begin \"Here is the doc\"\nmodule X end end")
quote
#= none:1 =#
#= none:1 =# Core.@doc "Here is the doc" module X
#= none:2 =#
#= none:2 =#
end
end
julia> MacroTools.striplines(cc)
quote
Core.@doc "Here is the doc" module X
end
end
这是一个重复的问题,我:
只需使用 Base.remove_linenums!(ex)
就可以了 (TM).
我正在使用以下函数从 Julia AST 中删除行号:
function filter_lineno(ex::Expr)
filter!(ex.args) do e
isa(e, LineNumberNode) && return false
if isa(e, Expr)
(e::Expr).head === :line && return false
filter_lineno(e::Expr)
end
return true
end
return ex
end
但是当代码中有宏时,这似乎不能正常工作。这是一个失败的例子:
expr = Meta.parse("begin run(``) end")
filter_lineno(expr)
我收到以下错误:
BoundsError: attempt to access 2-element Array{Any,1} at index [3]
处理文档字符串的另一个例子:
expr = Meta.parse("begin \"Here is the doc\"\nmodule X end end")
filter_lineno(expr)
产生以下结果:
quote
Core.@doc module X
end
end
这个函数有什么问题,我该如何解决?
只需使用 MacroTools
包:
julia> using MacroTools
julia> cc = Meta.parse("begin \"Here is the doc\"\nmodule X end end")
quote
#= none:1 =#
#= none:1 =# Core.@doc "Here is the doc" module X
#= none:2 =#
#= none:2 =#
end
end
julia> MacroTools.striplines(cc)
quote
Core.@doc "Here is the doc" module X
end
end
这是一个重复的问题,我Base.remove_linenums!(ex)
就可以了 (TM).