1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.util.ArrayList ; 25 26 37 public class ProductionPatternAlternative { 38 39 42 private ProductionPattern pattern; 43 44 47 private ArrayList elements = new ArrayList (); 48 49 52 private LookAheadSet lookAhead = null; 53 54 57 public ProductionPatternAlternative() { 58 } 59 60 68 public boolean isLeftRecursive() { 69 ProductionPatternElement elem; 70 71 for (int i = 0; i < elements.size(); i++) { 72 elem = (ProductionPatternElement) elements.get(i); 73 if (elem.getId() == pattern.getId()) { 74 return true; 75 } else if (elem.getMinCount() > 0) { 76 break; 77 } 78 } 79 return false; 80 } 81 82 90 public boolean isRightRecursive() { 91 ProductionPatternElement elem; 92 93 for (int i = elements.size() - 1; i >= 0; i--) { 94 elem = (ProductionPatternElement) elements.get(i); 95 if (elem.getId() == pattern.getId()) { 96 return true; 97 } else if (elem.getMinCount() > 0) { 98 break; 99 } 100 } 101 return false; 102 } 103 104 112 public boolean isMatchingEmpty() { 113 return getMinElementCount() == 0; 114 } 115 116 121 public ProductionPattern getPattern() { 122 return pattern; 123 } 124 125 132 void setPattern(ProductionPattern pattern) { 133 this.pattern = pattern; 134 } 135 136 141 public int getElementCount() { 142 return elements.size(); 143 } 144 145 152 public int getMinElementCount() { 153 ProductionPatternElement elem; 154 int min = 0; 155 156 for (int i = 0; i < elements.size(); i++) { 157 elem = (ProductionPatternElement) elements.get(i); 158 min += elem.getMinCount(); 159 } 160 return min; 161 } 162 163 170 public int getMaxElementCount() { 171 ProductionPatternElement elem; 172 int max = 0; 173 174 for (int i = 0; i < elements.size(); i++) { 175 elem = (ProductionPatternElement) elements.get(i); 176 if (elem.getMaxCount() >= Integer.MAX_VALUE) { 177 return Integer.MAX_VALUE; 178 } else { 179 max += elem.getMaxCount(); 180 } 181 } 182 return max; 183 } 184 185 192 public ProductionPatternElement getElement(int pos) { 193 return (ProductionPatternElement) elements.get(pos); 194 } 195 196 207 public void addToken(int id, int min, int max) { 208 addElement(new ProductionPatternElement(true, id, min, max)); 209 } 210 211 222 public void addProduction(int id, int min, int max) { 223 addElement(new ProductionPatternElement(false, id, min, max)); 224 } 225 226 232 public void addElement(ProductionPatternElement elem) { 233 elements.add(elem); 234 } 235 236 247 public void addElement(ProductionPatternElement elem, 248 int min, 249 int max) { 250 251 if (elem.isToken()) { 252 addToken(elem.getId(), min, max); 253 } else { 254 addProduction(elem.getId(), min, max); 255 } 256 } 257 258 268 public boolean equals(Object obj) { 269 ProductionPatternAlternative alt; 270 271 if (obj instanceof ProductionPatternAlternative) { 272 alt = (ProductionPatternAlternative) obj; 273 return elements.equals(alt.elements); 274 } else { 275 return false; 276 } 277 } 278 279 284 public String toString() { 285 StringBuffer buffer = new StringBuffer (); 286 287 for (int i = 0; i < elements.size(); i++) { 288 if (i > 0) { 289 buffer.append(" "); 290 } 291 buffer.append(elements.get(i)); 292 } 293 return buffer.toString(); 294 } 295 296 301 LookAheadSet getLookAhead() { 302 return lookAhead; 303 } 304 305 310 void setLookAhead(LookAheadSet lookAhead) { 311 this.lookAhead = lookAhead; 312 } 313 } 314 | Popular Tags |