Search in sources :

Example 1 with MismatchedNotSetException

use of org.antlr.runtime.MismatchedNotSetException in project drools by kiegroup.

the class DroolsParserExceptionFactory method createErrorMessage.

/**
 * This will take a RecognitionException, and create a sensible error
 * message out of it
 */
private List<String> createErrorMessage(RecognitionException e) {
    List<String> codeAndMessage = new ArrayList<String>(2);
    String message;
    if (e instanceof MismatchedTokenException) {
        MismatchedTokenException mte = (MismatchedTokenException) e;
        String expecting = mte instanceof DroolsMismatchedTokenException ? ((DroolsMismatchedTokenException) mte).getTokenText() : getBetterToken(mte.expecting);
        if (tokenNames != null && mte.expecting >= 0 && mte.expecting < tokenNames.length) {
            message = String.format(MISMATCHED_TOKEN_MESSAGE_COMPLETE, e.line, e.charPositionInLine, getBetterToken(e.token), expecting, formatParserLocation());
            codeAndMessage.add(message);
            codeAndMessage.add("ERR 102");
        } else {
            message = String.format(MISMATCHED_TOKEN_MESSAGE_PART, e.line, e.charPositionInLine, getBetterToken(e.token), formatParserLocation());
            codeAndMessage.add(message);
            codeAndMessage.add("ERR 102");
        }
    } else if (e instanceof MismatchedTreeNodeException) {
        MismatchedTreeNodeException mtne = (MismatchedTreeNodeException) e;
        if (mtne.expecting >= 0 && mtne.expecting < tokenNames.length) {
            message = String.format(MISMATCHED_TREE_NODE_MESSAGE_COMPLETE, e.line, e.charPositionInLine, getBetterToken(e.token), getBetterToken(mtne.expecting), formatParserLocation());
            codeAndMessage.add(message);
            codeAndMessage.add("ERR 106");
        } else {
            message = String.format(MISMATCHED_TREE_NODE_MESSAGE_PART, e.line, e.charPositionInLine, getBetterToken(e.token), formatParserLocation());
            codeAndMessage.add(message);
            codeAndMessage.add("ERR 106");
        }
    } else if (e instanceof NoViableAltException) {
        // NoViableAltException nvae = (NoViableAltException) e;
        message = String.format(NO_VIABLE_ALT_MESSAGE, e.line, e.charPositionInLine, getBetterToken(e.token), formatParserLocation());
        codeAndMessage.add(message);
        codeAndMessage.add("ERR 101");
    } else if (e instanceof EarlyExitException) {
        // EarlyExitException eee = (EarlyExitException) e;
        message = String.format(EARLY_EXIT_MESSAGE, e.line, e.charPositionInLine, getBetterToken(e.token), formatParserLocation());
        codeAndMessage.add(message);
        codeAndMessage.add("ERR 105");
    } else if (e instanceof MismatchedSetException) {
        MismatchedSetException mse = (MismatchedSetException) e;
        String expected = expectedTokensAsString(mse.expecting);
        message = String.format(MISMATCHED_SET_MESSAGE, e.line, e.charPositionInLine, getBetterToken(e.token), expected, formatParserLocation());
        codeAndMessage.add(message);
        codeAndMessage.add("ERR 107");
    } else if (e instanceof DroolsMismatchedSetException) {
        DroolsMismatchedSetException mse = (DroolsMismatchedSetException) e;
        String expected = Arrays.asList(mse.getTokenText()).toString();
        message = String.format(MISMATCHED_SET_MESSAGE, e.line, e.charPositionInLine, getBetterToken(e.token), expected, formatParserLocation());
        codeAndMessage.add(message);
        codeAndMessage.add("ERR 107");
    } else if (e instanceof MismatchedNotSetException) {
        MismatchedNotSetException mse = (MismatchedNotSetException) e;
        String expected = expectedTokensAsString(mse.expecting);
        message = String.format(MISMATCHED_NOT_SET_MESSAGE, e.line, e.charPositionInLine, getBetterToken(e.token), expected, formatParserLocation());
        codeAndMessage.add(message);
        codeAndMessage.add("ERR 108");
    } else if (e instanceof FailedPredicateException) {
        FailedPredicateException fpe = (FailedPredicateException) e;
        message = String.format(FAILED_PREDICATE_MESSAGE, e.line, e.charPositionInLine, fpe.ruleName, fpe.predicateText, formatParserLocation());
        codeAndMessage.add(message);
        codeAndMessage.add("ERR 103");
    }
    if (codeAndMessage.get(0).length() == 0) {
        codeAndMessage.add("?????");
    }
    return codeAndMessage;
}
Also used : MismatchedSetException(org.antlr.runtime.MismatchedSetException) MismatchedTreeNodeException(org.antlr.runtime.MismatchedTreeNodeException) NoViableAltException(org.antlr.runtime.NoViableAltException) MismatchedNotSetException(org.antlr.runtime.MismatchedNotSetException) ArrayList(java.util.ArrayList) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) EarlyExitException(org.antlr.runtime.EarlyExitException) FailedPredicateException(org.antlr.runtime.FailedPredicateException)

Aggregations

ArrayList (java.util.ArrayList)1 EarlyExitException (org.antlr.runtime.EarlyExitException)1 FailedPredicateException (org.antlr.runtime.FailedPredicateException)1 MismatchedNotSetException (org.antlr.runtime.MismatchedNotSetException)1 MismatchedSetException (org.antlr.runtime.MismatchedSetException)1 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)1 MismatchedTreeNodeException (org.antlr.runtime.MismatchedTreeNodeException)1 NoViableAltException (org.antlr.runtime.NoViableAltException)1