コード例 #1
0
ファイル: ParserATNSimulator.cs プロジェクト: antlr/antlr4
        public virtual int AdaptivePredict(ITokenStream input, int decision,
								   ParserRuleContext outerContext)
        {
            if (debug || debug_list_atn_decisions)
            {
                Console.WriteLine("adaptivePredict decision " + decision +
                                       " exec LA(1)==" + GetLookaheadName(input) +
                                  " line " + input.LT(1).Line + ":" + input.LT(1).Column);
            }

            this.input = input;
            startIndex = input.Index;
            context = outerContext;
            DFA dfa = decisionToDFA[decision];
            thisDfa = dfa;

            int m = input.Mark();
            int index = startIndex;

            // Now we are certain to have a specific decision's DFA
            // But, do we still need an initial state?
            try
            {
                DFAState s0;
                if (dfa.IsPrecedenceDfa)
                {
                    // the start state for a precedence DFA depends on the current
                    // parser precedence, and is provided by a DFA method.
                    s0 = dfa.GetPrecedenceStartState(parser.Precedence);
                }
                else {
                    // the start state for a "regular" DFA is just s0
                    s0 = dfa.s0;
                }

                if (s0 == null)
                {
                    if (outerContext == null) outerContext = ParserRuleContext.EmptyContext;
                    if (debug || debug_list_atn_decisions)
                    {
                        Console.WriteLine("predictATN decision " + dfa.decision +
                                           " exec LA(1)==" + GetLookaheadName(input) +
                                           ", outerContext=" + outerContext.ToString(parser));
                    }

                    bool fullCtx = false;
                    ATNConfigSet s0_closure =
                        ComputeStartState(dfa.atnStartState,
                                          ParserRuleContext.EmptyContext,
                                          fullCtx);

                    if (dfa.IsPrecedenceDfa)
                    {
                        /* If this is a precedence DFA, we use applyPrecedenceFilter
                         * to convert the computed start state to a precedence start
                         * state. We then use DFA.setPrecedenceStartState to set the
                         * appropriate start state for the precedence level rather
                         * than simply setting DFA.s0.
                         */
                        dfa.s0.configSet = s0_closure; // not used for prediction but useful to know start configs anyway
                        s0_closure = ApplyPrecedenceFilter(s0_closure);
                        s0 = AddDFAState(dfa, new DFAState(s0_closure));
                        dfa.SetPrecedenceStartState(parser.Precedence, s0);
                    }
                    else {
                        s0 = AddDFAState(dfa, new DFAState(s0_closure));
                        dfa.s0 = s0;
                    }
                }

                int alt = ExecATN(dfa, s0, input, index, outerContext);
                if (debug)
                    Console.WriteLine("DFA after predictATN: " + dfa.ToString(parser.Vocabulary));
                return alt;
            }
            finally
            {
                mergeCache = null; // wack cache after each prediction
                thisDfa = null;
                input.Seek(index);
                input.Release(m);
            }
        }