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

RecoverInline() public method

The default implementation attempts to recover from the mismatched input by using single token insertion and deletion as described below. If the recovery attempt fails, this method throws an InputMismatchException .

EXTRA TOKEN (single token deletion)

LA(1) is not what we are looking for. If LA(2) has the right token, however, then assume LA(1) is some extra spurious token and delete it. Then consume and return the next token (which was the LA(2) token) as the successful result of the match operation.

This recovery strategy is implemented by SingleTokenDeletion(Parser) .

MISSING TOKEN (single token insertion)

If current token (at LA(1) ) is consistent with what could come after the expected LA(1) token, then assume the token is missing and use the parser's ITokenFactory to create it on the fly. The "insertion" is performed by returning the created token as the successful result of the match operation.

This recovery strategy is implemented by SingleTokenInsertion(Parser) .

EXAMPLE

For example, Input i=(3; is clearly missing the ')' . When the parser returns from the nested call to expr , it will have call chain:

 stat → expr → atom 
and it will be trying to match the ')' at this point in the derivation:
 => ID '=' '(' INT ')' ('+' atom)* ';' ^ 
The attempt to match ')' will fail when it sees ';' and call RecoverInline(Parser) . To recover, it sees that LA(1)==';' is in the set of tokens that can follow the ')' token reference in rule atom . It can assume that you forgot the ')' .
public RecoverInline ( Parser recognizer ) : IToken
recognizer Parser
return IToken
        public virtual IToken RecoverInline(Parser recognizer)
        {
            // SINGLE TOKEN DELETION
            IToken matchedSymbol = SingleTokenDeletion(recognizer);
            if (matchedSymbol != null)
            {
                // we have deleted the extra token.
                // now, move past ttype token as if all were ok
                recognizer.Consume();
                return matchedSymbol;
            }
            // SINGLE TOKEN INSERTION
            if (SingleTokenInsertion(recognizer))
            {
                return GetMissingSymbol(recognizer);
            }
            // even that didn't work; must throw the exception
            throw new InputMismatchException(recognizer);
        }