用于在`Expr`中剥离`LineNumberNode`的通用函数(应该能够处理:macrocalls)? [英] Generic function for stripping `LineNumberNode` in `Expr`(should be able to deal with :macrocalls)?
本文介绍了用于在`Expr`中剥离`LineNumberNode`的通用函数(应该能够处理:macrocalls)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Expr
中是否有用于剥离 LineNumberNode
的内置 Julia 函数?特别是对于宏调用:
Is there a build-in Julia function for stripping LineNumberNode
in Expr
? especially for macrocalls:
julia> ex = :(@foo 1)
:(#= REPL[5]:1 =# @foo 1)
julia> dump(ex)
Expr
head: Symbol macrocall
args: Array{Any}((3,))
1: Symbol @foo
2: LineNumberNode
line: Int64 1
file: Symbol REPL[5]
3: Int64 1
试过MacroTools.striplines
,但是
julia> ex = :(@foo 1+1)
:(#= REPL[7]:1 =# @foo 1 + 1)
julia> MacroTools.striplines(ex) |> dump
Expr
head: Symbol macrocall
args: Array{Any}((3,))
1: Symbol @foo
2: LineNumberNode
line: Int64 1
file: Symbol REPL[7]
3: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Int64 1
3: Int64 1
我的用例是比较在不同文件中构造的两个不同的表达式(因此不同的行号信息).我目前的解决方法是显式编写 Expr(:macrocall, Symbol("@foo"), nothing, :(1+1)) ,这有点冗长.
My use-case is to compare two different exprs constructed in different files(so different line number info). My current workaround is to explicitly write Expr(:macrocall, Symbol("@foo"), nothing, :(1+1)) which is a little bit verbose.
推荐答案
内置函数是Base.remove_linenums!
:
julia> ex = quote begin
x = 3
y = 2
z = 4
foo(x) = 3
end
end
quote
#= REPL[2]:1 =#
begin
#= REPL[2]:2 =#
x = 3
#= REPL[2]:3 =#
y = 2
#= REPL[2]:4 =#
z = 4
#= REPL[2]:5 =#
foo(x) = begin
#= REPL[2]:5 =#
3
end
end
end
julia> Base.remove_linenums!(ex)
quote
begin
x = 3
y = 2
z = 4
foo(x) = begin
3
end
end
end
感谢 Alex Arslan 提醒我.
Credit to Alex Arslan for reminding me of it.
这篇关于用于在`Expr`中剥离`LineNumberNode`的通用函数(应该能够处理:macrocalls)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文