@Override public String format(final InvalidInputError error) { if (error == null) { return ""; } int len = error.getEndIndex() - error.getStartIndex(); StringBuilder sb = new StringBuilder("found"); if (len > 0) { sb.append(" '") .append( StringUtils.escape(String.valueOf(error.getInputBuffer().charAt( error.getStartIndex())))); if (len > 1) { sb.append("..."); } sb.append('\''); } String expectedString = getExpectedString(error); if (StringUtils.isNotEmpty(expectedString)) { sb.append(", expected: ").append(expectedString); } return sb.toString(); }
public String format(InvalidInputError error) { if (error == null) return ""; int len = error.getEndIndex() - error.getStartIndex(); StringBuilder sb = new StringBuilder(); if (len > 0) { char c = error.getInputBuffer().charAt(error.getStartIndex()); if (c == Chars.EOI) { sb.append("Unexpected end of input"); } else { sb.append("Invalid input '") .append(StringUtils.escape(String.valueOf(c))); if (len > 1) sb.append("..."); sb.append('\''); } } else { sb.append("Invalid input"); } String expectedString = getExpectedString(error); if (StringUtils.isNotEmpty(expectedString)) { sb.append(", expected ").append(expectedString); } return sb.toString(); }
public String getExpectedString(InvalidInputError error) { // In non recovery-mode there is no complexity in the error and start indices since they are all stable. // However, in recovery-mode the RecoveringParseRunner inserts characters into the InputBuffer, which requires // for all indices taken before to be shifted. The RecoveringParseRunner does this by changing the indexDelta // of the parse runner. All users of the ParseError will then automatically see shifted start and end indices // matching the state of the underlying InputBuffer. However, since the failed MatcherPaths still carry the // "original" indices we need to unapply the IndexDelta in order to be able to compare with them. int pathStartIndex = error.getStartIndex() - error.getIndexDelta(); List<String> labelList = new ArrayList<String>(); for (MatcherPath path : error.getFailedMatchers()) { Matcher labelMatcher = ErrorUtils.findProperLabelMatcher(path, pathStartIndex); if (labelMatcher == null) continue; String[] labels = getLabels(labelMatcher); for (String label : labels) { if (label != null && !labelList.contains(label)) { labelList.add(label); } } } return join(labelList); }
int pathStartIndex = error.getStartIndex() - error.getIndexDelta();