Java Code Examples for org.parboiled.parserunners.BasicParseRunner#run()

The following examples show how to use org.parboiled.parserunners.BasicParseRunner#run() . 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: 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 2
Source File: GroupWildcard.java    From batfish with Apache License 2.0 5 votes vote down vote up
public static String toJavaRegex(String wildcard) {
  GroupWildcard parser = Parboiled.createParser(GroupWildcard.class);
  BasicParseRunner<String> runner = new BasicParseRunner<>(parser.TopLevel());
  ParsingResult<String> result = runner.run(wildcard);
  if (!result.matched) {
    throw new IllegalArgumentException("Unhandled input: " + wildcard);
  }
  return result.resultValue;
}
 
Example 3
Source File: AsPathRegex.java    From batfish with Apache License 2.0 5 votes vote down vote up
/** Converts the given Juniper AS Path regular expression to a Java regular expression. */
@Nonnull
public static String convertToJavaRegex(String regex) {
  AsPathRegex parser = Parboiled.createParser(AsPathRegex.class);
  BasicParseRunner<String> runner = new BasicParseRunner<>(parser.TopLevel());
  ParsingResult<String> result = runner.run(regex);
  if (!result.matched) {
    throw new IllegalArgumentException("Unhandled input: " + regex);
  }
  return result.resultValue;
}