public static String displayError(IntStream stream, int line, int position, int length) { String[] lines = null; StringBuilder sb = new StringBuilder(); if (stream instanceof CharStream) { lines = ((CharStream) stream).substring(0, stream.size() - 1).split("\n"); } else if (stream instanceof TokenStream) { lines = ((TokenStream) stream).toString(0, stream.size() - 1).split("\n"); } sb.append(" at line ").append(line).append(" while parsing: \n"); if (lines != null) { int start = Math.max(0, line - 5); int end = Math.min(lines.length, line + 5); int badline = line - 1; for (int i = start; i < end; i++) { sb.append(String.format("%5d: %s\n", i + 1, lines[i])); if (i == badline) { for (int j = 0; j < position + 7; j++) sb.append("-"); for (int j = 0; j <= length; j++) sb.append("^"); sb.append("\n"); } } } return sb.toString(); }
@Override public String getText() { String badText = null; if (start != null) { int i = start.getTokenIndex(); int j = stop.getTokenIndex(); if (stop.getType() == Token.EOF) { j = input.size(); } badText = ((TokenStream)input).toString(i, j); } else { // people should subclass if they alter the tree type so this // next one is for sure correct. badText = "<unknown>"; } return badText; }
@Override public String getText() { String badText = null; if (start != null) { int i = start.getTokenIndex(); int j = stop.getTokenIndex(); if (stop.getType() == Token.EOF) { j = input.size(); } badText = ((TokenStream)input).toString(i, j); } else { // people should subclass if they alter the tree type so this // next one is for sure correct. badText = "<unknown>"; } return badText; }