Example usage for org.antlr.v4.runtime.tree ParseTree getParent

List of usage examples for org.antlr.v4.runtime.tree ParseTree getParent

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.tree ParseTree getParent.

Prototype

@Override
    ParseTree getParent();

Source Link

Usage

From source file:com.boothen.jsonedit.quickoutline.QuickOutlinePopup.java

License:Open Source License

@Override
public void setInput(Object input) {
    ParseTree parseTree = (ParseTree) input;
    treeViewer.setInput(new Container<ParseTree>(parseTree));
    filterText.setText("");

    // In theory, it should be possible to derive the parent,
    // but this is not trivial and needs to be reliable
    contentProvider.refreshParents(parseTree);

    final ISelection selection = sourceViewer.getSelectionProvider().getSelection();

    // try to find the element that contains the text cursor in sourceViewer and pre-select it
    if (selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        int start = textSelection.getOffset();
        int length = textSelection.getLength();

        PositionVisitor positionVisitor = new PositionVisitor();
        Map<ParseTree, Position> positions = parseTree.accept(positionVisitor);

        ParseTree element = parseTree.accept(new JsonContextTokenFinder(start, start + length, positions));
        // similar code exists in JsonContentOutlinePage
        while (element != null && !contentProvider.isKnown(element)) {
            element = element.getParent();
        }//ww  w . ja  v a  2  s .c  o m

        if (element != null) {
            treeViewer.reveal(element);
            treeViewer.setSelection(new TreeSelection(new TreePath(new Object[] { element })));
        }
    }

    treeViewer.expandAll();
}

From source file:com.cisco.yangide.core.parser.BasicValidations.java

License:Open Source License

static List<String> getAndCheckUniqueKeys(ParseTree ctx) {
    String key = ValidationUtil.getName(ctx);
    ParseTree parent = ctx.getParent();
    String rootParentName = ValidationUtil.getRootParentName(ctx);

    List<String> keyList = ValidationUtil.listKeysFromId(key);
    Set<String> duplicates = ValidationUtil.getDuplicates(keyList);

    if (duplicates.size() != 0) {
        ValidationUtil.ex(ctx,//  w ww .  j  a  va2 s  . c  o m
                ValidationUtil.f("(In (sub)module:%s) %s:%s, %s:%s contains duplicates:%s", rootParentName,
                        ValidationUtil.getSimpleStatementName(parent.getClass()),
                        ValidationUtil.getName(parent), ValidationUtil.getSimpleStatementName(ctx.getClass()),
                        key, duplicates));
    }
    return keyList;
}

From source file:com.cisco.yangide.core.parser.ValidationUtil.java

License:Open Source License

private static ParseTree getRootParent(ParseTree ctx) {
    ParseTree root = ctx;
    while (root.getParent() != null) {
        if (root.getClass().equals(Module_stmtContext.class)
                || root.getClass().equals(Submodule_stmtContext.class)) {
            break;
        }//  w  w w.  j a  v a  2  s. co m
        root = root.getParent();
    }
    return root;
}

From source file:com.github.gfx.android.orma.migration.sqliteparser.SQLiteParserUtils.java

License:Apache License

static void appendTokenList(final SQLiteComponent component, ParseTree node) {
    node.accept(new AbstractParseTreeVisitor<Void>() {
        @Override/*  w  w  w . j a v  a 2s  .  co  m*/
        public Void visitTerminal(TerminalNode node) {
            int type = node.getSymbol().getType();
            if (type == Token.EOF) {
                return null;
            }

            if (node.getParent() instanceof SQLiteParser.Any_nameContext) {
                component.tokens.add(new SQLiteComponent.Name(node.getText()));
            } else if (isKeyword(type)) {
                component.tokens.add(new SQLiteComponent.Keyword(node.getText()));
            } else {
                component.tokens.add(node.getText());
            }
            return null;
        }
    });
}

From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.java

License:Open Source License

/**
 * Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
 *
 * @param parseTreeNode root node of ParseTree
 * @return root of DetailNode tree// w  ww  .j  ava  2 s . c  o  m
 */
private DetailNode convertParseTreeToDetailNode(ParseTree parseTreeNode) {
    final JavadocNodeImpl rootJavadocNode = createRootJavadocNode(parseTreeNode);

    JavadocNodeImpl currentJavadocParent = rootJavadocNode;
    ParseTree parseTreeParent = parseTreeNode;

    while (currentJavadocParent != null) {
        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;
}

From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.java

License:Open Source License

/**
 * Gets next sibling of ParseTree node.//w  w  w.j  av a 2  s.  c o m
 * @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 i = 0;
        while (true) {
            final ParseTree currentNode = parent.getChild(i);
            if (currentNode.equals(node)) {
                if (i != childCount - 1) {
                    nextSibling = parent.getChild(i + 1);
                }
                break;
            }
            i++;
        }
    }
    return nextSibling;
}

From source file:com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.java

License:Open Source License

/**
 * Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
 *
 * @param parseTreeNode root node of ParseTree
 * @return root of DetailNode tree//from   ww  w. j  a  va  2 s .c o m
 */
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;
}

From source file:com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.java

License:Open Source License

/**
 * Gets next sibling of ParseTree node./*  ww  w  .  j  ava  2  s  .  com*/
 * @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;
}

From source file:de.codesourcery.luaparser.LuaToJSON.java

License:Apache License

private String printValue(ParseTree value) {
    if (value instanceof ExpContext) {
        return printValue((ExpContext) value);
    }//  w w w  .  ja  v  a 2s  .co m
    if (value instanceof StringContext) {
        return value.getText();
    }
    if (value instanceof NumberContext) {
        return value.getText();
    }
    if (value instanceof TableconstructorContext) {
        return printValue((TableconstructorContext) value, false);
    }
    if (value instanceof PrefixexpContext) {
        return "\"" + value.getText() + "\"";
    }
    if (value instanceof TerminalNodeImpl) {
        return "\"" + value.getText() + "\"";
    }
    throw new RuntimeException(
            "Unhandled value class: " + value.getClass().getName() + " = " + value.getParent().getText());
}

From source file:de.codesourcery.luaparser.LuaToJSON.java

License:Apache License

private TableconstructorContext findParentTableConstructor(ParseTree tree) {

    ParseTree current = tree;
    while (current != null && !(current instanceof TableconstructorContext)) {
        current = current.getParent();
    }/*from   www.jav a  2 s  .co m*/
    return (TableconstructorContext) current;
}