Search in sources :

Example 16 with ASTIdentifier

use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.

the class JexlNodeFactory method buildNullNode.

public static JexlNode buildNullNode(JexlNode original, ASTIdentifier identifier) {
    JexlNode copy;
    if (original instanceof ASTEQNode) {
        copy = new ASTEQNode(ParserTreeConstants.JJTEQNODE);
    } else if (original instanceof ASTNENode) {
        copy = new ASTNENode(ParserTreeConstants.JJTNENODE);
    } else if (original instanceof ASTERNode) {
        copy = new ASTERNode(ParserTreeConstants.JJTERNODE);
    } else if (original instanceof ASTNRNode) {
        copy = new ASTNRNode(ParserTreeConstants.JJTNRNODE);
    } else if (original instanceof ASTGTNode) {
        copy = new ASTGTNode(ParserTreeConstants.JJTGTNODE);
    } else if (original instanceof ASTGENode) {
        copy = new ASTGENode(ParserTreeConstants.JJTGENODE);
    } else if (original instanceof ASTLTNode) {
        copy = new ASTLTNode(ParserTreeConstants.JJTLTNODE);
    } else if (original instanceof ASTLENode) {
        copy = new ASTLENode(ParserTreeConstants.JJTLENODE);
    } else {
        throw new UnsupportedOperationException("Cannot handle " + original);
    }
    copy.jjtSetParent(original.jjtGetParent());
    ASTNullLiteral literalNode = new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL);
    return buildUntypedNewLiteralNode(copy, identifier, literalNode);
}
Also used : ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) ASTLENode(org.apache.commons.jexl2.parser.ASTLENode) ASTGENode(org.apache.commons.jexl2.parser.ASTGENode) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ExceededTermThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode) ASTGTNode(org.apache.commons.jexl2.parser.ASTGTNode) ASTNullLiteral(org.apache.commons.jexl2.parser.ASTNullLiteral) ASTLTNode(org.apache.commons.jexl2.parser.ASTLTNode)

Example 17 with ASTIdentifier

use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.

the class JexlNodeFactory method buildNode.

/**
 * Create a new JexlNode from the given node (possible an OR Node) and value
 *
 * @param original
 * @param node
 * @param fieldValue
 * @return
 */
public static JexlNode buildNode(JexlNode original, JexlNode node, Number fieldValue) {
    JexlNode newNode = JexlNodeFactory.shallowCopy(original);
    if (null != original) {
        newNode.jjtSetParent(original.jjtGetParent());
    }
    List<JexlNode> list = Lists.newArrayList();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        JexlNode kid = node.jjtGetChild(i);
        list.add(buildUntypedNewNode(newNode, (ASTIdentifier) JexlNodeFactory.shallowCopy(kid), fieldValue));
    }
    if (!list.isEmpty()) {
        return createOrNode(list);
    }
    return null;
}
Also used : ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ExceededTermThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier)

Example 18 with ASTIdentifier

use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.

the class JexlNodeFactory method buildUntypedNewLiteralNode.

