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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package scala.tools.reflect
package quasiquotes
import scala.tools.nsc.ast.parser.{Parsers => ScalaParser}
import scala.tools.nsc.ast.parser.Tokens._
import scala.compat.Platform.EOL
import scala.reflect.internal.util.{BatchSourceFile, SourceFile, FreshNameCreator}
import scala.collection.mutable.ListBuffer
import scala.util.Try
/** Builds upon the vanilla Scala parser and teams up together with Placeholders.scala to emulate holes.
* A principled solution to splicing into Scala syntax would be a parser that natively supports holes.
* Unfortunately, that's outside of our reach in Scala 2.11, so we have to emulate.
*/
trait Parsers { self: Quasiquotes =>
import global.{Try => _, _}
abstract class Parser extends {
val global: self.global.type = self.global
} with ScalaParser {
def parse(code: String): Tree = {
try {
val file = new BatchSourceFile(nme.QUASIQUOTE_FILE, code)
new QuasiquoteParser(file).parseRule(entryPoint)
} catch {
case mi: MalformedInput => c.abort(correspondingPosition(mi.offset), mi.msg)
}
}
def correspondingPosition(offset: Int): Position = {
val posMapList = posMap.toList
def containsOffset(start: Int, end: Int) = start <= offset && offset <= end
def fallbackPosition = posMapList match {
case (pos1, (start1, end1)) :: _ if start1 > offset => pos1
case _ :+ ((pos2, (start2, end2))) if offset > end2 => pos2.withPoint(pos2.point + (end2 - start2))
}
posMapList.sliding(2).collect {
case (pos1, (start1, end1)) :: _ if containsOffset(start1, end1) => (pos1, offset - start1)
case (pos1, (_, end1)) :: (_, (start2, _)) :: _ if containsOffset(end1, start2) => (pos1, end1)
case _ :: (pos2, (start2, end2)) :: _ if containsOffset(start2, end2) => (pos2, offset - start2)
}.map { case (pos, offset) =>
pos.withPoint(pos.point + offset)
}.toList.headOption.getOrElse(fallbackPosition)
}
override def token2string(token: Int): String = token match {
case EOF => "end of quote"
case _ => super.token2string(token)
}
def entryPoint: QuasiquoteParser => Tree
class QuasiquoteParser(source0: SourceFile) extends SourceFileParser(source0) { parser =>
def isHole: Boolean = isIdent && isHole(in.name)
def isHole(name: Name): Boolean = holeMap.contains(name)
override implicit def fresh: FreshNameCreator = new FreshNameCreator {
override def newName(prefix: String) = super.newName(nme.QUASIQUOTE_PREFIX + prefix)
}
override val treeBuilder = new ParserTreeBuilder {
override implicit def fresh: FreshNameCreator = parser.fresh
// q"(..$xs)"
override def makeTupleTerm(trees: List[Tree]): Tree =
Apply(Ident(nme.QUASIQUOTE_TUPLE), trees)
// tq"(..$xs)"
override def makeTupleType(trees: List[Tree]): Tree =
AppliedTypeTree(Ident(tpnme.QUASIQUOTE_TUPLE), trees)
// q"{ $x }"
override def makeBlock(stats: List[Tree]): Tree = stats match {
case (head @ Ident(name)) :: Nil if isHole(name) => Block(Nil, head)
case _ => super.makeBlock(stats)
}
// tq"$a => $b"
override def makeFunctionTypeTree(argtpes: List[Tree], restpe: Tree): Tree =
AppliedTypeTree(Ident(tpnme.QUASIQUOTE_FUNCTION), argtpes :+ restpe)
}
import treeBuilder.{global => _, unit => _, _}
// q"def foo($x)"
override def allowTypelessParams = true
// q"foo match { case $x }"
override def caseClause(): CaseDef =
if (isHole && lookingAhead { in.token == CASE || in.token == RBRACE || in.token == SEMI }) {
val c = makeCaseDef(Apply(Ident(nme.QUASIQUOTE_CASE), List(Ident(ident()))), EmptyTree, EmptyTree)
while (in.token == SEMI) in.nextToken()
c
} else
super.caseClause()
override def caseBlock(): Tree = super.caseBlock() match {
case Block(Nil, expr) => expr
case other => other
}
override def isAnnotation: Boolean = super.isAnnotation || (isHole && lookingAhead { isAnnotation })
override def isModifier: Boolean = super.isModifier || (isHole && lookingAhead { isModifier })
override def isLocalModifier: Boolean = super.isLocalModifier || (isHole && lookingAhead { isLocalModifier })
override def isTemplateIntro: Boolean = super.isTemplateIntro || (isHole && lookingAhead { isTemplateIntro })
override def isDclIntro: Boolean = super.isDclIntro || (isHole && lookingAhead { isDclIntro })
override def isStatSep(token: Int) = token == EOF || super.isStatSep(token)
override def expectedMsg(token: Int): String =
if (isHole) expectedMsgTemplate(token2string(token), "splicee")
else super.expectedMsg(token)
// $mods def foo
// $mods T
override def readAnnots(annot: => Tree): List[Tree] = in.token match {
case AT =>
in.nextToken()
annot :: readAnnots(annot)
case _ if isHole && lookingAhead { isAnnotation || isModifier || isDefIntro || isIdent || isStatSep || in.token == LPAREN } =>
val ann = Apply(Select(New(Ident(tpnme.QUASIQUOTE_MODS)), nme.CONSTRUCTOR), List(Literal(Constant(in.name.toString))))
in.nextToken()
ann :: readAnnots(annot)
case _ =>
Nil
}
override def refineStat(): List[Tree] =
if (isHole && !isDclIntro) {
val result = ValDef(NoMods, in.name, Ident(tpnme.QUASIQUOTE_REFINE_STAT), EmptyTree) :: Nil
in.nextToken()
result
} else super.refineStat()
override def ensureEarlyDef(tree: Tree) = tree match {
case Ident(name: TermName) if isHole(name) => ValDef(NoMods | Flag.PRESUPER, name, Ident(tpnme.QUASIQUOTE_EARLY_DEF), EmptyTree)
case _ => super.ensureEarlyDef(tree)
}
override def isTypedParam(tree: Tree) = super.isTypedParam(tree) || (tree match {
case Ident(name) if isHole(name) => true
case _ => false
})
override def topStat = super.topStat.orElse {
case _ if isHole =>
val stats = ValDef(NoMods, in.name, Ident(tpnme.QUASIQUOTE_PACKAGE_STAT), EmptyTree) :: Nil
in.nextToken()
stats
}
}
}
object TermParser extends Parser {
def entryPoint = { parser => gen.mkTreeOrBlock(parser.templateOrTopStatSeq()) }
}
object TypeParser extends Parser {
def entryPoint = _.typ()
}
object CaseParser extends Parser {
def entryPoint = _.caseClause()
}
object PatternParser extends Parser {
def entryPoint = { parser =>
val pat = parser.noSeq.pattern1()
parser.treeBuilder.patvarTransformer.transform(pat)
}
}
object FreshName {
def unapply(name: Name): Option[String] =
name.toString.split("\\$") match {
case Array(qq, left, right) if qq + "$" == nme.QUASIQUOTE_PREFIX && Try(right.toInt).isSuccess =>
Some(left + "$")
case _ =>
None
}
}
}
|