Can I convert ParseTree to CommonTree to ANTLR?

I want to build ParseTree subtrees, but the only class that contains the insertChild (index, Object) method seems to be CommonTree, and I cannot pass an object of type RuleContext because it tries to convert it to a Tree object and I get "java.lang .ClassCastException: Java8Parser $ ClassDeclarationContext could not be passed to the org.antlr.runtime.tree.Tree exception.

Any ideas? Thank you (Edit: code added)

private void getSubtrees(ParseTree tree){
    CommonTree commonTree = new CommonTree();
    setRecursiveDescendants(commonTree, tree);
}

private CommonTree setRecursiveDescendants(CommonTree commonTree,ParseTree parseTree){
    int n = parseTree.getChildCount();
        for (int i = 0; i < n; i++) {
            commonTree.insertChild(i, parseTree.getChild(i);
            setRecursiveDescendants(commonTree, parseTree.getChild(i));
        }
        return commonTree;
}
+4
source share

All Articles