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())); } }
/** * Pretty prints the given parse errors showing their location in the given input buffer. * * @param errors the parse errors * @return the pretty print text */ public static String printParseErrors(List<ParseError> errors) { checkArgNotNull(errors, "errors"); StringBuilder sb = new StringBuilder(); for (ParseError error : errors) { if (sb.length() > 0) sb.append("---\n"); sb.append(printParseError(error)); } return sb.toString(); }
/** * 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; } }
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()); }
private static Matcher findProperLabelMatcher0(MatcherPath path, int errorIndex) { checkArgNotNull(path, "path"); Matcher found = path.parent != null ? findProperLabelMatcher0(path.parent, errorIndex) : null; if (found != null) return found; Matcher m = path.element.matcher; if (m instanceof TestNotMatcher) throw UnderneathTestNot; if (path.element.startIndex == errorIndex && m.hasCustomLabel()) return m; return null; }
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); }
/** * Prints an error message showing a location in the given InputBuffer. * * @param format the format string, must include three placeholders for a string * (the error message) and two integers (the error line / column respectively) * @param errorMessage the error message * @param errorIndex the error location as an index into the inputBuffer * @param inputBuffer the underlying InputBuffer * @return the error message including the relevant line from the underlying input plus location indicator */ public static String printErrorMessage(String format, String errorMessage, int errorIndex, InputBuffer inputBuffer) { checkArgNotNull(inputBuffer, "inputBuffer"); return printErrorMessage(format, errorMessage, errorIndex, errorIndex + 1, inputBuffer); }
public boolean runMatcher() { try { if (matchHandler.match(this)) { if (parent != null) { parent.currentIndex = currentIndex; parent.currentChar = currentChar; } matcher = null; // "retire" this context return true; } matcher = null; // "retire" this context until is "activated" again by a getSubContext(...) on the parent return false; } catch (ParserRuntimeException e) { throw e; // don't wrap, just bubble up } catch (RecoveringParseRunner.TimeoutException e) { throw e; // don't wrap, just bubble up } catch (Throwable e) { throw new ParserRuntimeException(e, printParseError(new BasicParseError(inputBuffer, currentIndex, StringUtils.escape(String.format("Error while parsing %s '%s' at input position", matcher instanceof ActionMatcher ? "action" : "rule", getPath())))) + '\n' + e); } } }
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)); } }
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())); } }
public String getParsingErrorMessage() { return ErrorUtils.printParseErrors(parsingResult.parseErrors); } }
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())); } }
private ExpressionValidationResult(boolean validationSucceed, List<ParseError> parseErrors) { this.validationSucceed = validationSucceed; this.errorMessage = ErrorUtils.printParseErrors(parseErrors); }
/** * Pretty prints the given parse error showing its location in the given input buffer. * * @param error the parse error * @param formatter the formatter for InvalidInputErrors * @return the pretty print text */ public static String printParseError(ParseError error, Formatter<InvalidInputError> formatter) { checkArgNotNull(error, "error"); checkArgNotNull(formatter, "formatter"); String message = error.getErrorMessage() != null ? error.getErrorMessage() : error instanceof InvalidInputError ? formatter.format((InvalidInputError) error) : error.getClass().getSimpleName(); return printErrorMessage("%s (line %s, pos %s):", message, error.getStartIndex(), error.getEndIndex(), error.getInputBuffer()); }
/** * 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); }
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); }
System.out.println("\nParse Errors:\n" + ErrorUtils.printParseErrors(result));
@Override public MongoDBQueryDescriptor parseNativeQuery(String nativeQuery) { NativeQueryParser localParser = NATIVE_QUERY_PARSER.newInstance(); ParsingResult<MongoDBQueryDescriptorBuilder> parseResult = new RecoveringParseRunner<MongoDBQueryDescriptorBuilder>( localParser.Query() ) .run( nativeQuery ); if ( parseResult.hasErrors() ) { throw new IllegalArgumentException( "Unsupported native query: " + ErrorUtils.printParseErrors( parseResult.parseErrors ) ); } return parseResult.resultValue.build(); }