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
|
package scalatex
package stages
import acyclic.file
import scala.reflect.macros.whitebox.Context
import scala.reflect.internal.util.{Position, OffsetPosition}
/**
* Walks the parsed AST, converting it into a structured Scala c.Tree
*/
object Compiler{
def apply(c: Context)(fragPos: c.Position, template: Ast.Block): c.Tree = {
import c.universe._
def fragType = tq"scalatags.Text.all.Frag"
def incPosRec(trees: c.Tree, offset: Int): trees.type = {
println(s"incPosRec\t$offset\t$trees")
trees.foreach(incPos(_, offset))
trees
}
def incPos(tree: c.Tree, offset: Int): tree.type = {
println(s"incPos\t$offset\t$tree")
val current = if (tree.pos == NoPosition) 0 else tree.pos.point
c.internal.setPos(tree,
new OffsetPosition(
fragPos.source,
offset + current + fragPos.point
).asInstanceOf[c.universe.Position]
)
tree
}
println(template)
def compileChain(code: String, parts: Seq[Ast.Chain.Sub], offset: Int): c.Tree = {
println("CODE " + code)
val out = parts.foldLeft(incPosRec(c.parse(code), offset + 1)){
case (curr, Ast.Chain.Prop(str, offset2)) =>
incPos(q"$curr.${TermName(str)}", offset2 + 1)
case (curr, Ast.Chain.Args(str, offset2)) =>
val Apply(fun, args) = c.parse(s"omg$str")
incPos(Apply(curr, args.map(incPosRec(_, offset2 - 2))), offset2)
case (curr, Ast.Chain.TypeArgs(str, offset2)) =>
val TypeApply(fun, args) = c.parse(s"omg$str")
incPos(TypeApply(curr, args.map(incPosRec(_, offset2 - 2))), offset2)
case (curr, Ast.Block(parts, offset)) =>
q"$curr(..${compileBlock(parts, offset)})"
case (curr, Ast.Header(header, block, offset)) =>
q"$curr(${compileHeader(header, block, offset)})"
}
out.foreach(o => println(o.pos + "\t" + o))
out
}
def compileBlock(parts: Seq[Ast.Block.Sub], offset: Int): Seq[c.Tree] = {
parts.map{
case Ast.Block.Text(str, _) => q"$str"
case Ast.Chain(code, parts, offset) => compileChain(code, parts, offset)
case Ast.Header(header, block, offset) => compileHeader(header, block, offset)
case b @ Ast.Block.IfElse(condString, Ast.Block(parts2, offset2), elseBlock, offset) =>
println("AST " + b)
val If(cond, _, _) = c.parse(condString + "{}")
val elseCompiled = elseBlock match{
case Some(Ast.Block(parts3, offset3)) => wrapBlock(compileBlock(parts3, offset3))
case None => EmptyTree
}
val res = If(cond, wrapBlock(compileBlock(parts2, offset2)), elseCompiled)
println("Tree " + res)
res
case Ast.Block.For(generators, Ast.Block(parts2, offset2), offset) =>
val fresh = c.fresh()
val tree = c.parse(s"$generators yield $fresh" )
def rec(t: Tree): Tree = t match {
case a @ Apply(fun, List(f @ Function(vparams, body))) =>
val f2 = Function(vparams, rec(body))
val a2 = Apply(fun, List(f2))
a2
case Ident(x: TermName) if x.decoded == fresh =>
wrapBlock(compileBlock(parts2, offset2))
}
val out = rec(tree)
println(out)
out
}
}
def compileHeader(header: String, block: Ast.Block, offset: Int): c.Tree = {
val Block(stmts, expr) = c.parse(s"{$header\n ()}")
Block(stmts, wrapBlock(compileBlock(block.parts, block.offset)))
}
def wrapBlock(items: Seq[c.Tree]) = {
q"Seq[$fragType](..$items)"
}
val res = wrapBlock(compileBlock(template.parts, template.offset))
println("::::::::::::::::::::::::::::::::::::::::::::::::")
println(res)
println("::::::::::::::::::::::::::::::::::::::::::::::::")
res
}
}
|