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.
12 /// A token has properties: text, type, line, character position in the line
13 /// (so we can ignore tabs), token channel, index, and source from which
14 /// we obtained this token.
15 class ANTLR4CPP_PUBLIC Token {
17 #if __cplusplus >= 201703L
18 static constexpr size_t INVALID_TYPE = 0;
25 /// During lookahead operations, this "token" signifies we hit rule end ATN state
26 /// and did not follow it despite needing to.
27 #if __cplusplus >= 201703L
28 static constexpr size_t EPSILON = std::numeric_limits<size_t>::max() - 1;
29 static constexpr size_t MIN_USER_TOKEN_TYPE = 1;
30 static constexpr size_t EOF = IntStream::EOF;
33 EPSILON = static_cast<size_t>(-2), // std::numeric_limits<size_t>::max() - 1; doesn't work in VS 2013
34 MIN_USER_TOKEN_TYPE = 1,
41 /// All tokens go to the parser (unless skip() is called in that rule)
42 /// on a particular "channel". The parser tunes to a particular channel
43 /// so that whitespace etc... can go to the parser on a "hidden" channel.
44 #if __cplusplus >= 201703L
45 static constexpr size_t DEFAULT_CHANNEL = 0;
52 /// Anything on different channel than DEFAULT_CHANNEL is not parsed
54 #if __cplusplus >= 201703L
55 static constexpr size_t HIDDEN_CHANNEL = 1;
63 * This is the minimum constant value which can be assigned to a
64 * user-defined token channel.
67 * The non-negative numbers less than {@link #MIN_USER_CHANNEL_VALUE} are
68 * assigned to the predefined channels {@link #DEFAULT_CHANNEL} and
69 * {@link #HIDDEN_CHANNEL}.</p>
71 * @see Token#getChannel()
73 #if __cplusplus >= 201703L
74 static constexpr size_t MIN_USER_CHANNEL_VALUE = 2;
77 MIN_USER_CHANNEL_VALUE = 2,
81 /// Get the text of the token.
82 virtual std::string getText() const = 0;
84 /// Get the token type of the token
85 virtual size_t getType() const = 0;
87 /// The line number on which the 1st character of this token was matched, line=1..n
88 virtual size_t getLine() const = 0;
90 /// The index of the first character of this token relative to the
91 /// beginning of the line at which it occurs, 0..n-1
92 virtual size_t getCharPositionInLine() const = 0;
94 /// Return the channel this token. Each token can arrive at the parser
95 /// on a different channel, but the parser only "tunes" to a single channel.
96 /// The parser ignores everything not on DEFAULT_CHANNEL.
97 virtual size_t getChannel() const = 0;
99 /// An index from 0..n-1 of the token object in the input stream.
100 /// This must be valid in order to print token streams and
101 /// use TokenRewriteStream.
103 /// Return INVALID_INDEX to indicate that this token was conjured up since
104 /// it doesn't have a valid index.
105 virtual size_t getTokenIndex() const = 0;
107 /// The starting character index of the token
108 /// This method is optional; return INVALID_INDEX if not implemented.
109 virtual size_t getStartIndex() const = 0;
111 /// The last character index of the token.
112 /// This method is optional; return INVALID_INDEX if not implemented.
113 virtual size_t getStopIndex() const = 0;
115 /// Gets the <seealso cref="TokenSource"/> which created this token.
116 virtual TokenSource *getTokenSource() const = 0;
118 /// Gets the <seealso cref="CharStream"/> from which this token was derived.
119 virtual CharStream *getInputStream() const = 0;
121 virtual std::string toString() const = 0;
124 } // namespace antlr4