@@ -53,28 +53,25 @@ object StringContextMacro {
5353 def restoreReported () : Unit
5454 }
5555
56- /** Retrieves a String from an Expr containing it
57- *
58- * @param expression the Expr containing the String
59- * @return the String contained in the given Expr
60- * quotes an error if the given Expr does not contain a String
61- */
62- private def literalToString (expression : Expr [String ]) given (ctx : QuoteContext ) : String = expression match {
63- case Const (string : String ) => string
64- case _ => QuoteError (" Expected statically known literal" , expression)
65- }
66-
6756 /** Retrieves the parts from a StringContext, given inside an Expr, and returns them as a list of Expr of String
6857 *
6958 * @param strCtxExpr the Expr containing the StringContext
7059 * @return a list of Expr containing Strings, each corresponding to one parts of the given StringContext
7160 * quotes an error if the given Expr does not correspond to a StringContext
7261 */
73- def getPartsExprs (strCtxExpr : Expr [scala.StringContext ]) given QuoteContext : List [Expr [String ]] = {
62+ def getPartsExprs (strCtxExpr : Expr [scala.StringContext ]) given (qctx : QuoteContext ): Option [(List [Expr [String ]], List [String ])] = {
63+ def notStatic = {
64+ qctx.error(" Expected statically known String Context" , strCtxExpr)
65+ None
66+ }
67+ def splitParts (seq : Expr [Seq [String ]]) = (seq, seq) match {
68+ case (ExprSeq (p1), ConstSeq (p2)) => Some ((p1.toList, p2.toList))
69+ case _ => notStatic
70+ }
7471 strCtxExpr match {
75- case ' { StringContext ($ { ExprSeq ( parts)} : _* ) } => parts.toList
76- case ' { new StringContext ($ { ExprSeq ( parts)} : _* ) } => parts.toList
77- case _ => QuoteError ( " Expected statically known String Context " , strCtxExpr)
72+ case ' { StringContext ($parts : _* ) } => splitParts( parts)
73+ case ' { new StringContext ($parts : _* ) } => splitParts( parts)
74+ case _ => notStatic
7875 }
7976 }
8077
@@ -84,11 +81,14 @@ object StringContextMacro {
8481 * @return a list of Expr containing arguments
8582 * quotes an error if the given Expr does not contain a list of arguments
8683 */
87- def getArgsExprs (argsExpr : Expr [Seq [Any ]]) given (qctx : QuoteContext ): List [Expr [Any ]] = {
84+ def getArgsExprs (argsExpr : Expr [Seq [Any ]]) given (qctx : QuoteContext ): Option [ List [Expr [Any ] ]] = {
8885 import qctx .tasty ._
8986 argsExpr.unseal.underlyingArgument match {
90- case Typed (Repeated (args, _), _) => args.map(_.seal)
91- case tree => QuoteError (" Expected statically known argument list" , argsExpr)
87+ case Typed (Repeated (args, _), _) =>
88+ Some (args.map(_.seal))
89+ case tree =>
90+ qctx.error(" Expected statically known argument list" , argsExpr)
91+ None
9292 }
9393 }
9494
@@ -102,8 +102,14 @@ object StringContextMacro {
102102 import qctx .tasty ._
103103 val sourceFile = strCtxExpr.unseal.pos.sourceFile
104104
105- val partsExpr = getPartsExprs(strCtxExpr)
106- val args = getArgsExprs(argsExpr)
105+ val (partsExpr, parts) = getPartsExprs(strCtxExpr) match {
106+ case Some (x) => x
107+ case None => return ' {" " }
108+ }
109+ val args = getArgsExprs(argsExpr) match {
110+ case Some (args) => args
111+ case None => return ' {" " }
112+ }
107113
108114 val reporter = new Reporter {
109115 private [this ] var reported = false
@@ -148,7 +154,7 @@ object StringContextMacro {
148154 }
149155 }
150156
151- interpolate(partsExpr , args, argsExpr, reporter)
157+ interpolate(parts , args, argsExpr, reporter)
152158 }
153159
154160 /** Helper function for the interpolate function above
@@ -158,7 +164,7 @@ object StringContextMacro {
158164 * @param reporter the reporter to return any error/warning when a problem is encountered
159165 * @return the Expr containing the formatted and interpolated String or an error/warning report if the parameters are not correct
160166 */
161- def interpolate (partsExpr : List [Expr [ String ] ], args : List [Expr [Any ]], argsExpr : Expr [Seq [Any ]], reporter : Reporter ) given (qctx : QuoteContext ) : Expr [String ] = {
167+ def interpolate (parts0 : List [String ], args : List [Expr [Any ]], argsExpr : Expr [Seq [Any ]], reporter : Reporter ) given (qctx : QuoteContext ) : Expr [String ] = {
162168 import qctx .tasty ._
163169
164170 /** Checks if the number of arguments are the same as the number of formatting strings
@@ -721,10 +727,10 @@ object StringContextMacro {
721727 val argument = args.size
722728
723729 // check validity of formatting
724- checkSizes(partsExpr .size - 1 , argument)
730+ checkSizes(parts0 .size - 1 , argument)
725731
726732 // add default format
727- val parts = addDefaultFormat(partsExpr.map(literalToString) )
733+ val parts = addDefaultFormat(parts0 )
728734
729735 if (! parts.isEmpty && ! reporter.hasReported()) {
730736 if (parts.size == 1 && args.size == 0 && parts.head.size != 0 ){
0 commit comments