1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.impl.BitSet; 10 import persistence.antlr.collections.impl.Vector; 11 12 67 public class Lookahead implements Cloneable { 68 69 BitSet fset; 70 71 String cycle; 72 73 BitSet epsilonDepth; 74 78 boolean hasEpsilon = false; 79 80 public Lookahead() { 81 fset = new BitSet(); 82 } 83 84 85 public Lookahead(BitSet p) { 86 fset = p; 87 } 88 89 90 public Lookahead(String c) { 91 this(); 92 cycle = c; 93 } 94 95 96 public Object clone() { 97 Lookahead p = null; 98 try { 99 p = (Lookahead)super.clone(); 100 p.fset = (BitSet)fset.clone(); 101 p.cycle = cycle; if (epsilonDepth != null) { 103 p.epsilonDepth = (BitSet)epsilonDepth.clone(); 104 } 105 } 106 catch (CloneNotSupportedException e) { 107 throw new InternalError (); 108 } 109 return p; 110 } 111 112 public void combineWith(Lookahead q) { 113 if (cycle == null) { cycle = q.cycle; 115 } 116 117 if (q.containsEpsilon()) { 118 hasEpsilon = true; 119 } 120 121 if (epsilonDepth != null) { 123 if (q.epsilonDepth != null) { 124 epsilonDepth.orInPlace(q.epsilonDepth); 125 } 126 } 127 else if (q.epsilonDepth != null) { 128 epsilonDepth = (BitSet)q.epsilonDepth.clone(); 129 } 130 fset.orInPlace(q.fset); 131 } 132 133 public boolean containsEpsilon() { 134 return hasEpsilon; 135 } 136 137 140 public Lookahead intersection(Lookahead q) { 141 Lookahead p = new Lookahead(fset.and(q.fset)); 142 if (this.hasEpsilon && q.hasEpsilon) { 143 p.setEpsilon(); 144 } 145 return p; 146 } 147 148 public boolean nil() { 149 return fset.nil() && !hasEpsilon; 150 } 151 152 public static Lookahead of(int el) { 153 Lookahead look = new Lookahead(); 154 look.fset.add(el); 155 return look; 156 } 157 158 public void resetEpsilon() { 159 hasEpsilon = false; 160 } 161 162 public void setEpsilon() { 163 hasEpsilon = true; 164 } 165 166 public String toString() { 167 String e = "",b,f = "",d = ""; 168 b = fset.toString(","); 169 if (containsEpsilon()) { 170 e = "+<epsilon>"; 171 } 172 if (cycle != null) { 173 f = "; FOLLOW(" + cycle + ")"; 174 } 175 if (epsilonDepth != null) { 176 d = "; depths=" + epsilonDepth.toString(","); 177 } 178 return b + e + f + d; 179 180 } 181 182 public String toString(String separator, CharFormatter formatter) { 183 String e = "",b,f = "",d = ""; 184 b = fset.toString(separator, formatter); 185 if (containsEpsilon()) { 186 e = "+<epsilon>"; 187 } 188 if (cycle != null) { 189 f = "; FOLLOW(" + cycle + ")"; 190 } 191 if (epsilonDepth != null) { 192 d = "; depths=" + epsilonDepth.toString(","); 193 } 194 return b + e + f + d; 195 } 196 197 public String toString(String separator, CharFormatter formatter, Grammar g) { 198 if (g instanceof LexerGrammar) { 199 return toString(separator, formatter); 200 } 201 else { 202 return toString(separator, g.tokenManager.getVocabulary()); 203 } 204 } 205 206 public String toString(String separator, Vector vocab) { 207 String b,f = "",d = ""; 208 b = fset.toString(separator, vocab); 209 if (cycle != null) { 210 f = "; FOLLOW(" + cycle + ")"; 211 } 212 if (epsilonDepth != null) { 213 d = "; depths=" + epsilonDepth.toString(","); 214 } 215 return b + f + d; 216 } 217 } 218 | Popular Tags |