/proc/self/cwd/external/antlr4-cpp-runtime~/runtime/src/Recognizer.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 "ConsoleErrorListener.h" |
7 | | #include "RecognitionException.h" |
8 | | #include "support/CPPUtils.h" |
9 | | #include "Token.h" |
10 | | #include "atn/ATN.h" |
11 | | #include "atn/ATNSimulator.h" |
12 | | #include "support/CPPUtils.h" |
13 | | #include "support/StringUtils.h" |
14 | | |
15 | | #include "Vocabulary.h" |
16 | | |
17 | | #include "Recognizer.h" |
18 | | |
19 | | using namespace antlr4; |
20 | | using namespace antlr4::atn; |
21 | | using namespace antlr4::internal; |
22 | | |
23 | | std::map<const dfa::Vocabulary*, std::map<std::string_view, size_t>> Recognizer::_tokenTypeMapCache; |
24 | | std::map<std::vector<std::string>, std::map<std::string, size_t>> Recognizer::_ruleIndexMapCache; |
25 | | |
26 | 8.40k | Recognizer::Recognizer() { |
27 | 8.40k | InitializeInstanceFields(); |
28 | 8.40k | _proxListener.addErrorListener(&ConsoleErrorListener::INSTANCE); |
29 | 8.40k | } |
30 | | |
31 | 8.40k | Recognizer::~Recognizer() { |
32 | 8.40k | } |
33 | | |
34 | 0 | std::map<std::string_view, size_t> Recognizer::getTokenTypeMap() { |
35 | 0 | const dfa::Vocabulary& vocabulary = getVocabulary(); |
36 | |
|
37 | 0 | UniqueLock<Mutex> lck(_mutex); |
38 | 0 | std::map<std::string_view, size_t> result; |
39 | 0 | auto iterator = _tokenTypeMapCache.find(&vocabulary); |
40 | 0 | if (iterator != _tokenTypeMapCache.end()) { |
41 | 0 | result = iterator->second; |
42 | 0 | } else { |
43 | 0 | for (size_t i = 0; i <= getATN().maxTokenType; ++i) { |
44 | 0 | std::string_view literalName = vocabulary.getLiteralName(i); |
45 | 0 | if (!literalName.empty()) { |
46 | 0 | result[literalName] = i; |
47 | 0 | } |
48 | |
|
49 | 0 | std::string_view symbolicName = vocabulary.getSymbolicName(i); |
50 | 0 | if (!symbolicName.empty()) { |
51 | 0 | result[symbolicName] = i; |
52 | 0 | } |
53 | 0 | } |
54 | 0 | result["EOF"] = EOF; |
55 | 0 | _tokenTypeMapCache[&vocabulary] = result; |
56 | 0 | } |
57 | |
|
58 | 0 | return result; |
59 | 0 | } |
60 | | |
61 | 0 | std::map<std::string, size_t> Recognizer::getRuleIndexMap() { |
62 | 0 | const std::vector<std::string>& ruleNames = getRuleNames(); |
63 | 0 | if (ruleNames.empty()) { |
64 | 0 | throw "The current recognizer does not provide a list of rule names."; |
65 | 0 | } |
66 | | |
67 | 0 | UniqueLock<Mutex> lck(_mutex); |
68 | 0 | std::map<std::string, size_t> result; |
69 | 0 | auto iterator = _ruleIndexMapCache.find(ruleNames); |
70 | 0 | if (iterator != _ruleIndexMapCache.end()) { |
71 | 0 | result = iterator->second; |
72 | 0 | } else { |
73 | 0 | result = antlrcpp::toMap(ruleNames); |
74 | 0 | _ruleIndexMapCache[ruleNames] = result; |
75 | 0 | } |
76 | 0 | return result; |
77 | 0 | } |
78 | | |
79 | 0 | size_t Recognizer::getTokenType(std::string_view tokenName) { |
80 | 0 | const std::map<std::string_view, size_t> &map = getTokenTypeMap(); |
81 | 0 | auto iterator = map.find(tokenName); |
82 | 0 | if (iterator == map.end()) |
83 | 0 | return Token::INVALID_TYPE; |
84 | | |
85 | 0 | return iterator->second; |
86 | 0 | } |
87 | | |
88 | 0 | void Recognizer::setInterpreter(atn::ATNSimulator *interpreter) { |
89 | | // Usually the interpreter is set by the descendant (lexer or parser (simulator), but can also be exchanged |
90 | | // by the profiling ATN simulator. |
91 | 0 | delete _interpreter; |
92 | 0 | _interpreter = interpreter; |
93 | 0 | } |
94 | | |
95 | 0 | std::string Recognizer::getErrorHeader(RecognitionException *e) { |
96 | | // We're having issues with cross header dependencies, these two classes will need to be |
97 | | // rewritten to remove that. |
98 | 0 | size_t line = e->getOffendingToken()->getLine(); |
99 | 0 | size_t charPositionInLine = e->getOffendingToken()->getCharPositionInLine(); |
100 | 0 | return std::string("line ") + std::to_string(line) + ":" + std::to_string(charPositionInLine); |
101 | |
|
102 | 0 | } |
103 | | |
104 | 0 | std::string Recognizer::getTokenErrorDisplay(Token *t) { |
105 | 0 | if (t == nullptr) { |
106 | 0 | return "<no Token>"; |
107 | 0 | } |
108 | 0 | std::string s = t->getText(); |
109 | 0 | if (s == "") { |
110 | 0 | if (t->getType() == EOF) { |
111 | 0 | s = "<EOF>"; |
112 | 0 | } else { |
113 | 0 | s = std::string("<") + std::to_string(t->getType()) + std::string(">"); |
114 | 0 | } |
115 | 0 | } |
116 | |
|
117 | 0 | std::string result; |
118 | 0 | result.reserve(s.size() + 2); |
119 | 0 | result.push_back('\''); |
120 | 0 | antlrcpp::escapeWhitespace(result, s); |
121 | 0 | result.push_back('\''); |
122 | 0 | result.shrink_to_fit(); |
123 | 0 | return result; |
124 | 0 | } |
125 | | |
126 | 8.40k | void Recognizer::addErrorListener(ANTLRErrorListener *listener) { |
127 | 8.40k | _proxListener.addErrorListener(listener); |
128 | 8.40k | } |
129 | | |
130 | 0 | void Recognizer::removeErrorListener(ANTLRErrorListener *listener) { |
131 | 0 | _proxListener.removeErrorListener(listener); |
132 | 0 | } |
133 | | |
134 | 8.40k | void Recognizer::removeErrorListeners() { |
135 | 8.40k | _proxListener.removeErrorListeners(); |
136 | 8.40k | } |
137 | | |
138 | 2.56M | ProxyErrorListener& Recognizer::getErrorListenerDispatch() { |
139 | 2.56M | return _proxListener; |
140 | 2.56M | } |
141 | | |
142 | 0 | bool Recognizer::sempred(RuleContext * /*localctx*/, size_t /*ruleIndex*/, size_t /*actionIndex*/) { |
143 | 0 | return true; |
144 | 0 | } |
145 | | |
146 | 0 | bool Recognizer::precpred(RuleContext * /*localctx*/, int /*precedence*/) { |
147 | 0 | return true; |
148 | 0 | } |
149 | | |
150 | 0 | void Recognizer::action(RuleContext * /*localctx*/, size_t /*ruleIndex*/, size_t /*actionIndex*/) { |
151 | 0 | } |
152 | | |
153 | 8.40k | void Recognizer::InitializeInstanceFields() { |
154 | 8.40k | _stateNumber = ATNState::INVALID_STATE_NUMBER; |
155 | 8.40k | _interpreter = nullptr; |
156 | 8.40k | } |
157 | | |