Created
August 20, 2010 13:20
-
-
Save jto/540292 to your computer and use it in GitHub Desktop.
This file contains 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
package jto.demo | |
case class Log(v: String){ | |
def log = print("[LOG] " + v) | |
} | |
trait LogConv{ | |
implicit def toLog(v: String): Log = Log(v) | |
} | |
object HelloWorld extends LogConv{ | |
def main(args: Array[String]) { | |
println("Hello, world!") | |
// ==> "Hello, world!".log | |
} | |
} |
This file contains 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
package jto.scala.compiler.plugins | |
import scala.tools.nsc._ | |
import scala.tools.nsc.plugins._ | |
import scala.tools.nsc.ast.parser._ | |
/** | |
* Replace "println" in AST by "log" | |
*/ | |
class NoPrint(val global: scala.tools.nsc.Global) extends Plugin with Parsers{ | |
import global._ | |
val name = "NoPrint" | |
val description = """Find all "println"""" | |
val components = List[PluginComponent](NoPrintComponent) | |
object DropPrint extends Transformer{ | |
override def transform(tree: Tree) = { | |
tree match { | |
case Apply( id @ Ident(name), params) if(name == newTermName("println")) => { | |
reporter.info(id.pos, "println found!", true) | |
Select(params.head, newTermName("log")) | |
} | |
case _ => super.transform(tree) | |
} | |
} | |
} | |
private object NoPrintComponent extends PluginComponent{ | |
val global: NoPrint.this.global.type = NoPrint.this.global | |
val runsAfter = List[String]() | |
val phaseName = NoPrint.this.name | |
//We run this just after the parser phase | |
override val runsRightAfter= Some("parser") | |
def newPhase(_prev: Phase) = new NoPrintPhase(_prev) | |
class NoPrintPhase(_prev: Phase) extends StdPhase(_prev){ | |
override def name = NoPrint.this.name | |
def apply(unit: CompilationUnit) = { | |
//NoPrint.this.global.treeBrowser.browse(unit.body) | |
unit.body = DropPrint.transform(unit.body) | |
NoPrint.this.global.treeBrowser.browse(unit.body) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment