]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/CommonToken.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / CommonToken.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 "WritableToken.h"
9
10 namespace antlr4 {
11
12   class ANTLR4CPP_PUBLIC CommonToken : public WritableToken {
13   protected:
14     /**
15      * An empty {@link Pair} which is used as the default value of
16      * {@link #source} for tokens that do not have a source.
17      */
18     static const std::pair<TokenSource *, CharStream *> EMPTY_SOURCE;
19
20     /**
21      * This is the backing field for {@link #getType} and {@link #setType}.
22      */
23     size_t _type;
24
25     /**
26      * This is the backing field for {@link #getLine} and {@link #setLine}.
27      */
28     size_t _line;
29
30     /**
31      * This is the backing field for {@link #getCharPositionInLine} and
32      * {@link #setCharPositionInLine}.
33      */
34     size_t _charPositionInLine; // set to invalid position
35
36     /**
37      * This is the backing field for {@link #getChannel} and
38      * {@link #setChannel}.
39      */
40     size_t _channel;
41
42     /**
43      * This is the backing field for {@link #getTokenSource} and
44      * {@link #getInputStream}.
45      *
46      * <p>
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>
51      */
52
53     std::pair<TokenSource *, CharStream *> _source; // ml: pure references, usually from statically allocated classes.
54
55     /**
56      * This is the backing field for {@link #getText} when the token text is
57      * explicitly set in the constructor or via {@link #setText}.
58      *
59      * @see #getText()
60      */
61     std::string _text;
62
63     /**
64      * This is the backing field for {@link #getTokenIndex} and
65      * {@link #setTokenIndex}.
66      */
67     size_t _index;
68
69     /**
70      * This is the backing field for {@link #getStartIndex} and
71      * {@link #setStartIndex}.
72      */
73     size_t _start;
74
75     /**
76      * This is the backing field for {@link #getStopIndex} and
77      * {@link #setStopIndex}.
78      */
79     size_t _stop;
80
81   public:
82     /**
83      * Constructs a new {@link CommonToken} with the specified token type.
84      *
85      * @param type The token type.
86      */
87     CommonToken(size_t type);
88     CommonToken(std::pair<TokenSource*, CharStream*> source, size_t type, size_t channel, size_t start, size_t stop);
89
90     /**
91      * Constructs a new {@link CommonToken} with the specified token type and
92      * text.
93      *
94      * @param type The token type.
95      * @param text The text of the token.
96      */
97     CommonToken(size_t type, const std::string &text);
98
99     /**
100      * Constructs a new {@link CommonToken} as a copy of another {@link Token}.
101      *
102      * <p>
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>
109      *
110      * @param oldToken The token to copy.
111      */
112     CommonToken(Token *oldToken);
113
114     virtual size_t getType() const override;
115
116     /**
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.
120      *
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
123      * of the token.
124      */
125     virtual void setText(const std::string &text) override;
126     virtual std::string getText() const override;
127
128     virtual void setLine(size_t line) override;
129     virtual size_t getLine() const override;
130
131     virtual size_t getCharPositionInLine() const override;
132     virtual void setCharPositionInLine(size_t charPositionInLine) override;
133
134     virtual size_t getChannel() const override;
135     virtual void setChannel(size_t channel) override;
136
137     virtual void setType(size_t type) override;
138
139     virtual size_t getStartIndex() const override;
140     virtual void setStartIndex(size_t start);
141
142     virtual size_t getStopIndex() const override;
143     virtual void setStopIndex(size_t stop);
144
145     virtual size_t getTokenIndex() const override;
146     virtual void setTokenIndex(size_t index) override;
147
148     virtual TokenSource *getTokenSource() const override;
149     virtual CharStream *getInputStream() const override;
150
151     virtual std::string toString() const override;
152
153     virtual std::string toString(Recognizer *r) const;
154   private:
155     void InitializeInstanceFields();
156   };
157
158 } // namespace antlr4