Search in sources :

Example 11 with DetailNode

use of com.puppycrawl.tools.checkstyle.api.DetailNode in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method createRootJavadocNode.

/**
     * Creates root JavadocNodeImpl node base on ParseTree root node.
     * @param parseTreeNode ParseTree root node
     * @return root Javadoc node
     */
private JavadocNodeImpl createRootJavadocNode(ParseTree parseTreeNode) {
    final JavadocNodeImpl rootJavadocNode = createJavadocNode(parseTreeNode, null, -1);
    final int childCount = parseTreeNode.getChildCount();
    final JavadocNodeImpl[] children = new JavadocNodeImpl[childCount];
    for (int i = 0; i < childCount; i++) {
        final JavadocNodeImpl child = createJavadocNode(parseTreeNode.getChild(i), rootJavadocNode, i);
        children[i] = child;
    }
    rootJavadocNode.setChildren((DetailNode[]) children);
    return rootJavadocNode;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) JavadocNodeImpl(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl)

Example 12 with DetailNode

use of com.puppycrawl.tools.checkstyle.api.DetailNode in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method insertChildrenNodes.

/**
     * Creates child nodes for each node from 'nodes' array.
     * @param parseTreeParent original ParseTree parent node
     * @param nodes array of JavadocNodeImpl nodes
     */
private void insertChildrenNodes(final JavadocNodeImpl[] nodes, ParseTree parseTreeParent) {
    for (int i = 0; i < nodes.length; i++) {
        final JavadocNodeImpl currentJavadocNode = nodes[i];
        final ParseTree currentParseTreeNodeChild = parseTreeParent.getChild(i);
        final JavadocNodeImpl[] subChildren = createChildrenNodes(currentJavadocNode, currentParseTreeNodeChild);
        currentJavadocNode.setChildren((DetailNode[]) subChildren);
    }
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) JavadocNodeImpl(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 13 with DetailNode

use of com.puppycrawl.tools.checkstyle.api.DetailNode 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 = JavadocUtils.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.
    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 ParseTree parseTree = parseJavadocAsParseTree(javadocComment);
        final DetailNode tree = convertParseTreeToDetailNode(parseTree);
        // adjust first line to indent of /**
        adjustFirstLineToJavadocIndent(tree, javadocCommentAst.getColumnNo() + JAVADOC_START.length());
        result.setTree(tree);
    } catch (ParseCancellationException | IllegalArgumentException ex) {
        // 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 parseErrorMessage = errorListener.getErrorMessage();
        // There are cases when antlr error listener does not handle syntax error
        if (parseErrorMessage == null) {
            parseErrorMessage = new ParseErrorMessage(javadocCommentAst.getLineNo(), MSG_KEY_UNRECOGNIZED_ANTLR_ERROR, javadocCommentAst.getColumnNo(), ex.getMessage());
        }
        result.setParseErrorMessage(parseErrorMessage);
    }
    return result;
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 14 with DetailNode

use of com.puppycrawl.tools.checkstyle.api.DetailNode in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method createJavadocNode.

/**
     * Creates JavadocNodeImpl node on base of ParseTree node.
     *
     * @param parseTree ParseTree node
     * @param parent DetailNode that will be parent of new node
     * @param index child index that has new node
     * @return JavadocNodeImpl node on base of ParseTree node.
     */
private JavadocNodeImpl createJavadocNode(ParseTree parseTree, DetailNode parent, int index) {
    final JavadocNodeImpl node = new JavadocNodeImpl();
    node.setText(parseTree.getText());
    node.setColumnNumber(getColumn(parseTree));
    node.setLineNumber(getLine(parseTree) + blockCommentLineNumber);
    node.setIndex(index);
    node.setType(getTokenType(parseTree));
    node.setParent(parent);
    node.setChildren((DetailNode[]) new JavadocNodeImpl[parseTree.getChildCount()]);
    return node;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) JavadocNodeImpl(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl)

Example 15 with DetailNode

use of com.puppycrawl.tools.checkstyle.api.DetailNode in project checkstyle by checkstyle.

the class AstTreeStringPrinter method parseAndPrintJavadocTree.

/**
     * Parses block comment as javadoc and prints its tree.
     * @param node block comment begin
     * @return string javadoc tree
     */
private static String parseAndPrintJavadocTree(DetailAST node) {
    final DetailAST javadocBlock = node.getParent();
    final DetailNode tree = DetailNodeTreeStringPrinter.parseJavadocAsDetailNode(javadocBlock);
    String baseIndentation = getIndentation(node);
    baseIndentation = baseIndentation.substring(0, baseIndentation.length() - 2);
    final String rootPrefix = baseIndentation + "   `--";
    final String prefix = baseIndentation + "       ";
    return DetailNodeTreeStringPrinter.printTree(tree, rootPrefix, prefix);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Aggregations

DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)26 JavadocNodeImpl (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl)5 Test (org.junit.Test)5 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)4 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 JavadocDetailNodeParser (com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser)1 ArrayList (java.util.ArrayList)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1