Token.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package de.jplag;
  2. /**
  3. * This class represents a token in a source code. It can represents keywords, identifies, 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 int NO_VALUE = -1;
  10. private int line;
  11. private int column;
  12. private int length;
  13. private String file;
  14. private boolean basecode = false;
  15. protected int type;
  16. /**
  17. * Creates a token without information about the column or the length of the token in the line.
  18. * @param type is the token type.
  19. * @param file is the name of the source code file.
  20. * @param line is the line index in the source code where the token resides. Cannot be smaller than 1.
  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. // this is made to distinguish the character front end. Maybe other front ends can use it too?
  58. public int getIndex() {
  59. return NO_VALUE;
  60. }
  61. /**
  62. * Gives the length if the code sections represented by this token.
  63. * @return the length in characters.
  64. */
  65. public int getLength() {
  66. return length;
  67. }
  68. /**
  69. * Gives the line index denoting in which line the code sections represented by this token starts.
  70. * @return the line index.
  71. */
  72. public int getLine() {
  73. return line;
  74. }
  75. /**
  76. * @return the type identifier of the token.
  77. */
  78. public int getType() {
  79. return type;
  80. }
  81. /**
  82. * Sets the character index which denotes where the code sections represented by this token starts in the line.
  83. * @param column is the index in characters to set.
  84. */
  85. public void setColumn(int column) {
  86. this.column = column;
  87. }
  88. /**
  89. * Sets the line index denoting in which line the code sections represented by this token starts.
  90. * @param line is the line index to set.
  91. */
  92. public void setLine(int line) {
  93. this.line = line;
  94. }
  95. @Override
  96. public String toString() {
  97. return type2string();
  98. }
  99. /**
  100. * @return a string representation depending on the type of the token.
  101. */
  102. protected abstract String type2string();
  103. /**
  104. * @return whether this token is part of a basecode.
  105. */
  106. /* package-private */ boolean isBasecode() {
  107. return basecode;
  108. }
  109. /* package-private */ boolean setBasecode(boolean basecode) {
  110. this.basecode = basecode;
  111. return basecode;
  112. }
  113. /* package-private */ void setFile(String file) {
  114. this.file = file;
  115. }
  116. }