Token.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. private int line;
  9. private int column;
  10. private int length;
  11. private String file;
  12. private boolean marked;
  13. private boolean basecode = false;
  14. private int hash = -1; // hash-value. set and used by main algorithm (GSTiling)
  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. setLine(line > 0 ? line : 1);
  26. }
  27. /**
  28. * Creates a token with column and length information.
  29. * @param type is the token type.
  30. * @param file is the name of the source code file.
  31. * @param line is the line index in the source code where the token resides. Cannot be smaller than 1.
  32. * @param column is the column index, meaning where the token starts in the line.
  33. * @param length is the length of the token in the source code.
  34. */
  35. public Token(int type, String file, int line, int column, int length) {
  36. this(type, file, line);
  37. this.column = column;
  38. this.length = length;
  39. }
  40. /**
  41. * Returns the character index which denotes where the code sections represented by this token starts in the line.
  42. * @return the character index in the line.
  43. */
  44. public int getColumn() {
  45. return column;
  46. }
  47. /**
  48. * @return the name of the file where the source code that the token represents is located in.
  49. */
  50. public String getFile() {
  51. return file;
  52. }
  53. // this is made to distinguish the character front end. Maybe other front ends can use it too?
  54. public int getIndex() {
  55. return -1;
  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. /**
  85. * Sets the length if the code sections represented by this token.
  86. * @param length is the length in characters to set.
  87. */
  88. public void setLength(int length) {
  89. this.length = length;
  90. }
  91. /**
  92. * Sets the line index denoting in which line the code sections represented by this token starts.
  93. * @param line is the line index to set.
  94. */
  95. public void setLine(int line) {
  96. this.line = line;
  97. }
  98. @Override
  99. public String toString() {
  100. return type2string();
  101. }
  102. /**
  103. * @return a string representation depending on the type of the token.
  104. */
  105. protected abstract String type2string();
  106. /* package-private */ int getHash() {
  107. return hash;
  108. }
  109. /**
  110. * @return whether this token is part of a basecode.
  111. */
  112. /* package-private */ boolean isBasecode() {
  113. return basecode;
  114. }
  115. /**
  116. * @return whether this token is marked by the comparison algorithm.
  117. */
  118. /* package-private */ boolean isMarked() {
  119. return marked;
  120. }
  121. /* package-private */ boolean setBasecode(boolean basecode) {
  122. this.basecode = basecode;
  123. return basecode;
  124. }
  125. /* package-private */ void setFile(String file) {
  126. this.file = file;
  127. }
  128. /* package-private */ void setHash(int hash) {
  129. this.hash = hash;
  130. }
  131. /* package-private */ boolean setMarked(boolean marked) {
  132. this.marked = marked;
  133. return marked;
  134. }
  135. }