Search in sources :

Example 1 with ASTMethodNode

use of org.apache.commons.jexl3.parser.ASTMethodNode in project datawave by NationalSecurityAgency.

the class JexlNodeFactory method shallowCopy.

/**
 * A shallow copy of the given JexlNode, creates a new node of the same type with the same parent and image. Children are not copied
 *
 * @param original
 * @return
 */
public static JexlNode shallowCopy(JexlNode original) {
    if (null == original) {
        throw new IllegalArgumentException();
    }
    JexlNode copy;
    Class<?> clz = original.getClass();
    if (ASTAndNode.class.isAssignableFrom(clz)) {
        copy = new ASTAndNode(ParserTreeConstants.JJTANDNODE);
    } else if (ASTBitwiseAndNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseAndNode(ParserTreeConstants.JJTBITWISEANDNODE);
    } else if (ASTBitwiseComplNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseComplNode(ParserTreeConstants.JJTBITWISECOMPLNODE);
    } else if (ASTBitwiseOrNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseOrNode(ParserTreeConstants.JJTBITWISEORNODE);
    } else if (ASTBitwiseXorNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseXorNode(ParserTreeConstants.JJTBITWISEXORNODE);
    } else if (ASTEmptyFunction.class.isAssignableFrom(clz)) {
        copy = new ASTEmptyFunction(ParserTreeConstants.JJTEMPTYFUNCTION);
    } else if (ASTEQNode.class.isAssignableFrom(clz)) {
        copy = new ASTEQNode(ParserTreeConstants.JJTEQNODE);
    } else if (ASTERNode.class.isAssignableFrom(clz)) {
        copy = new ASTERNode(ParserTreeConstants.JJTERNODE);
    } else if (ASTFalseNode.class.isAssignableFrom(clz)) {
        copy = new ASTFalseNode(ParserTreeConstants.JJTFALSENODE);
    } else if (ASTGENode.class.isAssignableFrom(clz)) {
        copy = new ASTGENode(ParserTreeConstants.JJTGENODE);
    } else if (ASTGTNode.class.isAssignableFrom(clz)) {
        copy = new ASTGTNode(ParserTreeConstants.JJTGTNODE);
    } else if (ASTIdentifier.class.isAssignableFrom(clz)) {
        copy = new ASTIdentifier(ParserTreeConstants.JJTIDENTIFIER);
    } else if (ASTLENode.class.isAssignableFrom(clz)) {
        copy = new ASTLENode(ParserTreeConstants.JJTLENODE);
    } else if (ASTLTNode.class.isAssignableFrom(clz)) {
        copy = new ASTLTNode(ParserTreeConstants.JJTLTNODE);
    } else if (ASTNENode.class.isAssignableFrom(clz)) {
        copy = new ASTNENode(ParserTreeConstants.JJTNENODE);
    } else if (ASTNRNode.class.isAssignableFrom(clz)) {
        copy = new ASTNRNode(ParserTreeConstants.JJTNRNODE);
    } else if (ASTNotNode.class.isAssignableFrom(clz)) {
        copy = new ASTNotNode(ParserTreeConstants.JJTNOTNODE);
    } else if (ASTNullLiteral.class.isAssignableFrom(clz)) {
        copy = new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL);
    } else if (ASTNumberLiteral.class.isAssignableFrom(clz)) {
        copy = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
        JexlNodes.setLiteral((ASTNumberLiteral) copy, ((ASTNumberLiteral) original).getLiteral());
    } else if (ASTOrNode.class.isAssignableFrom(clz)) {
        copy = new ASTOrNode(ParserTreeConstants.JJTORNODE);
    } else if (ASTStringLiteral.class.isAssignableFrom(clz)) {
        copy = new ASTStringLiteral(ParserTreeConstants.JJTSTRINGLITERAL);
        JexlNodes.setLiteral((ASTStringLiteral) copy, ((ASTStringLiteral) original).getLiteral());
    } else if (ASTTrueNode.class.isAssignableFrom(clz)) {
        copy = new ASTTrueNode(ParserTreeConstants.JJTTRUENODE);
    } else if (ASTReferenceExpression.class.isAssignableFrom(clz)) {
        copy = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
    } else if (ASTReference.class.isAssignableFrom(clz)) {
        copy = new ASTReference(ParserTreeConstants.JJTREFERENCE);
    } else if (ASTAdditiveNode.class.isAssignableFrom(clz)) {
        copy = new ASTAdditiveNode(ParserTreeConstants.JJTADDITIVENODE);
    } else if (ASTMethodNode.class.isAssignableFrom(clz)) {
        copy = new ASTMethodNode(ParserTreeConstants.JJTMETHODNODE);
    } else if (ASTFunctionNode.class.isAssignableFrom(clz)) {
        copy = new ASTFunctionNode(ParserTreeConstants.JJTFUNCTIONNODE);
    } else if (ASTMulNode.class.isAssignableFrom(clz)) {
        copy = new ASTMulNode(ParserTreeConstants.JJTMULNODE);
    } else if (ASTAssignment.class.isAssignableFrom(clz)) {
        copy = new ASTAssignment(ParserTreeConstants.JJTASSIGNMENT);
    } else {
        throw new UnsupportedOperationException();
    }
    copy.jjtSetParent(original.jjtGetParent());
    copy.image = original.image;
    return copy;
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral) ASTMulNode(org.apache.commons.jexl2.parser.ASTMulNode) ASTBitwiseComplNode(org.apache.commons.jexl2.parser.ASTBitwiseComplNode) ASTMethodNode(org.apache.commons.jexl2.parser.ASTMethodNode) ASTBitwiseXorNode(org.apache.commons.jexl2.parser.ASTBitwiseXorNode) ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) ASTBitwiseAndNode(org.apache.commons.jexl2.parser.ASTBitwiseAndNode) ASTNotNode(org.apache.commons.jexl2.parser.ASTNotNode) ASTGENode(org.apache.commons.jexl2.parser.ASTGENode) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTAdditiveNode(org.apache.commons.jexl2.parser.ASTAdditiveNode) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) ASTTrueNode(org.apache.commons.jexl2.parser.ASTTrueNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) ASTAssignment(org.apache.commons.jexl2.parser.ASTAssignment) ASTGTNode(org.apache.commons.jexl2.parser.ASTGTNode) ASTNullLiteral(org.apache.commons.jexl2.parser.ASTNullLiteral) ASTLTNode(org.apache.commons.jexl2.parser.ASTLTNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTBitwiseOrNode(org.apache.commons.jexl2.parser.ASTBitwiseOrNode) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) ASTLENode(org.apache.commons.jexl2.parser.ASTLENode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ExceededTermThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode) ASTEmptyFunction(org.apache.commons.jexl2.parser.ASTEmptyFunction) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 2 with ASTMethodNode

