Search in sources :

Example 6 with ASTNumberLiteral

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

the class Interpreter method visit.

@Override
protected Object visit(final ASTUnaryMinusNode node, final Object data) {
    // use cached value if literal
    final Object value = node.jjtGetValue();
    if (value != null && !(value instanceof JexlMethod)) {
        return value;
    }
    final JexlNode valNode = node.jjtGetChild(0);
    final Object val = valNode.jjtAccept(this, data);
    try {
        final Object result = operators.tryOverload(node, JexlOperator.NEGATE, val);
        if (result != JexlEngine.TRY_FAILED) {
            return result;
        }
        Object number = arithmetic.negate(val);
        // cache if number literal and negate is idempotent
        if (number instanceof Number && valNode instanceof ASTNumberLiteral) {
            number = arithmetic.narrowNumber((Number) number, ((ASTNumberLiteral) valNode).getLiteralClass());
            if (arithmetic.isNegateStable()) {
                node.jjtSetValue(number);
            }
        }
        return number;
    } catch (final ArithmeticException xrt) {
        throw new JexlException(valNode, "- error", xrt);
    }
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Example 7 with ASTNumberLiteral

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

the class Interpreter method visit.

@Override
protected Object visit(final ASTUnaryPlusNode node, final Object data) {
    // use cached value if literal
    final Object value = node.jjtGetValue();
    if (value != null && !(value instanceof JexlMethod)) {
        return value;
    }
    final JexlNode valNode = node.jjtGetChild(0);
    final Object val = valNode.jjtAccept(this, data);
    try {
        final Object result = operators.tryOverload(node, JexlOperator.POSITIVIZE, val);
        if (result != JexlEngine.TRY_FAILED) {
            return result;
        }
        final Object number = arithmetic.positivize(val);
        if (valNode instanceof ASTNumberLiteral && number instanceof Number && arithmetic.isPositivizeStable()) {
            node.jjtSetValue(number);
        }
        return number;
    } catch (final ArithmeticException xrt) {
        throw new JexlException(valNode, "- error", xrt);
    }
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Example 8 with ASTNumberLiteral

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

the class TemplateScript method collectPrintScope.

/**
 * Collects the scope surrounding a call to jexl:print(i).
 * <p>This allows to later parse the blocks with the known symbols
 * in the frame visible to the parser.
 * @param node the visited node
 * @param minfo the map of printed expression number to node info
 */
private static void collectPrintScope(final JexlNode node, final Map<Integer, JexlNode.Info> minfo) {
    final int nc = node.jjtGetNumChildren();
    if (node instanceof ASTFunctionNode && nc == 2) {
        // 0 must be the prefix jexl:
        final ASTIdentifier nameNode = (ASTIdentifier) node.jjtGetChild(0);
        if ("print".equals(nameNode.getName()) && "jexl".equals(nameNode.getNamespace())) {
            final ASTArguments argNode = (ASTArguments) node.jjtGetChild(1);
            if (argNode.jjtGetNumChildren() == 1) {
                // seek the epression number
                final JexlNode arg0 = argNode.jjtGetChild(0);
                if (arg0 instanceof ASTNumberLiteral) {
                    final int exprNumber = ((ASTNumberLiteral) arg0).getLiteral().intValue();
                    minfo.put(exprNumber, new JexlNode.Info(nameNode));
                    return;
                }
            }
        }
    }
    for (int c = 0; c < nc; ++c) {
        collectPrintScope(node.jjtGetChild(c), minfo);
    }
}
Also used : ASTFunctionNode(org.apache.commons.jexl3.parser.ASTFunctionNode) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTArguments(org.apache.commons.jexl3.parser.ASTArguments) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Example 9 with ASTNumberLiteral

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

the class TemplateDebugger method getPrintStatement.

/**
 * In a template, any statement that is not 'jexl:print(n)' must be prefixed by "$$".
 * @param child the node to check
 * @return the expression number or -1 if the node is not a jexl:print
 */
private TemplateExpression getPrintStatement(final JexlNode child) {
    if (exprs != null && child instanceof ASTFunctionNode) {
        final ASTFunctionNode node = (ASTFunctionNode) child;
        final ASTIdentifier ns = (ASTIdentifier) node.jjtGetChild(0);
        final JexlNode args = node.jjtGetChild(1);
        if ("jexl".equals(ns.getNamespace()) && "print".equals(ns.getName()) && args.jjtGetNumChildren() == 1 && args.jjtGetChild(0) instanceof ASTNumberLiteral) {
            final ASTNumberLiteral exprn = (ASTNumberLiteral) args.jjtGetChild(0);
            final int n = exprn.getLiteral().intValue();
            if (n >= 0 && n < exprs.length) {
                return exprs[n];
            }
        }
    }
    return null;
}
Also used : ASTFunctionNode(org.apache.commons.jexl3.parser.ASTFunctionNode) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Example 10 with ASTNumberLiteral

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

the class FixNegativeNumbersVisitor method visit.

@Override
public Object visit(ASTUnaryMinusNode astumn, Object data) {
    if (astumn.jjtGetNumChildren() == 1 && astumn.jjtGetChild(0) instanceof ASTNumberLiteral) {
        ASTNumberLiteral node = (ASTNumberLiteral) astumn.jjtGetChild(0);
        ASTNumberLiteral newNode = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
        String value = "-" + node.image;
        newNode.image = value;
        newNode.jjtSetParent(node.jjtGetParent());
        if (JexlNodeFactory.NATURAL_NUMBERS.contains(node.getLiteralClass())) {
            newNode.setNatural(value);
        } else if (JexlNodeFactory.REAL_NUMBERS.contains(node.getLiteralClass())) {
            newNode.setReal(value);
        } else {
            throw new IllegalArgumentException("Could not ascertain type of ASTNumberLiteral: " + node);
        }
        newNode.jjtSetValue(value);
        return newNode;
    } else {
        return super.visit(astumn, data);
    }
}
Also used : ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Aggregations

ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)10 ASTNumberLiteral (org.apache.commons.jexl3.parser.ASTNumberLiteral)5 JexlNode (org.apache.commons.jexl3.parser.JexlNode)5 ASTReference (org.apache.commons.jexl2.parser.ASTReference)4 JexlNode (org.apache.commons.jexl2.parser.JexlNode)4 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)3 QueryException (datawave.webservice.query.exception.QueryException)3 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)3 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)3 ASTFunctionNode (org.apache.commons.jexl3.parser.ASTFunctionNode)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)2 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)2 JexlException (org.apache.commons.jexl3.JexlException)2 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)2 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