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.
8 #include "WritableToken.h"
12 class ANTLR4CPP_PUBLIC CommonToken : public WritableToken {
15 * An empty {@link Pair} which is used as the default value of
16 * {@link #source} for tokens that do not have a source.
18 static const std::pair<TokenSource *, CharStream *> EMPTY_SOURCE;
21 * This is the backing field for {@link #getType} and {@link #setType}.
26 * This is the backing field for {@link #getLine} and {@link #setLine}.
31 * This is the backing field for {@link #getCharPositionInLine} and
32 * {@link #setCharPositionInLine}.
34 size_t _charPositionInLine; // set to invalid position
37 * This is the backing field for {@link #getChannel} and
38 * {@link #setChannel}.
43 * This is the backing field for {@link #getTokenSource} and
44 * {@link #getInputStream}.
47 * These properties share a field to reduce the memory footprint of
48 * {@link CommonToken}. Tokens created by a {@link CommonTokenFactory} from
49 * the same source and input stream share a reference to the same
50 * {@link Pair} containing these values.</p>
53 std::pair<TokenSource *, CharStream *> _source; // ml: pure references, usually from statically allocated classes.
56 * This is the backing field for {@link #getText} when the token text is
57 * explicitly set in the constructor or via {@link #setText}.
64 * This is the backing field for {@link #getTokenIndex} and
65 * {@link #setTokenIndex}.
70 * This is the backing field for {@link #getStartIndex} and
71 * {@link #setStartIndex}.
76 * This is the backing field for {@link #getStopIndex} and
77 * {@link #setStopIndex}.
83 * Constructs a new {@link CommonToken} with the specified token type.
85 * @param type The token type.
87 CommonToken(size_t type);
88 CommonToken(std::pair<TokenSource*, CharStream*> source, size_t type, size_t channel, size_t start, size_t stop);
91 * Constructs a new {@link CommonToken} with the specified token type and
94 * @param type The token type.
95 * @param text The text of the token.
97 CommonToken(size_t type, const std::string &text);
100 * Constructs a new {@link CommonToken} as a copy of another {@link Token}.
103 * If {@code oldToken} is also a {@link CommonToken} instance, the newly
104 * constructed token will share a reference to the {@link #text} field and
105 * the {@link Pair} stored in {@link #source}. Otherwise, {@link #text} will
106 * be assigned the result of calling {@link #getText}, and {@link #source}
107 * will be constructed from the result of {@link Token#getTokenSource} and
108 * {@link Token#getInputStream}.</p>
110 * @param oldToken The token to copy.
112 CommonToken(Token *oldToken);
114 virtual size_t getType() const override;
117 * Explicitly set the text for this token. If {code text} is not
118 * {@code null}, then {@link #getText} will return this value rather than
119 * extracting the text from the input.
121 * @param text The explicit text of the token, or {@code null} if the text
122 * should be obtained from the input along with the start and stop indexes
125 virtual void setText(const std::string &text) override;
126 virtual std::string getText() const override;
128 virtual void setLine(size_t line) override;
129 virtual size_t getLine() const override;
131 virtual size_t getCharPositionInLine() const override;
132 virtual void setCharPositionInLine(size_t charPositionInLine) override;
134 virtual size_t getChannel() const override;
135 virtual void setChannel(size_t channel) override;
137 virtual void setType(size_t type) override;
139 virtual size_t getStartIndex() const override;
140 virtual void setStartIndex(size_t start);
142 virtual size_t getStopIndex() const override;
143 virtual void setStopIndex(size_t stop);
145 virtual size_t getTokenIndex() const override;
146 virtual void setTokenIndex(size_t index) override;
148 virtual TokenSource *getTokenSource() const override;
149 virtual CharStream *getInputStream() const override;
151 virtual std::string toString() const override;
153 virtual std::string toString(Recognizer *r) const;
155 void InitializeInstanceFields();
158 } // namespace antlr4