]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/Token.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / Token.h
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
6 #pragma once
7
8 #include "IntStream.h"
9
10 namespace antlr4 {
11
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 {
16   public:
17 #if __cplusplus >= 201703L
18     static constexpr size_t INVALID_TYPE = 0;
19 #else
20     enum : size_t {
21       INVALID_TYPE = 0,
22     };
23 #endif
24
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;
31 #else
32     enum : size_t {
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,
35       EOF = IntStream::EOF,
36     };
37 #endif
38
39     virtual ~Token();
40
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;
46 #else
47     enum : size_t {
48       DEFAULT_CHANNEL = 0,
49     };
50 #endif
51
52     /// Anything on different channel than DEFAULT_CHANNEL is not parsed
53     /// by parser.
54 #if __cplusplus >= 201703L
55     static constexpr size_t HIDDEN_CHANNEL = 1;
56 #else
57     enum : size_t {
58       HIDDEN_CHANNEL = 1,
59     };
60 #endif
61
62     /**
63      * This is the minimum constant value which can be assigned to a
64      * user-defined token channel.
65      *
66      * <p>
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>
70      *
71      * @see Token#getChannel()
72      */
73 #if __cplusplus >= 201703L
74     static constexpr size_t MIN_USER_CHANNEL_VALUE = 2;
75 #else
76     enum : size_t {
77       MIN_USER_CHANNEL_VALUE = 2,
78     };
79 #endif
80
81     /// Get the text of the token.
82     virtual std::string getText() const = 0;
83
84     /// Get the token type of the token
85     virtual size_t getType() const = 0;
86
87     /// The line number on which the 1st character of this token was matched,  line=1..n
88     virtual size_t getLine() const = 0;
89
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;
93
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;
98
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.
102     ///
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;
106
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;
110
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;
114
115     /// Gets the <seealso cref="TokenSource"/> which created this token.
116     virtual TokenSource *getTokenSource() const = 0;
117
118     /// Gets the <seealso cref="CharStream"/> from which this token was derived.
119     virtual CharStream *getInputStream() const = 0;
120
121     virtual std::string toString() const = 0;
122   };
123
124 } // namespace antlr4