Search in sources :

Example 11 with ASTReference

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

the class AddShardsAndDaysVisitor method addNewShardsAndDaysNode.

// Create and append a new SHARDS_AND_DAYS hint node and AND it to the given node.
private static void addNewShardsAndDaysNode(JexlNode node, String shardsAndDays) {
    JexlNode originalParent = node.jjtGetParent();
    JexlNode shardAndHintNode = JexlNodes.wrap(JexlNodeFactory.createAssignment(Constants.SHARD_DAY_HINT, shardsAndDays));
    // If the current node is unwrapped, wrap it before ANDing it with the new SHARDS_AND_DAYS node.
    JexlNode leftSide = !(node instanceof ASTReference) ? JexlNodes.wrap(node) : node;
    JexlNode andNode = JexlNodeFactory.createAndNode(Lists.newArrayList(leftSide, shardAndHintNode));
    // Update the grandparent's lineage.
    if (originalParent != null) {
        JexlNodes.replaceChild(originalParent, node, andNode);
    }
}
Also used : JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Example 12 with ASTReference

use of org.apache.commons.jexl3.parser.ASTReference in project nexus-public by sonatype.

the class OrientCselToSql method visit.

/**
 * Transform dotted references into format-specific attributes.
 */
@Override
protected Object visit(final ASTReference node, final Object data) {
    ASTIdentifierAccess subRef = (ASTIdentifierAccess) node.jjtGetChild(RIGHT);
    ((SelectorSqlBuilder) data).appendProperty(subRef.getName());
    return data;
}
Also used : ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess)

Example 13 with ASTReference

use of org.apache.commons.jexl3.parser.ASTReference 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)

Example 14 with ASTReference

use of org.apache.commons.jexl3.parser.ASTReference 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 15 with ASTReference

use of org.apache.commons.jexl3.parser.ASTReference in project nexus-public by sonatype.

the class DatastoreCselToSql method visit.

/**
 * Transform dotted references into format-specific attributes.
 */
@Override
protected Object visit(final ASTReference node, final Object data) {
    ASTIdentifierAccess subRef = (ASTIdentifierAccess) node.jjtGetChild(RIGHT);
    ((SelectorSqlBuilder) data).appendProperty(subRef.getName());
    return data;
}
Also used : SelectorSqlBuilder(org.sonatype.nexus.selector.SelectorSqlBuilder) ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess)

Aggregations

ASTReference (org.apache.commons.jexl2.parser.ASTReference)28 JexlNode (org.apache.commons.jexl2.parser.JexlNode)17 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)11 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)8 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)7 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)6 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)6 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)5 ArrayList (java.util.ArrayList)4 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)4 ASTNRNode (org.apache.commons.jexl2.parser.ASTNRNode)4 ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)4 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)3 List (java.util.List)3 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)3 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)3 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)3 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 ASTReference (org.apache.commons.jexl3.parser.ASTReference)3