@@ -34,6 +34,10 @@ object Matcher {
34
34
import reflection ._
35
35
// TODO improve performance
36
36
37
+ /** Create a new matching with the resulting binding for the symbol */
38
+ def bindingMatched (sym : Symbol ) =
39
+ Some (Tuple1 (new Binding (sym.name, sym)))
40
+
37
41
/** Check that the trees match and return the contents from the pattern holes.
38
42
* Return None if the trees do not match otherwise return Some of a tuple containing all the contents in the holes.
39
43
*
@@ -52,9 +56,6 @@ object Matcher {
52
56
sFlags.is(Lazy ) == pFlags.is(Lazy ) && sFlags.is(Mutable ) == pFlags.is(Mutable )
53
57
}
54
58
55
- def bindingMatch (sym : Symbol ) =
56
- Some (Tuple1 (new Binding (sym.name, sym)))
57
-
58
59
def hasBindingTypeAnnotation (tpt : TypeTree ): Boolean = tpt match {
59
60
case Annotated (tpt2, Apply (Select (New (TypeIdent (" patternBindHole" )), " <init>" ), Nil )) => true
60
61
case Annotated (tpt2, _) => hasBindingTypeAnnotation(tpt2)
@@ -148,7 +149,7 @@ object Matcher {
148
149
149
150
case (ValDef (_, tpt1, rhs1), ValDef (_, tpt2, rhs2)) if checkValFlags() =>
150
151
val bindMatch =
151
- if (hasBindingAnnotation(pattern.symbol) || hasBindingTypeAnnotation(tpt2)) bindingMatch (scrutinee.symbol)
152
+ if (hasBindingAnnotation(pattern.symbol) || hasBindingTypeAnnotation(tpt2)) bindingMatched (scrutinee.symbol)
152
153
else Some (())
153
154
val returnTptMatch = treeMatches(tpt1, tpt2)
154
155
val rhsEnv = env + (scrutinee.symbol -> pattern.symbol)
@@ -161,7 +162,7 @@ object Matcher {
161
162
if (paramss1.size != paramss2.size) None
162
163
else foldMatchings(paramss1.zip(paramss2).map { (params1, params2) => treesMatch(params1, params2) }: _* )
163
164
val bindMatch =
164
- if (hasBindingAnnotation(pattern.symbol)) bindingMatch (scrutinee.symbol)
165
+ if (hasBindingAnnotation(pattern.symbol)) bindingMatched (scrutinee.symbol)
165
166
else Some (())
166
167
val tptMatch = treeMatches(tpt1, tpt2)
167
168
val rhsEnv =
@@ -244,13 +245,24 @@ object Matcher {
244
245
* `None` if it did not match or `Some(tup: Tuple)` if it matched where `tup` contains the contents of the holes.
245
246
*/
246
247
def patternMatches (scrutinee : Pattern , pattern : Pattern )(implicit env : Set [(Symbol , Symbol )]): (Set [(Symbol , Symbol )], Option [Tuple ]) = (scrutinee, pattern) match {
247
- case (Pattern .Value (v1), Pattern .Unapply (TypeApply (Select (patternHole @ Ident (" patternHole" ), " unapply" ), List (tpt)), Nil , Nil ))
248
- if patternHole.symbol.owner.fullName == " scala.runtime.quoted.Matcher$" =>
249
- (env, Some (Tuple1 (v1.seal)))
248
+ // case (Pattern.Value(v1), Pattern.Unapply(TypeApply(Select(patternHole @ Ident("patternHole"), "unapply"), List(tpt)), Nil, Nil))
249
+ // if patternHole.symbol.owner.fullName == "scala.runtime.quoted.Matcher$" =>
250
+ // (env, Some(Tuple1(v1.seal)))
251
+
252
+ case (Pattern .Bind (name1, pat1), Pattern .Unapply (Select (Ident (" patternMatchBindHole" ), " unapply" ), Nil , List (Pattern .Bind (name2, pat2))))
253
+ // TODO if pattern.symbol == ... =>
254
+ =>
255
+ val (env1, patMatch) = patternMatches(pat1, pat2)
256
+ (env1, foldMatchings(bindingMatched(scrutinee.symbol), patMatch))
257
+
258
+ case (Pattern .Value (Ident (" _" )), Pattern .Value (Ident (" _" ))) => // TODO add Wildcard to patterns
259
+ val bindEnv = env + (scrutinee.symbol -> pattern.symbol)
260
+ (bindEnv, Some (()))
250
261
251
262
case (Pattern .Value (v1), Pattern .Value (v2)) =>
252
263
(env, treeMatches(v1, v2))
253
264
265
+
254
266
case (Pattern .Bind (name1, body1), Pattern .Bind (name2, body2)) =>
255
267
val bindEnv = env + (scrutinee.symbol -> pattern.symbol)
256
268
patternMatches(body1, body2)(bindEnv)
0 commit comments