-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,4 @@ typelevel-patmat.scala | |
typelevel.scala | ||
typelevel1.scala | ||
typelevel3.scala | ||
xml-interpolation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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))) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.