@@ -41,62 +41,45 @@ class StringInterpolatorOpt extends MiniPhase {
41
41
}
42
42
}
43
43
44
- /** Matches an s or raw string interpolator */
45
- private object SOrRawInterpolator {
46
- def unapply (tree : Tree )(implicit ctx : Context ): Option [(List [Literal ], List [Tree ])] = {
47
- if (tree.symbol.eq(defn.StringContextRaw ) || tree.symbol.eq(defn.StringContextS )) {
48
- tree match {
49
- case Apply (Select (Apply (StringContextApply (), List (Literals (strs))), _),
50
- List (SeqLiteral (elems, _))) if elems.length == strs.length - 1 =>
51
- Some (strs, elems)
52
- case _ => None
53
- }
54
- } else None
55
- }
56
- }
57
-
58
- /**
59
- * Match trees that resemble s and raw string interpolations. In the case of the s
60
- * interpolator, escapes the string constants. Exposes the string constants as well as
61
- * the variable references.
62
- */
63
- private object StringContextIntrinsic {
64
- def unapply (tree : Apply )(implicit ctx : Context ): Option [(List [Literal ], List [Tree ])] = {
44
+ override def transformApply (tree : Apply )(implicit ctx : Context ): Tree = {
45
+ /** Matches an s or raw string interpolator */
46
+ if (tree.symbol.eq(defn.StringContextRaw ) || tree.symbol.eq(defn.StringContextS )) {
65
47
tree match {
66
- case SOrRawInterpolator (strs, elems) =>
67
- if (tree.symbol == defn.StringContextRaw ) Some (strs, elems)
48
+ case Apply (Select (Apply (StringContextApply (), List (Literals (strs))), _),
49
+ List (SeqLiteral (elems, _))) if elems.length == strs.length - 1 =>
50
+ /** Match trees that resemble s and raw string interpolations. In the case of the s
51
+ * interpolator, escapes the string constants. Exposes the string constants as well as
52
+ * the variable references.
53
+ */
54
+ if (tree.symbol == defn.StringContextRaw ) interpolate(strs, elems)
68
55
else { // tree.symbol == defn.StringContextS
69
56
try {
70
57
val escapedStrs = strs.map { str =>
71
58
val escapedValue = StringContext .processEscapes(str.const.stringValue)
72
59
cpy.Literal (str)(Constant (escapedValue))
73
60
}
74
- Some (escapedStrs, elems)
61
+ interpolate (escapedStrs, elems)
75
62
} catch {
76
- case _ : StringContext .InvalidEscapeException => None
63
+ case _ : StringContext .InvalidEscapeException => tree
77
64
}
78
65
}
79
- case _ => None
66
+ case _ => tree
80
67
}
81
- }
68
+ } else tree
82
69
}
83
70
84
- override def transformApply (tree : Apply )(implicit ctx : Context ): Tree = {
85
- tree match {
86
- case StringContextIntrinsic (strs : List [Literal ], elems : List [Tree ]) =>
87
- val stri = strs.iterator
88
- val elemi = elems.iterator
89
- var result : Tree = stri.next
90
- def concat (tree : Tree ): Unit = {
91
- result = result.select(defn.String_+ ).appliedTo(tree)
92
- }
93
- while (elemi.hasNext) {
94
- concat(elemi.next)
95
- val str = stri.next
96
- if (! str.const.stringValue.isEmpty) concat(str)
97
- }
98
- result
99
- case _ => tree
71
+ private def interpolate (strs : List [Literal ], elems : List [Tree ])(implicit ctx : Context ): Tree = {
72
+ val stri = strs.iterator
73
+ val elemi = elems.iterator
74
+ var result : Tree = stri.next
75
+ def concat (tree : Tree ): Unit = {
76
+ result = result.select(defn.String_+ ).appliedTo(tree)
77
+ }
78
+ while (elemi.hasNext) {
79
+ concat(elemi.next)
80
+ val str = stri.next
81
+ if (! str.const.stringValue.isEmpty) concat(str)
100
82
}
83
+ result
101
84
}
102
85
}
0 commit comments