Coverage Report

Created: 2025-12-29 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/antlr4-cpp-runtime~/runtime/src/Lexer.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 "atn/LexerATNSimulator.h"
7
#include "Exceptions.h"
8
#include "misc/Interval.h"
9
#include "CommonTokenFactory.h"
10
#include "LexerNoViableAltException.h"
11
#include "ANTLRErrorListener.h"
12
#include "support/CPPUtils.h"
13
#include "CommonToken.h"
14
15
#include "Lexer.h"
16
17
#define DEBUG_LEXER 0
18
19
using namespace antlrcpp;
20
using namespace antlr4;
21
22
0
Lexer::Lexer() : Recognizer() {
23
0
  InitializeInstanceFields();
24
0
  _input = nullptr;
25
0
}
26
27
7.55k
Lexer::Lexer(CharStream *input) : Recognizer(), _input(input) {
28
7.55k
  InitializeInstanceFields();
29
7.55k
}
30
31
0
void Lexer::reset() {
32
  // wack Lexer state variables
33
0
  _input->seek(0); // rewind the input
34
35
0
  _syntaxErrors = 0;
36
0
  token.reset();
37
0
  type = Token::INVALID_TYPE;
38
0
  channel = Token::DEFAULT_CHANNEL;
39
0
  tokenStartCharIndex = INVALID_INDEX;
40
0
  tokenStartCharPositionInLine = 0;
41
0
  tokenStartLine = 0;
42
0
  type = 0;
43
0
  _text = "";
44
45
0
  hitEOF = false;
46
0
  mode = Lexer::DEFAULT_MODE;
47
0
  modeStack.clear();
48
49
0
  getInterpreter<atn::LexerATNSimulator>()->reset();
50
0
}
51
52
9.46M
std::unique_ptr<Token> Lexer::nextToken() {
53
  // Mark start location in char stream so unbuffered streams are
54
  // guaranteed at least have text of current token
55
9.46M
  ssize_t tokenStartMarker = _input->mark();
56
57
9.46M
  auto onExit = finally([this, tokenStartMarker]{
58
    // make sure we release marker after match or
59
    // unbuffered char stream will keep buffering
60
9.46M
    _input->release(tokenStartMarker);
61
9.46M
  });
62
63
9.46M
  while (true) {
64
13.5M
  outerContinue:
65
13.5M
    if (hitEOF) {
66
7.39k
      emitEOF();
67
7.39k
      return std::move(token);
68
7.39k
    }
69
70
13.5M
    token.reset();
71
13.5M
    channel = Token::DEFAULT_CHANNEL;
72
13.5M
    tokenStartCharIndex = _input->index();
73
13.5M
    tokenStartCharPositionInLine = getInterpreter<atn::LexerATNSimulator>()->getCharPositionInLine();
74
13.5M
    tokenStartLine = getInterpreter<atn::LexerATNSimulator>()->getLine();
75
13.5M
    _text = "";
76
13.5M
    do {
77
13.5M
      type = Token::INVALID_TYPE;
78
13.5M
      size_t ttype;
79
13.5M
      try {
80
13.5M
        ttype = getInterpreter<atn::LexerATNSimulator>()->match(_input, mode);
81
13.5M
      } catch (LexerNoViableAltException &e) {
82
4.12M
        notifyListeners(e); // report error
83
4.12M
        recover(e);
84
4.12M
        ttype = SKIP;
85
4.12M
      }
86
13.5M
      if (_input->LA(1) == EOF) {
87
7.40k
        hitEOF = true;
88
7.40k
      }
89
13.5M
      if (type == Token::INVALID_TYPE) {
90
13.5M
        type = ttype;
91
13.5M
      }
92
13.5M
      if (type == SKIP) {
93
4.12M
        goto outerContinue;
94
4.12M
      }
95
13.5M
    } while (type == MORE);
96
9.45M
    if (token == nullptr) {
97
9.45M
      emit();
98
9.45M
    }
99
9.45M
    return std::move(token);
100
13.5M
  }
101
9.46M
}
102
103
0
void Lexer::skip() {
104
0
  type = SKIP;
105
0
}
106
107
0
void Lexer::more() {
108
0
  type = MORE;
109
0
}
110
111
0
void Lexer::setMode(size_t m) {
112
0
  mode = m;
113
0
}
114
115
0
void Lexer::pushMode(size_t m) {
116
#if DEBUG_LEXER == 1
117
    std::cout << "pushMode " << m << std::endl;
118
#endif
119
120
0
  modeStack.push_back(mode);
121
0
  setMode(m);
122
0
}
123
124
0
size_t Lexer::popMode() {
125
0
  if (modeStack.empty()) {
126
0
    throw EmptyStackException();
127
0
  }
128
#if DEBUG_LEXER == 1
129
    std::cout << std::string("popMode back to ") << modeStack.back() << std::endl;
130
#endif
131
132
0
  setMode(modeStack.back());
133
0
  modeStack.pop_back();
134
0
  return mode;
135
0
}
136
137
138
4.22k
TokenFactory<CommonToken>* Lexer::getTokenFactory() {
139
4.22k
  return _factory;
140
4.22k
}
141
142
0
void Lexer::setInputStream(IntStream *input) {
143
0
  reset();
144
0
  _input = dynamic_cast<CharStream*>(input);
145
0
}
146
147
0
std::string Lexer::getSourceName() {
148
0
  return _input->getSourceName();
149
0
}
150
151
4.22k
CharStream* Lexer::getInputStream() {
152
4.22k
  return _input;
153
4.22k
}
154
155
9.46M
void Lexer::emit(std::unique_ptr<Token> newToken) {
156
9.46M
  token = std::move(newToken);
157
9.46M
}
158
159
9.45M
Token* Lexer::emit() {
160
9.45M
  emit(_factory->create({ this, _input }, type, _text, channel,
161
9.45M
    tokenStartCharIndex, getCharIndex() - 1, tokenStartLine, tokenStartCharPositionInLine));
162
9.45M
  return token.get();
163
9.45M
}
164
165
7.39k
Token* Lexer::emitEOF() {
166
7.39k
  size_t cpos = getCharPositionInLine();
167
7.39k
  size_t line = getLine();
168
7.39k
  emit(_factory->create({ this, _input }, EOF, "", Token::DEFAULT_CHANNEL, _input->index(), _input->index() - 1, line, cpos));
169
7.39k
  return token.get();
170
7.39k
}
171
172
9.47M
size_t Lexer::getLine() const {
173
9.47M
  return getInterpreter<atn::LexerATNSimulator>()->getLine();
174
9.47M
}
175
176
9.47M
size_t Lexer::getCharPositionInLine() {
177
9.47M
  return getInterpreter<atn::LexerATNSimulator>()->getCharPositionInLine();
178
9.47M
}
179
180
0
void Lexer::setLine(size_t line) {
181
0
  getInterpreter<atn::LexerATNSimulator>()->setLine(line);
182
0
}
183
184
0
void Lexer::setCharPositionInLine(size_t charPositionInLine) {
185
0
  getInterpreter<atn::LexerATNSimulator>()->setCharPositionInLine(charPositionInLine);
186
0
}
187
188
9.45M
size_t Lexer::getCharIndex() {
189
9.45M
  return _input->index();
190
9.45M
}
191
192
0
std::string Lexer::getText() {
193
0
  if (!_text.empty()) {
194
0
    return _text;
195
0
  }
196
0
  return getInterpreter<atn::LexerATNSimulator>()->getText(_input);
197
0
}
198
199
0
void Lexer::setText(const std::string &text) {
200
0
  _text = text;
201
0
}
202
203
0
std::unique_ptr<Token> Lexer::getToken() {
204
0
  return std::move(token);
205
0
}
206
207
0
void Lexer::setToken(std::unique_ptr<Token> newToken) {
208
0
  token = std::move(newToken);
209
0
}
210
211
0
void Lexer::setType(size_t ttype) {
212
0
  type = ttype;
213
0
}
214
215
0
size_t Lexer::getType() {
216
0
  return type;
217
0
}
218
219
14.5k
void Lexer::setChannel(size_t newChannel) {
220
14.5k
  channel = newChannel;
221
14.5k
}
222
223
0
size_t Lexer::getChannel() {
224
0
  return channel;
225
0
}
226
227
0
std::vector<std::unique_ptr<Token>> Lexer::getAllTokens() {
228
0
  std::vector<std::unique_ptr<Token>> tokens;
229
0
  std::unique_ptr<Token> t = nextToken();
230
0
  while (t->getType() != EOF) {
231
0
    tokens.push_back(std::move(t));
232
0
    t = nextToken();
233
0
  }
234
0
  return tokens;
235
0
}
236
237
4.12M
void Lexer::recover(const LexerNoViableAltException &/*e*/) {
238
4.12M
  if (_input->LA(1) != EOF) {
239
    // skip a char and try again
240
4.12M
    getInterpreter<atn::LexerATNSimulator>()->consume(_input);
241
4.12M
  }
242
4.12M
}
243
244
4.12M
void Lexer::notifyListeners(const LexerNoViableAltException & /*e*/) {
245
4.12M
  ++_syntaxErrors;
246
4.12M
  std::string text = _input->getText(misc::Interval(tokenStartCharIndex, _input->index()));
247
4.12M
  std::string msg = std::string("token recognition error at: '") + getErrorDisplay(text) + std::string("'");
248
249
4.12M
  ProxyErrorListener &listener = getErrorListenerDispatch();
250
4.12M
  listener.syntaxError(this, nullptr, tokenStartLine, tokenStartCharPositionInLine, msg, std::current_exception());
251
4.12M
}
252
253
4.12M
std::string Lexer::getErrorDisplay(const std::string &s) {
254
4.12M
  std::stringstream ss;
255
10.1M
  for (auto c : s) {
256
10.1M
    switch (c) {
257
1.43k
    case '\n':
258
1.43k
      ss << "\\n";
259
1.43k
      break;
260
1.23k
    case '\t':
261
1.23k
      ss << "\\t";
262
1.23k
      break;
263
634
    case '\r':
264
634
      ss << "\\r";
265
634
      break;
266
10.1M
    default:
267
10.1M
      ss << c;
268
10.1M
      break;
269
10.1M
    }
270
10.1M
  }
271
4.12M
  return ss.str();
272
4.12M
}
273
274
0
void Lexer::recover(RecognitionException * /*re*/) {
275
  // TODO: Do we lose character or line position information?
276
0
  _input->consume();
277
0
}
278
279
0
size_t Lexer::getNumberOfSyntaxErrors() {
280
0
  return _syntaxErrors;
281
0
}
282
283
7.55k
void Lexer::InitializeInstanceFields() {
284
7.55k
  _syntaxErrors = 0;
285
7.55k
  token = nullptr;
286
7.55k
  _factory = CommonTokenFactory::DEFAULT.get();
287
  tokenStartCharIndex = INVALID_INDEX;
288
7.55k
  tokenStartLine = 0;
289
7.55k
  tokenStartCharPositionInLine = 0;
290
7.55k
  hitEOF = false;
291
7.55k
  channel = 0;
292
7.55k
  type = 0;
293
7.55k
  mode = Lexer::DEFAULT_MODE;
294
7.55k
}