-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow macros calling inherited methods on modules #5575
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
nicolasstucki
merged 1 commit into
scala:master
from
dotty-staging:add-macro-interpolation-core
Dec 8, 2018
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
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,3 @@ | ||
Hello world! | ||
Hello world!\n | ||
Hello foo! |
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,95 @@ | ||
|
||
import scala.quoted._ | ||
import scala.tasty.Reflection | ||
import scala.language.implicitConversions | ||
import scala.quoted.Exprs.LiftedExpr | ||
import scala.quoted.Toolbox.Default._ | ||
|
||
object Macro { | ||
|
||
class StringContextOps(strCtx: => StringContext) { | ||
inline def s2(args: Any*): String = ~SIntepolator('(strCtx), '(args)) | ||
inline def raw2(args: Any*): String = ~RawIntepolator('(strCtx), '(args)) | ||
inline def foo(args: Any*): String = ~FooIntepolator('(strCtx), '(args)) | ||
} | ||
implicit inline def SCOps(strCtx: => StringContext): StringContextOps = new StringContextOps(strCtx) | ||
} | ||
|
||
object SIntepolator extends MacroStringInterpolator[String] { | ||
protected def interpolate(strCtx: StringContext, args: List[Expr[Any]])(implicit reflect: Reflection): Expr[String] = | ||
'((~strCtx.toExpr).s(~args.toExprOfList: _*)) | ||
} | ||
|
||
object RawIntepolator extends MacroStringInterpolator[String] { | ||
protected def interpolate(strCtx: StringContext, args: List[Expr[Any]])(implicit reflect: Reflection): Expr[String] = | ||
'((~strCtx.toExpr).raw(~args.toExprOfList: _*)) | ||
} | ||
|
||
object FooIntepolator extends MacroStringInterpolator[String] { | ||
protected def interpolate(strCtx: StringContext, args: List[Expr[Any]])(implicit reflect: Reflection): Expr[String] = | ||
'((~strCtx.toExpr).s(~args.map(_ => '("foo")).toExprOfList: _*)) | ||
} | ||
|
||
// TODO put this class in the stdlib or separate project? | ||
abstract class MacroStringInterpolator[T] { | ||
|
||
final def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(implicit reflect: Reflection): Expr[T] = { | ||
try interpolate(strCtxExpr, argsExpr) | ||
catch { | ||
case ex: NotStaticlyKnownError => | ||
// TODO use ex.expr to recover the position | ||
throw new QuoteError(ex.getMessage) | ||
case ex: StringContextError => | ||
// TODO use ex.idx to recover the position | ||
throw new QuoteError(ex.getMessage) | ||
case ex: ArgumentError => | ||
// TODO use ex.idx to recover the position | ||
throw new QuoteError(ex.getMessage) | ||
} | ||
} | ||
|
||
protected def interpolate(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(implicit reflect: Reflection): Expr[T] = | ||
interpolate(getStaticStringContext(strCtxExpr), getArgsList(argsExpr)) | ||
|
||
protected def interpolate(strCtx: StringContext, argExprs: List[Expr[Any]])(implicit reflect: Reflection): Expr[T] | ||
|
||
protected def getStaticStringContext(strCtxExpr: Expr[StringContext])(implicit reflect: Reflection): StringContext = { | ||
import reflect._ | ||
strCtxExpr.unseal.underlyingArgument match { | ||
case Term.Select(Term.Typed(Term.Apply(_, List(Term.Apply(_, List(Term.Typed(Term.Repeated(strCtxArgTrees), TypeTree.Inferred()))))), _), _) => | ||
val strCtxArgs = strCtxArgTrees.map { | ||
case Term.Literal(Constant.String(str)) => str | ||
case tree => throw new NotStaticlyKnownError("Expected statically known StringContext", tree.seal[Any]) | ||
} | ||
StringContext(strCtxArgs: _*) | ||
case tree => | ||
throw new NotStaticlyKnownError("Expected statically known StringContext", tree.seal[Any]) | ||
} | ||
} | ||
|
||
protected def getArgsList(argsExpr: Expr[Seq[Any]])(implicit reflect: Reflection): List[Expr[Any]] = { | ||
import reflect._ | ||
argsExpr.unseal.underlyingArgument match { | ||
case Term.Typed(Term.Repeated(args), _) => args.map(_.seal[Any]) | ||
case tree => throw new NotStaticlyKnownError("Expected statically known argument list", tree.seal[Any]) | ||
} | ||
} | ||
|
||
protected implicit def StringContextIsLiftable: Liftable[StringContext] = new Liftable[StringContext] { | ||
def toExpr(strCtx: StringContext): Expr[StringContext] = { | ||
// TODO define in stdlib? | ||
implicit def ListIsLiftable: Liftable[List[String]] = new Liftable[List[String]] { | ||
override def toExpr(list: List[String]): Expr[List[String]] = list match { | ||
case x :: xs => '(~x.toExpr :: ~toExpr(xs)) | ||
case Nil => '(Nil) | ||
} | ||
} | ||
'(StringContext(~strCtx.parts.toList.toExpr: _*)) | ||
} | ||
} | ||
|
||
protected class NotStaticlyKnownError(msg: String, expr: Expr[Any]) extends Exception(msg) | ||
protected class StringContextError(msg: String, idx: Int, start: Int = -1, end: Int = -1) extends Exception(msg) | ||
protected class ArgumentError(msg: String, idx: Int) extends Exception(msg) | ||
|
||
} |
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,10 @@ | ||
import Macro._ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val w = "world" | ||
println(s2"Hello $w!") | ||
println(raw2"Hello $w!\n") | ||
println(foo"Hello $w!") | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.