/**
 * 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;
}
Also used : ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Example 19 with ASTIdentifier

use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.

the class FunctionJexlNodeVisitor method makeFunctionFrom.

public static ASTFunctionNode makeFunctionFrom(String ns, String functionName, JexlNode... arguments) {
    ASTFunctionNode fn = new ASTFunctionNode(ParserTreeConstants.JJTFUNCTIONNODE);
    ASTIdentifier namespace = JexlNodes.makeIdentifierWithImage(ns);
    ASTIdentifier function = JexlNodes.makeIdentifierWithImage(functionName);
    ArrayList<JexlNode> nodes = Lists.newArrayList();
    nodes.add(namespace);
    nodes.add(function);
    Collections.addAll(nodes, arguments);
    return JexlNodes.children(fn, nodes.toArray(new JexlNode[nodes.size()]));
}
Also used : ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 20 with ASTIdentifier

use of org.apache.commons.jexl2.parser.ASTIdentifier in project commons-jexl by apache.

the class Interpreter method visit.

@Override
protected Object visit(final ASTForeachStatement node, final Object data) {
    Object result = null;
    /* first objectNode is the loop variable */
    final ASTReference loopReference = (ASTReference) node.jjtGetChild(0);
    final ASTIdentifier loopVariable = (ASTIdentifier) loopReference.jjtGetChild(0);
    final int symbol = loopVariable.getSymbol();
    // && node.getSymbolCount() > 0;
    final boolean lexical = options.isLexical();
    final LexicalFrame locals = lexical ? new LexicalFrame(frame, block) : null;
    final boolean loopSymbol = symbol >= 0 && loopVariable instanceof ASTVar;
    if (lexical) {
        // it may be a local previously declared
        if (loopSymbol && !defineVariable((ASTVar) loopVariable, locals)) {
            return redefinedVariable(node, loopVariable.getName());
        }
        block = locals;
    }
    Object forEach = null;
    try {
        /* second objectNode is the variable to iterate */
        final Object iterableValue = node.jjtGetChild(1).jjtAccept(this, data);
        // make sure there is a value to iterate upon
        if (iterableValue != null) {
            /* third objectNode is the statement to execute */
            final JexlNode statement = node.jjtGetNumChildren() >= 3 ? node.jjtGetChild(2) : null;
            // get an iterator for the collection/array etc via the introspector.
            forEach = operators.tryOverload(node, JexlOperator.FOR_EACH, iterableValue);
            final Iterator<?> itemsIterator = forEach instanceof Iterator ? (Iterator<?>) forEach : uberspect.getIterator(iterableValue);
            if (itemsIterator != null) {
                int cnt = 0;
                while (itemsIterator.hasNext()) {
                    cancelCheck(node);
                    // reset loop variable
                    if (lexical && cnt++ > 0) {
                        // clean up but remain current
                        block.pop();
                        // unlikely to fail
                        if (loopSymbol && !defineVariable((ASTVar) loopVariable, locals)) {
                            return redefinedVariable(node, loopVariable.getName());
                        }
                    }
                    // set loopVariable to value of iterator
                    final Object value = itemsIterator.next();
                    if (symbol < 0) {
                        setContextVariable(node, loopVariable.getName(), value);
                    } else {
                        frame.set(symbol, value);
                    }
                    if (statement != null) {
                        try {
                            // execute statement
                            result = statement.jjtAccept(this, data);
                        } catch (final JexlException.Break stmtBreak) {
                            break;
                        } catch (final JexlException.Continue stmtContinue) {
                        // continue;
                        }
                    }
                }
            }
        }
    } finally {
        // closeable iterator handling
        closeIfSupported(forEach);
        // restore lexical frame
        if (lexical) {
            block = block.pop();
        }
    }
    return result;
}
Also used : ASTVar(org.apache.commons.jexl3.parser.ASTVar) JexlException(org.apache.commons.jexl3.JexlException) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) ASTReference(org.apache.commons.jexl3.parser.ASTReference) Iterator(java.util.Iterator) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Aggregations

ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)26 JexlNode (org.apache.commons.jexl2.parser.JexlNode)19 ASTReference (org.apache.commons.jexl2.parser.ASTReference)10 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)10 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)8 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)7 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)7 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)7 JexlNode (org.apache.commons.jexl3.parser.JexlNode)7 ASTNENode (org.apache.commons.jexl2.parser.ASTNENode)6 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)5 ASTGENode (org.apache.commons.jexl2.parser.ASTGENode)5 ASTGTNode (org.apache.commons.jexl2.parser.ASTGTNode)5 ASTLENode (org.apache.commons.jexl2.parser.ASTLENode)5 ASTLTNode (org.apache.commons.jexl2.parser.ASTLTNode)5 ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)5 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)4 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)4 ASTNullLiteral (org.apache.commons.jexl2.parser.ASTNullLiteral)4 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)4