Coverage Report

Created: 2026-03-31 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
8.69M
CommonToken::CommonToken(std::pair<TokenSource*, CharStream*> source, size_t type, size_t channel, size_t start, size_t stop) {
31
8.69M
  InitializeInstanceFields();
32
8.69M
  _source = source;
33
8.69M
  _type = type;
34
8.69M
  _channel = channel;
35
8.69M
  _start = start;
36
8.69M
  _stop = stop;
37
8.69M
  if (_source.first != nullptr) {
38
8.69M
    _line = static_cast<int>(source.first->getLine());
39
8.69M
    _charPositionInLine = source.first->getCharPositionInLine();
40
8.69M
  }
41
8.69M
}
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
120M
size_t CommonToken::getType() const {
71
120M
  return _type;
72
120M
}
73
74
8.69M
void CommonToken::setLine(size_t line) {
75
8.69M
  _line = line;
76
8.69M
}
77
78
7.74M
std::string CommonToken::getText() const {
79
7.74M
  if (!_text.empty()) {
80
0
    return _text;
81
0
  }
82
83
7.74M
  CharStream *input = getInputStream();
84
7.74M
  if (input == nullptr) {
85
0
    return "";
86
0
  }
87
7.74M
  size_t n = input->size();
88
7.74M
  if (_start < n && _stop < n) {
89
7.74M
    return input->getText(misc::Interval(_start, _stop));
90
7.74M
  } else {
91
3.15k
    return "<EOF>";
92
3.15k
  }
93
7.74M
}
94
95
3.85k
void CommonToken::setText(const std::string &text) {
96
3.85k
  _text = text;
97
3.85k
}
98
99
31.5k
size_t CommonToken::getLine() const {
100
31.5k
  return _line;
101
31.5k
}
102
103
31.5k
size_t CommonToken::getCharPositionInLine() const {
104
31.5k
  return _charPositionInLine;
105
31.5k
}
106
107
8.69M
void CommonToken::setCharPositionInLine(size_t charPositionInLine) {
108
8.69M
  _charPositionInLine = charPositionInLine;
109
8.69M
}
110
111
66.6M
size_t CommonToken::getChannel() const {
112
66.6M
  return _channel;
113
66.6M
}
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
5.71M
size_t CommonToken::getStartIndex() const {
124
5.71M
  return _start;
125
5.71M
}
126
127
0
void CommonToken::setStartIndex(size_t start) {
128
0
  _start = start;
129
0
}
130
131
5.71M
size_t CommonToken::getStopIndex() const {
132
5.71M
  return _stop;
133
5.71M
}
134
135
0
void CommonToken::setStopIndex(size_t stop) {
136
0
  _stop = stop;
137
0
}
138
139
9.13k
size_t CommonToken::getTokenIndex() const {
140
9.13k
  return _index;
141
9.13k
}
142
143
8.68M
void CommonToken::setTokenIndex(size_t index) {
144
8.68M
  _index = index;
145
8.68M
}
146
147
7.70k
antlr4::TokenSource *CommonToken::getTokenSource() const {
148
7.70k
  return _source.first;
149
7.70k
}
150
151
7.74M
antlr4::CharStream *CommonToken::getInputStream() const {
152
7.74M
  return _source.second;
153
7.74M
}
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
8.69M
void CommonToken::InitializeInstanceFields() {
185
8.69M
  _type = 0;
186
8.69M
  _line = 0;
187
8.69M
  _charPositionInLine = INVALID_INDEX;
188
8.69M
  _channel = DEFAULT_CHANNEL;
189
8.69M
  _index = INVALID_INDEX;
190
8.69M
  _start = 0;
191
8.69M
  _stop = 0;
192
8.69M
  _source = EMPTY_SOURCE;
193
8.69M
}