Search in sources :

Example 1 with ASTNumberLiteral

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

the class JexlASTHelperTest method testFindLiteral.

@Test
public void testFindLiteral() throws Throwable {
    ASTJexlScript script = JexlASTHelper.parseJexlQuery("i == 10");
    if (log.isDebugEnabled()) {
        PrintingVisitor.printQuery(script);
    }
    JexlNode literal = JexlASTHelper.findLiteral(script);
    Assert.assertTrue(literal instanceof ASTNumberLiteral);
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral) Test(org.junit.Test)

Example 2 with ASTNumberLiteral

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

the class RebuildingVisitor method visit.

@Override
public Object visit(ASTFloatLiteral node, Object data) {
    ASTNumberLiteral newNode = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
    newNode.setReal(node.getLiteral().toString());
    newNode.jjtSetParent(node.jjtGetParent());
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        newNode.jjtAddChild((Node) node.jjtGetChild(i).jjtAccept(this, data), i);
    }
    return newNode;
}
Also used : ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 3 with ASTNumberLiteral

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

the class JexlNodeFactory method getLiteral.

private static ASTNumberLiteral getLiteral(Number fieldValue) {
    ASTNumberLiteral literal = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
    literal.image = fieldValue.toString();
    if (NATURAL_NUMBERS.contains(fieldValue.getClass())) {
        literal.setNatural(fieldValue.toString());
    } else if (REAL_NUMBERS.contains(fieldValue.getClass())) {
        literal.setReal(fieldValue.toString());
    } else {
        QueryException qe = new QueryException(DatawaveErrorCode.ASTNUMBERLITERAL_TYPE_ASCERTAIN_ERROR, MessageFormat.format("{0}", literal));
        throw new DatawaveFatalQueryException(qe);
    }
    return literal;
}
Also used : DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 4 with ASTNumberLiteral

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

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

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