Search in sources :

Example 26 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project checkstyle by checkstyle.

the class JavadocParseTreeTest method oneSimpleHtmlTag.

@Test
public void oneSimpleHtmlTag() throws IOException {
    final String filename = getHtmlPath("InputOneSimpleHtmlTag.txt");
    final ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
    final ParseTree expectedTree = ParseTreeBuilder.treeOneSimpleHtmlTag();
    compareTrees(expectedTree, generatedTree);
}
Also used : File(java.io.File) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 27 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project checkstyle by checkstyle.

the class JavadocParseTreeTest method authorWithMailto.

@Test
public void authorWithMailto() throws IOException {
    final String filename = getDocPath("InputAuthorWithMailto.txt");
    final ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
    final ParseTree expectedTree = ParseTreeBuilder.treeAuthorWithMailto();
    compareTrees(expectedTree, generatedTree);
}
Also used : File(java.io.File) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 28 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project checkstyle by checkstyle.

the class JavadocParseTreeTest method textBeforeJavadocTags.

@Test
public void textBeforeJavadocTags() throws IOException {
    final String filename = getDocPath("InputTextBeforeJavadocTags.txt");
    final ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
    final ParseTree expectedTree = ParseTreeBuilder.treeTextBeforeJavadocTags();
    compareTrees(expectedTree, generatedTree);
}
Also used : File(java.io.File) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 29 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method getNextSibling.

/**
     * Gets next sibling of ParseTree node.
     * @param node ParseTree node
     * @return next sibling of ParseTree node.
     */
private static ParseTree getNextSibling(ParseTree node) {
    ParseTree nextSibling = null;
    if (node.getParent() != null) {
        final ParseTree parent = node.getParent();
        final int childCount = parent.getChildCount();
        int index = 0;
        while (true) {
            final ParseTree currentNode = parent.getChild(index);
            if (currentNode.equals(node)) {
                if (index != childCount - 1) {
                    nextSibling = parent.getChild(index + 1);
                }
                break;
            }
            index++;
        }
    }
    return nextSibling;
}
Also used : ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 30 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method convertParseTreeToDetailNode.

/**
     * Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
     *
     * @param parseTreeNode root node of ParseTree
     * @return root of DetailNode tree
     */
private DetailNode convertParseTreeToDetailNode(ParseTree parseTreeNode) {
    final JavadocNodeImpl rootJavadocNode = createRootJavadocNode(parseTreeNode);
    JavadocNodeImpl currentJavadocParent = rootJavadocNode;
    ParseTree parseTreeParent = parseTreeNode;
    while (currentJavadocParent != null) {
        // remove unnecessary children tokens
        if (currentJavadocParent.getType() == JavadocTokenTypes.TEXT) {
            currentJavadocParent.setChildren((DetailNode[]) JavadocNodeImpl.EMPTY_DETAIL_NODE_ARRAY);
        }
        final JavadocNodeImpl[] children = (JavadocNodeImpl[]) currentJavadocParent.getChildren();
        insertChildrenNodes(children, parseTreeParent);
        if (children.length > 0) {
            currentJavadocParent = children[0];
            parseTreeParent = parseTreeParent.getChild(0);
        } else {
            JavadocNodeImpl nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(currentJavadocParent);
            ParseTree nextParseTreeSibling = getNextSibling(parseTreeParent);
            if (nextJavadocSibling == null) {
                JavadocNodeImpl tempJavadocParent = (JavadocNodeImpl) currentJavadocParent.getParent();
                ParseTree tempParseTreeParent = parseTreeParent.getParent();
                while (nextJavadocSibling == null && tempJavadocParent != null) {
                    nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(tempJavadocParent);
                    nextParseTreeSibling = getNextSibling(tempParseTreeParent);
                    tempJavadocParent = (JavadocNodeImpl) tempJavadocParent.getParent();
                    tempParseTreeParent = tempParseTreeParent.getParent();
                }
            }
            currentJavadocParent = nextJavadocSibling;
            parseTreeParent = nextParseTreeSibling;
        }
    }
    return rootJavadocNode;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) JavadocNodeImpl(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)89 CommonToken (org.antlr.v4.runtime.CommonToken)32 Test (org.junit.Test)32 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)24 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)19 DescriptionContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.DescriptionContext)15 HtmlElementContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementContext)12 JavadocTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocTagContext)12 JavadocInlineTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocInlineTagContext)10 ReferenceContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ReferenceContext)10 Grammar (org.antlr.v4.tool.Grammar)10 HtmlElementCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementCloseContext)9 HtmlElementOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementOpenContext)9 HtmlTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlTagContext)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)8 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)7 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)7