1 21 22 package net.percederberg.grammatica.parser; 23 24 33 public class Token extends Node { 34 35 38 private TokenPattern pattern; 39 40 44 private String image; 45 46 49 private int startLine; 50 51 54 private int startColumn; 55 56 59 private int endLine; 60 61 64 private int endColumn; 65 66 69 private Token previous = null; 70 71 74 private Token next = null; 75 76 84 public Token(TokenPattern pattern, String image, int line, int col) { 85 this.pattern = pattern; 86 this.image = image; 87 this.startLine = line; 88 this.startColumn = col; 89 this.endLine = line; 90 this.endColumn = col + image.length() - 1; 91 for (int pos = 0; image.indexOf('\n', pos) >= 0;) { 92 pos = image.indexOf('\n', pos) + 1; 93 this.endLine++; 94 this.endColumn = image.length() - pos; 95 } 96 } 97 98 105 public int getId() { 106 return pattern.getId(); 107 } 108 109 114 public String getName() { 115 return pattern.getName(); 116 } 117 118 124 public String getImage() { 125 return image; 126 } 127 128 133 public int getStartLine() { 134 return startLine; 135 } 136 137 142 public int getStartColumn() { 143 return startColumn; 144 } 145 146 151 public int getEndLine() { 152 return endLine; 153 } 154 155 160 public int getEndColumn() { 161 return endColumn; 162 } 163 164 169 TokenPattern getPattern() { 170 return pattern; 171 } 172 173 189 public Token getPreviousToken() { 190 return previous; 191 } 192 193 202 void setPreviousToken(Token previous) { 203 if (this.previous != null) { 204 this.previous.next = null; 205 } 206 this.previous = previous; 207 if (previous != null) { 208 previous.next = this; 209 } 210 } 211 212 228 public Token getNextToken() { 229 return next; 230 } 231 232 241 void setNextToken(Token next) { 242 if (this.next != null) { 243 this.next.previous = null; 244 } 245 this.next = next; 246 if (next != null) { 247 next.previous = this; 248 } 249 } 250 251 258 public String toString() { 259 StringBuffer buffer = new StringBuffer (); 260 int newline = image.indexOf('\n'); 261 262 buffer.append(pattern.getName()); 263 buffer.append("("); 264 buffer.append(pattern.getId()); 265 buffer.append("): \""); 266 if (newline >= 0) { 267 if (newline > 0 && image.charAt(newline - 1) == '\r') { 268 newline--; 269 } 270 buffer.append(image.substring(0, newline)); 271 buffer.append("(...)"); 272 } else { 273 buffer.append(image); 274 } 275 buffer.append("\", line: "); 276 buffer.append(startLine); 277 buffer.append(", col: "); 278 buffer.append(startColumn); 279 280 return buffer.toString(); 281 } 282 283 290 public String toShortString() { 291 StringBuffer buffer = new StringBuffer (); 292 int newline = image.indexOf('\n'); 293 294 buffer.append('"'); 295 if (newline >= 0) { 296 if (newline > 0 && image.charAt(newline - 1) == '\r') { 297 newline--; 298 } 299 buffer.append(image.substring(0, newline)); 300 buffer.append("(...)"); 301 } else { 302 buffer.append(image); 303 } 304 buffer.append('"'); 305 if (pattern.getType() == TokenPattern.REGEXP_TYPE) { 306 buffer.append(" <"); 307 buffer.append(pattern.getName()); 308 buffer.append(">"); 309 } 310 311 return buffer.toString(); 312 } 313 } 314 | Popular Tags |