use of org.apache.commons.jexl3.parser.ASTMethodNode in project commons-jexl by apache.

the class Interpreter method visit.

/**
 * Execute a method call, ie syntactically written as name.call(...).
 * @param node the actual method call node
 * @param antish non null when name.call is an antish variable
 * @param data the context
 * @return the method call result
 */
private Object visit(final ASTMethodNode node, final Object antish, final Object data) {
    Object object = antish;
    // left contains the reference to the method
    final JexlNode methodNode = node.jjtGetChild(0);
    Object method;
    // 1: determine object and method or functor
    if (methodNode instanceof ASTIdentifierAccess) {
        method = methodNode;
        if (object == null) {
            object = data;
            if (object == null) {
                // no object, we fail
                return node.isSafeLhs(isSafe()) ? null : unsolvableMethod(methodNode, "<null>.<?>(...)");
            }
        } else {
            // edge case of antish var used as functor
            method = object;
        }
    } else {
        method = methodNode.jjtAccept(this, data);
    }
    Object result = method;
    for (int a = 1; a < node.jjtGetNumChildren(); ++a) {
        if (result == null) {
            // no method, we fail// variable unknown in context and not a local
            return node.isSafeLhs(isSafe()) ? null : unsolvableMethod(methodNode, "<?>.<null>(...)");
        }
        final ASTArguments argNode = (ASTArguments) node.jjtGetChild(a);
        result = call(node, object, result, argNode);
        object = result;
    }
    return result;
}
Also used : JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTArguments(org.apache.commons.jexl3.parser.ASTArguments) ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess)

Example 3 with ASTMethodNode

use of org.apache.commons.jexl3.parser.ASTMethodNode in project commons-jexl by apache.

the class Interpreter method visit.

