diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-11-01 10:15:13 -0700 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-11-01 10:15:13 -0700 |
commit | c0b06c5e3f3caa60736794143f278c2af5cbe9fb (patch) | |
tree | f66c244943702c46daee94aaa1b98a07d6542830 /scalatexApi/src/main/scala | |
parent | aae594fd3c8397abca4cd4e55f538d41b172b4e3 (diff) | |
download | hands-on-scala-js-c0b06c5e3f3caa60736794143f278c2af5cbe9fb.tar.gz hands-on-scala-js-c0b06c5e3f3caa60736794143f278c2af5cbe9fb.tar.bz2 hands-on-scala-js-c0b06c5e3f3caa60736794143f278c2af5cbe9fb.zip |
scalatex tests pass
Diffstat (limited to 'scalatexApi/src/main/scala')
3 files changed, 41 insertions, 18 deletions
diff --git a/scalatexApi/src/main/scala/scalatex/package.scala b/scalatexApi/src/main/scala/scalatex/package.scala index 5732411..d86976e 100644 --- a/scalatexApi/src/main/scala/scalatex/package.scala +++ b/scalatexApi/src/main/scala/scalatex/package.scala @@ -15,11 +15,13 @@ package object scalatex { def twf(filename: String): Frag = macro Internals.applyMacroFile object Internals { + def twRuntimeErrors(expr: String): Frag = macro applyMacroRuntimeErrors def twDebug(expr: String): Frag = macro applyMacroDebug - def applyMacro(c: Context)(expr: c.Expr[String]): c.Expr[Frag] = applyMacroFull(c)(expr, false) + def applyMacro(c: Context)(expr: c.Expr[String]): c.Expr[Frag] = applyMacroFull(c)(expr, false, false) + def applyMacroDebug(c: Context)(expr: c.Expr[String]): c.Expr[Frag] = applyMacroFull(c)(expr, false, true) - def applyMacroDebug(c: Context)(expr: c.Expr[String]): c.Expr[Frag] = applyMacroFull(c)(expr, true) + def applyMacroRuntimeErrors(c: Context)(expr: c.Expr[String]): c.Expr[Frag] = applyMacroFull(c)(expr, true, false) def applyMacroFile(c: Context)(filename: c.Expr[String]): c.Expr[Frag] = { import c.universe._ @@ -34,34 +36,46 @@ package object scalatex { txt.toCharArray ) - compileThing(c)(txt, sourceFile, 0, false) + compileThing(c)(txt, sourceFile, 0, false, false) } case class DebugFailure(msg: String, pos: String) extends Exception(msg) - private[this] def applyMacroFull(c: Context)(expr: c.Expr[String], runtimeErrors: Boolean): c.Expr[Frag] = { + private[this] def applyMacroFull(c: Context) + (expr: c.Expr[String], + runtimeErrors: Boolean, + debug: Boolean) + : c.Expr[Frag] = { import c.universe._ val s = expr.tree - .asInstanceOf[Literal] - .value - .value - .asInstanceOf[String] + .asInstanceOf[Literal] + .value + .value + .asInstanceOf[String] val stringStart = expr.tree .pos .lineContent .drop(expr.tree.pos.column) .take(2) + val indented = s |> stages.IndentHandler + if (debug) println(indented) compileThing(c)( - s |> stages.IndentHandler, + indented, expr.tree.pos.source, expr.tree.pos.point + (if (stringStart == "\"\"") 1 else -1), - runtimeErrors + runtimeErrors, + debug ) } } - def compileThing(c: Context)(s: String, source: SourceFile, point: Int, runtimeErrors: Boolean) = { + def compileThing(c: Context) + (s: String, + source: SourceFile, + point: Int, + runtimeErrors: Boolean, + debug: Boolean) = { import c.universe._ def compile(s: String): c.Tree = { val realPos = new OffsetPosition(source, point).asInstanceOf[c.universe.Position] @@ -71,13 +85,15 @@ package object scalatex { import c.Position try { - c.Expr(c.typeCheck(compile(s))) + val compiled = compile(s) + if (debug) println(compiled) + c.Expr[Frag](c.typeCheck(compiled)) } catch { case e@TypecheckException(pos: Position, msg) => if (!runtimeErrors) c.abort(pos, msg) else { val posMsg = pos.lineContent + "\n" + (" " * pos.column) + "^" - c.Expr( q"""throw twist.Internals.DebugFailure($msg, $posMsg)""") + c.Expr( q"""throw scalatex.Internals.DebugFailure($msg, $posMsg)""") } } diff --git a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala index 85bb0f0..fcbe08b 100644 --- a/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala +++ b/scalatexApi/src/main/scala/scalatex/stages/Compiler.scala @@ -75,6 +75,7 @@ object Compiler{ q"if($cond){ Seq[$fragType](..$b1): $fragType } else { Seq[$fragType](..$b2): $fragType }" case xx @ WN.ScalaExp(WN.Simple(first, _) +: rest, offset) => + val firstTree = c.parse(first) firstTree.foreach{x => diff --git a/scalatexApi/src/main/scala/scalatex/stages/IndentHandler.scala b/scalatexApi/src/main/scala/scalatex/stages/IndentHandler.scala index 180d134..697ccc3 100644 --- a/scalatexApi/src/main/scala/scalatex/stages/IndentHandler.scala +++ b/scalatexApi/src/main/scala/scalatex/stages/IndentHandler.scala @@ -49,8 +49,15 @@ object IndentHandler extends (String => String){ // println(spaces, current) val declRemainder = successRemainder(Parser.parse(current.trim, _.templateDeclaration())) - - val exprRemainder = successRemainder(Parser.parse(current.trim, _.expression())).filter(_ == current.trim) +// println("::::::" + current.trim) +// println(successRemainder(Parser.parse(current.trim, _.expression()))) + val exprRemainder = successRemainder(Parser.parse(current.trim, _.expression())).filter { + // Either it takes up the entire line or the line ends with a =>, in which case + // we assume the guy wanted to pass a lambda + x => +// println(x) + x == current.trim || current.trim.endsWith("=>") + } /** @@ -81,14 +88,13 @@ object IndentHandler extends (String => String){ val newFirst = if (!before.startsWith("@else")) before else before.dropRight("@else".length)+ "} else" - + println(before, after, delta) if (delta > 0 && noBraceLine(after)) newFirst + "{" + after else if (delta <= 0 && noBraceLine(after) && after.trim != "") newFirst + "{" + after + "}" * (1 - elseCorrection) else current - - } + val closing = "}" * (-delta - elseCorrection) val newStack = |