org.parboiled.errors.ParserRuntimeException Java Examples

The following examples show how to use org.parboiled.errors.ParserRuntimeException. 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: 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 #3
Source File: MarkdownDoclet.java    From markdown-doclet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Default processing of any documentation node.
 *
 * @param doc              The documentation.
 * @param fixLeadingSpaces `true` if leading spaces should be fixed.
 *
 * @see Options#toHtml(String, boolean)
 */
protected void defaultProcess(Doc doc, boolean fixLeadingSpaces) {
    try {
        StringBuilder buf = new StringBuilder();
        buf.append(getOptions().toHtml(doc.commentText(), fixLeadingSpaces));
        buf.append('\n');
        for ( Tag tag : doc.tags() ) {
            processTag(tag, buf);
            buf.append('\n');
        }
        doc.setRawCommentText(buf.toString());
    }
    catch ( final ParserRuntimeException e ) {
        if ( doc instanceof RootDoc ) {
            printError(new SourcePosition() {
                @Override
                public File file() {
                    return options.getOverviewFile();
                }
                @Override
                public int line() {
                    return 0;
                }
                @Override
                public int column() {
                    return 0;
                }
            }, e.getMessage());
        }
        else {
            printError(doc.position(), e.getMessage());
        }
    }
}
 
Example #4
Source File: ParboiledExceptionMessageExtractor.java    From jtwig-core with Apache License 2.0 4 votes vote down vote up
public String extract (ParserRuntimeException exception) {
    String[] split = exception.getMessage().split("\n");
    return split[1] + "\n" + split[2];
}
 
Example #5
Source File: ParserOneAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParseIcmpType_invalidType() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid ICMP type");
  ParserUtils.getAst(getRunner().run("icmp/257"));
}
 
Example #6
Source File: ParserOneAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParseIcmpTypeCode_invalidCode() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid ICMP type/code");
  ParserUtils.getAst(getRunner().run("icmp/8/1"));
}
 
Example #7
Source File: ParserOneAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsePort_invalid() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid port number");
  ParserUtils.getAst(getRunner().run("tcp/808080"));
}
 
Example #8
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParseIcmpType_invalidType() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid ICMP type");
  ParserUtils.getAst(getRunner().run("icmp/257"));
}
 
Example #9
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParseIcmpTypeCode_invalidCode() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid ICMP type/code");
  ParserUtils.getAst(getRunner().run("icmp/8/1"));
}
 
Example #10
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsePort_invalid() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid port number");
  ParserUtils.getAst(getRunner().run("tcp/808080"));
}
 
Example #11
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsePortRange_invalid1() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid port number");
  ParserUtils.getAst(getRunner().run("tcp/80 - 808080"));
}
 
Example #12
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsePortRange_invalid2() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid port number");
  ParserUtils.getAst(getRunner().run("tcp/808080 - 80"));
}
 
Example #13
Source File: ParserAppTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsePortRange_invalid3() {
  _thrown.expect(ParserRuntimeException.class);
  _thrown.expectMessage("Invalid port range");
  ParserUtils.getAst(getRunner().run("tcp/82 - 80"));
}
 
Example #14
Source File: ParserIpSpaceTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testIpSpaceIpAddressFail() {
  _thrown.expectMessage("1111 is an invalid octet");
  _thrown.expect(ParserRuntimeException.class);
  getRunner().run("1.1.1.1111");
}
 
Example #15
Source File: ParserIpSpaceTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testIpSpacePrefixFail() {
  _thrown.expectMessage("Invalid prefix length");
  _thrown.expect(ParserRuntimeException.class);
  getRunner().run("1.1.1.1/33");
}