summaryrefslogtreecommitdiff
path: root/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
blob: 7d1492ca5fbc0d382295406c735bfa8c93bc7466 (plain) (blame)
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
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"

    println(template)
    def compileChain(code: String, parts: Seq[Ast.Chain.Sub], offset: Int): c.Tree = {
      println("CODE " + code)
      parts.foldLeft(c.parse(code)){
        case (curr, Ast.Chain.Prop(str, offset2)) => q"$curr.${TermName(str)}"
        case (curr, Ast.Chain.Args(str, offset2)) =>
          val Apply(fun, args) = c.parse(s"omg$str")
          Apply(curr, args)
        case (curr, Ast.Chain.TypeArgs(str, offset2)) =>
          val TypeApply(fun, args) = c.parse(s"omg$str")
          TypeApply(curr, args)
        case (curr, Ast.Block(parts, offset)) =>
          q"$curr(..${compileBlock(parts, offset)})"
        case (curr, Ast.Header(header, block, offset)) =>
          q"$curr(${compileHeader(header, block, offset)})"

      }
    }
    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
  }
}