private static String createMessage(RecognitionException e) { Token offendingToken = e.getOffendingToken(); String[] inputLines = e.getInputStream().toString().split("\r?\n|\r"); String errorLine = inputLines[offendingToken.getLine() - 1]; StringBuilder message = new StringBuilder(String.format("\nError on line %s, column %s:\n", offendingToken.getLine(), offendingToken.getCharPositionInLine())); message.append(errorLine).append("\n"); for (int i = 0; i < offendingToken.getCharPositionInLine(); i++) { message.append(" "); } message.append("^"); if (e instanceof InputMismatchException) { InputMismatchException ime = (InputMismatchException)e; return String.format("%s\nmatched '%s' as token <%s>, expecting token <%s>", message, offendingToken.getText(), tokenName(offendingToken.getType()), tokenNames(ime.getExpectedTokens())); } if (e instanceof FailedPredicateException) { FailedPredicateException fpe = (FailedPredicateException)e; return String.format("%s\nfailed predicate '%s' after position %s", message, fpe.getPredicate(), offendingToken.getCharPositionInLine()); } if (e instanceof NoViableAltException || e instanceof LexerNoViableAltException) { return String.format("%s\ncould not decide what path to take, at position %s", message, offendingToken.getCharPositionInLine()); } return message + "\nAn unknown error occurred!"; }