Access code file and line number from Scala macro?
Asked Answered
A

3

3

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

Absorbance answered 24/4, 2014 at 6:3 Comment(0)
F
4

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
  }
Firewater answered 24/4, 2014 at 6:34 Comment(0)
F
1

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.

Fibster answered 24/4, 2014 at 19:19 Comment(1)
>$ Actually I know that, but did this on purpose: the 1st reaction is "what is that ugly thing that should not be?", then you find out it's a macro, whether you're beginner or expert. From my experience, macros in any language can be source of great confusion. As a developer I want to know what is a macro, hence this awkward but eye-catching trick :) Conversely, by using canonical names for macros, readers believe it's just another regular method/function; then when a sequence doesn't behave as expected (and of course the doc isn't ready yet), many hours can be lost to find the bug. Cheers.Fibster
H
0

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")
}
Hershberger answered 25/4, 2014 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.