, , API . , API , , , , .
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.reflect.io.AbstractFile
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.internal.util.OffsetPosition
class Impl(val c: Context) {
def impl: c.Tree = {
val filePath = "foo.txt"
val af = AbstractFile.getFile(filePath)
val content = scala.io.Source.fromFile(filePath).mkString
val sf = new BatchSourceFile(af, content)
val pos = new OffsetPosition(sf, 3).asInstanceOf[c.universe.Position]
c.abort(pos, "it works")
}
}
object Macros {
def foo: Any = macro Impl.impl
}
object Test extends App {
Macros.foo
}
:
20:56 ~/Projects/Master/sandbox (master)$ cat foo.txt
hello
world
20:56 ~/Projects/Master/sandbox (master)$ scalac Test.scala
foo.txt:1: error: it works
hello
^
one error found
, scala.reflect.internal ( , scala-reflect.jar), , .