Coverage Report

Created: 2026-05-27 07:00

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
21.4k
Lexer::Lexer(CharStream *input) : Recognizer(), _input(input) {
28
21.4k
  InitializeInstanceFields();
29
21.4k
}
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.42M
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.42M
  ssize_t tokenStartMarker = _input->mark();
56
57
9.42M
  auto onExit = finally([this, tokenStartMarker]{
58
    // make sure we release marker after match or
59
    // unbuffered char stream will keep buffering
60
9.42M
    _input->release(tokenStartMarker);
61
9.42M
  });
62
63
9.42M
  while (true) {
64
17.7M
  outerContinue:
65
17.7M
    if (hitEOF) {
66
21.2k
      emitEOF();
67
21.2k
      return std::move(token);
68
21.2k
    }
69
70
17.7M
    token.reset();
71
17.7M
    channel = Token::DEFAULT_CHANNEL;
72
17.7M
    tokenStartCharIndex = _input->index();
73
17.7M
    tokenStartCharPositionInLine = getInterpreter<atn::LexerATNSimulator>()->getCharPositionInLine();
74
17.7M
    tokenStartLine = getInterpreter<atn::LexerATNSimulator>()->getLine();
75
17.7M
    _text = "";
76
17.7M
    do {
77
17.7M
      type = Token::INVALID_TYPE;
78
17.7M
      size_t ttype;
79
17.7M
      try {
80
17.7M
        ttype = getInterpreter<atn::LexerATNSimulator>()->match(_input, mode);
81
17.7M
      } catch (LexerNoViableAltException &e) {
82
8.32M
        notifyListeners(e); // report error
83
8.32M
        recover(e);
84
8.32M
        ttype = SKIP;
85
8.32M
      }
86
17.7M
      if (_input->LA(1) == EOF) {
87
21.3k
        hitEOF = true;
88
21.3k
      }
89
17.7M
      if (type == Token::INVALID_TYPE) {
90
17.7M
        type = ttype;
91
17.7M
      }
92
17.7M
      if (type == SKIP) {
93
8.32M
        goto outerContinue;
94
8.32M
      }
95
17.7M
    } while (type == MORE);
96
9.40M
    if (token == nullptr) {
97
9.40M
      emit();
98
9.40M
    }
99
9.40M
    return std::move(token);
100
17.7M
  }
101
9.42M
}
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
6.64k
TokenFactory<CommonToken>* Lexer::getTokenFactory() {
139
6.64k
  return _factory;
140
6.64k
}
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
6.64k
CharStream* Lexer::getInputStream() {
152
6.64k
  return _input;
153
6.64k
}
154
155
9.42M
void Lexer::emit(std::unique_ptr<Token> newToken) {
156
9.42M
  token = std::move(newToken);
157
9.42M
}
158
159
9.40M
Token* Lexer::emit() {
160
9.40M
  emit(_factory->create({ this, _input }, type, _text, channel,
161
9.40M
    tokenStartCharIndex, getCharIndex() - 1, tokenStartLine, tokenStartCharPositionInLine));
162
9.40M
  return token.get();
163
9.40M
}
164
165
21.2k
Token* Lexer::emitEOF() {
166
21.2k
  size_t cpos = getCharPositionInLine();
167
21.2k
  size_t line = getLine();
168
21.2k
  emit(_factory->create({ this, _input }, EOF, "", Token::DEFAULT_CHANNEL, _input->index(), _input->index() - 1, line, cpos));
169
21.2k
  return token.get();
170
21.2k
}
171
172
9.45M
size_t Lexer::getLine() const {
173
9.45M
  return getInterpreter<atn::LexerATNSimulator>()->getLine();
174
9.45M
}
175
176
9.45M
size_t Lexer::getCharPositionInLine() {
177
9.45M
  return getInterpreter<atn::LexerATNSimulator>()->getCharPositionInLine();
178
9.45M
}
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.40M
size_t Lexer::getCharIndex() {
189
9.40M
  return _input->index();
190
9.40M
}
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
24.2k
void Lexer::setChannel(size_t newChannel) {
220
24.2k
  channel = newChannel;
221
24.2k
}
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
8.32M
void Lexer::recover(const LexerNoViableAltException &/*e*/) {
238
8.32M
  if (_input->LA(1) != EOF) {
239
    // skip a char and try again
240
8.32M
    getInterpreter<atn::LexerATNSimulator>()->consume(_input);
241
8.32M
  }
242
8.32M
}
243
244
8.32M
void Lexer::notifyListeners(const LexerNoViableAltException & /*e*/) {
245
8.32M
  ++_syntaxErrors;
246
8.32M
  std::string text = _input->getText(misc::Interval(tokenStartCharIndex, _input->index()));
247
8.32M
  std::string msg = std::string("token recognition error at: '") + getErrorDisplay(text) + std::string("'");
248
249
8.32M
  ProxyErrorListener &listener = getErrorListenerDispatch();
250
8.32M
  listener.syntaxError(this, nullptr, tokenStartLine, tokenStartCharPositionInLine, msg, std::current_exception());
251
8.32M
}
252
253
8.32M
std::string Lexer::getErrorDisplay(const std::string &s) {
254
8.32M
  std::stringstream ss;
255
18.9M
  for (auto c : s) {
256
18.9M
    switch (c) {
257
1.42k
    case '\n':
258
1.42k
      ss << "\\n";
259
1.42k
      break;
260
2.95k
    case '\t':
261
2.95k
      ss << "\\t";
262
2.95k
      break;
263
932
    case '\r':
264
932
      ss << "\\r";
265
932
      break;
266
18.9M
    default:
267
18.9M
      ss << c;
268
18.9M
      break;
269
18.9M
    }
270
18.9M
  }
271
8.32M
  return ss.str();
272
8.32M
}
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
21.4k
void Lexer::InitializeInstanceFields() {
284
21.4k
  _syntaxErrors = 0;
285
21.4k
  token = nullptr;
286
21.4k
  _factory = CommonTokenFactory::DEFAULT.get();
287
  tokenStartCharIndex = INVALID_INDEX;
288
21.4k
  tokenStartLine = 0;
289
21.4k
  tokenStartCharPositionInLine = 0;
290
21.4k
  hitEOF = false;
291
21.4k
  channel = 0;
292
21.4k
  type = 0;
293
21.4k
  mode = Lexer::DEFAULT_MODE;
294
21.4k
}