private static String buildMessage(List<ParseError> errors) { if (errors == null || errors.isEmpty()) { throw new IllegalArgumentException( "Cannot build a CSSParseException without a list of errors"); } StringBuilder sb = new StringBuilder(); for (ParseError pe : errors) { Position pos = pe.getInputBuffer().getPosition(pe.getStartIndex()); String message = pe.getErrorMessage() != null ? pe.getErrorMessage() : pe instanceof InvalidInputError ? new DefaultInvalidInputErrorFormatter() .format((InvalidInputError) pe) : pe.getClass().getSimpleName(); sb.append(message) .append(" (line ") .append(pos.line) .append(", column ") .append(pos.column) .append(")"); sb.append('\n'); } sb.setLength(sb.length() - 1); 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); }
/** * Pretty prints the given parse error showing its location in the given input buffer. * * @param error the parse error * @return the pretty print text */ public static String printParseError(ParseError error) { checkArgNotNull(error, "error"); return printParseError(error, new DefaultInvalidInputErrorFormatter()); }
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(); }