SHOW:
|
|
- or go back to the newest paste.
1 | import java.io.File | |
2 | import language.experimental.macros | |
3 | import scala.reflect.macros.Context | |
4 | ||
5 | object Macros { | |
6 | // get current line in source code | |
7 | def L_ : Int = macro lineImpl | |
8 | def lineImpl( c: Context ): c.Expr[Int] = { | |
9 | - | |
9 | + | |
10 | val line = Literal( Constant( c.enclosingPosition.line ) ) | |
11 | c.Expr[Int]( line ) | |
12 | } | |
13 | ||
14 | // get current file from source code (relative path) | |
15 | def F_ : String = macro fileImpl | |
16 | def fileImpl( c: Context ): c.Expr[String] = { | |
17 | import c.universe._ | |
18 | val absolute = c.enclosingPosition.source.file.file.toURI | |
19 | - | |
19 | + | |
20 | val path = Literal( Constant( c.enclosingPosition.source.file.file.getName() ) ) | |
21 | c.Expr[String]( path ) | |
22 | } | |
23 | ||
24 | // get current class/object (a bit sketchy) | |
25 | def C_ : String = macro classImpl | |
26 | def classImpl( c: Context ): c.Expr[String] = { | |
27 | import c.universe._ | |
28 | ||
29 | val class_ = Literal( Constant( c.enclosingClass.toString.split(" ")( 1 ) ) ) | |
30 | c.Expr[String]( class_ ) | |
31 | } | |
32 | - | |
32 | + | |
33 | } |