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.
6 #include "atn/RuleStopState.h"
7 #include "atn/Transition.h"
8 #include "atn/RuleTransition.h"
9 #include "atn/SingletonPredictionContext.h"
10 #include "atn/AbstractPredicateTransition.h"
11 #include "atn/WildcardTransition.h"
12 #include "atn/NotSetTransition.h"
13 #include "misc/IntervalSet.h"
14 #include "atn/ATNConfig.h"
15 #include "atn/EmptyPredictionContext.h"
17 #include "support/CPPUtils.h"
19 #include "atn/LL1Analyzer.h"
21 using namespace antlr4;
22 using namespace antlr4::atn;
23 using namespace antlrcpp;
25 LL1Analyzer::LL1Analyzer(const ATN &atn) : _atn(atn) {
28 LL1Analyzer::~LL1Analyzer() {
31 std::vector<misc::IntervalSet> LL1Analyzer::getDecisionLookahead(ATNState *s) const {
32 std::vector<misc::IntervalSet> look;
38 look.resize(s->transitions.size()); // Fills all interval sets with defaults.
39 for (size_t alt = 0; alt < s->transitions.size(); alt++) {
40 bool seeThruPreds = false; // fail to get lookahead upon pred
42 ATNConfig::Set lookBusy;
43 antlrcpp::BitSet callRuleStack;
44 _LOOK(s->transitions[alt]->target, nullptr, PredictionContext::EMPTY,
45 look[alt], lookBusy, callRuleStack, seeThruPreds, false);
47 // Wipe out lookahead for this alternative if we found nothing
48 // or we had a predicate when we !seeThruPreds
49 if (look[alt].size() == 0 || look[alt].contains(HIT_PRED)) {
56 misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, RuleContext *ctx) const {
57 return LOOK(s, nullptr, ctx);
60 misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, ATNState *stopState, RuleContext *ctx) const {
62 bool seeThruPreds = true; // ignore preds; get all lookahead
63 Ref<PredictionContext> lookContext = ctx != nullptr ? PredictionContext::fromRuleContext(_atn, ctx) : nullptr;
65 ATNConfig::Set lookBusy;
66 antlrcpp::BitSet callRuleStack;
67 _LOOK(s, stopState, lookContext, r, lookBusy, callRuleStack, seeThruPreds, true);
72 void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref<PredictionContext> const& ctx, misc::IntervalSet &look,
73 ATNConfig::Set &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const {
75 Ref<ATNConfig> c = std::make_shared<ATNConfig>(s, 0, ctx);
77 if (lookBusy.count(c) > 0) // Keep in mind comparison is based on members of the class, not the actual instance.
82 // ml: s can never be null, hence no need to check if stopState is != null.
85 look.add(Token::EPSILON);
87 } else if (ctx->isEmpty() && addEOF) {
93 if (s->getStateType() == ATNState::RULE_STOP) {
95 look.add(Token::EPSILON);
97 } else if (ctx->isEmpty() && addEOF) {
102 if (ctx != PredictionContext::EMPTY) {
103 bool removed = calledRuleStack.test(s->ruleIndex);
104 calledRuleStack[s->ruleIndex] = false;
105 auto onExit = finally([removed, &calledRuleStack, s] {
107 calledRuleStack.set(s->ruleIndex);
110 // run thru all possible stack tops in ctx
111 for (size_t i = 0; i < ctx->size(); i++) {
112 ATNState *returnState = _atn.states[ctx->getReturnState(i)];
113 _LOOK(returnState, stopState, ctx->getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
119 size_t n = s->transitions.size();
120 for (size_t i = 0; i < n; i++) {
121 Transition *t = s->transitions[i];
123 if (t->getSerializationType() == Transition::RULE) {
124 if (calledRuleStack[(static_cast<RuleTransition*>(t))->target->ruleIndex]) {
128 Ref<PredictionContext> newContext = SingletonPredictionContext::create(ctx, (static_cast<RuleTransition*>(t))->followState->stateNumber);
129 auto onExit = finally([t, &calledRuleStack] {
130 calledRuleStack[(static_cast<RuleTransition*>(t))->target->ruleIndex] = false;
133 calledRuleStack.set((static_cast<RuleTransition*>(t))->target->ruleIndex);
134 _LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
136 } else if (is<AbstractPredicateTransition *>(t)) {
138 _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
142 } else if (t->isEpsilon()) {
143 _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
144 } else if (t->getSerializationType() == Transition::WILDCARD) {
145 look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, static_cast<ssize_t>(_atn.maxTokenType)));
147 misc::IntervalSet set = t->label();
148 if (!set.isEmpty()) {
149 if (is<NotSetTransition*>(t)) {
150 set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, static_cast<ssize_t>(_atn.maxTokenType)));