Antlr4.Runtime.DefaultErrorStrategy.SingleTokenInsertion C# (CSharp) Method

SingleTokenInsertion() protected method

This method implements the single-token insertion inline error recovery strategy.
This method implements the single-token insertion inline error recovery strategy. It is called by RecoverInline(Parser) if the single-token deletion strategy fails to recover from the mismatched input. If this method returns , recognizer will be in error recovery mode.

This method determines whether or not single-token insertion is viable by checking if the LA(1) input symbol could be successfully matched if it were instead the LA(2) symbol. If this method returns , the caller is responsible for creating and inserting a token with the correct type to produce this behavior.

protected SingleTokenInsertion ( Parser recognizer ) : bool
recognizer Parser the parser instance
return bool
        protected internal virtual bool SingleTokenInsertion(Parser recognizer)
        {
            int currentSymbolType = ((ITokenStream)recognizer.InputStream).LA(1);
            // if current token is consistent with what could come after current
            // ATN state, then we know we're missing a token; error recovery
            // is free to conjure up and insert the missing token
            ATNState currentState = recognizer.Interpreter.atn.states[recognizer.State];
            ATNState next = currentState.Transition(0).target;
            ATN atn = recognizer.Interpreter.atn;
            IntervalSet expectingAtLL2 = atn.NextTokens(next, recognizer.RuleContext);
            if (expectingAtLL2.Contains(currentSymbolType))
            {
                ReportMissingToken(recognizer);
                return true;
            }
            return false;
        }