org.parboiled.parserunners.ReportingParseRunner Java Examples

The following examples show how to use org.parboiled.parserunners.ReportingParseRunner. 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: ParboiledNodeSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
static NodeAstNode getAst(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(Parser.instance().getInputRule(Grammar.NODE_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.NODE_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof NodeAstNode, "Unexpected AST for nodeSpec input '%s': '%s'", input, ast);

  return (NodeAstNode) ast;
}
 
Example #2
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 #3
Source File: ParboiledAutoCompleteTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
/** Test that we produce auto completion suggestions even for valid inputs */
@Test
public void testRunValidInput() {
  String query = "(1.1.1.1)"; //

  // first ensure that the query is valid input
  ParsingResult<?> result =
      new ReportingParseRunner<>(TestParser.instance().input(TestParser.instance().TestSpec()))
          .run(query);
  assertTrue(result.parseErrors.isEmpty());

  // commma is the only viable auto completion after a valid input
  assertThat(
      getTestPAC(query).run(),
      containsInAnyOrder(new ParboiledAutoCompleteSuggestion(",", 9, NODE_SET_OP)));
}
 
Example #4
Source File: ParserUtilsTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetErrorStringHasUrl() {
  String input = new String(Character.toChars(0x26bd)); // invalid input

  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.IP_SPACE_SPECIFIER))
          .run(input);

  String errorString =
      ParserUtils.getErrorString(
          input,
          Grammar.IP_SPACE_SPECIFIER,
          (InvalidInputError) result.parseErrors.get(0),
          Parser.ANCHORS);

  assertThat(errorString, containsString(Grammar.IP_SPACE_SPECIFIER.getFullUrl()));
}
 
Example #5
Source File: ParboiledAutoComplete.java    From batfish with Apache License 2.0 6 votes vote down vote up
private Set<PotentialMatch> getPotentialMatches(String query) {
  /**
   * Before passing the query to the parser, we make it illegal by adding a funny, non-ascii
   * character (soccer ball :)). We will not get any errors backs if the string is legal.
   */
  String testQuery = query + new String(Character.toChars(ILLEGAL_CHAR));

  ParsingResult<AstNode> result = new ReportingParseRunner<AstNode>(_inputRule).run(testQuery);
  if (result.parseErrors.isEmpty()) {
    throw new IllegalStateException("Failed to force erroneous input");
  }

  InvalidInputError error = (InvalidInputError) result.parseErrors.get(0);

  return ParserUtils.getPotentialMatches(error, _completionTypes, false);
}
 
Example #6
Source File: ParboiledLocationSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link LocationSpecifier} based on {@code input} which is parsed as {@link
 * Grammar#LOCATION_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledLocationSpecifier parse(String input) {

  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.LOCATION_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.LOCATION_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof LocationAstNode,
      "ParboiledLocationSpecifier requires a LocationSpecifier input");

  return new ParboiledLocationSpecifier((LocationAstNode) ast);
}
 
Example #7
Source File: ParboiledInterfaceSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link InterfaceSpecifier} based on {@code input} which is parsed as {@link
 * Grammar#INTERFACE_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledInterfaceSpecifier parse(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.INTERFACE_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.INTERFACE_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof InterfaceAstNode,
      "ParboiledInterfaceSpecifier requires an InterfaceSpecifier input");

  return new ParboiledInterfaceSpecifier((InterfaceAstNode) ast);
}
 
Example #8
Source File: ParboiledNameSetSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link NameSetSpecifier} based on parsing the {@code input} according to the
 * specified grammar
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledNameSetSpecifier parse(String input, Grammar grammar) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(Parser.instance().getInputRule(grammar)).run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input, grammar, (InvalidInputError) result.parseErrors.get(0), Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof NameSetAstNode,
      "Unexpected AST when parsing '%s' as enum grammar: %s",
      input,
      ast);

  return new ParboiledNameSetSpecifier((NameSetAstNode) ast, grammar);
}
 
Example #9
Source File: ParboiledEnumSetSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link EnumSetSpecifier} based on parsing the {@code input} according to the
 * specified grammar
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static <T> ParboiledEnumSetSpecifier<T> parse(String input, Grammar grammar) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(Parser.instance().getInputRule(grammar)).run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input, grammar, (InvalidInputError) result.parseErrors.get(0), Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof EnumSetAstNode,
      "Unexpected AST when parsing '%s' as enum grammar: %s",
      input,
      ast);

  return new ParboiledEnumSetSpecifier<>((EnumSetAstNode) ast, grammar);
}
 
Example #10
Source File: ParboiledIpSpaceSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link IpSpaceSpecifier} based on {@code input} which is parsed as {@link
 * Grammar#IP_SPACE_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledIpSpaceSpecifier parse(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.IP_SPACE_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.IP_SPACE_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof IpSpaceAstNode, "ParboiledIpSpaceSpecifier requires an IpSpace input");

  return new ParboiledIpSpaceSpecifier((IpSpaceAstNode) ast);
}
 
Example #11
Source File: ParboiledAppSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link ApplicationSpecifier} based on {@code input} which is parsed as {@link
 * Grammar#APPLICATION_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledAppSpecifier parse(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.APPLICATION_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.APPLICATION_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof AppAstNode, "ParboiledAppSpecifier requires an IP protocol specifier input");

  return new ParboiledAppSpecifier((AppAstNode) ast);
}
 
Example #12
Source File: ParboiledFilterSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link FilterSpecifier} based on {@code input} which is parsed as {@link
 * Grammar#FILTER_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledFilterSpecifier parse(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(Parser.instance().getInputRule(Grammar.FILTER_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.FILTER_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof FilterAstNode, "ParboiledFilterSpecifier requires an FilterSpecifier input");

  return new ParboiledFilterSpecifier((FilterAstNode) ast);
}
 
