Token.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package de.jplag;
  2. import java.io.File;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. /**
  6. * This class represents a token in a source code. It can represent keywords, identifiers, syntactical structures etc.
  7. * What types of tokens there are depends on the specific language, meaning JPlag does not enforce a specific token set.
  8. * The language parsers decide what is a token and what is not.
  9. */
  10. public class Token {
  11. private Logger logger = LoggerFactory.getLogger(this.getClass());
  12. /** Indicates that the requested field has no value. */
  13. public static final int NO_VALUE = -1;
  14. private int line;
  15. private int column;
  16. private int length;
  17. private File file;
  18. private TokenType type;
  19. /**
  20. * Creates a token of type {@link SharedTokenType#FILE_END FILE_END} without information about line, column, and length.
  21. * @param file is the name of the source code file.
  22. */
  23. public static Token fileEnd(File file) {
  24. return new Token(SharedTokenType.FILE_END, file, NO_VALUE, NO_VALUE, NO_VALUE);
  25. }
  26. /**
  27. * Creates a token with column and length information.
  28. * @param type is the token type.
  29. * @param file is the name of the source code file.
  30. * @param line is the line index in the source code where the token resides. Index is 1-based.
  31. * @param column is the column index, meaning where the token starts in the line. Index is 1-based.
  32. * @param length is the length of the token in the source code.
  33. */
  34. public Token(TokenType type, File file, int line, int column, int length) {
  35. if (line == 0) {
  36. logger.warn("Creating a token with line index 0 while index is 1-based");
  37. }
  38. if (column == 0) {
  39. logger.warn("Creating a token with column index 0 while index is 1-based");
  40. }
  41. this.type = type;
  42. this.file = file;
  43. this.line = line;
  44. this.column = column;
  45. this.length = length;
  46. }
  47. /**
  48. * Returns the character index which denotes where the code sections represented by this token starts in the line.
  49. * @return the character index in the line.
  50. */
  51. public int getColumn() {
  52. return column;
  53. }
  54. /**
  55. * @return the name of the file where the source code that the token represents is located in.
  56. */
  57. public File getFile() {
  58. return file;
  59. }
  60. /**
  61. * Gives the length if the code sections represented by this token.
  62. * @return the length in characters.
  63. */
  64. public int getLength() {
  65. return length;
  66. }
  67. /**
  68. * Gives the line index denoting in which line the code sections represented by this token starts.
  69. * @return the line index.
  70. */
  71. public int getLine() {
  72. return line;
  73. }
  74. /**
  75. * @return the type of the token.
  76. */
  77. public TokenType getType() {
  78. return type;
  79. }
  80. }