本文介绍了剥离Expr中的LineNumberNode的泛型函数(应该可以处理:macrocalls)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有内置的Julia函数用于剥离Expr中的LineNumberNode?尤其是对于宏调用:

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(因此,不同的行号信息).我当前的解决方法是显式编写Expr(:macrocall,Symbol("@ foo"),什么都没有,:(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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 22:05