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