1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.util.ArrayList ; 25 26 36 public class ProductionPattern { 37 38 41 private int id; 42 43 46 private String name; 47 48 53 private boolean synthetic; 54 55 58 private ArrayList alternatives; 59 60 65 private int defaultAlt; 66 67 70 private LookAheadSet lookAhead; 71 72 78 public ProductionPattern(int id, String name) { 79 this.id = id; 80 this.name = name; 81 this.synthetic = false; 82 this.alternatives = new ArrayList (); 83 this.defaultAlt = -1; 84 this.lookAhead = null; 85 } 86 87 99 public boolean isSynthetic() { 100 return synthetic; 101 } 102 103 116 public boolean isSyntetic() { 117 return isSynthetic(); 118 } 119 120 128 public boolean isLeftRecursive() { 129 ProductionPatternAlternative alt; 130 131 for (int i = 0; i < alternatives.size(); i++) { 132 alt = (ProductionPatternAlternative) alternatives.get(i); 133 if (alt.isLeftRecursive()) { 134 return true; 135 } 136 } 137 return false; 138 } 139 140 148 public boolean isRightRecursive() { 149 ProductionPatternAlternative alt; 150 151 for (int i = 0; i < alternatives.size(); i++) { 152 alt = (ProductionPatternAlternative) alternatives.get(i); 153 if (alt.isRightRecursive()) { 154 return true; 155 } 156 } 157 return false; 158 } 159 160 168 public boolean isMatchingEmpty() { 169 ProductionPatternAlternative alt; 170 171 for (int i = 0; i < alternatives.size(); i++) { 172 alt = (ProductionPatternAlternative) alternatives.get(i); 173 if (alt.isMatchingEmpty()) { 174 return true; 175 } 176 } 177 return false; 178 } 179 180 185 public int getId() { 186 return id; 187 } 188 189 194 public String getName() { 195 return name; 196 } 197 198 208 public void setSynthetic(boolean synthetic) { 209 this.synthetic = synthetic; 210 } 211 212 223 public void setSyntetic(boolean synthetic) { 224 setSynthetic(synthetic); 225 } 226 227 232 public int getAlternativeCount() { 233 return alternatives.size(); 234 } 235 236 243 public ProductionPatternAlternative getAlternative(int pos) { 244 return (ProductionPatternAlternative) alternatives.get(pos); 245 } 246 247 255 public void addAlternative(ProductionPatternAlternative alt) 256 throws ParserCreationException { 257 258 if (alternatives.contains(alt)) { 259 throw new ParserCreationException( 260 ParserCreationException.INVALID_PRODUCTION_ERROR, 261 name, 262 "two identical alternatives exist"); 263 } 264 alt.setPattern(this); 265 alternatives.add(alt); 266 } 267 268 273 public String toString() { 274 StringBuffer buffer = new StringBuffer (); 275 StringBuffer indent = new StringBuffer (); 276 int i; 277 278 buffer.append(name); 279 buffer.append("("); 280 buffer.append(id); 281 buffer.append(") "); 282 for (i = 0; i < buffer.length(); i++) { 283 indent.append(" "); 284 } 285 for (i = 0; i < alternatives.size(); i++) { 286 if (i == 0) { 287 buffer.append("= "); 288 } else { 289 buffer.append("\n"); 290 buffer.append(indent); 291 buffer.append("| "); 292 } 293 buffer.append(alternatives.get(i)); 294 } 295 return buffer.toString(); 296 } 297 298 303 LookAheadSet getLookAhead() { 304 return lookAhead; 305 } 306 307 312 void setLookAhead(LookAheadSet lookAhead) { 313 this.lookAhead = lookAhead; 314 } 315 316 323 ProductionPatternAlternative getDefaultAlternative() { 324 if (defaultAlt >= 0) { 325 Object obj = alternatives.get(defaultAlt); 326 return (ProductionPatternAlternative) obj; 327 } else { 328 return null; 329 } 330 } 331 332 338 void setDefaultAlternative(int pos) { 339 if (pos >= 0 && pos < alternatives.size()) { 340 this.defaultAlt = pos; 341 } 342 } 343 } 344 | Popular Tags |