Search in sources :

Example 1 with JavadocLexer

use of com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method parseJavadocAsParseTree.

/**
     * Parses block comment content as javadoc comment.
     * @param blockComment
     *        block comment content.
     * @return parse tree
     */
private ParseTree parseJavadocAsParseTree(String blockComment) {
    final ANTLRInputStream input = new ANTLRInputStream(blockComment);
    final JavadocLexer lexer = new JavadocLexer(input);
    // remove default error listeners
    lexer.removeErrorListeners();
    // add custom error listener that logs parsing errors
    lexer.addErrorListener(errorListener);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final JavadocParser parser = new JavadocParser(tokens);
    // remove default error listeners
    parser.removeErrorListeners();
    // add custom error listener that logs syntax errors
    parser.addErrorListener(errorListener);
    // This strategy stops parsing when parser error occurs.
    // By default it uses Error Recover Strategy which is slow and useless.
    parser.setErrorHandler(new BailErrorStrategy());
    return parser.javadoc();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavadocParser(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) JavadocLexer(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 2 with JavadocLexer

use of com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer in project contribution by checkstyle.

the class ExpectedParseTreeGenerator method parseJavadocFromFile.

private static ParseTree parseJavadocFromFile(File file) throws IOException {
    final String content = Files.toString(file, Charsets.UTF_8);
    final InputStream in = new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));
    final ANTLRInputStream input = new ANTLRInputStream(in);
    final JavadocLexer lexer = new JavadocLexer(input);
    lexer.removeErrorListeners();
    final BaseErrorListener errorListener = new FailOnErrorListener();
    lexer.addErrorListener(errorListener);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final JavadocParser parser = new JavadocParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    return parser.javadoc();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavadocParser(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser) ByteArrayInputStream(java.io.ByteArrayInputStream) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JavadocLexer(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

JavadocLexer (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer)2 JavadocParser (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)1