Search in sources :

Example 1 with JavadocParser

use of com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method createJavadocParser.

/**
 * Parses block comment content as javadoc comment.
 *
 * @param blockComment
 *        block comment content.
 * @param errorListener custom error listener
 * @return parse tree
 */
private static JavadocParser createJavadocParser(String blockComment, DescriptiveErrorListener errorListener) {
    final JavadocLexer lexer = new JavadocLexer(CharStreams.fromString(blockComment), true);
    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);
    // JavadocParserErrorStrategy stops parsing on first parse error encountered unlike the
    // DefaultErrorStrategy used by ANTLR which rather attempts error recovery.
    parser.setErrorHandler(new CheckstyleParserErrorStrategy());
    return parser;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavadocParser(com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser) JavadocLexer(com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocLexer)

Example 2 with JavadocParser

use of com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method parseJavadocAsDetailNode.

/**
 * Parses Javadoc comment as DetailNode tree.
 *
 * @param javadocCommentAst
 *        DetailAST of Javadoc comment
 * @return DetailNode tree of Javadoc comment
 */
public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
    blockCommentLineNumber = javadocCommentAst.getLineNo();
    final String javadocComment = JavadocUtil.getJavadocCommentContent(javadocCommentAst);
    // Use a new error listener each time to be able to use
    // one check instance for multiple files to be checked
    // without getting side effects.
    final DescriptiveErrorListener errorListener = new DescriptiveErrorListener();
    // Log messages should have line number in scope of file,
    // not in scope of Javadoc comment.
    // Offset is line number of beginning of Javadoc comment.
    errorListener.setOffset(javadocCommentAst.getLineNo() - 1);
    final ParseStatus result = new ParseStatus();
    try {
        final JavadocParser javadocParser = createJavadocParser(javadocComment, errorListener);
        final ParseTree javadocParseTree = javadocParser.javadoc();
        final DetailNode tree = convertParseTreeToDetailNode(javadocParseTree);
        // adjust first line to indent of /**
        adjustFirstLineToJavadocIndent(tree, javadocCommentAst.getColumnNo() + JAVADOC_START.length());
        result.setTree(tree);
        result.firstNonTightHtmlTag = getFirstNonTightHtmlTag(javadocParser, errorListener.offset);
    } catch (ParseCancellationException | IllegalArgumentException ex) {
        ParseErrorMessage parseErrorMessage = null;
        if (ex.getCause() instanceof FailedPredicateException || ex.getCause() instanceof NoViableAltException) {
            final RecognitionException recognitionEx = (RecognitionException) ex.getCause();
            if (recognitionEx.getCtx() instanceof JavadocParser.HtmlTagContext) {
                final Token htmlTagNameStart = getMissedHtmlTag(recognitionEx);
                parseErrorMessage = new ParseErrorMessage(errorListener.offset + htmlTagNameStart.getLine(), MSG_JAVADOC_MISSED_HTML_CLOSE, htmlTagNameStart.getCharPositionInLine(), htmlTagNameStart.getText());
            }
        }
        if (parseErrorMessage == null) {
            // If syntax error occurs then message is printed by error listener
            // and parser throws this runtime exception to stop parsing.
            // Just stop processing current Javadoc comment.
            parseErrorMessage = errorListener.getErrorMessage();
        }
        result.setParseErrorMessage(parseErrorMessage);
    }
    return result;
}
Also used : Token(org.antlr.v4.runtime.Token) CommonToken(org.antlr.v4.runtime.CommonToken) JavadocParser(com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) FailedPredicateException(org.antlr.v4.runtime.FailedPredicateException) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

JavadocParser (com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser)2 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)1 JavadocLexer (com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocLexer)1 CommonToken (org.antlr.v4.runtime.CommonToken)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 FailedPredicateException (org.antlr.v4.runtime.FailedPredicateException)1 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 Token (org.antlr.v4.runtime.Token)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1