File tree 4 files changed +60
-0
lines changed
library/src-3.x/scala/quoted/matching
quote-matcher-string-interpolator-3
4 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ package scala .quoted .matching
2
+
3
+ import scala .quoted .Expr
4
+
5
+ import scala .tasty .Reflection // TODO do not depend on reflection directly
6
+
7
+ /** Literal sequence of literal constant value expressions */
8
+ object ConstSeq {
9
+
10
+ /** Matches literal sequence of literal constant value expressions */
11
+ def unapply [T ](expr : Expr [Seq [T ]]) given Reflection : Option [Seq [T ]] = expr match {
12
+ case ExprSeq (elems) =>
13
+ elems.foldRight(Option (List .empty[T ])) { (elem, acc) =>
14
+ (elem, acc) match {
15
+ case (Const (value), Some (lst)) => Some (value :: lst)
16
+ case (_, _) => None
17
+ }
18
+ }
19
+ case _ => None
20
+ }
21
+
22
+ }
Original file line number Diff line number Diff line change
1
+ HELLO WORLD
2
+ HELLO World
Original file line number Diff line number Diff line change
1
+ import scala .quoted ._
2
+ import scala .quoted .matching ._
3
+
4
+ import scala .tasty .Reflection
5
+
6
+ object Macros {
7
+
8
+ inline def (self : => StringContext ) S (args : => String * ): String = $ {impl(' self , ' args )}
9
+
10
+ private def impl (self : Expr [StringContext ], args : Expr [Seq [String ]]) given Reflection : Expr [String ] = {
11
+ self match {
12
+ case ' { StringContext ($ {ConstSeq (parts)}: _* ) } =>
13
+ val upprerParts : List [String ] = parts.toList.map(_.toUpperCase)
14
+ val upprerPartsExpr : Expr [List [String ]] = upprerParts.map(_.toExpr).toExprOfList
15
+ ' { StringContext ($upprerPartsExpr : _* ).s($args : _* ) }
16
+ case _ =>
17
+ ' {
18
+ val parts : Seq [String ] = $self.parts
19
+ val upprerParts = parts.map(_.toUpperCase)
20
+ StringContext (upprerParts : _* ).s($args : _* )
21
+ }
22
+ }
23
+
24
+ }
25
+
26
+ }
Original file line number Diff line number Diff line change
1
+ import Macros ._
2
+
3
+ object Test {
4
+
5
+ def main (args : Array [String ]): Unit = {
6
+ println(S " Hello World " )
7
+ println(S " Hello ${" World" }" )
8
+ }
9
+
10
+ }
You can’t perform that action at this time.
0 commit comments