1 21 22 package net.percederberg.grammatica.parser; 23 24 33 public class TokenPattern { 34 35 39 public static final int STRING_TYPE = 1; 40 41 45 public static final int REGEXP_TYPE = 2; 46 47 50 private int id; 51 52 55 private String name; 56 57 60 private int type; 61 62 65 private String pattern; 66 67 74 private boolean error = false; 75 76 82 private String errorMessage = null; 83 84 92 private boolean ignore = false; 93 94 101 private String ignoreMessage = null; 102 103 111 public TokenPattern(int id, String name, int type, String pattern) { 112 113 this.id = id; 114 this.name = name; 115 this.type = type; 116 this.pattern = pattern; 117 } 118 119 127 public boolean isError() { 128 return error; 129 } 130 131 138 public boolean isIgnore() { 139 return ignore; 140 } 141 142 147 public int getId() { 148 return id; 149 } 150 151 156 public String getName() { 157 return name; 158 } 159 160 168 public int getType() { 169 return type; 170 } 171 172 177 public String getPattern() { 178 return pattern; 179 } 180 181 187 public String getErrorMessage() { 188 return errorMessage; 189 } 190 191 197 public String getIgnoreMessage() { 198 return ignoreMessage; 199 } 200 201 204 public void setError() { 205 setError("unrecognized token found"); 206 } 207 208 214 public void setError(String message) { 215 error = true; 216 errorMessage = message; 217 } 218 219 222 public void setIgnore() { 223 setIgnore(null); 224 } 225 226 232 public void setIgnore(String message) { 233 ignore = true; 234 ignoreMessage = message; 235 } 236 237 242 public String toString() { 243 StringBuffer buffer = new StringBuffer (); 244 245 buffer.append(name); 246 buffer.append(" ("); 247 buffer.append(id); 248 buffer.append(") = "); 249 if (type == STRING_TYPE) { 250 buffer.append("\""); 251 buffer.append(pattern); 252 buffer.append("\""); 253 } else if (type == REGEXP_TYPE) { 254 buffer.append("<<"); 255 buffer.append(pattern); 256 buffer.append(">>"); 257 } 258 if (error) { 259 buffer.append(" ERROR: \""); 260 buffer.append(errorMessage); 261 buffer.append("\""); 262 } 263 if (ignore) { 264 buffer.append(" IGNORE"); 265 if (ignoreMessage != null) { 266 buffer.append(": \""); 267 buffer.append(ignoreMessage); 268 buffer.append("\""); 269 } 270 } 271 272 return buffer.toString(); 273 } 274 275 280 public String toShortString() { 281 StringBuffer buffer = new StringBuffer (); 282 int newline = pattern.indexOf('\n'); 283 284 if (type == STRING_TYPE) { 285 buffer.append("\""); 286 if (newline >= 0) { 287 if (newline > 0 && pattern.charAt(newline - 1) == '\r') { 288 newline--; 289 } 290 buffer.append(pattern.substring(0, newline)); 291 buffer.append("(...)"); 292 } else { 293 buffer.append(pattern); 294 } 295 buffer.append("\""); 296 } else { 297 buffer.append("<"); 298 buffer.append(name); 299 buffer.append(">"); 300 } 301 302 return buffer.toString(); 303 } 304 } 305 | Popular Tags |