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; }
/** {@inheritDoc} */ public DebugInfo debugInfo() { JexlNode node = this; while (node != null) { if (node.value instanceof DebugInfo) { return (DebugInfo) node.value; } node = node.jjtGetParent(); } return null; }
/** * Checks if a terminal node is the the cause to debug & adds its * representation to the rebuilt expression. * @param node the child node * @param image the child node token image (may be null) * @param data visitor pattern argument * @return visitor pattern value */ private Object check(JexlNode node, String image, Object data) { if (node == cause) { start = builder.length(); } if (image != null) { builder.append(image); } else { builder.append(node.toString()); } if (node == cause) { end = builder.length(); } return data; }
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; }
public static JexlNode dereference(JexlNode node) { while (node.jjtGetNumChildren() == 1 && (node instanceof ASTReference || node instanceof ASTReferenceExpression)) { node = node.jjtGetChild(0); } return node; }
boolean containsIgnorable = false; Set<STATE> states = new HashSet<>(); for (int i = 0; i < node.jjtGetNumChildren(); i++) { states.add((STATE) (node.jjtGetChild(i).jjtAccept(this, data + PREFIX))); output.writeLine(data + node.toString() + '[' + states + (containsIgnorable ? ",IGNORABLE" : "") + "] -> " + state);
@Override public Object visit(ASTAndNode node, Object data) { List<JexlNode> leaves = new ArrayList<>(); Map<LiteralRange<?>,List<JexlNode>> ranges = JexlASTHelper.getBoundedRanges(node, this.dataTypeFilter, this.helper, leaves, false); JexlNode andNode = JexlNodes.newInstanceOfType(node); andNode.image = node.image; andNode.jjtSetParent(node.jjtGetParent()); // We have a bounded range completely inside of an AND/OR if (!ranges.isEmpty()) { andNode = delayIndexBoundedRange(ranges, leaves, node, andNode, data); } else { // We have no bounded range to replace, just proceed as normal JexlNodes.ensureCapacity(andNode, node.jjtGetNumChildren()); for (int i = 0; i < node.jjtGetNumChildren(); i++) { JexlNode newChild = (JexlNode) node.jjtGetChild(i).jjtAccept(this, data); andNode.jjtAddChild(newChild, i); newChild.jjtSetParent(andNode); } } return andNode; }
public Object descend(JexlNode node, Object data) { if (node.jjtGetNumChildren() > 1) { QueryException qe = new QueryException(DatawaveErrorCode.MORE_THAN_ONE_CHILD, MessageFormat.format("Class: {0}", node.getClass().getSimpleName())); throw new DatawaveFatalQueryException(qe); } if (node.jjtGetNumChildren() == 1) { return node.jjtGetChild(0).jjtAccept(this, data); } else { return data; } }
@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; }
public static JexlNode buildNewLiteralNode(JexlNode original, String fieldName, Number fieldValue) { ASTReference literalReference = new ASTReference(ParserTreeConstants.JJTREFERENCE); ASTNumberLiteral literal = getLiteral(fieldValue); literalReference.jjtAddChild(literal, 0); literal.jjtSetParent(literalReference); // we don't know whether the left or right side is the literal to replace. find it. if (original.jjtGetChild(0) instanceof ASTReference && original.jjtGetChild(0).jjtGetChild(0) instanceof ASTIdentifier) { original.jjtAddChild(literalReference, 1); // replace the original reference/literal (on left) with new reference/literal } if (original.jjtGetChild(1) instanceof ASTReference && original.jjtGetChild(1).jjtGetChild(0) instanceof ASTIdentifier) { original.jjtAddChild(literalReference, 0); // replace the original reference/literal (on right) with new reference/literal } literalReference.jjtSetParent(original); return original; }
/** * This is the opposite of dereference in that this will climb back up reference and reference expression nodes that only contain one child. * * @param node * @return the parent reference/referenceexpression or this node */ public static JexlNode rereference(JexlNode node) { while (node.jjtGetParent() != null && node.jjtGetParent().jjtGetNumChildren() == 1 && (node.jjtGetParent() instanceof ASTReference || node.jjtGetParent() instanceof ASTReferenceExpression)) { node = node.jjtGetParent(); } return 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 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; }
for (String fieldName : fieldNames) { JexlNode child = buildUntypedNode(node, fieldName, literal); parentNode.jjtAddChild(child, i); child.jjtSetParent(parentNode); wrapped.jjtSetParent(node.jjtGetParent());
private JexlNode getOrNode(JexlNode node) { if (node instanceof ASTOrNode) { return node; } else if (node instanceof ASTReference) { return getOrNode(node.jjtGetChild(0)); } else if (node instanceof ASTReferenceExpression) { return getOrNode(node.jjtGetChild(0)); } return null; }
public static JexlNode otherChild(JexlNode parent, JexlNode child) { Preconditions.checkArgument(parent.jjtGetNumChildren() == 2, "Jexl tree must be binary, but received node with %s children.", parent.jjtGetNumChildren()); JexlNode otherChild = null; for (JexlNode n : children(parent)) if (child != n) otherChild = n; return Preconditions.checkNotNull(otherChild); }
@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; }
@Override public Object visit(ASTStringLiteral node, Object data) { // strings are always wrapped in a reference node, so the op is the gparent JexlNode op = node.jjtGetParent().jjtGetParent(); if (op instanceof ASTEQNode) { JexlNode field = JexlNodes.otherChild(op, node.jjtGetParent()).jjtGetChild(0); if (log.isTraceEnabled()) { log.trace("Found field " + JexlASTHelper.deconstructIdentifier(field.image) + "==" + node.getLiteral()); } values.addLiteral(node.getLiteral().toString(), JexlASTHelper.deconstructIdentifier(field.image)); } else if (op instanceof ASTERNode) { JexlNode field = JexlNodes.otherChild(op, node.jjtGetParent()).jjtGetChild(0); values.addPattern(node.getLiteral(), JexlASTHelper.deconstructIdentifier(field.image)); } return null; }