]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/RecognitionException.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / RecognitionException.cpp
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/ATN.h"
7 #include "Recognizer.h"
8 #include "support/StringUtils.h"
9 #include "ParserRuleContext.h"
10 #include "misc/IntervalSet.h"
11
12 #include "RecognitionException.h"
13
14 using namespace antlr4;
15
16 RecognitionException::RecognitionException(Recognizer *recognizer, IntStream *input, ParserRuleContext *ctx,
17                                            Token *offendingToken)
18   : RecognitionException("", recognizer, input, ctx, offendingToken) {
19 }
20
21 RecognitionException::RecognitionException(const std::string &message, Recognizer *recognizer, IntStream *input,
22                                            ParserRuleContext *ctx, Token *offendingToken)
23   : RuntimeException(message), _recognizer(recognizer), _input(input), _ctx(ctx), _offendingToken(offendingToken) {
24   InitializeInstanceFields();
25   if (recognizer != nullptr) {
26     _offendingState = recognizer->getState();
27   }
28 }
29
30 RecognitionException::~RecognitionException() {
31 }
32
33 size_t RecognitionException::getOffendingState() const {
34   return _offendingState;
35 }
36
37 void RecognitionException::setOffendingState(size_t offendingState) {
38   _offendingState = offendingState;
39 }
40
41 misc::IntervalSet RecognitionException::getExpectedTokens() const {
42   if (_recognizer) {
43     return _recognizer->getATN().getExpectedTokens(_offendingState, _ctx);
44   }
45   return misc::IntervalSet::EMPTY_SET;
46 }
47
48 RuleContext* RecognitionException::getCtx() const {
49   return _ctx;
50 }
51
52 IntStream* RecognitionException::getInputStream() const {
53   return _input;
54 }
55
56 Token* RecognitionException::getOffendingToken() const {
57   return _offendingToken;
58 }
59
60 Recognizer* RecognitionException::getRecognizer() const {
61   return _recognizer;
62 }
63
64 void RecognitionException::InitializeInstanceFields() {
65   _offendingState = INVALID_INDEX;
66 }