Search in sources :

Example 21 with DetailNode

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

the class DetailNodeTreeStringPrinter method getIndentation.

/**
     * Get indentation for a node.
     * @param node the DetailNode to get the indentation for.
     * @return the indentation in String format.
     */
private static String getIndentation(DetailNode node) {
    final boolean isLastChild = JavadocUtils.getNextSibling(node) == null;
    DetailNode currentNode = node;
    final StringBuilder indentation = new StringBuilder();
    while (currentNode.getParent() != null) {
        currentNode = currentNode.getParent();
        if (currentNode.getParent() == null) {
            if (isLastChild) {
                // only ASCII symbols must be used due to
                // problems with running tests on Windows
                indentation.append("`--");
            } else {
                indentation.append("|--");
            }
        } else {
            if (JavadocUtils.getNextSibling(currentNode) == null) {
                indentation.insert(0, "    ");
            } else {
                indentation.insert(0, "|   ");
            }
        }
    }
    return indentation.toString();
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Example 22 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 23 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 24 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 25 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)

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