summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-11-01 14:00:03 -0700
committerLi Haoyi <haoyi@dropbox.com>2014-11-01 14:00:03 -0700
commit76dab1a2729ada22c3c3a395aae98d3c4532d5ef (patch)
tree178f7c6ba2ddfd28d8bd04bde4c64da5e3d35893
parent65d3ac2eddb3938db3793763b4c03dda69f279c1 (diff)
downloadhands-on-scala-js-76dab1a2729ada22c3c3a395aae98d3c4532d5ef.tar.gz
hands-on-scala-js-76dab1a2729ada22c3c3a395aae98d3c4532d5ef.tar.bz2
hands-on-scala-js-76dab1a2729ada22c3c3a395aae98d3c4532d5ef.zip
encapsulate fragPos
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Compiler.scala50
1 files changed, 27 insertions, 23 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
index da7d0da..f30ee57 100644
--- a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
+++ b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
@@ -24,24 +24,28 @@ object Compiler{
def compileTree(frag: WN.TemplateTree): Tree = {
// println(frag)
- val fragPos = posFor(literalPos.point + frag.offset)
- def fragPosFor(offset: Int) = {
- println(posFor(fragPos.point + offset))
- posFor(fragPos.point + offset)
- }
- def atFragPos(t: Tree, offset: Int) = {
- println(t)
- atPos(fragPosFor(offset))(t)
- }
- def setFragPos(t: Tree, offset: Int) = {
- println(t)
- c.internal.setPos(t, fragPosFor(offset))
- t
+ object fragPos{
+ private val fragPos = posFor(literalPos.point + frag.offset)
+ private def fragPosFor(offset: Int) = {
+ println(posFor(fragPos.point + offset))
+ posFor(fragPos.point + offset)
+ }
+ def at(t: Tree, offset: Int) = {
+ println(t)
+ atPos(fragPosFor(offset))(t)
+ }
+ def set(t: Tree, offset: Int) = {
+ println(t)
+ c.internal.setPos(t, fragPosFor(offset))
+ t
+ }
}
+
+
// println(s"${frag.offset}\n${literalPos.point}\n${pos.point}\n$frag\n")
val f: Tree = frag match {
- case WN.Plain(text, offset) => atPos(fragPos)(q"$text")
+ case WN.Plain(text, offset) => fragPos.at(q"$text", 0)
case WN.Display(exp, offset) => compileTree(exp)
case WN.Comment(msg, offset) => q""
case WN.ScalaExp(Seq(WN.Simple(first, _), WN.Block(ws, args, content, _)), offset)
@@ -51,7 +55,7 @@ object Compiler{
// println("FIRST " + first)
skeleton.foreach{x =>
x
- if (x.pos != NoPosition) setFragPos(x, x.pos.point + 1)
+ if (x.pos != NoPosition) fragPos.set(x, x.pos.point + 1)
}
val b = content.map(compileTree(_))
def rec(t: Tree): Tree = t match {
@@ -72,7 +76,7 @@ object Compiler{
val b1 = content1.map(compileTree(_))
val tree = c.parse(first + "{}").asInstanceOf[If]
tree.foreach{x =>
- setFragPos(x, x.pos.point + 1)
+ fragPos.set(x, x.pos.point + 1)
}
val If(cond, _, _) = tree
val b2 = rest match{
@@ -87,7 +91,7 @@ object Compiler{
val firstTree = c.parse(first)
firstTree.foreach{x =>
- setFragPos(x, x.pos.point)
+ fragPos.set(x, x.pos.point)
}
val s = rest.foldLeft[Tree](firstTree) {
@@ -103,14 +107,14 @@ object Compiler{
val res = t match {
case Apply(fun, args) =>
for(arg <- args; tree <- arg if tree.pos != NoPosition){
- c.internal.setPos(tree, fragPosFor(tree.pos.point + first.length - fresh.length))
+ fragPos.set(tree, tree.pos.point + first.length - fresh.length)
}
Apply(rec(fun), args)
case Select(qualifier, name) => Select(rec(qualifier), name)
case Ident(x: TermName) if x.decoded == fresh => l
}
- atFragPos(res, t.pos.point + first.length - fresh.length)
+ fragPos.at(res, t.pos.point + first.length - fresh.length)
// println(Position.formatMessage(newPos.asInstanceOf[scala.reflect.internal.util.Position], "", true))
res
}
@@ -118,7 +122,7 @@ object Compiler{
case (l, WN.Block(ws, None, content, offset)) =>
val contentTrees = content.map(compileTree(_))
- atFragPos(q"$l(..$contentTrees)", offset)
+ fragPos.at(q"$l(..$contentTrees)", offset)
case (l, WN.Block(ws, Some(args), content, offset)) =>
@@ -129,14 +133,14 @@ object Compiler{
vparams.map(_.foreach { t =>
if (t.pos != NoPosition)
- setFragPos(t, t.pos.point)
+ fragPos.set(t, t.pos.point)
})
val contentTrees = content.map{compileTree(_)}
val func = Function(vparams, q"Seq[$fragType](..$contentTrees)")
- atFragPos(func, skeleton.pos.point)
+ fragPos.at(func, skeleton.pos.point)
val res = q"$l($func)"
- atFragPos(res, offset)
+ fragPos.at(res, offset)
res
}