public CaretReachedException(RuleContext finalContext, ICaretToken caretToken, IDictionary<ATNConfig, IList<Transition>> transitions, RecognitionException innerException) : base(innerException != null ? innerException.Message : "The caret was reached.", innerException) { _finalContext = finalContext; _caretToken = caretToken; _transitions = transitions; }
public CaretReachedException(RuleContext finalContext, ICaretToken caretToken, IDictionary <ATNConfig, IList <Transition> > transitions, RecognitionException innerException) : base(innerException != null ? innerException.Message : "The caret was reached.", innerException) { _finalContext = finalContext; _caretToken = caretToken; _transitions = transitions; }
protected override DFAState CreateDFAState(DFA dfa, ATNConfigSet configs) { int t = _input.La(1); if (t == AntlrV4.CaretToken.CaretTokenType && !_computingStartState) { _caretToken = (ICaretToken)_input.Lt(1); throw NoViableAlt(_input, _outerContext, configs, _startIndex); } return(base.CreateDFAState(dfa, configs)); }
protected virtual ITerminalNode FindFirstNodeAfterOffset(IParseTree parseTree, int offset) { ITerminalNode lastNode = ParseTrees.GetStopNode(parseTree); if (lastNode == null) { return(null); } ICaretToken caretToken = lastNode.Symbol as ICaretToken; if (caretToken != null) { throw new NotImplementedException(); } else if (lastNode.Symbol.StartIndex < offset) { return(null); } lastNode = parseTree as ITerminalNode; if (lastNode != null) { return(lastNode); } for (int i = 0; i < parseTree.ChildCount; i++) { ITerminalNode node = FindFirstNodeAfterOffset(parseTree.GetChild(i), offset); if (node != null) { return(node); } } return(null); }
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); }
protected override DFAState CreateDFAState(DFA dfa, ATNConfigSet configs) { int t = _input.La(1); if (t == AntlrV4.CaretToken.CaretTokenType && !_computingStartState) { _caretToken = (ICaretToken)_input.Lt(1); throw NoViableAlt(_input, _outerContext, configs, _startIndex); } return base.CreateDFAState(dfa, configs); }
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)); }