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 "dfa/DFA.h"
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"
19 #include "atn/ATN.h"
20 #include "atn/RuleStopState.h"
21 #include "Lexer.h"
22 #include "Token.h"
23 #include "Vocabulary.h"
24 #include "InputMismatchException.h"
25 #include "CommonToken.h"
26 #include "tree/ErrorNode.h"
27 
28 #include "support/CPPUtils.h"
29 
30 #include "ParserInterpreter.h"
31 
32 using namespace antlr4;
33 using namespace antlr4::atn;
34 using namespace antlr4::misc;
35 
36 using namespace antlrcpp;
37 
ParserInterpreter(const std::string & grammarFileName,const std::vector<std::string> & tokenNames,const std::vector<std::string> & ruleNames,const atn::ATN & atn,TokenStream * input)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) {
41 }
42 
ParserInterpreter(const std::string & grammarFileName,const dfa::Vocabulary & vocabulary,const std::vector<std::string> & ruleNames,const atn::ATN & atn,TokenStream * 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) {
46 
47   for (size_t i = 0; i < atn.maxTokenType; ++i) {
48     _tokenNames.push_back(vocabulary.getDisplayName(i));
49   }
50 
51   // init decision DFA
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));
55   }
56 
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 */
59 }
60 
~ParserInterpreter()61 ParserInterpreter::~ParserInterpreter() {
62   delete _interpreter;
63 }
64 
reset()65 void ParserInterpreter::reset() {
66   Parser::reset();
67   _overrideDecisionReached = false;
68   _overrideDecisionRoot = nullptr;
69 }
70 
getATN() const71 const atn::ATN& ParserInterpreter::getATN() const {
72   return _atn;
73 }
74 
getTokenNames() const75 const std::vector<std::string>& ParserInterpreter::getTokenNames() const {
76   return _tokenNames;
77 }
78 
getVocabulary() const79 const dfa::Vocabulary& ParserInterpreter::getVocabulary() const {
80   return _vocabulary;
81 }
82 
getRuleNames() const83 const std::vector<std::string>& ParserInterpreter::getRuleNames() const {
84   return _ruleNames;
85 }
86 
getGrammarFileName() const87 std::string ParserInterpreter::getGrammarFileName() const {
88   return _grammarFileName;
89 }
90 
parse(size_t startRuleIndex)91 ParserRuleContext* ParserInterpreter::parse(size_t startRuleIndex) {
92   atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[startRuleIndex];
93 
94   _rootContext = createInterpreterRuleContext(nullptr, atn::ATNState::INVALID_STATE_NUMBER, startRuleIndex);
95 
96   if (startRuleStartState->isLeftRecursiveRule) {
97     enterRecursionRule(_rootContext, startRuleStartState->stateNumber, startRuleIndex, 0);
98   } else {
99     enterRule(_rootContext, startRuleStartState->stateNumber, startRuleIndex);
100   }
101 
102   while (true) {
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);
113             return result;
114           } else {
115             exitRule();
116             return _rootContext;
117           }
118         }
119 
120         visitRuleStopState(p);
121         break;
122 
123       default :
124         try {
125           visitState(p);
126         }
127         catch (RecognitionException &e) {
128           setState(_atn.ruleToStopState[p->ruleIndex]->stateNumber);
129           getErrorHandler()->reportError(this, e);
130           getContext()->exception = std::current_exception();
131           recover(e);
132         }
133 
134         break;
135     }
136   }
137 }
138 
enterRecursionRule(ParserRuleContext * localctx,size_t state,size_t ruleIndex,int precedence)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);
142 }
143 
addDecisionOverride(int decision,int tokenIndex,int forcedAlt)144 void ParserInterpreter::addDecisionOverride(int decision, int tokenIndex, int forcedAlt) {
145   _overrideDecision = decision;
146   _overrideDecisionInputIndex = tokenIndex;
147   _overrideDecisionAlt = forcedAlt;
148 }
149 
getOverrideDecisionRoot() const150 Ref<InterpreterRuleContext> ParserInterpreter::getOverrideDecisionRoot() const {
151   return _overrideDecisionRoot;
152 }
153 
getRootContext()154 InterpreterRuleContext* ParserInterpreter::getRootContext() {
155   return _rootContext;
156 }
157 
getATNState()158 atn::ATNState* ParserInterpreter::getATNState() {
159   return _atn.states[getState()];
160 }
161 
visitState(atn::ATNState * p)162 void ParserInterpreter::visitState(atn::ATNState *p) {
163   size_t predictedAlt = 1;
164   if (is<DecisionState *>(p)) {
165     predictedAlt = visitDecisionState(dynamic_cast<DecisionState *>(p));
166   }
167 
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()));
179       }
180       break;
181 
182     case atn::Transition::ATOM:
183       match(static_cast<int>(static_cast<atn::AtomTransition*>(transition)->_label));
184       break;
185 
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)) {
190         recoverInline();
191       }
192       matchWildcard();
193       break;
194 
195     case atn::Transition::WILDCARD:
196       matchWildcard();
197       break;
198 
199     case atn::Transition::RULE:
200     {
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);
206       } else {
207         enterRule(newctx, transition->target->stateNumber, ruleIndex);
208       }
209     }
210       break;
211 
212     case atn::Transition::PREDICATE:
213     {
214       atn::PredicateTransition *predicateTransition = static_cast<atn::PredicateTransition*>(transition);
215       if (!sempred(_ctx, predicateTransition->ruleIndex, predicateTransition->predIndex)) {
216         throw FailedPredicateException(this);
217       }
218     }
219       break;
220 
221     case atn::Transition::ACTION:
222     {
223       atn::ActionTransition *actionTransition = static_cast<atn::ActionTransition*>(transition);
224       action(_ctx, actionTransition->ruleIndex, actionTransition->actionIndex);
225     }
226       break;
227 
228     case atn::Transition::PRECEDENCE:
229     {
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) +  ")");
232       }
233     }
234       break;
235 
236     default:
237       throw UnsupportedOperationException("Unrecognized ATN transition type.");
238   }
239 
240   setState(transition->target->stateNumber);
241 }
242 
visitDecisionState(DecisionState * p)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;
251     } else {
252       predictedAlt = getInterpreter<ParserATNSimulator>()->adaptivePredict(_input, decision, _ctx);
253     }
254   }
255   return predictedAlt;
256 }
257 
createInterpreterRuleContext(ParserRuleContext * parent,size_t invokingStateNumber,size_t ruleIndex)258 InterpreterRuleContext* ParserInterpreter::createInterpreterRuleContext(ParserRuleContext *parent,
259   size_t invokingStateNumber, size_t ruleIndex) {
260   return _tracker.createInstance<InterpreterRuleContext>(parent, invokingStateNumber, ruleIndex);
261 }
262 
visitRuleStopState(atn::ATNState * p)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();
268 
269     unrollRecursionContexts(parentContext.first);
270     setState(parentContext.second);
271   } else {
272     exitRule();
273   }
274 
275   atn::RuleTransition *ruleTransition = static_cast<atn::RuleTransition*>(_atn.states[getState()]->transitions[0]);
276   setState(ruleTransition->followState->stateNumber);
277 }
278 
recover(RecognitionException & e)279 void ParserInterpreter::recover(RecognitionException &e) {
280   size_t i = _input->index();
281   getErrorHandler()->recover(this, std::make_exception_ptr(e));
282 
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()));
293     }
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()));
300     }
301   }
302 }
303 
recoverInline()304 Token* ParserInterpreter::recoverInline() {
305   return _errHandler->recoverInline(this);
306 }
307