LexerfulParseErrorFormatter { public String format(List<Token> tokens, int errorIndex) { StringBuilder sb = new StringBuilder(); Pos errorPos = errorIndex < tokens.size() ? getTokenStart(tokens.get(errorIndex)) : getTokenEnd(tokens.get(tokens.size() - 1)); sb.append("Parse error at line ").append(errorPos.line) .append(" column ").append(errorPos.column) .append(":\n\n"); appendSnippet(sb, tokens, errorIndex, errorPos.line); return sb.toString(); }  String format(List<Token> tokens, int errorIndex);  }
@Test public void test() { List<Token> tokens = Arrays.asList( token(2, 1, "foo\nbar\nbaz"), token(4, 6, "qux"), token(6, 3, "end")); String expected = new StringBuilder() .append("Parse error at line 4 column 6:\n") .append("\n") .append(" 2: foo\n") .append(" 3: bar\n") .append(" --> baz qux\n") .append(" 5: \n") .append(" 6: end\n") .toString(); assertThat(new LexerfulParseErrorFormatter().format(tokens, 1)).isEqualTo(expected); }