1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package scala.tools.reflect
package quasiquotes
import scala.reflect.macros.runtime.Context
abstract class Quasiquotes extends Parsers
with Holes
with Placeholders
with Reifiers {
val c: Context
val global: c.universe.type = c.universe
import c.universe._
def debug(msg: String): Unit =
if (settings.Yquasiquotedebug.value) println(msg)
lazy val (universe: Tree, args, parts, parse, reify, method) = c.macroApplication match {
case Apply(build.SyntacticTypeApplied(Select(Select(Apply(Select(universe0, _), List(Apply(_, parts0))), interpolator0), method0), _), args0) =>
debug(s"\nparse prefix:\nuniverse=$universe0\nparts=$parts0\ninterpolator=$interpolator0\nmethod=$method0\nargs=$args0\n")
val parts1 = parts0.map {
case lit @ Literal(Constant(s: String)) => s -> lit.pos
case part => c.abort(part.pos, "Quasiquotes can only be used with literal strings")
}
val reify0 = method0 match {
case nme.apply => new ApplyReifier().reifyFillingHoles(_)
case nme.unapply => new UnapplyReifier().reifyFillingHoles(_)
case other => global.abort(s"Unknown quasiquote api method: $other")
}
val parse0 = interpolator0 match {
case nme.q => TermParser.parse(_)
case nme.tq => TypeParser.parse(_)
case nme.cq => CaseParser.parse(_)
case nme.pq => PatternParser.parse(_)
case other => global.abort(s"Unknown quasiquote flavor: $other")
}
(universe0, args0, parts1, parse0, reify0, method0)
case _ =>
global.abort(s"Couldn't parse call prefix tree ${c.macroApplication}.")
}
lazy val u = universe // shortcut
lazy val universeTypes = new definitions.UniverseDependentTypes(universe)
def expandQuasiquote = {
debug(s"\nmacro application:\n${c.macroApplication}\n")
debug(s"\ncode to parse:\n$code\n")
val tree = parse(code)
debug(s"parsed:\n${showRaw(tree)}\n$tree\n")
val reified = reify(tree)
val sreified =
reified
.toString
.replace("scala.reflect.runtime.`package`.universe.build.", "")
.replace("scala.reflect.runtime.`package`.universe.", "")
.replace("scala.collection.immutable.", "")
debug(s"reified tree:\n$sreified\n")
reified
}
}
|