Last active
August 29, 2015 14:01
-
-
Save arjunguha/723389cce08226a3e759 to your computer and use it in GitHub Desktop.
Yrangepos error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion in ThisBuild := "2.11.1" | |
scalacOptions in ThisBuild += "-Yrangepos" | |
libraryDependencies in ThisBuild += "org.scala-lang" % "scala-reflect" % "2.11.1" | |
lazy val root = project.aggregate(client, macro) | |
lazy val macro = project | |
lazy val client = project.dependsOn(macro) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Place in client subdirectory | |
import Macro._ | |
object Main extends App { | |
mymacro { | |
println("Printed range") | |
println("Should span several characters, but does not :(") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Place in macro subdirectory | |
import scala.language.experimental.macros | |
import scala.reflect.macros.blackbox.Context | |
object Macro { | |
def macroImpl(c: Context)(body : c.Expr[Unit]) : c.Expr[Unit] = { | |
import c.universe._ | |
val p = body.tree.pos | |
val str = s"Line: ${p.line}. Character range: ${p.start} --- ${p.end}" | |
val loc = c.Expr[String](Literal(Constant(str))) | |
reify { | |
println(loc.splice) | |
body.splice | |
} | |
} | |
def mymacro(body : Unit) : Unit = macro macroImpl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment