Token.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package de.jplag;
  2. /**
  3. * This class represents a token in a source code. It can represent keywords, identifiers, syntactical structures etc.
  4. * What types of tokens there are depends on the specific language, meaning JPlag does not enforce a specific token set.
  5. * The language parsers decide what is a token and what is not.
  6. */
  7. public abstract class Token {
  8. /** Indicates that the requested field has no value. */
  9. public static final int NO_VALUE = -1;
  10. private int line;
  11. private int column;
  12. private int length;
  13. private String file;
  14. protected int type;
  15. /**
  16. * Creates a token without information about the column or the length of the token in the line.
  17. * @param type is the token type.
  18. * @param file is the name of the source code file.
  19. * @param line is the line index in the source code where the token resides. Cannot be smaller than 1. For
  20. * {@link TokenConstants#FILE_END FILE_END} it is automatically set to {@link #NO_VALUE}.
  21. */
  22. public Token(int type, String file, int line) {
  23. this.type = type;
  24. this.file = file;
  25. if (type == TokenConstants.FILE_END) {
  26. this.line = NO_VALUE;
  27. } else {
  28. this.line = line > 0 ? line : 1;
  29. }
  30. }
  31. /**
  32. * Creates a token with column and length information.
  33. * @param type is the token type.
  34. * @param file is the name of the source code file.
  35. * @param line is the line index in the source code where the token resides. Cannot be smaller than 1.
  36. * @param column is the column index, meaning where the token starts in the line.
  37. * @param length is the length of the token in the source code.
  38. */
  39. public Token(int type, String file, int line, int column, int length) {
  40. this(type, file, line);
  41. this.column = column;
  42. this.length = length;
  43. }
  44. /**
  45. * Returns the character index which denotes where the code sections represented by this token starts in the line.
  46. * @return the character index in the line.
  47. */
  48. public int getColumn() {
  49. return column;
  50. }
  51. /**
  52. * @return the name of the file where the source code that the token represents is located in.
  53. */
  54. public String getFile() {
  55. return file;
  56. }
  57. /**
  58. * Gives the length if the code sections represented by this token.
  59. * @return the length in characters.
  60. */
  61. public int getLength() {
  62. return length;
  63. }
  64. /**
  65. * Gives the line index denoting in which line the code sections represented by this token starts.
  66. * @return the line index.
  67. */
  68. public int getLine() {
  69. return line;
  70. }
  71. /**
  72. * @return the type identifier of the token.
  73. */
  74. public int getType() {
  75. return type;
  76. }
  77. /**
  78. * Sets the character index which denotes where the code sections represented by this token starts in the line.
  79. * @param column is the index in characters to set.
  80. */
  81. public void setColumn(int column) {
  82. this.column = column;
  83. }
  84. @Override
  85. public String toString() {
  86. return type2string();
  87. }
  88. /**
  89. * @return a string representation depending on the type of the token.
  90. */
  91. protected abstract String type2string();
  92. }