Exemple #1
0
        public override void Recover(Parser recognizer, RecognitionException e)
        {
            if (recognizer is ICodeCompletionParser &&
                ((ICodeCompletionParser)recognizer).Interpreter.CaretTransitions != null)
            {
                //                    int stateNumber = recognizer.getContext().s;
                //                    ATNState state = recognizer.getATN().states.get(stateNumber);
                //                    if (state instanceof DecisionState && recognizer.getInputStream() instanceof ObjectStream) {
                //                        int decision = ((DecisionState)state).decision;
                //                        ParserATNSimulator simulator = recognizer.getInterpreter();
                //                        int prediction = simulator.adaptivePredict((ObjectStream)recognizer.getInputStream(), decision, recognizer.getContext());
                //                    }

                ICodeCompletionParser        parser      = (ICodeCompletionParser)recognizer;
                ICaretToken                  token       = parser.Interpreter.CaretToken;
                CompletionParserATNSimulator interpreter = parser.Interpreter;

                throw new CaretReachedException(parser.Context, token, interpreter.CaretTransitions, e);
            }

            // TODO: incorporate error recovery as a fallback option if no trees match correctly
            throw e;
            //super.recover(recognizer, e);
        }
Exemple #2
0
        public override IToken RecoverInline(Parser recognizer)
        {
            if (recognizer is ICodeCompletionParser &&
                ((ITokenStream)recognizer.InputStream).Lt(1) is ICaretToken)
            {
                ICodeCompletionParser parser = (ICodeCompletionParser)recognizer;
                ICaretToken           token  = (ICaretToken)((ITokenStream)recognizer.InputStream).Lt(1);

                CompletionParserATNSimulator interp = parser.Interpreter;
                int      stateNumber = recognizer.State;
                ATNState state       = interp.atn.states[stateNumber];

                PredictionContext context      = PredictionContext.FromRuleContext(interp.atn, recognizer.Context, false);
                ATNConfigSet      intermediate = new ATNConfigSet();
                ATNConfigSet      closure      = new ATNConfigSet();
                for (int i = 0; i < state.NumberOfTransitions; i++)
                {
                    Transition transition = state.Transition(i);
                    if (transition.IsEpsilon)
                    {
                        ATNState  target = transition.target;
                        ATNConfig config = ATNConfig.Create(target, i + 1, context);
                        intermediate.Add(config);
                    }
                }

                bool collectPredicates = false;
                bool hasMoreContext    = true;
                interp.ClosureHelper(intermediate, closure, collectPredicates, hasMoreContext, PredictionContextCache.Uncached, false);

                if (!state.OnlyHasEpsilonTransitions)
                {
                    for (int i = 0; i < state.NumberOfTransitions; i++)
                    {
                        closure.Add(ATNConfig.Create(state, i + 1, PredictionContext.FromRuleContext(interp.atn, recognizer.Context)));
                    }
                }

                Dictionary <ATNConfig, IList <Transition> > transitions = null;
                int ncl = closure.Count;
                // TODO: foreach
                for (int ci = 0; ci < ncl; ci++)
                {
                    ATNConfig c = closure[ci];

                    List <Transition> configTransitions = null;

                    int n = c.State.NumberOfTransitions;
                    for (int ti = 0; ti < n; ti++)
                    {               // for each transition
                        Transition trans  = c.State.Transition(ti);
                        ATNState   target = interp.GetReachableTargetHelper(c, trans, CaretToken.CaretTokenType);
                        if (target != null)
                        {
                            if (transitions == null)
                            {
                                transitions = new Dictionary <ATNConfig, IList <Transition> >();
                            }

                            if (configTransitions == null)
                            {
                                configTransitions = new List <Transition>();
                                transitions[c]    = configTransitions;
                            }

                            configTransitions.Add(trans);
                        }
                    }
                }

                /*
                 * This should be null if the intended token is not "wordlike", and
                 * should be a single transition from the current state.
                 */
                if (transitions != null)
                {
                    Debug.Assert(transitions.Count == 1);
                    Debug.Assert(transitions.Values.First().Count == 1);
                    Debug.Assert(state.NumberOfTransitions == 1);
                    Debug.Assert(transitions.Values.First()[0] == state.Transition(0));
                }

                throw new CaretReachedException(parser.Context, token, transitions, null);
            }

            return(base.RecoverInline(recognizer));
        }