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