Skip to content

Stub implementation of xml interpolator #5096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/test/dotc/run-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ typelevel-patmat.scala
typelevel.scala
typelevel1.scala
typelevel3.scala
xml-interpolation
11 changes: 11 additions & 0 deletions tests/run/xml-interpolation/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XmlQuote._

object Test {
def main(args: Array[String]): Unit = {
// TODO: enable once #5119 is fixed
// assert(xml"Hello Allan!" == Xml("Hello Allan!", Nil)

val name = new Object{}
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
}
}
61 changes: 61 additions & 0 deletions tests/run/xml-interpolation/XmlQuote_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import scala.quoted._
import scala.tasty.Tasty

import scala.language.implicitConversions

case class Xml(parts: String, args: List[Any])

// Ideally should be an implicit class but the implicit conversion
// has to be a rewrite method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think this is the best solution. Otherwise, we would need something like implicit rewrite class XmlQuote(...). Maybe with proper extension methods this will not be an issue.

class XmlQuote(ctx: => StringContext) {
rewrite def xml(args: => Any*): Xml = ~XmlQuote.impl('(ctx), '(args))
}

object XmlQuote {
implicit rewrite def XmlQuote(ctx: => StringContext): XmlQuote = new XmlQuote(ctx)

def impl(ctx: Expr[StringContext], args: Expr[Seq[Any]])
(implicit tasty: Tasty): Expr[Xml] = {
import tasty._
import Term._

def abort(msg: String): Nothing =
throw new QuoteError(msg)

// for debugging purpose
def pp(tree: Tree): Unit = {
println(tree.show)
println(tasty.showSourceCode.showTree(tree))
}

def liftListOfAny(lst: List[Term]): Expr[List[Any]] = lst match {
case x :: xs =>
val head = x.toExpr[Any]
val tail = liftListOfAny(xs)
'{ ~head :: ~tail }
case Nil => '(Nil)
}

def isStringConstant(tree: Term) = tree match {
case Literal(_) => true
case _ => false
}

// _root_.scala.StringContext.apply([p0, ...]: String*)
val parts = ctx.toTasty match {
case Inlined(_, _,
Apply(
Select(Select(Select(Ident("_root_"), "scala", _), "StringContext", _), "apply", _),
List(Typed(Repeated(values), _)))) if values.forall(isStringConstant) =>
values.collect { case Literal(Constant.String(value)) => value }
case tree =>
abort("String literal expected")
}

// [a0, ...]: Any*
val Inlined(_, _, Typed(Repeated(args0), _)) = args.toTasty

val string = parts.mkString("??")
'(new Xml(~string.toExpr, ~liftListOfAny(args0)))
}
}