1 21 22 package net.percederberg.grammatica.parser; 23 24 34 public class ProductionPatternElement { 35 36 40 private boolean token; 41 42 45 private int id; 46 47 50 private int min; 51 52 55 private int max; 56 57 60 private LookAheadSet lookAhead; 61 62 72 public ProductionPatternElement(boolean isToken, 73 int id, 74 int min, 75 int max) { 76 77 this.token = isToken; 78 this.id = id; 79 if (min < 0) { 80 min = 0; 81 } 82 this.min = min; 83 if (max <= 0) { 84 max = Integer.MAX_VALUE; 85 } else if (max < min) { 86 max = min; 87 } 88 this.max = max; 89 this.lookAhead = null; 90 } 91 92 98 public boolean isToken() { 99 return token; 100 } 101 102 108 public boolean isProduction() { 109 return !token; 110 } 111 112 122 public boolean isMatch(Token token) { 123 return isToken() && token != null && token.getId() == id; 124 } 125 126 131 public int getId() { 132 return id; 133 } 134 135 140 public int getMinCount() { 141 return min; 142 } 143 144 149 public int getMaxCount() { 150 return max; 151 } 152 153 162 public boolean equals(Object obj) { 163 ProductionPatternElement elem; 164 165 if (obj instanceof ProductionPatternElement) { 166 elem = (ProductionPatternElement) obj; 167 return this.token == elem.token 168 && this.id == elem.id 169 && this.min == elem.min 170 && this.max == elem.max; 171 } else { 172 return false; 173 } 174 } 175 176 181 public String toString() { 182 StringBuffer buffer = new StringBuffer (); 183 184 buffer.append(id); 185 if (token) { 186 buffer.append("(Token)"); 187 } else { 188 buffer.append("(Production)"); 189 } 190 if (min != 1 || max != 1) { 191 buffer.append("{"); 192 buffer.append(min); 193 buffer.append(","); 194 buffer.append(max); 195 buffer.append("}"); 196 } 197 return buffer.toString(); 198 } 199 200 205 LookAheadSet getLookAhead() { 206 return lookAhead; 207 } 208 209 214 void setLookAhead(LookAheadSet lookAhead) { 215 this.lookAhead = lookAhead; 216 } 217 } 218 | Popular Tags |