Access code file and line number from Scala macro?

让人想犯罪 __ 提交于 2019-12-23 17:20:57

问题


How can I access the name of the code file and line number in a Scala macro? I looked at SIP-19 and it says it can be easily implemented using macros...

EDIT: To clarify, I want the code file and line number of the caller. I already have a debug macro and I want to modify it to print the line number and file name of whoever calls debug


回答1:


You want c.macroApplication.pos, where c is for Context.

c.enclosingPosition finds the nearest macro on the stack that has a position. (See the other answer.) For instance, if your assert macro generates a tree for F"%p: $msg" but doesn't assign a position, the F macro would be positionless.

Example from a string interpolator macro, F"%p":

  /* Convert enhanced conversions to something format likes.
   * %Q for quotes, %p for position, %Pf for file, %Pn line number,
   * %Pc column %Po offset.
   */
  private def downConvert(parts: List[Tree]): List[Tree] = {
    def fixup(t: Tree): Tree = {
      val Literal(Constant(s: String)) = t
      val r = "(?<!%)%(p|Q|Pf|Po|Pn|Pc)".r
      def p = c.macroApplication.pos
      def f(m: Match): String = m group 1 match {
        case "p"  => p.toString
        case "Pf" => p.source.file.name
        case "Po" => p.point.toString
        case "Pn" => p.line.toString
        case "Pc" => p.column.toString
        case "Q"  => "\""
      }
      val z = r.replaceAllIn(s, f _)
      Literal(Constant(z)) //setPos t.pos
    }
    parts map fixup
  }



回答2:


If you mean file name and line number of the current position in the source code, for 2.10, my answer to that SO question is what your looking for:

def $currentPosition:String = macro _currentPosition
def _currentPosition(c:Context):c.Expr[String]={ import c.universe._
  val pos = c.enclosingPosition
  c.Expr(Literal(Constant(s"${pos.source.path}: line ${pos.line}, column ${pos.column}")))
}

That should work with 2.11 as well, although this way of creating the AST seems deprecated.

You can also have a look at that excerpt of my project Scart; it's how I use this technique to emit traces for debugging purposes.




回答3:


The example in 'Writing Scala Compiler Plugins' shows how to access the line name and current number of the current position, as the others answers have mentioned.

http://www.scala-lang.org/old/node/140

In addition to the answers above, you can also get the position from the AST returned from a CompilationUnit.

For example:

def apply(unit: CompilationUnit) {
    // Get the AST
    val tree = unit.body  

    // Get the Position
    // Scala.util.parsing.input.Position
    val myPos = tree.pos 

    // Do something with the pos
    unit.warning(pos, "Hello world")
}


来源:https://stackoverflow.com/questions/23260990/access-code-file-and-line-number-from-scala-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!