/** * Gets last child of a parse tree node. * * @param ctx A node * @return Last child of a node, or null if node has no children */ public static ParseTree getLastChild(ParseTree ctx) { if (ctx.getChildCount() == 0) { return null; } return ctx.getChild(ctx.getChildCount() - 1); }
private ParseTree findChildrenByType(ParseTree parseTree, Class<?> classtype) { for (int i = 0; i < parseTree.getChildCount(); i++) { ParseTree chl = parseTree.getChild(i); if (chl.getClass().equals(classtype)) { return chl; } } return null; }
/** * Creates children Javadoc nodes base on ParseTree node's children. * @param parentJavadocNode node that will be parent for created children * @param parseTreeNode original ParseTree node * @return array of Javadoc nodes */ private JavadocNodeImpl[] createChildrenNodes(JavadocNodeImpl parentJavadocNode, ParseTree parseTreeNode) { final JavadocNodeImpl[] children = new JavadocNodeImpl[parseTreeNode.getChildCount()]; for (int j = 0; j < children.length; j++) { final JavadocNodeImpl child = createJavadocNode(parseTreeNode.getChild(j), parentJavadocNode, j); children[j] = child; } return children; }
static List<TerminalNode> terminals(ParseTree context) { List<TerminalNode> terminals = new ArrayList<>(); for (int i = 0; i < context.getChildCount(); i++) { if (context.getChild(i) instanceof TerminalNode) { terminals.add((TerminalNode) context.getChild(i)); } else { terminals.addAll(terminals(context.getChild(i))); } } return terminals; }
/** * Gets token type of ParseTree node from JavadocTokenTypes class. * @param node ParseTree node. * @return token type from JavadocTokenTypes */ private static int getTokenType(ParseTree node) { final int tokenType; if (node.getChildCount() == 0) { tokenType = ((TerminalNode) node).getSymbol().getType(); } else { final String className = getNodeClassNameWithoutContext(node); final String typeName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, className); tokenType = JavadocUtil.getTokenId(typeName); } return tokenType; }
/** * Gets right sibling of a parse tree node. * * @param ctx A node * @return Right sibling of a node, or null if no sibling is found */ public static ParseTree getRightSibling(ParseTree ctx) { int index = ParseTreeUtil.getNodeIndex(ctx); ParseTree parent = ctx.getParent(); if (index < 0 || index >= parent.getChildCount() - 1) { return null; } return parent.getChild(index + 1); }
/** * Returns node's index with in its parent's child array. * * @param node A child node * @return Node's index or -1 if node is null or doesn't have a parent */ public static int getNodeIndex(ParseTree node) { if (node == null || node.getParent() == null) { return -1; } ParseTree parent = node.getParent(); for (int i = 0; i < parent.getChildCount(); i++) { if (parent.getChild(i) == node) { return i; } } return -1; }
/** * 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 DetailNode[] children = rootJavadocNode.getChildren(); for (int i = 0; i < childCount; i++) { final JavadocNodeImpl child = createJavadocNode(parseTreeNode.getChild(i), rootJavadocNode, i); children[i] = child; } rootJavadocNode.setChildren(children); return rootJavadocNode; }
@Override public void enterLimit(@NotNull PQL2Parser.LimitContext ctx) { // Can either be LIMIT <maxRows> or LIMIT <offset>, <maxRows> (the second is a MySQL syntax extension) if (ctx.getChild(0).getChildCount() == 2) { pushNode(new LimitAstNode(Integer.parseInt(ctx.getChild(0).getChild(1).getText()))); } else { pushNode(new LimitAstNode(Integer.parseInt(ctx.getChild(0).getChild(3).getText()), Integer.parseInt(ctx.getChild(0).getChild(1).getText()))); } }
/** * 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(); if (parseTree.getChildCount() == 0 || "Text".equals(getNodeClassNameWithoutContext(parseTree))) { node.setText(parseTree.getText()); } else { node.setText(getFormattedNodeClassNameWithoutContext(parseTree)); } 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; }
@Override public void enterOperatorDeclaration(SwiftParser.OperatorDeclarationContext ctx) { for (int i = 0; i < ctx.getChild(0).getChildCount(); i++) { if (ctx.getChild(0).getChild(i) instanceof SwiftParser.OperatorContext) { SwiftParser.OperatorContext op = (SwiftParser.OperatorContext) ctx.getChild(0).getChild(i); Token before = ParseTreeUtil.getStopTokenForNode(ctx.getChild(0).getChild(i - 1)); if (verifier.checkLeftSpaces(before, op.getStart(), 1)) { printer.error(Rules.OPERATOR_WHITESPACE, Messages.OPERATOR + Messages.SPACE_BEFORE, ListenerUtil.getContextStartLocation(op)); } // TODO: Verify one space after colon for infix operator declarations } } } }
private Object getPayload(ParseTree tree) { if (tree.getChildCount() == 0) { return tree.getPayload(); } else { String ruleName = tree.getClass().getSimpleName().replace("Context", ""); return Character.toLowerCase(ruleName.charAt(0)) + ruleName.substring(1); } }
/** Get all descendents; includes t itself. * * @since 4.5.1 */ public static List<ParseTree> getDescendants(ParseTree t) { List<ParseTree> nodes = new ArrayList<ParseTree>(); nodes.add(t); int n = t.getChildCount(); for (int i = 0 ; i < n ; i++){ nodes.addAll(getDescendants(t.getChild(i))); } return nodes; }
ParseTree chld = arg0.getParent().getChild(arg0.getParent().getChildCount() - 1); if (!chld.equals(arg0)) return; addQuery(classUtils.expandExpression(arg0.getParent().getText(), registry, classUtils.DO_NON_STATIC), cursor);
private static void walk(ParseTree tree, AST ast) { if (tree.getChildCount() == 0) { new AST(ast, tree); } else if (tree.getChildCount() == 1) { walk(tree.getChild(0), ast); } else if (tree.getChildCount() > 1) { for (int i = 0; i < tree.getChildCount(); i++) { AST temp = new AST(ast, tree.getChild(i)); if (!(temp.payload instanceof Token)) { walk(tree.getChild(i), temp); } } } }
/** * Return a list of all nodes starting at {@code t} as root that satisfy the * path. The root {@code /} is relative to the node passed to * {@link #evaluate}. */ public Collection<ParseTree> evaluate(final ParseTree t) { ParserRuleContext dummyRoot = new ParserRuleContext(); dummyRoot.children = Collections.singletonList(t); // don't set t's parent. Collection<ParseTree> work = Collections.<ParseTree>singleton(dummyRoot); int i = 0; while ( i < elements.length ) { Collection<ParseTree> next = new LinkedHashSet<ParseTree>(); for (ParseTree node : work) { if ( node.getChildCount()>0 ) { // only try to match next element if it has children // e.g., //func/*/stat might have a token node for which // we can't go looking for stat nodes. Collection<? extends ParseTree> matching = elements[i].evaluate(node); next.addAll(matching); } } i++; work = next; } return work; } }
ctx.getChild(0) instanceof TypeDeclarationContext && ctx.getChild(3) instanceof ExpressionContext && ctx.getChild(3).getChildCount() > 0) { if (!ctx.getChild(1).getText().contains(".")) { if (ctx.getChild(3).getChild(0) instanceof PathExpressionContext) {
public static void _findAllNodes(ParseTree t, int index, boolean findTokens, List<? super ParseTree> nodes) { // check this node (the root) first if ( findTokens && t instanceof TerminalNode ) { TerminalNode tnode = (TerminalNode)t; if ( tnode.getSymbol().getType()==index ) nodes.add(t); } else if ( !findTokens && t instanceof ParserRuleContext ) { ParserRuleContext ctx = (ParserRuleContext)t; if ( ctx.getRuleIndex() == index ) nodes.add(t); } // check children for (int i = 0; i < t.getChildCount(); i++){ _findAllNodes(t.getChild(i), index, findTokens, nodes); } }
if (currentNode.getChildCount() > 0) { nodeStack.push(currentNode); indexStack.push(currentIndex);
/** Find smallest subtree of t enclosing range startTokenIndex..stopTokenIndex * inclusively using postorder traversal. Recursive depth-first-search. * * @since 4.5.1 */ public static ParserRuleContext getRootOfSubtreeEnclosingRegion(ParseTree t, int startTokenIndex, // inclusive int stopTokenIndex) // inclusive { int n = t.getChildCount(); for (int i = 0; i<n; i++) { ParseTree child = t.getChild(i); ParserRuleContext r = getRootOfSubtreeEnclosingRegion(child, startTokenIndex, stopTokenIndex); if ( r!=null ) return r; } if ( t instanceof ParserRuleContext ) { ParserRuleContext r = (ParserRuleContext) t; if ( startTokenIndex>=r.getStart().getTokenIndex() && // is range fully contained in t? (r.getStop()==null || stopTokenIndex<=r.getStop().getTokenIndex()) ) { // note: r.getStop()==null likely implies that we bailed out of parser and there's nothing to the right return r; } } return null; }