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 "ANTLRErrorStrategy.h"
7 #include "CommonToken.h"
8 #include "FailedPredicateException.h"
9 #include "InputMismatchException.h"
10 #include "InterpreterRuleContext.h"
11 #include "Lexer.h"
12 #include "Token.h"
13 #include "Vocabulary.h"
14 #include "atn/ATN.h"
15 #include "atn/ActionTransition.h"
16 #include "atn/AtomTransition.h"
17 #include "atn/LoopEndState.h"
18 #include "atn/ParserATNSimulator.h"
19 #include "atn/PrecedencePredicateTransition.h"
20 #include "atn/PredicateTransition.h"
21 #include "atn/RuleStartState.h"
22 #include "atn/RuleStopState.h"
23 #include "atn/RuleTransition.h"
24 #include "atn/StarLoopEntryState.h"
25 #include "dfa/DFA.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,
39                                      const std::vector<std::string>& tokenNames,
40                                      const std::vector<std::string>& ruleNames,
41                                      const atn::ATN& atn, TokenStream* input)
42     : ParserInterpreter(grammarFileName,
43                         dfa::Vocabulary::fromTokenNames(tokenNames), ruleNames,
44                         atn, input) {}
45 
ParserInterpreter(const std::string & grammarFileName,const dfa::Vocabulary & vocabulary,const std::vector<std::string> & ruleNames,const atn::ATN & atn,TokenStream * input)46 ParserInterpreter::ParserInterpreter(const std::string& grammarFileName,
47                                      const dfa::Vocabulary& vocabulary,
48                                      const std::vector<std::string>& ruleNames,
49                                      const atn::ATN& atn, TokenStream* input)
50     : Parser(input),
51       _grammarFileName(grammarFileName),
52       _atn(atn),
53       _ruleNames(ruleNames),
54       _vocabulary(vocabulary) {
55   for (size_t i = 0; i < atn.maxTokenType; ++i) {
56     _tokenNames.push_back(vocabulary.getDisplayName(i));
57   }
58 
59   // init decision DFA
60   for (size_t i = 0; i < atn.getNumberOfDecisions(); ++i) {
61     atn::DecisionState* decisionState = atn.getDecisionState(i);
62     _decisionToDFA.push_back(dfa::DFA(decisionState, i));
63   }
64 
65   // get atn simulator that knows how to do predictions
66   _interpreter = new atn::ParserATNSimulator(
67       this, atn, _decisionToDFA,
68       _sharedContextCache); /* mem-check: deleted in d-tor */
69 }
70 
~ParserInterpreter()71 ParserInterpreter::~ParserInterpreter() { delete _interpreter; }
72 
reset()73 void ParserInterpreter::reset() {
74   Parser::reset();
75   _overrideDecisionReached = false;
76   _overrideDecisionRoot = nullptr;
77 }
78 
getATN() const79 const atn::ATN& ParserInterpreter::getATN() const { return _atn; }
80 
getTokenNames() const81 const std::vector<std::string>& ParserInterpreter::getTokenNames() const {
82   return _tokenNames;
83 }
84 
getVocabulary() const85 const dfa::Vocabulary& ParserInterpreter::getVocabulary() const {
86   return _vocabulary;
87 }
88 
getRuleNames() const89 const std::vector<std::string>& ParserInterpreter::getRuleNames() const {
90   return _ruleNames;
91 }
92 
getGrammarFileName() const93 std::string ParserInterpreter::getGrammarFileName() const {
94   return _grammarFileName;
95 }
96 
parse(size_t startRuleIndex)97 ParserRuleContext* ParserInterpreter::parse(size_t startRuleIndex) {
98   atn::RuleStartState* startRuleStartState =
99       _atn.ruleToStartState[startRuleIndex];
100 
101   _rootContext = createInterpreterRuleContext(
102       nullptr, atn::ATNState::INVALID_STATE_NUMBER, startRuleIndex);
103 
104   if (startRuleStartState->isLeftRecursiveRule) {
105     enterRecursionRule(_rootContext, startRuleStartState->stateNumber,
106                        startRuleIndex, 0);
107   } else {
108     enterRule(_rootContext, startRuleStartState->stateNumber, startRuleIndex);
109   }
110 
111   while (true) {
112     atn::ATNState* p = getATNState();
113     switch (p->getStateType()) {
114       case atn::ATNState::RULE_STOP:
115         // pop; return from rule
116         if (_ctx->isEmpty()) {
117           if (startRuleStartState->isLeftRecursiveRule) {
118             ParserRuleContext* result = _ctx;
119             auto parentContext = _parentContextStack.top();
120             _parentContextStack.pop();
121             unrollRecursionContexts(parentContext.first);
122             return result;
123           } else {
124             exitRule();
125             return _rootContext;
126           }
127         }
128 
129         visitRuleStopState(p);
130         break;
131 
132       default:
133         try {
134           visitState(p);
135         } catch (RecognitionException& e) {
136           setState(_atn.ruleToStopState[p->ruleIndex]->stateNumber);
137           getErrorHandler()->reportError(this, e);
138           getContext()->exception = std::current_exception();
139           recover(e);
140         }
141 
142         break;
143     }
144   }
145 }
146 
enterRecursionRule(ParserRuleContext * localctx,size_t state,size_t ruleIndex,int precedence)147 void ParserInterpreter::enterRecursionRule(ParserRuleContext* localctx,
148                                            size_t state, size_t ruleIndex,
149                                            int precedence) {
150   _parentContextStack.push({_ctx, localctx->invokingState});
151   Parser::enterRecursionRule(localctx, state, ruleIndex, precedence);
152 }
153 
addDecisionOverride(int decision,int tokenIndex,int forcedAlt)154 void ParserInterpreter::addDecisionOverride(int decision, int tokenIndex,
155                                             int forcedAlt) {
156   _overrideDecision = decision;
157   _overrideDecisionInputIndex = tokenIndex;
158   _overrideDecisionAlt = forcedAlt;
159 }
160 
getOverrideDecisionRoot() const161 Ref<InterpreterRuleContext> ParserInterpreter::getOverrideDecisionRoot() const {
162   return _overrideDecisionRoot;
163 }
164 
getRootContext()165 InterpreterRuleContext* ParserInterpreter::getRootContext() {
166   return _rootContext;
167 }
168 
getATNState()169 atn::ATNState* ParserInterpreter::getATNState() {
170   return _atn.states[getState()];
171 }
172 
visitState(atn::ATNState * p)173 void ParserInterpreter::visitState(atn::ATNState* p) {
174   size_t predictedAlt = 1;
175   if (is<DecisionState*>(p)) {
176     predictedAlt = visitDecisionState(dynamic_cast<DecisionState*>(p));
177   }
178 
179   atn::Transition* transition = p->transitions[predictedAlt - 1];
180   switch (transition->getSerializationType()) {
181     case atn::Transition::EPSILON:
182       if (p->getStateType() == ATNState::STAR_LOOP_ENTRY &&
183           (dynamic_cast<StarLoopEntryState*>(p))->isPrecedenceDecision &&
184           !is<LoopEndState*>(transition->target)) {
185         // We are at the start of a left recursive rule's (...)* loop
186         // and we're not taking the exit branch of loop.
187         InterpreterRuleContext* localctx = createInterpreterRuleContext(
188             _parentContextStack.top().first, _parentContextStack.top().second,
189             static_cast<int>(_ctx->getRuleIndex()));
190         pushNewRecursionContext(
191             localctx, _atn.ruleToStartState[p->ruleIndex]->stateNumber,
192             static_cast<int>(_ctx->getRuleIndex()));
193       }
194       break;
195 
196     case atn::Transition::ATOM:
197       match(static_cast<int>(
198           static_cast<atn::AtomTransition*>(transition)->_label));
199       break;
200 
201     case atn::Transition::RANGE:
202     case atn::Transition::SET:
203     case atn::Transition::NOT_SET:
204       if (!transition->matches(static_cast<int>(_input->LA(1)),
205                                Token::MIN_USER_TOKEN_TYPE,
206                                Lexer::MAX_CHAR_VALUE)) {
207         recoverInline();
208       }
209       matchWildcard();
210       break;
211 
212     case atn::Transition::WILDCARD:
213       matchWildcard();
214       break;
215 
216     case atn::Transition::RULE: {
217       atn::RuleStartState* ruleStartState =
218           static_cast<atn::RuleStartState*>(transition->target);
219       size_t ruleIndex = ruleStartState->ruleIndex;
220       InterpreterRuleContext* newctx =
221           createInterpreterRuleContext(_ctx, p->stateNumber, ruleIndex);
222       if (ruleStartState->isLeftRecursiveRule) {
223         enterRecursionRule(
224             newctx, ruleStartState->stateNumber, ruleIndex,
225             static_cast<atn::RuleTransition*>(transition)->precedence);
226       } else {
227         enterRule(newctx, transition->target->stateNumber, ruleIndex);
228       }
229     } break;
230 
231     case atn::Transition::PREDICATE: {
232       atn::PredicateTransition* predicateTransition =
233           static_cast<atn::PredicateTransition*>(transition);
234       if (!sempred(_ctx, predicateTransition->ruleIndex,
235                    predicateTransition->predIndex)) {
236         throw FailedPredicateException(this);
237       }
238     } break;
239 
240     case atn::Transition::ACTION: {
241       atn::ActionTransition* actionTransition =
242           static_cast<atn::ActionTransition*>(transition);
243       action(_ctx, actionTransition->ruleIndex, actionTransition->actionIndex);
244     } break;
245 
246     case atn::Transition::PRECEDENCE: {
247       if (!precpred(_ctx,
248                     static_cast<atn::PrecedencePredicateTransition*>(transition)
249                         ->precedence)) {
250         throw FailedPredicateException(
251             this,
252             "precpred(_ctx, " +
253                 std::to_string(
254                     static_cast<atn::PrecedencePredicateTransition*>(transition)
255                         ->precedence) +
256                 ")");
257       }
258     } break;
259 
260     default:
261       throw UnsupportedOperationException("Unrecognized ATN transition type.");
262   }
263 
264   setState(transition->target->stateNumber);
265 }
266 
visitDecisionState(DecisionState * p)267 size_t ParserInterpreter::visitDecisionState(DecisionState* p) {
268   size_t predictedAlt = 1;
269   if (p->transitions.size() > 1) {
270     getErrorHandler()->sync(this);
271     int decision = p->decision;
272     if (decision == _overrideDecision &&
273         _input->index() == _overrideDecisionInputIndex &&
274         !_overrideDecisionReached) {
275       predictedAlt = _overrideDecisionAlt;
276       _overrideDecisionReached = true;
277     } else {
278       predictedAlt = getInterpreter<ParserATNSimulator>()->adaptivePredict(
279           _input, decision, _ctx);
280     }
281   }
282   return predictedAlt;
283 }
284 
createInterpreterRuleContext(ParserRuleContext * parent,size_t invokingStateNumber,size_t ruleIndex)285 InterpreterRuleContext* ParserInterpreter::createInterpreterRuleContext(
286     ParserRuleContext* parent, size_t invokingStateNumber, size_t ruleIndex) {
287   return _tracker.createInstance<InterpreterRuleContext>(
288       parent, invokingStateNumber, ruleIndex);
289 }
290 
visitRuleStopState(atn::ATNState * p)291 void ParserInterpreter::visitRuleStopState(atn::ATNState* p) {
292   atn::RuleStartState* ruleStartState = _atn.ruleToStartState[p->ruleIndex];
293   if (ruleStartState->isLeftRecursiveRule) {
294     std::pair<ParserRuleContext*, size_t> parentContext =
295         _parentContextStack.top();
296     _parentContextStack.pop();
297 
298     unrollRecursionContexts(parentContext.first);
299     setState(parentContext.second);
300   } else {
301     exitRule();
302   }
303 
304   atn::RuleTransition* ruleTransition = static_cast<atn::RuleTransition*>(
305       _atn.states[getState()]->transitions[0]);
306   setState(ruleTransition->followState->stateNumber);
307 }
308 
recover(RecognitionException & e)309 void ParserInterpreter::recover(RecognitionException& e) {
310   size_t i = _input->index();
311   getErrorHandler()->recover(this, std::make_exception_ptr(e));
312 
313   if (_input->index() == i) {
314     // no input consumed, better add an error node
315     if (is<InputMismatchException*>(&e)) {
316       InputMismatchException& ime = static_cast<InputMismatchException&>(e);
317       Token* tok = e.getOffendingToken();
318       size_t expectedTokenType =
319           ime.getExpectedTokens().getMinElement();  // get any element
320       _errorToken = getTokenFactory()->create(
321           {tok->getTokenSource(), tok->getTokenSource()->getInputStream()},
322           expectedTokenType, tok->getText(), Token::DEFAULT_CHANNEL,
323           INVALID_INDEX, INVALID_INDEX,  // invalid start/stop
324           tok->getLine(), tok->getCharPositionInLine());
325       _ctx->addChild(createErrorNode(_errorToken.get()));
326     } else {  // NoViableAlt
327       Token* tok = e.getOffendingToken();
328       _errorToken = getTokenFactory()->create(
329           {tok->getTokenSource(), tok->getTokenSource()->getInputStream()},
330           Token::INVALID_TYPE, tok->getText(), Token::DEFAULT_CHANNEL,
331           INVALID_INDEX, INVALID_INDEX,  // invalid start/stop
332           tok->getLine(), tok->getCharPositionInLine());
333       _ctx->addChild(createErrorNode(_errorToken.get()));
334     }
335   }
336 }
337 
recoverInline()338 Token* ParserInterpreter::recoverInline() {
339   return _errHandler->recoverInline(this);
340 }
341