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.
7 #include "atn/RuleStartState.h"
8 #include "InterpreterRuleContext.h"
9 #include "atn/ParserATNSimulator.h"
10 #include "ANTLRErrorStrategy.h"
11 #include "atn/LoopEndState.h"
12 #include "FailedPredicateException.h"
13 #include "atn/StarLoopEntryState.h"
14 #include "atn/AtomTransition.h"
15 #include "atn/RuleTransition.h"
16 #include "atn/PredicateTransition.h"
17 #include "atn/PrecedencePredicateTransition.h"
18 #include "atn/ActionTransition.h"
20 #include "atn/RuleStopState.h"
23 #include "Vocabulary.h"
24 #include "InputMismatchException.h"
25 #include "CommonToken.h"
26 #include "tree/ErrorNode.h"
28 #include "support/CPPUtils.h"
30 #include "ParserInterpreter.h"
32 using namespace antlr4;
33 using namespace antlr4::atn;
34 using namespace antlr4::misc;
36 using namespace antlrcpp;
38 ParserInterpreter::ParserInterpreter(const std::string &grammarFileName, const std::vector<std::string>& tokenNames,
39 const std::vector<std::string>& ruleNames, const atn::ATN &atn, TokenStream *input)
40 : ParserInterpreter(grammarFileName, dfa::Vocabulary::fromTokenNames(tokenNames), ruleNames, atn, input) {
43 ParserInterpreter::ParserInterpreter(const std::string &grammarFileName, const dfa::Vocabulary &vocabulary,
44 const std::vector<std::string> &ruleNames, const atn::ATN &atn, TokenStream *input)
45 : Parser(input), _grammarFileName(grammarFileName), _atn(atn), _ruleNames(ruleNames), _vocabulary(vocabulary) {
47 for (size_t i = 0; i < atn.maxTokenType; ++i) {
48 _tokenNames.push_back(vocabulary.getDisplayName(i));
52 for (size_t i = 0; i < atn.getNumberOfDecisions(); ++i) {
53 atn::DecisionState *decisionState = atn.getDecisionState(i);
54 _decisionToDFA.push_back(dfa::DFA(decisionState, i));
57 // get atn simulator that knows how to do predictions
58 _interpreter = new atn::ParserATNSimulator(this, atn, _decisionToDFA, _sharedContextCache); /* mem-check: deleted in d-tor */
61 ParserInterpreter::~ParserInterpreter() {
65 void ParserInterpreter::reset() {
67 _overrideDecisionReached = false;
68 _overrideDecisionRoot = nullptr;
71 const atn::ATN& ParserInterpreter::getATN() const {
75 const std::vector<std::string>& ParserInterpreter::getTokenNames() const {
79 const dfa::Vocabulary& ParserInterpreter::getVocabulary() const {
83 const std::vector<std::string>& ParserInterpreter::getRuleNames() const {
87 std::string ParserInterpreter::getGrammarFileName() const {
88 return _grammarFileName;
91 ParserRuleContext* ParserInterpreter::parse(size_t startRuleIndex) {
92 atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[startRuleIndex];
94 _rootContext = createInterpreterRuleContext(nullptr, atn::ATNState::INVALID_STATE_NUMBER, startRuleIndex);
96 if (startRuleStartState->isLeftRecursiveRule) {
97 enterRecursionRule(_rootContext, startRuleStartState->stateNumber, startRuleIndex, 0);
99 enterRule(_rootContext, startRuleStartState->stateNumber, startRuleIndex);
103 atn::ATNState *p = getATNState();
104 switch (p->getStateType()) {
105 case atn::ATNState::RULE_STOP :
106 // pop; return from rule
107 if (_ctx->isEmpty()) {
108 if (startRuleStartState->isLeftRecursiveRule) {
109 ParserRuleContext *result = _ctx;
110 auto parentContext = _parentContextStack.top();
111 _parentContextStack.pop();
112 unrollRecursionContexts(parentContext.first);
120 visitRuleStopState(p);
127 catch (RecognitionException &e) {
128 setState(_atn.ruleToStopState[p->ruleIndex]->stateNumber);
129 getErrorHandler()->reportError(this, e);
130 getContext()->exception = std::current_exception();
139 void ParserInterpreter::enterRecursionRule(ParserRuleContext *localctx, size_t state, size_t ruleIndex, int precedence) {
140 _parentContextStack.push({ _ctx, localctx->invokingState });
141 Parser::enterRecursionRule(localctx, state, ruleIndex, precedence);
144 void ParserInterpreter::addDecisionOverride(int decision, int tokenIndex, int forcedAlt) {
145 _overrideDecision = decision;
146 _overrideDecisionInputIndex = tokenIndex;
147 _overrideDecisionAlt = forcedAlt;
150 Ref<InterpreterRuleContext> ParserInterpreter::getOverrideDecisionRoot() const {
151 return _overrideDecisionRoot;
154 InterpreterRuleContext* ParserInterpreter::getRootContext() {
158 atn::ATNState* ParserInterpreter::getATNState() {
159 return _atn.states[getState()];
162 void ParserInterpreter::visitState(atn::ATNState *p) {
163 size_t predictedAlt = 1;
164 if (is<DecisionState *>(p)) {
165 predictedAlt = visitDecisionState(dynamic_cast<DecisionState *>(p));
168 atn::Transition *transition = p->transitions[predictedAlt - 1];
169 switch (transition->getSerializationType()) {
170 case atn::Transition::EPSILON:
171 if (p->getStateType() == ATNState::STAR_LOOP_ENTRY &&
172 (dynamic_cast<StarLoopEntryState *>(p))->isPrecedenceDecision &&
173 !is<LoopEndState *>(transition->target)) {
174 // We are at the start of a left recursive rule's (...)* loop
175 // and we're not taking the exit branch of loop.
176 InterpreterRuleContext *localctx = createInterpreterRuleContext(_parentContextStack.top().first,
177 _parentContextStack.top().second, static_cast<int>(_ctx->getRuleIndex()));
178 pushNewRecursionContext(localctx, _atn.ruleToStartState[p->ruleIndex]->stateNumber, static_cast<int>(_ctx->getRuleIndex()));
182 case atn::Transition::ATOM:
183 match(static_cast<int>(static_cast<atn::AtomTransition*>(transition)->_label));
186 case atn::Transition::RANGE:
187 case atn::Transition::SET:
188 case atn::Transition::NOT_SET:
189 if (!transition->matches(static_cast<int>(_input->LA(1)), Token::MIN_USER_TOKEN_TYPE, Lexer::MAX_CHAR_VALUE)) {
195 case atn::Transition::WILDCARD:
199 case atn::Transition::RULE:
201 atn::RuleStartState *ruleStartState = static_cast<atn::RuleStartState*>(transition->target);
202 size_t ruleIndex = ruleStartState->ruleIndex;
203 InterpreterRuleContext *newctx = createInterpreterRuleContext(_ctx, p->stateNumber, ruleIndex);
204 if (ruleStartState->isLeftRecursiveRule) {
205 enterRecursionRule(newctx, ruleStartState->stateNumber, ruleIndex, static_cast<atn::RuleTransition*>(transition)->precedence);
207 enterRule(newctx, transition->target->stateNumber, ruleIndex);
212 case atn::Transition::PREDICATE:
214 atn::PredicateTransition *predicateTransition = static_cast<atn::PredicateTransition*>(transition);
215 if (!sempred(_ctx, predicateTransition->ruleIndex, predicateTransition->predIndex)) {
216 throw FailedPredicateException(this);
221 case atn::Transition::ACTION:
223 atn::ActionTransition *actionTransition = static_cast<atn::ActionTransition*>(transition);
224 action(_ctx, actionTransition->ruleIndex, actionTransition->actionIndex);
228 case atn::Transition::PRECEDENCE:
230 if (!precpred(_ctx, static_cast<atn::PrecedencePredicateTransition*>(transition)->precedence)) {
231 throw FailedPredicateException(this, "precpred(_ctx, " + std::to_string(static_cast<atn::PrecedencePredicateTransition*>(transition)->precedence) + ")");
237 throw UnsupportedOperationException("Unrecognized ATN transition type.");
240 setState(transition->target->stateNumber);
243 size_t ParserInterpreter::visitDecisionState(DecisionState *p) {
244 size_t predictedAlt = 1;
245 if (p->transitions.size() > 1) {
246 getErrorHandler()->sync(this);
247 int decision = p->decision;
248 if (decision == _overrideDecision && _input->index() == _overrideDecisionInputIndex && !_overrideDecisionReached) {
249 predictedAlt = _overrideDecisionAlt;
250 _overrideDecisionReached = true;
252 predictedAlt = getInterpreter<ParserATNSimulator>()->adaptivePredict(_input, decision, _ctx);
258 InterpreterRuleContext* ParserInterpreter::createInterpreterRuleContext(ParserRuleContext *parent,
259 size_t invokingStateNumber, size_t ruleIndex) {
260 return _tracker.createInstance<InterpreterRuleContext>(parent, invokingStateNumber, ruleIndex);
263 void ParserInterpreter::visitRuleStopState(atn::ATNState *p) {
264 atn::RuleStartState *ruleStartState = _atn.ruleToStartState[p->ruleIndex];
265 if (ruleStartState->isLeftRecursiveRule) {
266 std::pair<ParserRuleContext *, size_t> parentContext = _parentContextStack.top();
267 _parentContextStack.pop();
269 unrollRecursionContexts(parentContext.first);
270 setState(parentContext.second);
275 atn::RuleTransition *ruleTransition = static_cast<atn::RuleTransition*>(_atn.states[getState()]->transitions[0]);
276 setState(ruleTransition->followState->stateNumber);
279 void ParserInterpreter::recover(RecognitionException &e) {
280 size_t i = _input->index();
281 getErrorHandler()->recover(this, std::make_exception_ptr(e));
283 if (_input->index() == i) {
284 // no input consumed, better add an error node
285 if (is<InputMismatchException *>(&e)) {
286 InputMismatchException &ime = static_cast<InputMismatchException&>(e);
287 Token *tok = e.getOffendingToken();
288 size_t expectedTokenType = ime.getExpectedTokens().getMinElement(); // get any element
289 _errorToken = getTokenFactory()->create({ tok->getTokenSource(), tok->getTokenSource()->getInputStream() },
290 expectedTokenType, tok->getText(), Token::DEFAULT_CHANNEL, INVALID_INDEX, INVALID_INDEX, // invalid start/stop
291 tok->getLine(), tok->getCharPositionInLine());
292 _ctx->addChild(createErrorNode(_errorToken.get()));
294 else { // NoViableAlt
295 Token *tok = e.getOffendingToken();
296 _errorToken = getTokenFactory()->create({ tok->getTokenSource(), tok->getTokenSource()->getInputStream() },
297 Token::INVALID_TYPE, tok->getText(), Token::DEFAULT_CHANNEL, INVALID_INDEX, INVALID_INDEX, // invalid start/stop
298 tok->getLine(), tok->getCharPositionInLine());
299 _ctx->addChild(createErrorNode(_errorToken.get()));
304 Token* ParserInterpreter::recoverInline() {
305 return _errHandler->recoverInline(this);