summaryrefslogtreecommitdiff
path: root/scalatexApi
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-11-05 22:57:36 -0800
committerLi Haoyi <haoyi@dropbox.com>2014-11-05 22:57:36 -0800
commit75e505e6a433657cff28501f296605012e1e759a (patch)
tree8fc20469bf01b549e229cc9f3960b2524686dc43 /scalatexApi
parent7ced5349435f0c6699c048576e09cede37245452 (diff)
downloadhands-on-scala-js-75e505e6a433657cff28501f296605012e1e759a.tar.gz
hands-on-scala-js-75e505e6a433657cff28501f296605012e1e759a.tar.bz2
hands-on-scala-js-75e505e6a433657cff28501f296605012e1e759a.zip
The book compiles again!
Diffstat (limited to 'scalatexApi')
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Compiler.scala37
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Parser.scala2
2 files changed, 20 insertions, 19 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
index af474d0..43c23a8 100644
--- a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
+++ b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
@@ -16,12 +16,12 @@ object Compiler{
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(
@@ -32,9 +32,8 @@ object Compiler{
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)
@@ -45,15 +44,15 @@ object Compiler{
val TypeApply(fun, args) = c.parse(s"omg$str")
incPos(TypeApply(curr, args.map(incPosRec(_, offset2 - 2))), offset2)
case (curr, Ast.Block(parts, offset1)) =>
- incPos(q"$curr(${compileBlock(parts, offset1)})", offset1)
+ incPos(q"$curr(..${compileBlock(parts, offset1)})", offset1)
case (curr, Ast.Header(header, block, offset1)) =>
incPos(q"$curr(${compileHeader(header, block, offset1)})", offset1)
}
- out.foreach(o => println(o.pos + "\t" + o))
- out
+
+ q"$out: $fragType"
}
- def compileBlock(parts: Seq[Ast.Block.Sub], offset: Int): c.Tree = {
+ def compileBlock(parts: Seq[Ast.Block.Sub], offset: Int): Seq[c.Tree] = {
val res = parts.map{
case Ast.Block.Text(str, offset1) =>
incPos(q"$str", offset1)
@@ -64,11 +63,11 @@ object Compiler{
case Ast.Block.IfElse(condString, Ast.Block(parts2, offset2), elseBlock, offset1) =>
val If(cond, _, _) = c.parse(condString + "{}")
val elseCompiled = elseBlock match{
- case Some(Ast.Block(parts3, offset3)) => compileBlock(parts3, offset3)
+ case Some(Ast.Block(parts3, offset3)) => compileBlockWrapped(parts3, offset3)
case None => EmptyTree
}
- val res = If(incPosRec(cond, offset1 + 2), compileBlock(parts2, offset2), elseCompiled)
+ val res = If(incPosRec(cond, offset1 + 2), compileBlockWrapped(parts2, offset2), elseCompiled)
println("Tree " + res)
incPos(res, offset1)
@@ -84,24 +83,26 @@ object Compiler{
val a2 = Apply(fun, List(f2))
a2
case Ident(x: TermName) if x.decoded == fresh =>
- compileBlock(parts2, offset2)
+ compileBlockWrapped(parts2, offset2)
}
val out = rec(tree)
println(out)
- out
+
+ q"$out: $fragType"
+
}
- incPos(q"Seq[$fragType](..$res)", offset)
+ res
+ }
+ def compileBlockWrapped(parts: Seq[Ast.Block.Sub], offset: Int): c.Tree = {
+ incPos(q"Seq[$fragType](..${compileBlock(parts, offset)})", offset)
}
def compileHeader(header: String, block: Ast.Block, offset: Int): c.Tree = {
val Block(stmts, expr) = c.parse(s"{$header\n ()}")
- Block(stmts, compileBlock(block.parts, block.offset))
+ Block(stmts, compileBlockWrapped(block.parts, block.offset))
}
- val res = compileBlock(template.parts, template.offset)
- println("::::::::::::::::::::::::::::::::::::::::::::::::")
- println(res)
- println("::::::::::::::::::::::::::::::::::::::::::::::::")
+ val res = compileBlockWrapped(template.parts, template.offset)
res
}
} \ No newline at end of file
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
index 06659d0..3b79878 100644
--- a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
+++ b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
@@ -105,7 +105,7 @@ class Parser(input: ParserInput, indent: Int = 0, offset: Int = 0) extends Scala
def TypeArgs2 = rule { '[' ~ Ws ~ Types ~ ']' }
def ArgumentExprs2 = rule {
'(' ~ Ws ~
- (optional(Exprs ~ ',' ~ Ws) ~ PostfixExpr ~ ':' ~ Ws ~ '_' ~ Ws ~ '*' ~ Ws | run(println("ArgumentExprs2 B")) ~ optional(Exprs) ~ run(println("ArgumentExprs2 C"))) ~
+ (optional(Exprs ~ ',' ~ Ws) ~ PostfixExpr ~ ':' ~ Ws ~ '_' ~ Ws ~ '*' ~ Ws | optional(Exprs) ) ~
')'
}
def BlockExpr2: Rule0 = rule { '{' ~ Ws ~ (CaseClauses | Block) ~ '}' }