org.antlr.runtime.UnwantedTokenException Java Examples

The following examples show how to use org.antlr.runtime.UnwantedTokenException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PhoenixParserException.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static SQLExceptionCode getErrorCode(Throwable e) {
    if (e instanceof MissingTokenException) {
        return SQLExceptionCode.MISSING_TOKEN;
    } else if (e instanceof UnwantedTokenException) {
        return SQLExceptionCode.UNWANTED_TOKEN;
    } else if (e instanceof MismatchedTokenException) {
        return SQLExceptionCode.MISMATCHED_TOKEN;
    } else if (e instanceof UnknownFunctionException) {
        return SQLExceptionCode.UNKNOWN_FUNCTION;
    } else {
        return SQLExceptionCode.PARSER_ERROR;
    }
}
 
Example #2
Source File: RecognizerErrorHandler.java    From legstar-core2 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Format an error message as expected by ANTLR. It is basically the
 * same error message that ANTL BaseRecognizer generates with some
 * additional data.
 * Also used to log debugging information.
 * @param log the logger to use at debug time
 * @param recognizer the lexer or parser who generated the error
 * @param e the exception that occured
 * @param superMessage the error message that the super class generated
 * @param tokenNames list of token names
 * @return a formatted error message
 */
public static String getErrorMessage(
        final Logger log,
        final BaseRecognizer recognizer,
        final RecognitionException e,
        final String superMessage,
        final String[] tokenNames) {
    if (log.isDebugEnabled()) {
        List < ? > stack = BaseRecognizer.getRuleInvocationStack(
                e, recognizer.getClass().getSuperclass().getName());
        String debugMsg = recognizer.getErrorHeader(e)
            + " " + e.getClass().getSimpleName()
            + ": " + superMessage
            + ":";
        if (e instanceof NoViableAltException) {
            NoViableAltException nvae = (NoViableAltException) e;
            debugMsg += " (decision=" + nvae.decisionNumber
            + " state=" + nvae.stateNumber + ")"
            + " decision=<<" + nvae.grammarDecisionDescription + ">>";
        } else if (e instanceof UnwantedTokenException) {
            UnwantedTokenException ute = (UnwantedTokenException) e;
            debugMsg += " (unexpected token=" + toString(ute.getUnexpectedToken(), tokenNames) + ")";

        } else if (e instanceof EarlyExitException) {
            EarlyExitException eea = (EarlyExitException) e;
            debugMsg += " (decision=" + eea.decisionNumber + ")";
        }
        debugMsg += " ruleStack=" + stack.toString();
        log.debug(debugMsg);
    }

    return makeUserMsg(e, superMessage);
}
 
Example #3
Source File: RecognizerErrorHandler.java    From legstar-core2 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Simplify error message text for end users.
 * @param e exception that occurred
 * @param msg as formatted by ANTLR
 * @return a more readable error message
 */
public static String makeUserMsg(final RecognitionException e, final String msg) {
    if (e instanceof NoViableAltException) {
        return msg.replace("no viable alternative at", "unrecognized");
    } else if (e instanceof UnwantedTokenException) {
        return msg.replace("extraneous input", "unexpected token");
    } else if (e instanceof MismatchedTokenException) {
        if (msg.contains("mismatched input '<EOF>'")) {
            return msg.replace("mismatched input '<EOF>' expecting", "reached end of file looking for");
        } else {
            return msg.replace("mismatched input", "unexpected token");
        }
    } else if (e instanceof EarlyExitException) {
        return msg.replace("required (...)+ loop did not match anything", "required tokens not found");
    } else if (e instanceof FailedPredicateException) {
        if (msg.contains("picture_string failed predicate: {Unbalanced parentheses}")) {
            return "Unbalanced parentheses in picture string";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Contains invalid picture symbols}")) {
            return "Picture string contains invalid symbols";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Syntax error in last picture clause}")) {
            return "Syntax error in last picture clause";
        }
        if (msg.contains("DATA_NAME failed predicate: {Syntax error in last clause}")) {
            return "Syntax error in last COBOL clause";
        }
    }
    return msg;
}
 
Example #4
Source File: PhoenixParserException.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static SQLExceptionCode getErrorCode(Throwable e) {
    if (e instanceof MissingTokenException) {
        return SQLExceptionCode.MISSING_TOKEN;
    } else if (e instanceof UnwantedTokenException) {
        return SQLExceptionCode.UNWANTED_TOKEN;
    } else if (e instanceof MismatchedTokenException) {
        return SQLExceptionCode.MISMATCHED_TOKEN;
    } else if (e instanceof UnknownFunctionException) {
        return SQLExceptionCode.UNKNOWN_FUNCTION;
    } else {
        return SQLExceptionCode.PARSER_ERROR;
    }
}
 
Example #5
Source File: PhoenixParserException.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static SQLExceptionCode getErrorCode(Throwable e) {
    if (e instanceof MissingTokenException) {
        return SQLExceptionCode.MISSING_TOKEN;
    } else if (e instanceof UnwantedTokenException) {
        return SQLExceptionCode.UNWANTED_TOKEN;
    } else if (e instanceof MismatchedTokenException) {
        return SQLExceptionCode.MISMATCHED_TOKEN;
    } else if (e instanceof UnknownFunctionException) {
        return SQLExceptionCode.UNKNOWN_FUNCTION;
    } else {
        return SQLExceptionCode.PARSER_ERROR;
    }
}