summaryrefslogtreecommitdiff
path: root/scalatexApi/src/main/scala
diff options
context:
space:
mode:
Diffstat (limited to 'scalatexApi/src/main/scala')
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Compiler.scala28
-rw-r--r--scalatexApi/src/main/scala/scalatex/stages/Parser.scala19
2 files changed, 34 insertions, 13 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
index 7d1492c..9c1c3c4 100644
--- a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
+++ b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala
@@ -15,23 +15,43 @@ object Compiler{
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)
- parts.foldLeft(c.parse(code)){
- case (curr, Ast.Chain.Prop(str, offset2)) => q"$curr.${TermName(str)}"
+ 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")
- Apply(curr, args)
+ 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")
- TypeApply(curr, args)
+ 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{
diff --git a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
index 7b694da..45f82e0 100644
--- a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
+++ b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala
@@ -11,12 +11,13 @@ import Util._
* enough until we stuff the code-strings into the real Scala
* parser later
*/
-object Parser extends (String => Ast.Block){
- def apply(input: String): Ast.Block = {
+object Parser extends ((String, Int) => Ast.Block){
+ def apply(input: String, offset: Int = 0): Ast.Block = {
new Parser(input).Body.run().get
}
}
-class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
+class Parser(input: ParserInput, indent: Int = 0, offset: Int = 0) extends ScalaSyntax(input) {
+ def offsetCursor = offset + cursor
val txt = input.sliceString(0, input.length)
val indentTable = txt.split('\n').map{ s =>
if (s.trim == "") -1
@@ -45,7 +46,7 @@ class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
}
def HeaderBlock: Rule1[Ast.Header] = rule{
- Header ~ zeroOrMore(capture(NewlineS) ~ Header ~> (_ + _)) ~ runSubParser{new Parser(_, indent).Body0} ~> {
+ Header ~ zeroOrMore(capture(NewlineS) ~ Header ~> (_ + _)) ~ runSubParser{new Parser(_, indent, cursor).Body0} ~> {
(start: String, heads: Seq[String], body: Ast.Block) => Ast.Header(start + heads.mkString, body)
}
}
@@ -63,7 +64,7 @@ class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
def IndentBlock = rule{
&("\n") ~
test(cursorNextIndent() > indent) ~
- runSubParser(new Parser(_, cursorNextIndent()).Body)
+ runSubParser(new Parser(_, cursorNextIndent(), cursor).Body)
}
def IfHead = rule{ "@" ~ capture("if" ~ "(" ~ Expr ~ ")") }
def IfElse1 = rule{
@@ -91,12 +92,12 @@ class Parser(input: ParserInput, indent: Int = 0) extends ScalaSyntax(input) {
}
def ScalaChain = rule {
- Code ~ zeroOrMore(Extension) ~> {Ast.Chain(_, _)}
+ push(offsetCursor) ~ Code ~ zeroOrMore(Extension) ~> { (a, b, c) => Ast.Chain(b, c, a)}
}
def Extension: Rule1[Ast.Chain.Sub] = rule {
- ('.' ~ capture(Id) ~> (Ast.Chain.Prop(_))) |
- (capture(TypeArgs2) ~> (Ast.Chain.TypeArgs(_))) |
- (capture(ArgumentExprs2) ~> (Ast.Chain.Args(_))) |
+ (push(offsetCursor) ~ '.' ~ capture(Id) ~> ((x, y) => Ast.Chain.Prop(y, x))) |
+ (push(offsetCursor) ~ capture(TypeArgs2) ~> ((x, y) => Ast.Chain.TypeArgs(y, x))) |
+ (push(offsetCursor) ~ capture(ArgumentExprs2) ~> ((x, y) => Ast.Chain.Args(y, x))) |
BraceBlock
}
def Ws = Whitespace