Java Code Examples for org.parboiled.support.ParsingResult#hasErrors()

The following examples show how to use org.parboiled.support.ParsingResult#hasErrors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RunParser.java    From immutables with Apache License 2.0 6 votes vote down vote up
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));
  }
 
Example 2
Source File: ParboiledJtwigParser.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Override
public Node parse(Environment environment, ResourceReference resource) {
    ResourceService resourceService = environment.getResourceEnvironment().getResourceService();
    BasicParseRunner<Node> runner = new BasicParseRunner<Node>(
            ParserContext.instance(resource, configuration,
                    configuration.getAddonParserProviders(),
                    configuration.getUnaryOperators(),
                    configuration.getBinaryOperators(),
                    configuration.getTestExpressionParsers())
                    .parser(DocumentParser.class)
                    .NodeRule()
    );

    try {
        ResourceMetadata resourceMetadata = resourceService.loadMetadata(resource);
        Charset charset = resourceMetadata.getCharset()
                .or(environment.getResourceEnvironment().getDefaultInputCharset());
        ParsingResult<Node> result = runner.run(readAllText(resourceMetadata.load(), charset));
        if (result.hasErrors()) {
            throw new ParseException(toMessage(result.parseErrors));
        } else if (!result.matched) {
            throw new ParseException("Invalid template format");
        } else {
            return result.valueStack.pop();
        }
    } catch (ParserRuntimeException e) {
        if ((e.getCause() != null) && (e.getCause() instanceof ParseException)) {
            ParseException cause = (ParseException) e.getCause();
            throw new ParseException(String.format("%s\n%s", cause.getMessage(), exceptionMessageExtractor.extract(e)), cause.getCause());
        } else {
            throw new ParseException(e);
        }
    }
}
 
Example 3
Source File: Processor.java    From immutables with Apache License 2.0 5 votes vote down vote up
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);
}