/** * 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); }
/** * 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(); int index = 0; while (true) { final ParseTree currentNode = parent.getChild(index); if (currentNode.equals(node)) { nextSibling = parent.getChild(index + 1); break; } index++; } } return nextSibling; }
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 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); } }
/** * 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; }
/** * Gets left sibling of a parse tree node. * * @param ctx A node * @return Left sibling of a node, or null if no sibling is found */ public static ParseTree getLeftSibling(ParseTree ctx) { int index = ParseTreeUtil.getNodeIndex(ctx); if (index < 1) { return null; } return ctx.getParent().getChild(index - 1); }
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; }
@Override public void enterInPredicate(@NotNull PQL2Parser.InPredicateContext ctx) { boolean isNotInClause = false; if ("not".equalsIgnoreCase(ctx.getChild(0).getChild(1).getText())) { isNotInClause = true; } pushNode(new InPredicateAstNode(isNotInClause)); }
/** * 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); }
@Override public void enterParameterClause(SwiftParser.ParameterClauseContext ctx) { verifier.verifyBracketContentWhitespace(ctx, Messages.PARENTHESES); ParserRuleContext parent = ctx.getParent(); if (parent instanceof FunctionSignatureContext) { ParseTree declaration = parent.getParent(); // 1st child is functionHead // 2nd child is functionName FunctionNameContext functionName = (FunctionNameContext) declaration.getChild(1); // Check for operator overloaded methods if (functionName.operator() != null && !(declaration.getChild(2) instanceof GenericParameterClauseContext)) { verifier.verifyLeadingWhitespaceBeforeBracket(ctx, Messages.OPERATOR_OVERLOADING_ONE_SPACE, 1); return; } } verifier.verifyLeadingWhitespaceBeforeBracket(ctx, Messages.PARENTHESES, Messages.NO_WHITESPACE_BEFORE, 0); }
/** * 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; }
@Override public void enterComparisonPredicate(@NotNull PQL2Parser.ComparisonPredicateContext ctx) { pushNode(new ComparisonPredicateAstNode(ctx.getChild(0).getChild(1).getText())); }
/** * 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 exitAssignmentExpression(AssignmentExpressionContext ctx) { if (ctx.getChildCount() == 3 && ctx.getChild(1).getText().equals("=") && !ctx.getChild(0).getText().contains(".")) { ParseTree child = ctx.getChild(2).getChild(0); if (child instanceof PathExpressionContext) { handlePathExpressionContext(ctx); } else if (child instanceof NewInstanceRuleContext || child instanceof NewArrayRuleContext) { handleRuleContext(ctx, child); } else { handleRestOfCases(ctx); } } }
@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()))); } }
private void nameDeclaration(DeclarationRuleContext ctx) { ParseTree t = findChildrenByType(ctx.getChild(3).getChild(0), ClassNameExpressionContext.class); if (t != null) { String ttype = t.getText().trim(); AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(1).getText()); registry.addCandidate(c); if (GroovyCompletionTypes.debug) logger.info("define variable of type " + ctx.getChild(1).getText() + " " + ttype); if (ttype != null) classUtils.defineVariable(ctx.getChild(1).getText(), ttype); } }
@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 void handlePathExpressionContext(AssignmentExpressionContext ctx) { String typpen = ctx.getChild(2).getChild(0).getText().trim(); AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(0).getText()); registry.addCandidate(c); if (GroovyCompletionTypes.debug) logger.info("define variable of type " + ctx.getChild(0).getText() + " " + typpen); if (classUtils.getVariableType(typpen) != null) { classUtils.defineVariable(ctx.getChild(0).getText(), classUtils.getVariableType(typpen)); } }
ctx.getChild(3).getChildCount() > 0) { if (!ctx.getChild(1).getText().contains(".")) { if (ctx.getChild(3).getChild(0) instanceof PathExpressionContext) { String typpen = ctx.getChild(3).getChild(0).getText().trim(); AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(1).getText()); registry.addCandidate(c); classUtils.defineVariable(ctx.getChild(1).getText(), classUtils.getVariableType(typpen)); } else if (ctx.getChild(3).getChild(0) instanceof NewInstanceRuleContext) { nameDeclaration(ctx); } else {
parseTreeParent = parseTreeParent.getChild(0);