Tool for generating source code from Scala ASTs.
Can be used to:
- increase macros support in the IDE
- simplify macro debugging
- generate code from transformed trees
For information on Sprinter please refer to the project page.
To publish library to local repo run (from project's root directory):
$ sbt publish-local
Target jar should have similar path:
/path/to/.ivy2/local/org.scala-lang/sprinter_2.10/0.2.0/jars/sprinter_2.10.jar
See http://scala-sbt.org/release/docs/Getting-Started/Setup.html for instructions to setup sbt.
In the target project add to project's build file following options:
libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.2",
"org.scala-lang" %% "sprinter" % "0.2.0")
If you use sprinter as a dependency for compiler plugin it's required to add lib's jar to scalac toolcp or include printer's classes to project's jar.
In the case of sbt-based project you can use assembly plugin - https://github.com/sbt/sbt-assembly to create common jar.
Example can be found in printPlugin's build file - https://github.com/VladimirNik/printPlugin/blob/master/project/SourcePrinter.scala.
For default projects to add sprinter's jar to scalac toolcp you can use compiler's toolcp option:
$ scalac -toolcp /path/to/jar/sprinter_2.10.jar -Xplugin:/path/to/plugin/printplugin-2.10.jar hello/world/*.scala
To use sprinter import PrettyPrinters class:
import scala.sprinter.printers.PrettyPrinters
Create its instance (using object of type nsc.Global):
val printers = PrettyPrinters(global)
and then pass to printers show method required AST:
printers.show(tree)
For tree:
PackageDef(
Select(Ident(newTermName("hello")), newTermName("world")),
List(
ModuleDef(Modifiers(), newTermName("Main"),
Template(List(Select(Ident(scala), newTypeName("AnyRef"))),
emptyValDef,
List(
DefDef(Modifiers(), nme.CONSTRUCTOR, List(),
List(List()), TypeTree(),
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())),
Literal(Constant(())))),
DefDef(Modifiers(), newTermName("main"), List(),
List(List(ValDef(Modifiers(PARAM), newTermName("args"),
AppliedTypeTree(Ident(newTypeName("Array")), List(Ident(newTypeName("String")))), EmptyTree))),
Select(Ident(scala), newTypeName("Unit")),
Apply(Ident(newTermName("println")),
List(Literal(Constant("Hello, world!"))))))
)
)
)
)
we'll have the following generated source (version 0.2.0):
package hello.world {
object Main {
def main(args: Array[String]): scala.Unit = println("Hello, world!")
}
}
- https://github.com/VladimirNik/printPlugin - plugin that uses sprinter to regenerate project's sources based on ASTs.
- https://github.com/VladimirNik/treePrintTester - example of printPlugin usage.
- http://scala-sbt.org/release/docs/Getting-Started/Setup.html - sbt setup
- site template is based on https://github.com/jsuereth/jsuereth.github.com