Example #13
Source File: ParboiledIpProtocolSpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link IpProtocolSpecifier} based on {@code input} which is parsed as {@link
 * Grammar#IP_PROTOCOL_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledIpProtocolSpecifier parse(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.IP_PROTOCOL_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.IP_PROTOCOL_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);
  checkArgument(
      ast instanceof IpProtocolAstNode,
      "ParboiledIpProtocolSpecifier requires an IP protocol specifier input");

  return new ParboiledIpProtocolSpecifier((IpProtocolAstNode) ast);
}
 
Example #14
Source File: ParboiledRoutingPolicySpecifier.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link RoutingPolicySpecifier} based on {@code input} which is parsed as {@link
 * Grammar#ROUTING_POLICY_SPECIFIER}.
 *
 * @throws IllegalArgumentException if the parsing fails or does not produce the expected AST
 */
public static ParboiledRoutingPolicySpecifier parse(String input) {
  ParsingResult<AstNode> result =
      new ReportingParseRunner<AstNode>(
              Parser.instance().getInputRule(Grammar.ROUTING_POLICY_SPECIFIER))
          .run(input);

  if (!result.parseErrors.isEmpty()) {
    throw new IllegalArgumentException(
        ParserUtils.getErrorString(
            input,
            Grammar.ROUTING_POLICY_SPECIFIER,
            (InvalidInputError) result.parseErrors.get(0),
            Parser.ANCHORS));
  }

  AstNode ast = ParserUtils.getAst(result);

  checkArgument(
      ast instanceof RoutingPolicyAstNode,
      "ParboiledRoutingPolicySpecifier requires an RoutingPolicySpecifier input");

  return new ParboiledRoutingPolicySpecifier((RoutingPolicyAstNode) ast);
}
 
Example #15
Source File: ParboiledInputValidator.java    From batfish with Apache License 2.0 5 votes vote down vote up
/** This is the entry point for all validations */
InputValidationNotes run() {

  ParsingResult<AstNode> result;
  try {
    result = new ReportingParseRunner<AstNode>(_inputRule).run(_query);
  } catch (ParserRuntimeException e) {
    if (e.getCause() instanceof IllegalArgumentException) {
      return new InputValidationNotes(
          Validity.INVALID, getErrorMessage((IllegalArgumentException) e.getCause()), -1);
    } else {
      return new InputValidationNotes(Validity.INVALID, e.getMessage(), -1);
    }
  }

  if (!result.parseErrors.isEmpty()) {
    InvalidInputError error = (InvalidInputError) result.parseErrors.get(0);
    return new InputValidationNotes(
        Validity.INVALID,
        getErrorMessage(_grammar.getFriendlyName(), error.getStartIndex()),
        error.getStartIndex());
  }

  AstNode resultAst = ParserUtils.getAst(result);
  List<String> noMatchMessages = noMatchMessages(resultAst);
  if (!noMatchMessages.isEmpty()) {
    return new InputValidationNotes(Validity.NO_MATCH, noMatchMessages.get(0));
  }

  Set<String> expansions = expand(resultAst);
  return new InputValidationNotes(Validity.VALID, ImmutableList.copyOf(expansions));
}
 
Example #16
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);
}
 
Example #17
Source File: TestBase.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
TestBase() {
    parser = Parboiled.createParser(CfgPropsParboiled.class);
    tracingRunner = new TracingParseRunner(parser.cfgProps());
    reportingRunner = new ReportingParseRunner(parser.cfgProps());
}
 
Example #18
Source File: JidCorpusParser.java    From jxmpp with Apache License 2.0 4 votes vote down vote up
private ReportingParseRunner<J> getReportingParserRunner() {
	Rule parserRootRule = getParserRootRule();
	ReportingParseRunner<J> runner = new ReportingParseRunner<>(parserRootRule);
	return runner;
}
 
Example #19
Source File: ParserNameSetTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner(Grammar grammar) {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(grammar));
}
 
Example #20
Source File: ParserIpSpaceTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(Grammar.IP_SPACE_SPECIFIER));
}
 
Example #21
Source File: ParserUtilsTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(TestParser.instance().getInputRule());
}
 
Example #22
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(
      Parser.instance().getInputRule(Grammar.APPLICATION_SPECIFIER));
}
 
Example #23
Source File: ParserFilterTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(Grammar.FILTER_SPECIFIER));
}
 
Example #24
Source File: ParserInterfaceTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(Grammar.INTERFACE_SPECIFIER));
}
 
Example #25
Source File: ParserEnumSetTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner(Collection<?> allValues) {
  Parser parser = Parser.instance();
  return new ReportingParseRunner<>(parser.input(parser.EnumSetSpec(allValues)));
}
 
Example #26
Source File: ParserEnumSetTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner(Grammar grammar) {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(grammar));
}
 
Example #27
Source File: ParserRoutingPolicyTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(
      Parser.instance().getInputRule(Grammar.ROUTING_POLICY_SPECIFIER));
}
 
Example #28
Source File: ParserLocationTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(Grammar.LOCATION_SPECIFIER));
}
 
Example #29
Source File: ParserOneAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(
      Parser.instance().getInputRule(Grammar.SINGLE_APPLICATION_SPECIFIER));
}
 
Example #30
Source File: ParserNodeTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static AbstractParseRunner<AstNode> getRunner() {
  return new ReportingParseRunner<>(Parser.instance().getInputRule(Grammar.NODE_SPECIFIER));
}