Guest User

Macro

a guest
Feb 25th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     import c.universe._
  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.     val base = new File( "." ).toURI
  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.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment