CommonToken.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  2. * Use of this file is governed by the BSD 3-clause license that
  3. * can be found in the LICENSE.txt file in the project root.
  4. */
  5. #pragma once
  6. #include "WritableToken.h"
  7. namespace antlr4 {
  8. class ANTLR4CPP_PUBLIC CommonToken : public WritableToken {
  9. protected:
  10. /**
  11. * An empty {@link Pair} which is used as the default value of
  12. * {@link #source} for tokens that do not have a source.
  13. */
  14. static const std::pair<TokenSource *, CharStream *> EMPTY_SOURCE;
  15. /**
  16. * This is the backing field for {@link #getType} and {@link #setType}.
  17. */
  18. size_t _type;
  19. /**
  20. * This is the backing field for {@link #getLine} and {@link #setLine}.
  21. */
  22. size_t _line;
  23. /**
  24. * This is the backing field for {@link #getCharPositionInLine} and
  25. * {@link #setCharPositionInLine}.
  26. */
  27. size_t _charPositionInLine; // set to invalid position
  28. /**
  29. * This is the backing field for {@link #getChannel} and
  30. * {@link #setChannel}.
  31. */
  32. size_t _channel;
  33. /**
  34. * This is the backing field for {@link #getTokenSource} and
  35. * {@link #getInputStream}.
  36. *
  37. * <p>
  38. * These properties share a field to reduce the memory footprint of
  39. * {@link CommonToken}. Tokens created by a {@link CommonTokenFactory} from
  40. * the same source and input stream share a reference to the same
  41. * {@link Pair} containing these values.</p>
  42. */
  43. std::pair<TokenSource *, CharStream *> _source; // ml: pure references, usually from statically allocated classes.
  44. /**
  45. * This is the backing field for {@link #getText} when the token text is
  46. * explicitly set in the constructor or via {@link #setText}.
  47. *
  48. * @see #getText()
  49. */
  50. std::string _text;
  51. /**
  52. * This is the backing field for {@link #getTokenIndex} and
  53. * {@link #setTokenIndex}.
  54. */
  55. size_t _index;
  56. /**
  57. * This is the backing field for {@link #getStartIndex} and
  58. * {@link #setStartIndex}.
  59. */
  60. size_t _start;
  61. /**
  62. * This is the backing field for {@link #getStopIndex} and
  63. * {@link #setStopIndex}.
  64. */
  65. size_t _stop;
  66. public:
  67. /**
  68. * Constructs a new {@link CommonToken} with the specified token type.
  69. *
  70. * @param type The token type.
  71. */
  72. CommonToken(size_t type);
  73. CommonToken(std::pair<TokenSource*, CharStream*> source, size_t type, size_t channel, size_t start, size_t stop);
  74. /**
  75. * Constructs a new {@link CommonToken} with the specified token type and
  76. * text.
  77. *
  78. * @param type The token type.
  79. * @param text The text of the token.
  80. */
  81. CommonToken(size_t type, const std::string &text);
  82. /**
  83. * Constructs a new {@link CommonToken} as a copy of another {@link Token}.
  84. *
  85. * <p>
  86. * If {@code oldToken} is also a {@link CommonToken} instance, the newly
  87. * constructed token will share a reference to the {@link #text} field and
  88. * the {@link Pair} stored in {@link #source}. Otherwise, {@link #text} will
  89. * be assigned the result of calling {@link #getText}, and {@link #source}
  90. * will be constructed from the result of {@link Token#getTokenSource} and
  91. * {@link Token#getInputStream}.</p>
  92. *
  93. * @param oldToken The token to copy.
  94. */
  95. CommonToken(Token *oldToken);
  96. virtual size_t getType() const override;
  97. /**
  98. * Explicitly set the text for this token. If {code text} is not
  99. * {@code null}, then {@link #getText} will return this value rather than
  100. * extracting the text from the input.
  101. *
  102. * @param text The explicit text of the token, or {@code null} if the text
  103. * should be obtained from the input along with the start and stop indexes
  104. * of the token.
  105. */
  106. virtual void setText(const std::string &text) override;
  107. virtual std::string getText() const override;
  108. virtual void setLine(size_t line) override;
  109. virtual size_t getLine() const override;
  110. virtual size_t getCharPositionInLine() const override;
  111. virtual void setCharPositionInLine(size_t charPositionInLine) override;
  112. virtual size_t getChannel() const override;
  113. virtual void setChannel(size_t channel) override;
  114. virtual void setType(size_t type) override;
  115. virtual size_t getStartIndex() const override;
  116. virtual void setStartIndex(size_t start);
  117. virtual size_t getStopIndex() const override;
  118. virtual void setStopIndex(size_t stop);
  119. virtual size_t getTokenIndex() const override;
  120. virtual void setTokenIndex(size_t index) override;
  121. virtual TokenSource *getTokenSource() const override;
  122. virtual CharStream *getInputStream() const override;
  123. virtual std::string toString() const override;
  124. virtual std::string toString(Recognizer *r) const;
  125. private:
  126. void InitializeInstanceFields();
  127. };
  128. } // namespace antlr4