org.parboiled.parserunners.BasicParseRunner Java Examples

The following examples show how to use org.parboiled.parserunners.BasicParseRunner. 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;
}
 
Example #4
Source File: AbstractParserTest.java    From jtwig-core with Apache License 2.0 4 votes vote down vote up
protected <T> ParsingResult<T> parse(Rule rule, String input) {
    ParseRunner<T> handler = new BasicParseRunner<T>(rule);
    return handler.run(input);
}
 
Example #5
Source File: SQLParserUtils.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static Map<String, String> process(
    SQLParser parser,
    String sql,
    int type, // One of OracleCDCOperationCode constants.
    boolean allowNulls,
    boolean caseSensitive,
    Set<String> columnsExpected
) throws UnparseableSQLException {
  Rule parseRule;
  switch (type) {
    case OracleCDCOperationCode.INSERT_CODE:
       parseRule = parser.Insert();
       break;
    case OracleCDCOperationCode.UPDATE_CODE:
    case OracleCDCOperationCode.SELECT_FOR_UPDATE_CODE:
      parseRule = parser.Update();
      break;
    case OracleCDCOperationCode.DELETE_CODE:
      parseRule = parser.Delete();
      break;
    default:
      throw new UnparseableSQLException(sql);
  }
  ParseRunner<?> runner = new BasicParseRunner<>(parseRule);
  ParsingResult<?> result = runner.run(sql);
  if (!result.matched) {
    throw new UnparseableSQLException(sql);
  }
  Collection<Node<Object>> names = new ArrayList<>();

  ParseTreeUtils.collectNodes(
      (Node<Object>) result.parseTreeRoot,
      input -> input.getLabel().equals(COLUMN_NAME_RULE),
      names
  );

  Collection<Node<Object>> values = new ArrayList<>();
  ParseTreeUtils.collectNodes(
      (Node<Object>) result.parseTreeRoot,
      input -> input.getLabel().equals(COLUMN_VALUE_RULE),
      values
  );

  Map<String, String> colVals = new HashMap<>();
  for (int i = 0; i < names.size(); i++) {
    Node<?> name = ((ArrayList<Node<Object>>) names).get(i);
    Node<?> val = ((ArrayList<Node<Object>>) values).get(i);

    final String colName = sql.substring(name.getStartIndex(), name.getEndIndex());
    final String key = formatName(colName, caseSensitive);
    if (!colVals.containsKey(key)) {
      colVals.put(key, formatValue(sql.substring(val.getStartIndex(), val.getEndIndex())));
    }
  }
  if (allowNulls && columnsExpected != null) {
    columnsExpected.forEach(col -> colVals.putIfAbsent(col,  null));
  }
  return colVals;
}
 
Example #6
Source File: CommonParserTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static boolean matches(String query, Rule rule) {
  return new BasicParseRunner<AstNode>(rule).run(query).matched;
}