public RootNode parseInternal(char[] source) { ParsingResult<Node> result = parseToParsingResult(source); if (result.hasErrors()) { throw new RuntimeException("Internal error during markdown parsing:\n--- ParseErrors ---\n" + printParseErrors(result)/* + "\n--- ParseTree ---\n" + printNodeTree(result)*/ ); } return (RootNode) result.resultValue; }
private static void printContext(Object caller, Context<Object> context) { String message = ErrorUtils.printErrorMessage("%s:%s:%s", "", context.getCurrentIndex(), context.getCurrentIndex() + 1, context.getInputBuffer()); System.err.println("*** " + caller + message + "\n\t" + Joiner.on("\n\t - ").join(context.getValueStack())); } }
/** * Throws a GrammarException if the given condition is not met. * * @param condition the condition * @param errorMessage the error message */ public static void ensure(boolean condition, String errorMessage) { if (!condition) { throw new GrammarException(errorMessage); } }
@Override public String apply(ParseError input) { return String.format("%d, %d -> %s", input.getStartIndex(), input.getEndIndex(), input.getErrorMessage()); } }), "\n");
protected String match(ParseError error) { return error.getInputBuffer().extract(error.getStartIndex(), error.getEndIndex()); }
protected Position getPosition(ParseError error) { return error.getInputBuffer().getPosition(error.getStartIndex()); }
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); }
public String extract (ParserRuntimeException exception) { String[] split = exception.getMessage().split("\n"); return split[1] + "\n" + split[2]; } }
throw new InvalidInputError("Input must be a string"); // Output: Uncaught InvalidInputError: Input must be a string
/** * 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()); }
private char throwParsingException() { throw new ParserRuntimeException("Parser read more than 100K chars beyond EOI, " + "verify that your grammar does not consume EOI indefinitely!"); }
/** * Finds the Matcher in the given failedMatcherPath whose label is best for presentation in "expected" strings * of parse error messages, given the provided lastMatchPath. * * @param path the path to the failed matcher * @param errorIndex the start index of the respective parse error * @return the matcher whose label is best for presentation in "expected" strings */ static Matcher findProperLabelMatcher(MatcherPath path, int errorIndex) { try { return findProperLabelMatcher0(path, errorIndex); } catch(RuntimeException e) { if (e == UnderneathTestNot) return null; else throw e; } }
@Override public String apply(ParseError input) { return String.format("%d, %d -> %s", input.getStartIndex(), input.getEndIndex(), input.getErrorMessage()); } }), "\n");
private Unit parseUnit(String templateText) throws Exception { ParsingResult<Object> result = new ReportingParseRunner<>(parser.Unit()).run(templateText); if (result.hasErrors()) { String errors = ErrorUtils.printParseErrors(result.parseErrors); throw new Exception(errors); } return (Unit) Iterables.getOnlyElement(result.valueStack); }
public String extract (ParserRuntimeException exception) { String[] split = exception.getMessage().split("\n"); return split[1] + "\n" + split[2]; } }
/** * Throws a GrammarException if the given condition is not met. * * @param condition the condition * @param errorMessageFormat the error message format * @param errorMessageArgs the error message arguments */ public static void ensure(boolean condition, String errorMessageFormat, Object... errorMessageArgs) { if (!condition) { throw new GrammarException(errorMessageFormat, errorMessageArgs); } }
public static void main(String... args) { Parser templateParser = Parboiled.createParser(Parser.class); ParsingResult<Object> result = new ReportingParseRunner<>(templateParser.Unit()).run(input2); ImmutableList<Object> copy = ImmutableList.copyOf(result.valueStack.iterator()); if (!copy.isEmpty()) { Unit unit = (Unit) copy.get(0); Unit balance = Balancing.balance(unit); System.out.println(balance); } if (result.hasErrors()) { System.err.println(ErrorUtils.printParseErrors(result.parseErrors)); } // System.out.println(ParseTreeUtils.printNodeTree(result)); } }
public String getParsingErrorMessage() { return ErrorUtils.printParseErrors(parsingResult.parseErrors); } }
private ExpressionValidationResult(boolean validationSucceed, List<ParseError> parseErrors) { this.validationSucceed = validationSucceed; this.errorMessage = ErrorUtils.printParseErrors(parseErrors); }
/** * Pretty prints the parse errors of the given ParsingResult showing their location in the given input buffer. * * @param parsingResult the parsing result * @return the pretty print text */ public static String printParseErrors(ParsingResult<?> parsingResult) { checkArgNotNull(parsingResult, "parsingResult"); return printParseErrors(parsingResult.parseErrors); }