Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/sablecc/sablecc/LR1ItemSet.java


1   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2    * This file is part of SableCC.                             *
3    * See the file "LICENSE" for copyright information and the  *
4    * terms and conditions for copying, distribution and        *
5    * modification of SableCC.                                  *
6    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7   
8   package org.sablecc.sablecc;
9   
10  import java.util.*;
11  import com.sun.java.util.collections.*;
12  import java.util.Vector;
13  
14  final class LR1ItemSet implements Cloneable, Comparable
15  {
16      private final TreeMap items;
17      private int hashCode = 0;
18  
19      LR1ItemSet()
20      {
21          this.items = new TreeMap();
22      }
23  
24      private LR1ItemSet(LR1ItemSet set)
25      {
26          this.items = (TreeMap) set.items.clone();
27          this.hashCode = set.hashCode;
28      }
29  
30      void set(LR1Item item)
31      {
32          if(items.put(item, item) == null)
33          {
34              hashCode += item.hashCode();
35              modified_ = true;
36          }
37      }
38  
39      boolean get(LR1Item item)
40      {
41          return items.get(item) != null;
42      }
43  
44  
45      LR1Item[] items_;
46      boolean modified_ = true;
47  
48      private void computeArray()
49      {
50          Vector itemVector = new Vector(0);
51  
52          for(Iterator e = items.keySet().iterator(); e.hasNext();)
53          {
54              itemVector.addElement(e.next());
55          }
56  
57          items_ = new LR1Item[itemVector.size()];
58          itemVector.copyInto(items_);
59          modified_ = false;
60      }
61  
62      LR1Item[] items()
63      {
64          if(modified_)
65          {
66              computeArray();
67          }
68  
69          return items_;
70      }
71  
72      public String toString()
73      {
74          String nl = System.getProperty("line.separator");
75  
76          StringBuffer result = new StringBuffer();
77          result.append("{" + nl + "\t");
78  
79          Production[] productions = Production.productions();
80          Symbol[] terminals = Symbol.terminals();
81          boolean comma = false;
82          for(int i = 0; i < productions.length; i++)
83          {
84              int rightsideLength = productions[i].rightside().length;
85  
86              for(int j = 0; j <= rightsideLength; j++)
87              {
88                  LR0Item lr0Item = new LR0Item(productions[i].index, j);
89  
90                  for(int k = 0; k < terminals.length; k++)
91                  {
92                      LR1Item item = new LR1Item(lr0Item, terminals[k].index);
93                      if(get(item))
94                      {
95                          if(comma)
96                          {
97                              result.append("," + nl + "\t");
98                          }
99                          else
100                         {
101                             comma = true;
102                         }
103 
104                         result.append(item);
105                     }
106                 }
107             }
108         }
109 
110         result.append(nl + "}");
111         return result.toString();
112     }
113 
114     public String toString(Symbol lookahead)
115     {
116         String nl = System.getProperty("line.separator");
117 
118         LR1Item[] items = items();
119         int length = items.length;
120 
121         TreeSet strings = new TreeSet();
122 
123         for(int i = 0; i < length; i++)
124         {
125             String s = items[i].toString(lookahead);
126 
127             if(s != null)
128             {
129                 strings.add(s);
130             }
131         }
132 
133         StringBuffer result = new StringBuffer();
134         result.append("{");
135 
136         boolean colon = false;
137         for(Iterator i = strings.iterator(); i.hasNext(); )
138         {
139             if(colon)
140             {
141                 result.append(",");
142                 result.append(nl);
143             }
144             else
145             {
146                 colon = true;
147                 result.append(nl);
148             }
149 
150             result.append("\t");
151             result.append(i.next());
152         }
153 
154         result.append(nl);
155         result.append("}");
156 
157         return result.toString();
158     }
159 
160     public Object clone()
161     {
162         return new LR1ItemSet(this);
163     }
164 
165     public boolean equals(Object obj)
166     {
167         if((obj == null) ||
168             (obj.getClass() != this.getClass()))
169         {
170             return false;
171         }
172 
173         LR1ItemSet set = (LR1ItemSet) obj;
174 
175         if(set.items.size() != items.size())
176         {
177             return false;
178         }
179 
180         for(Iterator e = items.keySet().iterator(); e.hasNext();)
181         {
182             if(!set.get((LR1Item) e.next()))
183             {
184                 return false;
185             }
186         }
187 
188         return true;
189     }
190 
191     public int hashCode()
192     {
193         return hashCode;
194     }
195 
196     public int compareTo(Object object)
197     {
198         LR1ItemSet set = (LR1ItemSet) object;
199 
200         int result = items.size() - set.items.size();
201 
202         if(result == 0)
203         {
204             Iterator e = items.keySet().iterator();
205             Iterator f = set.items.keySet().iterator();
206 
207             while(e.hasNext() && f.hasNext() && (result == 0))
208             {
209                 result = ((LR1Item) e.next()).compareTo(f.next());
210             }
211 
212             if(result == 0)
213             {
214                 if(e.hasNext())
215                 {
216                     return 1;
217                 }
218 
219                 if(f.hasNext())
220                 {
221                     return -1;
222                 }
223             }
224         }
225 
226         return result;
227     }
228 }
229