scala> import scala.meta._
import scala.meta._
scala> val res2 = "foo + bar".parse[Term]
res2: scala.meta.parsers.Parsed[scala.meta.Term] = foo + bar
scala> val res3 = res2.get
res3: scala.meta.Term = foo + bar
scala> val res4 = res3.structure
res4: String = Term.ApplyInfix(Term.Name("foo"), Term.Name("+"), Nil, Seq(Term.Name("bar")))
scala> val res5 = "foo.+(bar)".parse[Term].get
res5: scala.meta.Term = foo.+(bar)
scala> val res6 = res5.structure
res6: String = Term.Apply(Term.Select(Term.Name("foo"), Term.Name("+")), Seq(Term.Name("bar")))
scala> val res7 = """
| def foo = bar
| /* hello world */
| foo
| """
res7: String =
"
def foo = bar
/* hello world */
foo
"
scala> res7.parse[Term]
res0: scala.meta.parsers.Parsed[scala.meta.Term] =
<input>:2: error: illegal start of simple expression
def foo = bar
^
scala> val res9 = ("{" + res7 + "}").parse[Term]
res9: scala.meta.parsers.Parsed[scala.meta.Term] =
{
def foo = bar
/* hello world */
foo
}
scala> res9.get.structure
res1: String = Term.Block(Seq(Defn.Def(Nil, Term.Name("foo"), Nil, Nil, None, Term.Name("bar")), Term.Name("foo")))
scala> res9.get.tokens
res2: scala.meta.tokens.Tokens =
Tokens(, {,
, , , , , , def, , foo, , =, , bar,
, , , , , , , , , , , , , /* hello world */,
, , , , foo,
, }, )
scala> res9.get.tokens.structure
res3: String = Tokens(BOF [0..0), { [0..1), \n [1..2), [2..3), [3..4), [4..5), [5..6), [6..7), def [7..10), [10..11), foo [11..14), [14..15), = [15..16), [16..17), bar [17..20), \n [20..21), [21..22), [22..23), [23..24), [24..25), [25..26), [26..27), [27..28), [28..29), [29..30), [30..31), [31..32), [32..33), /* hello world */ [33..50), \n [50..51), [51..52), [52..53), [53..54), foo [54..57), \n [57..58), } [58..59), EOF [59..59))
scala> // sbt support
| val buildsbt = new java.io.File("build.sbt")
buildsbt: java.io.File = build.sbt
scala> buildsbt.parse[Source] // fails, sbt file
res5: scala.meta.parsers.Parsed[scala.meta.Source] =
build.sbt:1: error: expected class or object definition
name := "Scala Days"
^
scala> import scala.meta.dialects.Sbt0137
import scala.meta.dialects.Sbt0137
scala> val res18 = Sbt0137(buildsbt).parse[Source]
res18: scala.meta.parsers.Parsed[scala.meta.Source] =
name := "Scala Days"
scalaVersion := "2.11.8"
version := "0.0.1-SNAPSHOT"
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
scalacOptions ++= Seq("-feature","-deprecation", "-unchecked", "-Xlint")
scalacOptions in Test ++= Seq("-Yrangepos")
testOptions in Test := Seq(
Tests.Argument(TestFrameworks.Specs2, "-xonly")
)
libraryDependencies ++= Seq(
"org.specs2" %% "specs2-core" % "3.6" % "test",
"org.specs2" %% "specs2-mock" % "3.6" % "test",
"org.scalameta" %% "scalameta" % "0.20.0"
)
tutSettings
scala> res18.get.structure
res6: String = Source(Seq(Term.ApplyInfix(Term.Name("name"), Term.Name(":="), Nil, Seq(Lit("Scala Days"))), Term.ApplyInfix(Term.Name("scalaVersion"), Term.Name(":="), Nil, Seq(Lit("2.11.8"))), Term.ApplyInfix(Term.Name("version"), Term.Name(":="), Nil, Seq(Lit("0.0.1-SNAPSHOT"))), Term.ApplyInfix(Term.Name("resolvers"), Term.Name("+="), Nil, Seq(Term.ApplyInfix(Lit("scalaz-bintray"), Term.Name("at"), Nil, Seq(Lit("http://dl.bintray.com/scalaz/releases"))))), Term.ApplyInfix(Term.Name("scalacOptions"), Term.Name("++="), Nil, Seq(Term.Apply(Term.Name("Seq"), Seq(Lit("-feature"), Lit("-deprecation"), Lit("-unchecked"), Lit("-Xlint"))))), Term.ApplyInfix(Term.ApplyInfix(Term.Name("scalacOptions"), Term.Name("in"), Nil, Seq(Term.Name("Test"))), Term.Name("++="), Ni...
scala> val foo = q"foo"
foo: meta.Term.Name = foo
scala> q"$foo + bar"
res7: meta.Term.ApplyInfix = foo + bar
scala> val q"$x + $y" = q"$foo + bar"
x: scala.meta.Term = foo
y: meta.Term.Arg = bar
Last active
May 12, 2016 13:55
-
-
Save williamho/5d143e29f2b2ea695b2ef79a29198e9c to your computer and use it in GitHub Desktop.
Demo from scaladays
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment