Search in sources :

Example 1 with ASTIdentifierAccess

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

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

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

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

the class Dumper method dump.

private void dump(final JexlNode node, final Object data) {
    final int num = node.jjtGetNumChildren();
    indent();
    strb.append(node.getClass().getSimpleName());
    if (node instanceof ASTIdentifier) {
        strb.append("@");
        strb.append(node.toString());
    } else if (node instanceof ASTIdentifierAccess) {
        strb.append("@");
        strb.append(node.toString());
    }
    strb.append('(');
    indent += 1;
    for (int c = 0; c < num; ++c) {
        final JexlNode child = node.jjtGetChild(c);
        if (c > 0) {
            strb.append(',');
        }
        strb.append('\n');
        dump(child, data);
    }
    indent -= 1;
    if (num > 0) {
        strb.append('\n');
        indent();
    }
    strb.append(')');
}
Also used : ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess)

Example 5 with ASTIdentifierAccess

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

ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)9 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)5 JexlNode (org.apache.commons.jexl3.parser.JexlNode)5 ASTArrayAccess (org.apache.commons.jexl3.parser.ASTArrayAccess)4 JexlException (org.apache.commons.jexl3.JexlException)3 JexlPropertyGet (org.apache.commons.jexl3.introspection.JexlPropertyGet)2 ASTMethodNode (org.apache.commons.jexl3.parser.ASTMethodNode)2 ASTReference (org.apache.commons.jexl3.parser.ASTReference)2 JexlOperator (org.apache.commons.jexl3.JexlOperator)1 JexlScript (org.apache.commons.jexl3.JexlScript)1 JxltEngine (org.apache.commons.jexl3.JxltEngine)1 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)1 ASTArguments (org.apache.commons.jexl3.parser.ASTArguments)1 ASTFunctionNode (org.apache.commons.jexl3.parser.ASTFunctionNode)1 ASTIdentifierAccessJxlt (org.apache.commons.jexl3.parser.ASTIdentifierAccessJxlt)1 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)1 ASTNumberLiteral (org.apache.commons.jexl3.parser.ASTNumberLiteral)1 ASTStringLiteral (org.apache.commons.jexl3.parser.ASTStringLiteral)1 ASTVar (org.apache.commons.jexl3.parser.ASTVar)1 SelectorSqlBuilder (org.sonatype.nexus.selector.SelectorSqlBuilder)1