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.
6 #include "TokenSource.h"
7 #include "CharStream.h"
8 #include "Recognizer.h"
9 #include "Vocabulary.h"
11 #include "misc/Interval.h"
13 #include "support/StringUtils.h"
14 #include "support/CPPUtils.h"
16 #include "CommonToken.h"
18 using namespace antlr4;
19 using namespace antlr4::misc;
21 using namespace antlrcpp;
23 const std::pair<TokenSource*, CharStream*> CommonToken::EMPTY_SOURCE;
25 CommonToken::CommonToken(size_t type) {
26 InitializeInstanceFields();
30 CommonToken::CommonToken(std::pair<TokenSource*, CharStream*> source, size_t type, size_t channel, size_t start, size_t stop) {
31 InitializeInstanceFields();
37 if (_source.first != nullptr) {
38 _line = static_cast<int>(source.first->getLine());
39 _charPositionInLine = source.first->getCharPositionInLine();
43 CommonToken::CommonToken(size_t type, const std::string &text) {
44 InitializeInstanceFields();
46 _channel = DEFAULT_CHANNEL;
48 _source = EMPTY_SOURCE;
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();
61 if (is<CommonToken *>(oldToken)) {
62 _text = (static_cast<CommonToken *>(oldToken))->_text;
63 _source = (static_cast<CommonToken *>(oldToken))->_source;
65 _text = oldToken->getText();
66 _source = { oldToken->getTokenSource(), oldToken->getInputStream() };
70 size_t CommonToken::getType() const {
74 void CommonToken::setLine(size_t line) {
78 std::string CommonToken::getText() const {
83 CharStream *input = getInputStream();
84 if (input == nullptr) {
87 size_t n = input->size();
88 if (_start < n && _stop < n) {
89 return input->getText(misc::Interval(_start, _stop));
95 void CommonToken::setText(const std::string &text) {
99 size_t CommonToken::getLine() const {
103 size_t CommonToken::getCharPositionInLine() const {
104 return _charPositionInLine;
107 void CommonToken::setCharPositionInLine(size_t charPositionInLine) {
108 _charPositionInLine = charPositionInLine;
111 size_t CommonToken::getChannel() const {
115 void CommonToken::setChannel(size_t channel) {
119 void CommonToken::setType(size_t type) {
123 size_t CommonToken::getStartIndex() const {
127 void CommonToken::setStartIndex(size_t start) {
131 size_t CommonToken::getStopIndex() const {
135 void CommonToken::setStopIndex(size_t stop) {
139 size_t CommonToken::getTokenIndex() const {
143 void CommonToken::setTokenIndex(size_t index) {
147 antlr4::TokenSource *CommonToken::getTokenSource() const {
148 return _source.first;
151 antlr4::CharStream *CommonToken::getInputStream() const {
152 return _source.second;
155 std::string CommonToken::toString() const {
156 return toString(nullptr);
159 std::string CommonToken::toString(Recognizer *r) const {
160 std::stringstream ss;
162 std::string channelStr;
164 channelStr = ",channel=" + std::to_string(_channel);
166 std::string txt = getText();
168 antlrcpp::replaceAll(txt, "\n", "\\n");
169 antlrcpp::replaceAll(txt, "\r", "\\r");
170 antlrcpp::replaceAll(txt, "\t", "\\t");
175 std::string typeString = std::to_string(symbolToNumeric(_type));
177 typeString = r->getVocabulary().getDisplayName(_type);
179 ss << "[@" << symbolToNumeric(getTokenIndex()) << "," << symbolToNumeric(_start) << ":" << symbolToNumeric(_stop)
180 << "='" << txt << "',<" << typeString << ">" << channelStr << "," << _line << ":"
181 << getCharPositionInLine() << "]";
186 void CommonToken::InitializeInstanceFields() {
189 _charPositionInLine = INVALID_INDEX;
190 _channel = DEFAULT_CHANNEL;
191 _index = INVALID_INDEX;
194 _source = EMPTY_SOURCE;