1 30 31 package de.susebox.jtopas; 32 33 37 54 public class Token { 55 56 60 63 public static final byte NORMAL = 0; 64 65 68 public static final byte KEYWORD = 1; 69 70 74 public static final byte STRING = 2; 75 76 80 public static final byte PATTERN = 3; 81 82 89 public static final byte SPECIAL_SEQUENCE = 4; 90 91 96 public static final byte SEPARATOR = 5; 97 98 104 public static final byte WHITESPACE = 6; 105 106 111 public static final byte LINE_COMMENT = 7; 112 113 117 public static final byte BLOCK_COMMENT = 8; 118 119 123 public static final byte EOF = -1; 124 125 128 public static final byte UNKNOWN = -2; 129 130 131 135 142 public void setType(int type) { 143 _type = type; 144 } 145 146 153 public int getType() { 154 return _type; 155 } 156 157 166 public void setImage(String image) { 167 if ((_image = image) == null) { 168 _length = 0; 169 } else { 170 _length = _image.length(); 171 } 172 } 173 174 183 public String getImage() { 184 return _image; 185 } 186 187 249 public String [] getImageParts() { 250 if (_imageParts != null) { 251 return _imageParts; 252 } else { 253 return new String [] { getImage() }; 254 } 255 } 256 257 264 public void setImageParts(String [] imageParts) { 265 _imageParts = imageParts; 266 } 267 268 285 public void setLength(int length) { 286 _length = length; 287 } 288 289 297 public int getLength() { 298 return _length; 299 } 300 301 308 public void setCompanion(Object companion) { 309 _companion = companion; 310 } 311 312 318 public Object getCompanion() { 319 return _companion; 320 } 321 322 331 public void setStartPosition(int startPosition) { 332 _startPosition = startPosition; 333 } 334 335 343 public int getStartPosition() { 344 return _startPosition; 345 } 346 347 364 public void setEndPosition(int endPosition) { 365 setLength(endPosition - _startPosition); 366 } 367 368 378 public int getEndPosition() { 379 return getLength() - getStartPosition(); 380 } 381 382 390 public void setStartLine(int lineno) { 391 _startLine = lineno; 392 } 393 394 403 public int getStartLine() { 404 return _startLine; 405 } 406 407 415 public void setStartColumn(int colno) { 416 _startColumn = colno; 417 } 418 419 428 public int getStartColumn() { 429 return _startColumn; 430 } 431 432 442 public void setEndLine(int lineno) { 443 _endLine = lineno; 444 } 445 446 455 public int getEndLine() { 456 return _endLine; 457 } 458 459 468 public void setEndColumn(int colno) { 469 _endColumn = colno; 470 } 471 472 481 public int getEndColumn() { 482 return _endColumn; 483 } 484 485 486 490 493 public Token() { 494 this(UNKNOWN, null, null); 495 } 496 497 503 public Token(int type) { 504 this(type, null, null); 505 } 506 507 514 public Token(int type, String image) { 515 this(type, image, null); 516 } 517 518 526 public Token(int type, String image, Object companion) { 527 setType(type); 528 setImage(image); 529 setCompanion(companion); 530 setStartPosition(-1); 531 setStartLine(-1); 532 setStartColumn(-1); 533 setEndLine(-1); 534 setEndColumn(-1); 535 setImageParts(null); 536 } 537 538 539 543 553 public boolean equals(Object object) { 554 if (object == null) { 556 return false; 557 } else if (object == this) { 558 return true; 559 } else if (object.getClass() != getClass()) { 560 return false; 561 } 562 563 Token other = (Token)object; 565 566 if (getType() != other.getType()) { 567 return false; 568 } else if (getStartPosition() != other.getStartPosition()) { 569 return false; 570 } else if (getLength() != other.getLength()) { 571 return false; 572 } else if (getStartLine() != other.getStartLine()) { 573 return false; 574 } else if (getStartColumn() != other.getStartColumn()) { 575 return false; 576 } else if (getEndLine() != other.getEndLine()) { 577 return false; 578 } else if (getEndColumn() != other.getEndColumn()) { 579 return false; 580 } else if ( (getCompanion() == null && other.getCompanion() != null) 581 || (getCompanion() != null && ! getCompanion().equals(other.getCompanion()))) { 582 return false; 583 } else if ( (getImage() == null && other.getImage() != null) 584 || (getImage() != null && ! getImage().equals(other.getImage()))) { 585 return false; 586 } 587 return true; 588 } 589 590 595 public String toString() { 596 StringBuffer buffer = new StringBuffer (); 597 598 buffer.append("Type "); 600 buffer.append(Token.getTypeName(getType())); 601 602 if (getType() != EOF) { 604 buffer.append(": "); 605 if (getImage() != null) { 606 buffer.append('"'); 607 buffer.append(getImage()); 608 buffer.append('"'); 609 } else { 610 buffer.append("no image, length "); 611 buffer.append(getLength()); 612 } 613 } 614 return buffer.toString(); 615 } 616 617 624 public static String getTypeName(int type) { 625 switch (type) { 626 case NORMAL: 627 return "NORMAL"; 628 case KEYWORD: 629 return "KEYWORD"; 630 case STRING: 631 return "STRING"; 632 case PATTERN: 633 return "PATTERN"; 634 case SPECIAL_SEQUENCE: 635 return "SPECIAL_SEQUENCE"; 636 case SEPARATOR: 637 return "SEPARATOR"; 638 case WHITESPACE: 639 return "WHITESPACE"; 640 case LINE_COMMENT: 641 return "LINE_COMMENT"; 642 case BLOCK_COMMENT: 643 return "BLOCK_COMMENT"; 644 case EOF: 645 return "EOF"; 646 default: 647 return "UNKNOWN"; 648 } 649 } 650 651 652 656 662 protected int _type; 663 664 671 protected String _image; 672 673 679 protected int _length; 680 681 689 protected Object _companion; 690 691 697 protected int _startPosition; 698 699 707 protected int _startLine; 708 709 717 protected int _startColumn; 718 719 727 protected int _endLine; 728 729 737 protected int _endColumn; 738 739 742 protected String [] _imageParts; 743 } 744 | Popular Tags |