diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-11-05 21:04:51 -0800 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-11-05 21:04:51 -0800 |
commit | fae3a76404caed88bcb604011f2f68fdbb90f071 (patch) | |
tree | 97b65ba49b11e69c27a5df12325de9ae847dfc2c /scalatexApi/src/main/scala | |
parent | 14ede69b487af5ebb8b0311a410f4d49dc66f540 (diff) | |
download | hands-on-scala-js-fae3a76404caed88bcb604011f2f68fdbb90f071.tar.gz hands-on-scala-js-fae3a76404caed88bcb604011f2f68fdbb90f071.tar.bz2 hands-on-scala-js-fae3a76404caed88bcb604011f2f68fdbb90f071.zip |
Block positions work
Diffstat (limited to 'scalatexApi/src/main/scala')
-rw-r--r-- | scalatexApi/src/main/scala/scalatex/stages/Compiler.scala | 20 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/scalatex/stages/Parser.scala | 8 |
2 files changed, 13 insertions, 15 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala index 9c1c3c4..5021540 100644 --- a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala +++ b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala @@ -45,7 +45,7 @@ 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, offset)) => - q"$curr(..${compileBlock(parts, offset)})" + q"$curr(${compileBlock(parts, offset)})" case (curr, Ast.Header(header, block, offset)) => q"$curr(${compileHeader(header, block, offset)})" @@ -53,8 +53,8 @@ object Compiler{ out.foreach(o => println(o.pos + "\t" + o)) out } - def compileBlock(parts: Seq[Ast.Block.Sub], offset: Int): Seq[c.Tree] = { - parts.map{ + def compileBlock(parts: Seq[Ast.Block.Sub], offset: Int): c.Tree = { + val res = 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) @@ -62,11 +62,11 @@ object Compiler{ println("AST " + b) val If(cond, _, _) = c.parse(condString + "{}") val elseCompiled = elseBlock match{ - case Some(Ast.Block(parts3, offset3)) => wrapBlock(compileBlock(parts3, offset3)) + case Some(Ast.Block(parts3, offset3)) => compileBlock(parts3, offset3) case None => EmptyTree } - val res = If(cond, wrapBlock(compileBlock(parts2, offset2)), elseCompiled) + val res = If(cond, compileBlock(parts2, offset2), elseCompiled) println("Tree " + res) res case Ast.Block.For(generators, Ast.Block(parts2, offset2), offset) => @@ -79,23 +79,21 @@ object Compiler{ val a2 = Apply(fun, List(f2)) a2 case Ident(x: TermName) if x.decoded == fresh => - wrapBlock(compileBlock(parts2, offset2)) + compileBlock(parts2, offset2) } val out = rec(tree) println(out) out } + incPos(q"Seq[$fragType](..$res)", offset) } 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))) + Block(stmts, compileBlock(block.parts, block.offset)) } - def wrapBlock(items: Seq[c.Tree]) = { - q"Seq[$fragType](..$items)" - } - val res = wrapBlock(compileBlock(template.parts, template.offset)) + val res = compileBlock(template.parts, template.offset) println("::::::::::::::::::::::::::::::::::::::::::::::::") println(res) println("::::::::::::::::::::::::::::::::::::::::::::::::") diff --git a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala index d78ec8d..9f41dd8 100644 --- a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala +++ b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala @@ -121,13 +121,13 @@ class Parser(input: ParserInput, indent: Int = 0, offset: Int = 0) extends Scala ScalaChain ~> (Seq(_: Ast.Block.Sub)) } def Body = rule{ - oneOrMore(BodyItem) ~> {x => - Ast.Block(x.flatten) + push(offsetCursor) ~ oneOrMore(BodyItem) ~> {(i, x) => + Ast.Block(x.flatten, i) } } def Body0 = rule{ - zeroOrMore(BodyItem) ~> {x => - Ast.Block(x.flatten) + push(offsetCursor) ~ zeroOrMore(BodyItem) ~> {(i, x) => + Ast.Block(x.flatten, i) } } } |