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 | |
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')
3 files changed, 46 insertions, 42 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) } } } diff --git a/scalatexApi/src/test/scala/scalatex/ParserTests.scala b/scalatexApi/src/test/scala/scalatex/ParserTests.scala index 7454fbb..7cd5002 100644 --- a/scalatexApi/src/test/scala/scalatex/ParserTests.scala +++ b/scalatexApi/src/test/scala/scalatex/ParserTests.scala @@ -103,7 +103,8 @@ object ParserTests extends utest.TestSuite{ Ast.Header( "import math.abs\nimport math.sin", Ast.Block( - Seq(Text("\n"), Text("\n"), Text("hello world"), Text("\n")) + Seq(Text("\n"), Text("\n"), Text("hello world"), Text("\n")), + 33 ) ) ) @@ -119,14 +120,14 @@ object ParserTests extends utest.TestSuite{ } 'Block{ - * - check("{i am a cow}", _.BraceBlock.run(), Block(Seq(Block.Text("i am a cow")))) + * - check("{i am a cow}", _.BraceBlock.run(), Block(Seq(Block.Text("i am a cow")), 1)) * - check("{i @am a @cow}", _.BraceBlock.run(), Block(Seq( Block.Text("i "), Chain("am",Seq(), 3), Block.Text(" a "), Chain("cow",Seq(), 9) - )) + ), 1) ) } 'Chain{ @@ -141,10 +142,10 @@ object ParserTests extends utest.TestSuite{ ) * - check("@omg{bbq}.cow(moo){a @b}\n", _.ScalaChain.run(), Chain("omg",Seq( - Block(Seq(Block.Text("bbq"))), + Block(Seq(Block.Text("bbq")), 5), Chain.Prop("cow", 9), Chain.Args("(moo)", 13), - Block(Seq(Block.Text("a "), Chain("b", Nil, 21))) + Block(Seq(Block.Text("a "), Chain("b", Nil, 21)), 19) )) ) } @@ -153,7 +154,7 @@ object ParserTests extends utest.TestSuite{ 'for - check( "@for(x <- 0 until 3){lol}", _.ForLoop.run(), - For("for(x <- 0 until 3)", Block(Seq(Text("lol")))) + For("for(x <- 0 until 3)", Block(Seq(Text("lol")), 21)) ) 'forBlock - check( """ @@ -162,7 +163,7 @@ object ParserTests extends utest.TestSuite{ _.Body.run(), Block(Seq( Text("\n"), - For("for(x <- 0 until 3)", Block(Seq(Text("\n "), Text("lol")))) + For("for(x <- 0 until 3)", Block(Seq(Text("\n "), Text("lol")), 21)) )) ) 'forBlockBraces - check( @@ -173,7 +174,7 @@ object ParserTests extends utest.TestSuite{ _.Body.run(), Block(Seq( Text("\n"), - For("for(x <- 0 until 3)", Block(Seq(Text("\n "), Text("lol"), Text("\n")))) + For("for(x <- 0 until 3)", Block(Seq(Text("\n "), Text("lol"), Text("\n")), 22)) )) ) } @@ -181,19 +182,19 @@ object ParserTests extends utest.TestSuite{ 'if - check( "@if(true){lol}", _.IfElse.run(), - IfElse("if(true)", Block(Seq(Text("lol"))), None) + IfElse("if(true)", Block(Seq(Text("lol")), 10), None) ) 'ifElse - check( "@if(true){lol}else{ omg }", _.IfElse.run(), - IfElse("if(true)", Block(Seq(Text("lol"))), Some(Block(Seq(Text(" omg "))))) + IfElse("if(true)", Block(Seq(Text("lol")), 10), Some(Block(Seq(Text(" omg ")), 19))) ) 'ifBlock - check( """ |@if(true) | omg""".stripMargin, _.IfElse.run(), - IfElse("if(true)", Block(Seq(Text("\n "), Text("omg"))), None) + IfElse("if(true)", Block(Seq(Text("\n "), Text("omg")), 10), None) ) 'ifBlockElseBlock - check( """ @@ -204,8 +205,8 @@ object ParserTests extends utest.TestSuite{ _.IfElse.run(), IfElse( "if(true)", - Block(Seq(Text("\n "), Text("omg"))), - Some(Block(Seq(Text("\n "), Text("wtf")))) + Block(Seq(Text("\n "), Text("omg")), 10), + Some(Block(Seq(Text("\n "), Text("wtf")), 22)) ) ) 'ifBlockElseBraceBlock - check( @@ -217,8 +218,8 @@ object ParserTests extends utest.TestSuite{ _.IfElse.run(), IfElse( "if(true)", - Block(Seq(Text("\n "), Text("omg"), Text("\n"))), - Some(Block(Seq(Text("\n "), Text("wtf"), Text("\n")))) + Block(Seq(Text("\n "), Text("omg"), Text("\n")), 10), + Some(Block(Seq(Text("\n "), Text("wtf"), Text("\n")), 23)) ) ) 'ifBlockElseBraceBlockNested - { @@ -236,11 +237,14 @@ object ParserTests extends utest.TestSuite{ Text("\n"), Chain("p",Vector(Block(Vector( Text("\n "), - IfElse("if(true)", Block(Vector( - Text("\n "), Text("Hello"), Text("\n "))), + IfElse("if(true)", + Block(Vector( + Text("\n "), Text("Hello"), Text("\n ") + ), 16), Some(Block(Vector( - Text("\n "), Text("lols"), Text("\n ")))) - )))), 1), + Text("\n "), Text("lols"), Text("\n ") + ), 35)) + )), 3)), 1), Text("\n") )) assert(res == expected) @@ -253,8 +257,8 @@ object ParserTests extends utest.TestSuite{ _.IfElse.run(), IfElse( "if(true)", - Block(Seq(Text("\n "), Text("omg"), Text("\n"))), - Some(Block(Seq(Text("\n "), Text("wtf")))) + Block(Seq(Text("\n "), Text("omg"), Text("\n")), 10), + Some(Block(Seq(Text("\n "), Text("wtf")), 22)) ) ) } @@ -277,9 +281,9 @@ object ParserTests extends utest.TestSuite{ Chain("bbq",Seq(Block(Seq( Text("\n "), Chain("lol",Seq(), 16) - ))), 12) - ))), 8) - ))), 1) + ), 9)), 12) + ), 7)), 8) + ), 5)), 1) )) ) 'dedents - check( @@ -294,7 +298,8 @@ object ParserTests extends utest.TestSuite{ Seq( Text("\n "), Chain("wtf",Seq(), 8) - ) + ), + 5 )), 1), Text("\n"), Chain("bbq", Seq(), 13) @@ -314,7 +319,8 @@ object ParserTests extends utest.TestSuite{ Text("\n "), Chain("wtf",Seq(), 9), Text("\n") - ) + ), + 6 )), 1), Text("\n"), Chain("bbq", Seq(), 16) @@ -333,7 +339,7 @@ object ParserTests extends utest.TestSuite{ Block(Seq( Text("\n "), Chain("wtf",Seq(), 21) - )) + ), 18) ), 1), Text("\n"), Text("bbq") |