/** * Fluid wrapper for calling {child.jjtSetParent(parent)}. * * @param child * @param parent * @return */ public static <T extends JexlNode> T newParent(T child, JexlNode parent) { child.jjtSetParent(parent); return child; }
protected static JexlNode buildUntypedDblLiteralNode(JexlNode newNode, JexlNode literal1, JexlNode literal2) { newNode.jjtAddChild(literal1, 0); newNode.jjtAddChild(literal2, 1); literal1.jjtSetParent(newNode); literal2.jjtSetParent(newNode); return newNode; }
private void adopt(JexlNode parent, JexlNode child, int i) { if (child != null && parent != null) { child.jjtSetParent(parent); parent.jjtAddChild(child, i); } }
public void applyNode(JexlNode node) { JexlNode copy = RebuildingVisitor.copy(node); copy.jjtSetParent(null); myNode = copy; for (IndexMatch match : uids) { match.add(node); } }
/** * Add the children JexlNodes to the parent JexlNode, correctly setting parent pointers, parenthesis, and reference nodes. * * @param parent * @param children * @return */ public static JexlNode setChildren(JexlNode parent, Iterable<? extends JexlNode> children) { int i = 0; JexlNodes.ensureCapacity(parent, Iterables.size(children)); // hopefully // we got // passed a // collection... for (JexlNode child : children) { parent.jjtAddChild(child, i); child.jjtSetParent(parent); i++; } return parent; }
public static JexlNode copyInto(JexlNode root, JexlNode target) { RebuildingVisitor visitor = new RebuildingVisitor(); JexlNode copyRoot = (JexlNode) root.jjtAccept(visitor, null); if (null != copyRoot) { target.jjtSetParent(copyRoot.jjtGetParent()); for (int i = 0; i < copyRoot.jjtGetNumChildren(); i++) { JexlNode child = copyRoot.jjtGetChild(i); target.jjtAddChild(child, i); child.jjtSetParent(target); } } return target; }
/** * Create an ASTJexlScript with the provided child * * @param child * @return */ public static ASTJexlScript createScript(JexlNode child) { ASTJexlScript script = new ASTJexlScript(ParserTreeConstants.JJTJEXLSCRIPT); // Wrap the child in parens to be sure we don't misconstrue the intent // of the query JexlNode wrappedChild = wrap(child); script.jjtAddChild(wrappedChild, 0); wrappedChild.jjtSetParent(script); return script; }
/** * Add the children JexlNodes to the parent JexlNode, correctly setting parent pointers, parenthesis, and reference nodes. * * @param parent * @param children * @return */ public static JexlNode wrapChildren(JexlNode parent, Iterable<? extends JexlNode> children) { parent = setChildren(parent, children); JexlNode grandParent = parent.jjtGetParent(); // If we have more than one element in the new node, wrap it in // parentheses JexlNode wrapped = wrap(parent); // Set the parent pointer wrapped.jjtSetParent(grandParent); return wrapped; }
protected JexlNode flattenTree(JexlNode currentNode, JexlNode newNode, Object data) { if (!currentNode.getClass().equals(newNode.getClass())) { log.warn("newNode is not the same type as currentNode ... something has probably gone horribly wrong"); } for (int i = 0; i < currentNode.jjtGetNumChildren(); i++) { JexlNode node = (JexlNode) currentNode.jjtGetChild(i).jjtAccept(this, null); JexlNode dereferenced = JexlASTHelper.dereference(node); if (acceptableNodesToCombine(currentNode, dereferenced)) { flattenTree(dereferenced, newNode, data); } else { newNode.jjtAddChild(node, newNode.jjtGetNumChildren()); node.jjtSetParent(newNode); } } return newNode; }
private JexlNode reparent(JexlNode in, JexlNode out) { int j = 0; for (int i = in.jjtGetNumChildren() - 1; i >= 0; i--) { JexlNode kid = in.jjtGetChild(i); kid = (JexlNode) kid.jjtAccept(this, null); out.jjtAddChild(kid, j++); kid.jjtSetParent(out); } return out; }
protected static JexlNode buildUntypedDblIdentifierNode(JexlNode newNode, JexlNode identifier1, JexlNode identifier2) { ASTReference identifierReference1 = new ASTReference(ParserTreeConstants.JJTREFERENCE); identifierReference1.jjtAddChild(identifier1, 0); identifier1.jjtSetParent(identifierReference1); ASTReference identifierReference2 = new ASTReference(ParserTreeConstants.JJTREFERENCE); identifierReference2.jjtAddChild(identifier2, 0); identifier2.jjtSetParent(identifierReference2); newNode.jjtAddChild(identifierReference1, 0); newNode.jjtAddChild(identifierReference2, 1); identifierReference1.jjtSetParent(newNode); identifierReference2.jjtSetParent(newNode); return newNode; }
protected void setupSource(JexlNode source) { this.jjtSetParent(source.jjtGetParent()); // create the assignment using the class name wrapped in an expression JexlNode refNode1 = JexlNodeFactory.createExpression(JexlNodeFactory.createAssignment(getClass().getSimpleName(), true)); // wrap the source in an expression JexlNode refNode2 = JexlNodeFactory.createExpression(source); // wrap the assignment and source in an AND node JexlNode andNode = JexlNodeFactory.createUnwrappedAndNode(Arrays.asList(refNode1, refNode2)); // wrap the and node with an expression (see JexlNodeFactory.createAndNode) ASTReferenceExpression refExpNode1 = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION); andNode.jjtSetParent(refExpNode1); refExpNode1.jjtAddChild(andNode, 0); // and make a child of this refExpNode1.jjtSetParent(this); this.jjtAddChild(refExpNode1, 0); }
@Override public Object visit(ASTJexlScript node, Object data) { setPushDown((PushDownVisitor) data); costEstimator = new CostEstimator(parentVisitor); if (log.isTraceEnabled()) log.trace("Setting cost estimator"); // don't rewrite yet ASTJexlScript newScript = new ASTJexlScript(ParserTreeConstants.JJTJEXLSCRIPT); // for this to work we should only have a single child Preconditions.checkArgument(node.jjtGetNumChildren() == 1); JexlNode child = node.jjtGetChild(0); if (ASTDelayedPredicate.instanceOf(child)) { child = child.jjtGetChild(0); child = (JexlNode) child.jjtAccept(this, data); child.jjtSetParent(newScript); newScript.jjtAddChild(child, 0); return newScript; } else return node; }
/** * @param delayedPredicates * @return */ protected JexlNode reverseDepth(JexlNode parentNode, List<JexlNode> delayedPredicates) { JexlNode returnNode = ASTDelayedPredicate.create(parentNode); JexlNode newAnd = new ASTAndNode(ParserTreeConstants.JJTANDNODE); int i = 0; for (JexlNode delayedNode : delayedPredicates) { newAnd.jjtAddChild(delayedNode.jjtGetChild(0), i); i++; } newAnd.jjtSetParent(returnNode); returnNode.jjtAddChild(newAnd, 0); return returnNode; }
@Override public Object visit(ASTOrNode node, Object data) { ASTOrNode newNode = new ASTOrNode(ParserTreeConstants.JJTORNODE); for (int i = 0; i < node.jjtGetNumChildren(); i++) { JexlNode child = (JexlNode) node.jjtGetChild(i).jjtAccept(this, data); newNode.jjtAddChild(child, newNode.jjtGetNumChildren()); child.jjtSetParent(newNode); } return JexlNodeFactory.wrap(newNode); } }
/** * Wrap an ASTAndNode or ASTOrNode in parenthesis if it has more than one child. Will return itself if wrapping is unnecessary. * * @param toWrap * @return */ public static JexlNode wrap(JexlNode toWrap) { if ((toWrap instanceof ASTAndNode || toWrap instanceof ASTOrNode) && toWrap.jjtGetNumChildren() > 1) { ASTReference reference = new ASTReference(ParserTreeConstants.JJTREFERENCE); ASTReferenceExpression parens = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION); parens.jjtAddChild(toWrap, 0); toWrap.jjtSetParent(parens); reference.jjtAddChild(parens, 0); parens.jjtSetParent(reference); return reference; } return toWrap; }
/** * Like {@link #buildUntypedNewNode(JexlNode, ASTIdentifier, String)} except it does not wrap {@code literal} in an {@link ASTReference} * * @param newNode * @param identifier * @param literal * @return */ protected static JexlNode buildUntypedNewLiteralNode(JexlNode newNode, ASTIdentifier identifier, JexlNode literal) { ASTReference identifierReference = new ASTReference(ParserTreeConstants.JJTREFERENCE); identifierReference.jjtAddChild(identifier, 0); identifier.jjtSetParent(identifierReference); newNode.jjtAddChild(identifierReference, 0); newNode.jjtAddChild(literal, 1); identifierReference.jjtSetParent(newNode); literal.jjtSetParent(newNode); return newNode; }
/** * Creates a reference expression fro a child node * * @param child * @return */ public static JexlNode createExpression(JexlNode child) { ASTReference ref = new ASTReference(ParserTreeConstants.JJTREFERENCE); ASTReferenceExpression exp = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION); child.jjtSetParent(exp); exp.jjtAddChild(child, 0); exp.jjtSetParent(ref); ref.jjtAddChild(exp, 0); ref.jjtSetParent(child.jjtGetParent()); return ref; }
protected static JexlNode buildUntypedNewNode(JexlNode newNode, JexlNode literal, ASTIdentifier identifier) { ASTReference literalReference = new ASTReference(ParserTreeConstants.JJTREFERENCE), identifierReference = new ASTReference( ParserTreeConstants.JJTREFERENCE); literalReference.jjtAddChild(literal, 0); literal.jjtSetParent(literalReference); identifierReference.jjtAddChild(identifier, 0); identifier.jjtSetParent(identifierReference); newNode.jjtAddChild(literalReference, 0); newNode.jjtAddChild(identifierReference, 1); identifierReference.jjtSetParent(newNode); literalReference.jjtSetParent(newNode); return newNode; }
/** * Given the provide newNode, add the fieldName (as an identifier) and the literal * * @param newNode * @param identifier * @param literal * @return */ protected static JexlNode buildUntypedNewNode(JexlNode newNode, ASTIdentifier identifier, JexlNode literal) { ASTReference literalReference = new ASTReference(ParserTreeConstants.JJTREFERENCE), identifierReference = new ASTReference( ParserTreeConstants.JJTREFERENCE); literalReference.jjtAddChild(literal, 0); literal.jjtSetParent(literalReference); identifierReference.jjtAddChild(identifier, 0); identifier.jjtSetParent(identifierReference); newNode.jjtAddChild(identifierReference, 0); newNode.jjtAddChild(literalReference, 1); identifierReference.jjtSetParent(newNode); literalReference.jjtSetParent(newNode); return newNode; }