@Override
protected Object visit(final ASTReference node, final Object data) {
    cancelCheck(node);
    final int numChildren = node.jjtGetNumChildren();
    final JexlNode parent = node.jjtGetParent();
    // pass first piece of data in and loop through children
    Object object = null;
    JexlNode objectNode = null;
    JexlNode ptyNode = null;
    StringBuilder ant = null;
    boolean antish = !(parent instanceof ASTReference);
    int v = 1;
    main: for (int c = 0; c < numChildren; c++) {
        objectNode = node.jjtGetChild(c);
        if (objectNode instanceof ASTMethodNode) {
            antish = false;
            if (object == null) {
                // we may be performing a method call on an antish var
                if (ant != null) {
                    final JexlNode child = objectNode.jjtGetChild(0);
                    if (child instanceof ASTIdentifierAccess) {
                        final int alen = ant.length();
                        ant.append('.');
                        ant.append(((ASTIdentifierAccess) child).getName());
                        object = context.get(ant.toString());
                        if (object != null) {
                            object = visit((ASTMethodNode) objectNode, object, context);
                            continue;
                        }
                        // remove method name from antish
                        ant.delete(alen, ant.length());
                        ptyNode = objectNode;
                    }
                }
                break;
            }
        } else if (objectNode instanceof ASTArrayAccess) {
            antish = false;
            if (object == null) {
                ptyNode = objectNode;
                break;
            }
        }
        // attempt to evaluate the property within the object (visit(ASTIdentifierAccess node))
        object = objectNode.jjtAccept(this, object);
        cancelCheck(node);
        if (object != null) {
            // disallow mixing antish variable & bean with same root; avoid ambiguity
            antish = false;
        } else if (antish) {
            // create first from first node
            if (ant == null) {
                // if we still have a null object, check for an antish variable
                final JexlNode first = node.jjtGetChild(0);
                if (!(first instanceof ASTIdentifier)) {
                    // not an identifier, not antish
                    ptyNode = objectNode;
                    break main;
                }
                final ASTIdentifier afirst = (ASTIdentifier) first;
                ant = new StringBuilder(afirst.getName());
                // *... and continue
                if (!options.isAntish()) {
                    antish = false;
                    continue;
                }
                // skip the first node case since it was trialed in jjtAccept above and returned null
                if (c == 0) {
                    continue;
                }
            }
            // catch up to current node
            for (; v <= c; ++v) {
                final JexlNode child = node.jjtGetChild(v);
                if (!(child instanceof ASTIdentifierAccess)) {
                    // not an identifier, not antish
                    ptyNode = objectNode;
                    break main;
                }
                final ASTIdentifierAccess achild = (ASTIdentifierAccess) child;
                if (achild.isSafe() || achild.isExpression()) {
                    break main;
                }
                ant.append('.');
                ant.append(achild.getName());
            }
            // solve antish
            object = context.get(ant.toString());
        } else if (c != numChildren - 1) {
            // only the last one may be null
            ptyNode = objectNode;
            // 
            break;
        }
    }
    // dealing with null
    if (object == null) {
        if (ptyNode != null) {
            if (ptyNode.isSafeLhs(isSafe())) {
                return null;
            }
            if (ant != null) {
                final String aname = ant.toString();
                final boolean defined = isVariableDefined(frame, block, aname);
                return unsolvableVariable(node, aname, !defined);
            }
            return unsolvableProperty(node, stringifyProperty(ptyNode), ptyNode == objectNode, null);
        }
        if (antish) {
            if (node.isSafeLhs(isSafe())) {
                return null;
            }
            final String aname = ant != null ? ant.toString() : "?";
            final boolean defined = isVariableDefined(frame, block, aname);
            // defined but null; arg of a strict operator?
            if (defined && !isStrictOperand(node)) {
                return null;
            }
            return unsolvableVariable(node, aname, !defined);
        }
    }
    return object;
}
Also used : JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) ASTMethodNode(org.apache.commons.jexl3.parser.ASTMethodNode) ASTReference(org.apache.commons.jexl3.parser.ASTReference) ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess) ASTArrayAccess(org.apache.commons.jexl3.parser.ASTArrayAccess)

Example 4 with ASTMethodNode

use of org.apache.commons.jexl3.parser.ASTMethodNode in project commons-jexl by apache.

the class Engine method getVariables.

/**
 * Fills up the list of variables accessed by a node.
 * @param script the owning script
 * @param node the node
 * @param collector the variable collector
 */
