diff options
author | Christopher L. Conway <christopherleeconway@gmail.com> | 2010-04-02 20:04:34 +0000 |
---|---|---|
committer | Christopher L. Conway <christopherleeconway@gmail.com> | 2010-04-02 20:04:34 +0000 |
commit | b6544992abea751ec48ac0892925d7f04e5cbf10 (patch) | |
tree | 63bd7a7d8eb6eae9e89e3638ff9b1ec23b196744 /src/parser/antlr_input.cpp | |
parent | aa345c679983d0f9197d9a7b2d24d6ccd92f90f3 (diff) |
Overriding ANTLR3 error recovery routine
Diffstat (limited to 'src/parser/antlr_input.cpp')
-rw-r--r-- | src/parser/antlr_input.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/parser/antlr_input.cpp b/src/parser/antlr_input.cpp index 0d6f63812..3d9006e04 100644 --- a/src/parser/antlr_input.cpp +++ b/src/parser/antlr_input.cpp @@ -104,6 +104,47 @@ void AntlrInput::parseError(const std::string& message) d_lexer->getCharPositionInLine(d_lexer)); } +void * +AntlrInput::recoverFromMismatchedToken(pANTLR3_BASE_RECOGNIZER recognizer, + ANTLR3_UINT32 ttype, + pANTLR3_BITSET_LIST follow) { + + pANTLR3_PARSER parser = (pANTLR3_PARSER) (recognizer->super); + pANTLR3_INT_STREAM is = parser->tstream->istream; + void *matchedSymbol; + + + // Create an exception if we need one + // + if(recognizer->state->exception == NULL) { + antlr3RecognitionExceptionNew(recognizer); + } + + if(recognizer->mismatchIsUnwantedToken(recognizer, is, ttype) == ANTLR3_TRUE) { + recognizer->state->exception->type = ANTLR3_UNWANTED_TOKEN_EXCEPTION; + recognizer->state->exception->message + = (void*)ANTLR3_UNWANTED_TOKEN_EXCEPTION_NAME; + } + + if(recognizer->mismatchIsMissingToken(recognizer, is, follow)) { + // We can fake the missing token and proceed + // + matchedSymbol = recognizer->getMissingSymbol(recognizer, is, + recognizer->state->exception, + ttype, follow); + recognizer->state->exception->type = ANTLR3_MISSING_TOKEN_EXCEPTION; + recognizer->state->exception->message = (void*)ANTLR3_MISSING_TOKEN_EXCEPTION_NAME; + recognizer->state->exception->token = matchedSymbol; + recognizer->state->exception->expecting = ttype; + + // Print out the error after we insert so that ANTLRWorks sees the + // token in the exception. + // + } + reportError(recognizer); + Unreachable("reportError should have thrown exception in AntlrInput::recoverFromMismatchedToken"); +} + void AntlrInput::reportError(pANTLR3_BASE_RECOGNIZER recognizer) { pANTLR3_EXCEPTION ex = recognizer->state->exception; pANTLR3_UINT8 * tokenNames = recognizer->state->tokenNames; @@ -310,6 +351,7 @@ void AntlrInput::setParser(pANTLR3_PARSER pParser) { // pass it in as an address anyway. d_parser->super = getParserState(); d_parser->rec->reportError = &reportError; + d_parser->rec->recoverFromMismatchedToken = &recoverFromMismatchedToken; } |