Fuzzer.java 文件源码

java
阅读 21 收藏 0 点赞 0 评论 0

项目:abnffuzzer 作者:
/**
 * Create a new {@code Fuzzer} by reading ABNF rules from a reader.
 *
 * @param rules
 *            ANBF rules
 * @throws IOException
 *             if the rules can't be read
 */
@SuppressWarnings("serial")
public Fuzzer(final Reader rules) throws IOException {
    final AbnfLexer l = new AbnfLexer(new ANTLRInputStream(rules));
    final CommonTokenStream tokens = new CommonTokenStream(l);
    final AbnfParser p = new AbnfParser(tokens);
    p.setBuildParseTree(true);
    p.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer,
                final Object offendingSymbol, final int line,
                final int charPositionInLine, final String msg,
                final RecognitionException e) {
            throw new IllegalStateException(
                    "failed to parse at line " + line + " due to " + msg,
                    e);
        }
    });

    final ParseTree tree = p.rulelist();
    ruleList = Collections.unmodifiableMap(new RuleList() {
        {
            for (int i = 0; i < tree.getChildCount(); i++) {
                final ParseTree child = tree.getChild(i);
                if (child instanceof Rule_Context
                        && child.getChildCount() == 3) {
                    // rule definition
                    final ParseTree name = child.getChild(0);
                    if (!(name instanceof TerminalNode)) {
                        throw new IllegalArgumentException();
                    }
                    final ParseTree equalSign = child.getChild(1);
                    if (!(equalSign instanceof TerminalNode)
                            || !"=".equals(equalSign.toString())) {
                        throw new IllegalArgumentException();
                    }
                    final ParseTree elements = child.getChild(2);
                    if (!(elements instanceof ElementsContext)) {
                        throw new IllegalArgumentException();
                    }

                    put(name.toString(),
                            new Rule((ElementsContext) elements));
                }
            }
        }
    });
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号