protected void getVariables(final ASTJexlScript script, final JexlNode node, final VarCollector collector) {
    if (node instanceof ASTIdentifier) {
        final JexlNode parent = node.jjtGetParent();
        if (parent instanceof ASTMethodNode || parent instanceof ASTFunctionNode) {
            // skip identifiers for methods and functions
            collector.collect(null);
            return;
        }
        final ASTIdentifier identifier = (ASTIdentifier) node;
        final int symbol = identifier.getSymbol();
        // symbols that are captured are considered "global" variables
        if (symbol >= 0 && script != null && !script.isCapturedSymbol(symbol)) {
            collector.collect(null);
        } else {
            // start collecting from identifier
            collector.collect(identifier);
            collector.add(identifier.getName());
        }
    } else if (node instanceof ASTIdentifierAccess) {
        final JexlNode parent = node.jjtGetParent();
        if (parent instanceof ASTMethodNode || parent instanceof ASTFunctionNode) {
            // skip identifiers for methods and functions
            collector.collect(null);
            return;
        }
        // belt and suspender since an identifier should have been seen first
        if (collector.isCollecting()) {
            collector.add(((ASTIdentifierAccess) node).getName());
        }
    } else if (node instanceof ASTArrayAccess && collector.mode > 0) {
        final int num = node.jjtGetNumChildren();
        // collect only if array access is const and follows an identifier
        boolean collecting = collector.isCollecting();
        for (int i = 0; i < num; ++i) {
            final JexlNode child = node.jjtGetChild(i);
            if (collecting && child.isConstant()) {
                // collect all constants or only string and number literals
                final boolean collect = collector.mode > 1 || (child instanceof ASTStringLiteral || child instanceof ASTNumberLiteral);
                if (collect) {
                    final String image = child.toString();
                    collector.add(image);
                }
            } else {
                collecting = false;
                collector.collect(null);
                getVariables(script, child, collector);
                collector.collect(null);
            }
        }
    } else {
        final int num = node.jjtGetNumChildren();
        for (int i = 0; i < num; ++i) {
            getVariables(script, node.jjtGetChild(i), collector);
        }
        collector.collect(null);
    }
}
Also used : ASTStringLiteral(org.apache.commons.jexl3.parser.ASTStringLiteral) ASTFunctionNode(org.apache.commons.jexl3.parser.ASTFunctionNode) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTMethodNode(org.apache.commons.jexl3.parser.ASTMethodNode) ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess) ASTArrayAccess(org.apache.commons.jexl3.parser.ASTArrayAccess) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Example 5 with ASTMethodNode

use of org.apache.commons.jexl3.parser.ASTMethodNode in project datawave by NationalSecurityAgency.

the class DatawaveInterpreter method hasSiblings.

/**
 * a function node that has siblings has a method paired with it, like the size method in includeRegex(foo,bar).size() It must return its collection of
 * results for the other method to use, instead of a boolean indicating that there were results
 *
 * @param node
 * @return
 */
private boolean hasSiblings(ASTFunctionNode node) {
    JexlNode parent = node.jjtGetParent();
    if (parent.jjtGetNumChildren() > 1) {
        return true;
    }
    JexlNode grandparent = parent.jjtGetParent();
    if (grandparent instanceof ASTMethodNode) {
        return true;
    }
    return false;
}
Also used : ExceededOrThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededOrThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTMethodNode(org.apache.commons.jexl2.parser.ASTMethodNode)

Aggregations

ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)3 JexlNode (org.apache.commons.jexl3.parser.JexlNode)3 ASTMethodNode (org.apache.commons.jexl2.parser.ASTMethodNode)2 JexlNode (org.apache.commons.jexl2.parser.JexlNode)2 ASTArrayAccess (org.apache.commons.jexl3.parser.ASTArrayAccess)2 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)2 ASTMethodNode (org.apache.commons.jexl3.parser.ASTMethodNode)2 ExceededOrThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededOrThresholdMarkerJexlNode)1 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)1 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)1 ASTAdditiveNode (org.apache.commons.jexl2.parser.ASTAdditiveNode)1 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)1 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)1 ASTBitwiseAndNode (org.apache.commons.jexl2.parser.ASTBitwiseAndNode)1 ASTBitwiseComplNode (org.apache.commons.jexl2.parser.ASTBitwiseComplNode)1 ASTBitwiseOrNode (org.apache.commons.jexl2.parser.ASTBitwiseOrNode)1 ASTBitwiseXorNode (org.apache.commons.jexl2.parser.ASTBitwiseXorNode)1 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)1 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)1 ASTEmptyFunction (org.apache.commons.jexl2.parser.ASTEmptyFunction)1