protected virtual object RecoverFromMismatchedToken( IIntStream input, int ttype, BitSet follow )
{
RecognitionException e = null;
// if next token is what we are looking for then "delete" this token
if ( MismatchIsUnwantedToken( input, ttype ) )
{
e = new UnwantedTokenException( ttype, input, TokenNames );
/*
System.err.println("recoverFromMismatchedToken deleting "+
((TokenStream)input).LT(1)+
" since "+((TokenStream)input).LT(2)+" is what we want");
*/
BeginResync();
input.Consume(); // simply delete extra token
EndResync();
ReportError( e ); // report after consuming so AW sees the token in the exception
// we want to return the token we're actually matching
object matchedSymbol = GetCurrentInputSymbol( input );
input.Consume(); // move past ttype token as if all were ok
return matchedSymbol;
}
// can't recover with single token deletion, try insertion
if ( MismatchIsMissingToken( input, follow ) )
{
object inserted = GetMissingSymbol( input, e, ttype, follow );
e = new MissingTokenException( ttype, input, inserted );
ReportError( e ); // report after inserting so AW sees the token in the exception
return inserted;
}
// even that didn't work; must throw the exception
e = new MismatchedTokenException(ttype, input, TokenNames);
throw e;
}