Antlr4.Runtime.Atn.ParserATNSimulator.ExecATNWithFullContext C# (CSharp) Method

ExecATNWithFullContext() protected method

protected ExecATNWithFullContext ( DFA dfa, DFAState D, ATNConfigSet s0, ITokenStream input, int startIndex, ParserRuleContext outerContext ) : int
dfa Antlr4.Runtime.Dfa.DFA
D Antlr4.Runtime.Dfa.DFAState
s0 ATNConfigSet
input ITokenStream
startIndex int
outerContext ParserRuleContext
return int
        protected int ExecATNWithFullContext(DFA dfa,
											 DFAState D, // how far we got in SLL DFA before failing over
											 ATNConfigSet s0,
											 ITokenStream input, int startIndex,
											 ParserRuleContext outerContext)
        {
            if (debug || debug_list_atn_decisions)
            {
                Console.WriteLine("execATNWithFullContext " + s0);
            }
            bool fullCtx = true;
            bool foundExactAmbig = false;
            ATNConfigSet reach = null;
            ATNConfigSet previous = s0;
            input.Seek(startIndex);
            int t = input.LA(1);
            int predictedAlt;
            while (true)
            { // while more work
              //			Console.WriteLine("LL REACH "+GetLookaheadName(input)+
              //							   " from configs.size="+previous.size()+
              //							   " line "+input.LT(1)Line+":"+input.LT(1).Column);
                reach = ComputeReachSet(previous, t, fullCtx);
                if (reach == null)
                {
                    // if any configs in previous dipped into outer context, that
                    // means that input up to t actually finished entry rule
                    // at least for LL decision. Full LL doesn't dip into outer
                    // so don't need special case.
                    // We will get an error no matter what so delay until after
                    // decision; better error message. Also, no reachable target
                    // ATN states in SLL implies LL will also get nowhere.
                    // If conflict in states that dip out, choose min since we
                    // will get error no matter what.
                    NoViableAltException e = NoViableAlt(input, outerContext, previous, startIndex);
                    input.Seek(startIndex);
                    int alt = GetSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext);
                    if (alt != ATN.INVALID_ALT_NUMBER)
                    {
                        return alt;
                    }
                    throw e;
                }

                ICollection<BitSet> altSubSets = PredictionMode.GetConflictingAltSubsets(reach.configs);
                if (debug)
                {
                    Console.WriteLine("LL altSubSets=" + altSubSets +
                                       ", predict=" + PredictionMode.GetUniqueAlt(altSubSets) +
                                       ", ResolvesToJustOneViableAlt=" +
                                           PredictionMode.ResolvesToJustOneViableAlt(altSubSets));
                }

                //			Console.WriteLine("altSubSets: "+altSubSets);
                //			System.err.println("reach="+reach+", "+reach.conflictingAlts);
                reach.uniqueAlt = GetUniqueAlt(reach);
                // unique prediction?
                if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER)
                {
                    predictedAlt = reach.uniqueAlt;
                    break;
                }
                if (mode != PredictionMode.LL_EXACT_AMBIG_DETECTION)
                {
                    predictedAlt = PredictionMode.ResolvesToJustOneViableAlt(altSubSets);
                    if (predictedAlt != ATN.INVALID_ALT_NUMBER)
                    {
                        break;
                    }
                }
                else {
                    // In exact ambiguity mode, we never try to terminate early.
                    // Just keeps scarfing until we know what the conflict is
                    if (PredictionMode.AllSubsetsConflict(altSubSets) &&
                         PredictionMode.AllSubsetsEqual(altSubSets))
                    {
                        foundExactAmbig = true;
                        predictedAlt = PredictionMode.GetSingleViableAlt(altSubSets);
                        break;
                    }
                    // else there are multiple non-conflicting subsets or
                    // we're not sure what the ambiguity is yet.
                    // So, keep going.
                }

                previous = reach;
                if (t != IntStreamConstants.EOF)
                {
                    input.Consume();
                    t = input.LA(1);
                }
            }

            // If the configuration set uniquely predicts an alternative,
            // without conflict, then we know that it's a full LL decision
            // not SLL.
            if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER)
            {
                ReportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.Index);
                return predictedAlt;
            }

            // We do not check predicates here because we have checked them
            // on-the-fly when doing full context prediction.

            /*
            In non-exact ambiguity detection mode, we might	actually be able to
            detect an exact ambiguity, but I'm not going to spend the cycles
            needed to check. We only emit ambiguity warnings in exact ambiguity
            mode.

            For example, we might know that we have conflicting configurations.
            But, that does not mean that there is no way forward without a
            conflict. It's possible to have nonconflicting alt subsets as in:

               LL altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]

            from

               [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
                (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]

            In this case, (17,1,[5 $]) indicates there is some next sequence that
            would resolve this without conflict to alternative 1. Any other viable
            next sequence, however, is associated with a conflict.  We stop
            looking for input because no amount of further lookahead will alter
            the fact that we should predict alternative 1.  We just can't say for
            sure that there is an ambiguity without looking further.
            */
            ReportAmbiguity(dfa, D, startIndex, input.Index, foundExactAmbig, reach.GetAlts(), reach);

            return predictedAlt;
        }