summaryrefslogtreecommitdiff
path: root/sources/scala/tools/nsc/matching/MatcherLabels.scala
blob: 15f6eae9b97939f9f901876d381e10dc7f7021dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package scala.tools.nsc.matching ;

[_trait_] abstract class MatcherLabels: TransMatcher {

  import global._ ;

  /**
   * This class represents the label that a transition in an automaton
   * may carry. These get translated to specific (boolean) tests
   */

  class Label {

      //case class RLabel(Object rstate, Label lab, Symbol vars[]);

      override def hashCode(): Int = this match {
        case DefaultLabel() =>
          return 0;
        case SimpleLabel(lit) =>
          return lit.value.hashCode();
        case TreeLabel(pat) =>
          // if pat is an  Apply, than this case can only be correctly
          // handled there are no other similar Applys (nondeterminism)
          return pat.tpe.hashCode();
        case TypeLabel(tpe) =>
          return tpe.hashCode();
        case _ =>
          return super.hashCode();
      }

      override def  equals( o: Any ): Boolean = {
            if( !(o.isInstanceOf[Label] ))
              return false;
        val oL = o.asInstanceOf[Label];
        //System.out.print(this + " equals " + oL);
         this match {
           case DefaultLabel()=>
           oL match {
             case DefaultLabel() =>
               return true;
             case _ => false;
           } //
           case SimpleLabel( lit ) =>
           oL match {
             case SimpleLabel( lit2 ) =>
             return /*(lit.kind == lit2.kind)
	     && */lit.value.equals( lit2.value );
             case _ => false;
           }

           case TreeLabel( pat ) =>
             oL match {
               case TreeLabel( pat2 ) =>
	       pat match {
		 case Apply( _, _ ) =>
		   pat2 match {
		     case Apply( _, _ ) =>
		       return
                         (treeInfo.methPart/*methSymbol?*/( pat )
                          == treeInfo.methPart/*methSymbol*/( pat2 ));
		   }
                 case _ => false;
	       }
               case _ => false
             }

           case TypeLabel(tpe) =>
             oL match {
               case TypeLabel( tpe2) =>
                 return tpe.equals(tpe2);
               case _ => false;
             }
           case LPair(state, lab) =>
             oL match {
               case LPair(state2, lab2) =>
                 return  state.equals(state2) && lab.equals(lab2);
               case _ => false;
             }
           case _ => return false;
         }
      }


  def toString2(): String = {
    val ext = System.getProperty("extendedMatching");
    if ((ext != null) && ext.equals("true")) {
      this match {
        case DefaultLabel() =>
          return "<>:p"+p;
        case SimpleLabel( lit ) =>
          return lit.toString()+":p"+p;
        case TreeLabel( pat ) =>
          return pat.tpe.toString()+":p"+p;

        case _ => scala.Predef.error("this never happens");
      }
    }
    scala.Predef.error("this never happens");
  }

  override def toString(): String = this match {
    case DefaultLabel() =>
      "<>";
    case SimpleLabel(  lit) =>
      lit.toString();
    case TreeLabel(pat) =>
      pat.toString();
    case TypeLabel(tpe) =>
      tpe.toString();
    case LPair(state, lab) =>
      "(" + state.toString() + "," + lab.toString() + ")";
    case _ =>
      scala.Predef.error("this never happens");
  }

  val p = -1; // tree state - only needed for extended matching

}

  case class DefaultLabel()                    extends Label;
  case class SimpleLabel(lit: Literal)         extends Label;
  case class TreeLabel(pat: Tree)              extends Label; // Apply, Sequence
  case class TypeLabel(tpe: Type)              extends Label; // Apply, Sequence
  case class LPair(state: Integer, lab: Label) extends Label;

}