1 46 47 package org.codehaus.groovy.syntax; 48 49 import org.codehaus.groovy.GroovyBugError; 50 import org.codehaus.groovy.syntax.CSTNode; 51 52 53 67 68 public class Token extends CSTNode 69 { 70 public static final Token NULL = new Token(); 71 public static final Token EOF = new Token( Types.EOF, "", -1, -1 ); 72 73 74 77 private int type = Types.UNKNOWN; private int meaning = Types.UNKNOWN; 80 private String text = ""; private int startLine = -1; private int startColumn = -1; 84 85 88 89 public Token( int type, String text, int startLine, int startColumn ) 90 { 91 this.type = type; 92 this.meaning = type; 93 this.text = text; 94 this.startLine = startLine; 95 this.startColumn = startColumn; 96 } 97 98 99 102 103 private Token() { } 104 105 106 107 110 111 public Token dup() 112 { 113 Token token = new Token( this.type, this.text, this.startLine, this.startColumn ); 114 token.setMeaning( this.meaning ); 115 116 return token; 117 } 118 119 120 121 122 125 126 130 131 public int getMeaning() 132 { 133 return meaning; 134 } 135 136 137 138 143 144 public CSTNode setMeaning( int meaning ) 145 { 146 this.meaning = meaning; 147 return this; 148 } 149 150 151 152 156 157 public int getType() 158 { 159 return type; 160 } 161 162 163 164 165 168 169 172 173 public int size() 174 { 175 return 1; 176 } 177 178 179 180 183 184 public CSTNode get( int index ) 185 { 186 if( index > 0 ) 187 { 188 throw new GroovyBugError( "attempt to access Token element other than root" ); 189 } 190 191 return this; 192 } 193 194 195 196 201 202 public Token getRoot() 203 { 204 return this; 205 } 206 207 208 209 214 215 public String getRootText() 216 { 217 return text; 218 } 219 220 221 222 226 227 public String getText() 228 { 229 return text; 230 } 231 232 233 234 238 239 public void setText( String text ) 240 { 241 this.text = text; 242 } 243 244 245 246 250 251 public int getStartLine() 252 { 253 return startLine; 254 } 255 256 257 258 262 263 public int getStartColumn() 264 { 265 return startColumn; 266 } 267 268 269 270 271 274 275 279 280 public Reduction asReduction() 281 { 282 return new Reduction( this ); 283 } 284 285 286 287 291 292 public Reduction asReduction( CSTNode second ) 293 { 294 Reduction created = asReduction(); 295 created.add( second ); 296 return created; 297 } 298 299 300 301 305 306 public Reduction asReduction( CSTNode second, CSTNode third ) 307 { 308 Reduction created = asReduction( second ); 309 created.add( third ); 310 return created; 311 } 312 313 314 315 319 320 public Reduction asReduction( CSTNode second, CSTNode third, CSTNode fourth ) 321 { 322 Reduction created = asReduction( second, third ); 323 created.add( fourth ); 324 return created; 325 } 326 327 328 329 330 333 334 338 339 public static Token newKeyword( String text, int startLine, int startColumn ) 340 { 341 342 int type = Types.lookupKeyword( text ); 343 if( type != Types.UNKNOWN ) 344 { 345 return new Token( type, text, startLine, startColumn ); 346 } 347 348 return null; 349 350 } 351 352 353 356 357 public static Token newString( String text, int startLine, int startColumn ) 358 { 359 return new Token( Types.STRING, text, startLine, startColumn ); 360 } 361 362 363 366 367 public static Token newIdentifier( String text, int startLine, int startColumn ) 368 { 369 return new Token( Types.IDENTIFIER, text, startLine, startColumn ); 370 } 371 372 373 376 377 public static Token newInteger( String text, int startLine, int startColumn ) 378 { 379 return new Token( Types.INTEGER_NUMBER, text, startLine, startColumn ); 380 } 381 382 383 386 387 public static Token newDecimal( String text, int startLine, int startColumn ) 388 { 389 return new Token( Types.DECIMAL_NUMBER, text, startLine, startColumn ); 390 } 391 392 393 396 397 public static Token newSymbol( int type, int startLine, int startColumn ) 398 { 399 return new Token( type, Types.getText(type), startLine, startColumn ); 400 } 401 402 403 406 407 public static Token newSymbol( String type, int startLine, int startColumn ) 408 { 409 return new Token( Types.lookupSymbol(type), type, startLine, startColumn ); 410 } 411 412 413 416 417 public static Token newPlaceholder( int type ) 418 { 419 Token token = new Token( Types.UNKNOWN, "", -1, -1 ); 420 token.setMeaning( type ); 421 422 return token; 423 } 424 425 } 426 | Popular Tags |