]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/CommonToken.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / CommonToken.cpp
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 #include "TokenSource.h"
7 #include "CharStream.h"
8 #include "Recognizer.h"
9 #include "Vocabulary.h"
10
11 #include "misc/Interval.h"
12
13 #include "support/StringUtils.h"
14 #include "support/CPPUtils.h"
15
16 #include "CommonToken.h"
17
18 using namespace antlr4;
19 using namespace antlr4::misc;
20
21 using namespace antlrcpp;
22
23 const std::pair<TokenSource*, CharStream*> CommonToken::EMPTY_SOURCE;
24
25 CommonToken::CommonToken(size_t type) {
26   InitializeInstanceFields();
27   _type = type;
28 }
29
30 CommonToken::CommonToken(std::pair<TokenSource*, CharStream*> source, size_t type, size_t channel, size_t start, size_t stop) {
31   InitializeInstanceFields();
32   _source = source;
33   _type = type;
34   _channel = channel;
35   _start = start;
36   _stop = stop;
37   if (_source.first != nullptr) {
38     _line = static_cast<int>(source.first->getLine());
39     _charPositionInLine = source.first->getCharPositionInLine();
40   }
41 }
42
43 CommonToken::CommonToken(size_t type, const std::string &text) {
44   InitializeInstanceFields();
45   _type = type;
46   _channel = DEFAULT_CHANNEL;
47   _text = text;
48   _source = EMPTY_SOURCE;
49 }
50
51 CommonToken::CommonToken(Token *oldToken) {
52   InitializeInstanceFields();
53   _type = oldToken->getType();
54   _line = oldToken->getLine();
55   _index = oldToken->getTokenIndex();
56   _charPositionInLine = oldToken->getCharPositionInLine();
57   _channel = oldToken->getChannel();
58   _start = oldToken->getStartIndex();
59   _stop = oldToken->getStopIndex();
60
61   if (is<CommonToken *>(oldToken)) {
62     _text = (static_cast<CommonToken *>(oldToken))->_text;
63     _source = (static_cast<CommonToken *>(oldToken))->_source;
64   } else {
65     _text = oldToken->getText();
66     _source = { oldToken->getTokenSource(), oldToken->getInputStream() };
67   }
68 }
69
70 size_t CommonToken::getType() const {
71   return _type;
72 }
73
74 void CommonToken::setLine(size_t line) {
75   _line = line;
76 }
77
78 std::string CommonToken::getText() const {
79   if (!_text.empty()) {
80     return _text;
81   }
82
83   CharStream *input = getInputStream();
84   if (input == nullptr) {
85     return "";
86   }
87   size_t n = input->size();
88   if (_start < n && _stop < n) {
89     return input->getText(misc::Interval(_start, _stop));
90   } else {
91     return "<EOF>";
92   }
93 }
94
95 void CommonToken::setText(const std::string &text) {
96   _text = text;
97 }
98
99 size_t CommonToken::getLine() const {
100   return _line;
101 }
102
103 size_t CommonToken::getCharPositionInLine() const {
104   return _charPositionInLine;
105 }
106
107 void CommonToken::setCharPositionInLine(size_t charPositionInLine) {
108   _charPositionInLine = charPositionInLine;
109 }
110
111 size_t CommonToken::getChannel() const {
112   return _channel;
113 }
114
115 void CommonToken::setChannel(size_t channel) {
116   _channel = channel;
117 }
118
119 void CommonToken::setType(size_t type) {
120   _type = type;
121 }
122
123 size_t CommonToken::getStartIndex() const {
124   return _start;
125 }
126
127 void CommonToken::setStartIndex(size_t start) {
128   _start = start;
129 }
130
131 size_t CommonToken::getStopIndex() const {
132   return _stop;
133 }
134
135 void CommonToken::setStopIndex(size_t stop) {
136   _stop = stop;
137 }
138
139 size_t CommonToken::getTokenIndex() const {
140   return _index;
141 }
142
143 void CommonToken::setTokenIndex(size_t index) {
144   _index = index;
145 }
146
147 antlr4::TokenSource *CommonToken::getTokenSource() const {
148   return _source.first;
149 }
150
151 antlr4::CharStream *CommonToken::getInputStream() const {
152   return _source.second;
153 }
154
155 std::string CommonToken::toString() const {
156   return toString(nullptr);
157 }
158
159 std::string CommonToken::toString(Recognizer *r) const {
160   std::stringstream ss;
161
162   std::string channelStr;
163   if (_channel > 0) {
164     channelStr = ",channel=" + std::to_string(_channel);
165   }
166   std::string txt = getText();
167   if (!txt.empty()) {
168     antlrcpp::replaceAll(txt, "\n", "\\n");
169     antlrcpp::replaceAll(txt, "\r", "\\r");
170     antlrcpp::replaceAll(txt, "\t", "\\t");
171   } else {
172     txt = "<no text>";
173   }
174
175   std::string typeString = std::to_string(symbolToNumeric(_type));
176   if (r != nullptr)
177     typeString = r->getVocabulary().getDisplayName(_type);
178
179   ss << "[@" << symbolToNumeric(getTokenIndex()) << "," << symbolToNumeric(_start) << ":" << symbolToNumeric(_stop)
180     << "='" << txt << "',<" << typeString << ">" << channelStr << "," << _line << ":"
181     << getCharPositionInLine() << "]";
182
183   return ss.str();
184 }
185
186 void CommonToken::InitializeInstanceFields() {
187   _type = 0;
188   _line = 0;
189   _charPositionInLine = INVALID_INDEX;
190   _channel = DEFAULT_CHANNEL;
191   _index = INVALID_INDEX;
192   _start = 0;
193   _stop = 0;
194   _source = EMPTY_SOURCE;
195 }