Search in sources :

Example 6 with ExprTreeHolder

use of org.apache.asterix.external.classad.ExprTreeHolder in project asterixdb by apache.

the class ClassAdParser method parseLogicalORExpression.

// LogicalORExpression .= LogicalANDExpression
// | LogicalORExpression '||' LogicalANDExpression
private boolean parseLogicalORExpression(ExprTreeHolder tree) throws IOException {
    if (!parseLogicalANDExpression(tree)) {
        return false;
    }
    while ((lexer.peekToken()) == TokenType.LEX_LOGICAL_OR) {
        ExprTreeHolder treeL = tree;
        ExprTreeHolder treeR = objectPool.mutableExprPool.get();
        lexer.consumeToken();
        parseLogicalANDExpression(treeR);
        if (treeL.getInnerTree() != null && treeR.getInnerTree() != null) {
            Operation newTree = objectPool.operationPool.get();
            Operation.createOperation(Operation.OpKind_LOGICAL_OR_OP, treeL, treeR, null, newTree);
            tree.setInnerTree(newTree);
        } else {
            tree.setInnerTree(null);
            return false;
        }
    }
    return true;
}
Also used : ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) Operation(org.apache.asterix.external.classad.Operation)

Example 7 with ExprTreeHolder

use of org.apache.asterix.external.classad.ExprTreeHolder in project asterixdb by apache.

the class ClassAdParser method parsePrimaryExpression.

// PrimaryExpression .= Identifier
// | FunctionCall
// | '.' Identifier
// | '(' Expression ')'
// | Literal
// FunctionCall .= Identifier ArgumentList
// ( Constant may be
// boolean,undefined,error,string,integer,real,classad,list )
// ( ArgumentList non-terminal includes parentheses )
private boolean parsePrimaryExpression(ExprTreeHolder tree) throws IOException {
    ExprTreeHolder treeL;
    TokenValue tv = objectPool.tokenValuePool.get();
    TokenType tt;
    tree.setInnerTree(null);
    switch((tt = lexer.peekToken(tv))) {
        // identifiers
        case LEX_IDENTIFIER:
            isExpr = true;
            lexer.consumeToken();
            // check for funcion call
            if ((tt = lexer.peekToken()) == TokenType.LEX_OPEN_PAREN) {
                ExprList argList = objectPool.exprListPool.get();
                if (!parseArgumentList(argList)) {
                    tree.setInnerTree(null);
                    return false;
                }
                ;
                // string literal
                if (shouldEvaluateAtParseTime(tv.getStrValue().toString(), argList)) {
                    tree.setInnerTree(evaluateFunction(tv.getStrValue().toString(), argList));
                } else {
                    tree.setInnerTree(FunctionCall.createFunctionCall(tv.getStrValue().toString(), argList, objectPool));
                }
            } else {
                // I don't think this is ever hit
                tree.setInnerTree(AttributeReference.createAttributeReference(null, tv.getStrValue(), false, objectPool));
            }
            return (tree.getInnerTree() != null);
        case LEX_SELECTION:
            isExpr = true;
            lexer.consumeToken();
            if ((tt = lexer.consumeToken(tv)) == TokenType.LEX_IDENTIFIER) {
                // the boolean final arg signifies that reference is absolute
                tree.setInnerTree(AttributeReference.createAttributeReference(null, tv.getStrValue(), true, objectPool));
                return (tree.size() != 0);
            }
            // not an identifier following the '.'
            throw new HyracksDataException("need identifier in selection expression (got" + Lexer.strLexToken(tt) + ")");
        // parenthesized expression
        case LEX_OPEN_PAREN:
            {
                isExpr = true;
                lexer.consumeToken();
                treeL = objectPool.mutableExprPool.get();
                parseExpression(treeL);
                if (treeL.getInnerTree() == null) {
                    tree.resetExprTree(null);
                    return false;
                }
                if ((tt = lexer.consumeToken()) != TokenType.LEX_CLOSE_PAREN) {
                    throw new HyracksDataException("exptected LEX_CLOSE_PAREN, but got " + Lexer.strLexToken(tt));
                // tree.resetExprTree(null);
                // return false;
                }
                // assume make operation will return a new tree
                tree.setInnerTree(Operation.createOperation(Operation.OpKind_PARENTHESES_OP, treeL, objectPool));
                return (tree.size() != 0);
            }
        // constants
        case LEX_OPEN_BOX:
            {
                isExpr = true;
                ClassAd newAd = objectPool.classAdPool.get();
                if (!parseClassAd(newAd)) {
                    tree.resetExprTree(null);
                    return false;
                }
                tree.setInnerTree(newAd);
            }
            return true;
        case LEX_OPEN_BRACE:
            {
                isExpr = true;
                ExprList newList = objectPool.exprListPool.get();
                if (!parseExprList(newList)) {
                    tree.setInnerTree(null);
                    return false;
                }
                tree.setInnerTree(newList);
            }
            return true;
        case LEX_UNDEFINED_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setUndefinedValue();
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_ERROR_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setErrorValue();
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_BOOLEAN_VALUE:
            {
                Value val = objectPool.valuePool.get();
                MutableBoolean b = new MutableBoolean();
                tv.getBoolValue(b);
                lexer.consumeToken();
                val.setBooleanValue(b);
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_INTEGER_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setIntegerValue(tv.getIntValue());
                tree.setInnerTree(Literal.createLiteral(val, tv.getFactor(), objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_REAL_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setRealValue(tv.getRealValue());
                tree.setInnerTree(Literal.createLiteral(val, tv.getFactor(), objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_STRING_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setStringValue(tv.getStrValue());
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_ABSOLUTE_TIME_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setAbsoluteTimeValue(tv.getTimeValue());
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_RELATIVE_TIME_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setRelativeTimeValue(tv.getTimeValue().getRelativeTime());
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        default:
            tree.setInnerTree(null);
            return false;
    }
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ClassAd(org.apache.asterix.external.classad.ClassAd) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) ExprList(org.apache.asterix.external.classad.ExprList) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) TokenValue(org.apache.asterix.external.classad.TokenValue)

Example 8 with ExprTreeHolder

use of org.apache.asterix.external.classad.ExprTreeHolder in project asterixdb by apache.

the class ClassAdParser method parseEqualityExpression.

// EqualityExpression .= RelationalExpression
// | EqualityExpression '==' RelationalExpression
// | EqualityExpression '!=' RelationalExpression
// | EqualityExpression '=?=' RelationalExpression
// | EqualityExpression '=!=' RelationalExpression
private boolean parseEqualityExpression(ExprTreeHolder tree) throws IOException {
    TokenType tt;
    int op = Operation.OpKind_NO_OP;
    if (!parseRelationalExpression(tree)) {
        return false;
    }
    tt = lexer.peekToken();
    while (tt == TokenType.LEX_EQUAL || tt == TokenType.LEX_NOT_EQUAL || tt == TokenType.LEX_META_EQUAL || tt == TokenType.LEX_META_NOT_EQUAL) {
        ExprTreeHolder treeL = tree;
        ExprTreeHolder treeR = objectPool.mutableExprPool.get();
        lexer.consumeToken();
        parseRelationalExpression(treeR);
        switch(tt) {
            case LEX_EQUAL:
                op = Operation.OpKind_EQUAL_OP;
                break;
            case LEX_NOT_EQUAL:
                op = Operation.OpKind_NOT_EQUAL_OP;
                break;
            case LEX_META_EQUAL:
                op = Operation.OpKind_META_EQUAL_OP;
                break;
            case LEX_META_NOT_EQUAL:
                op = Operation.OpKind_META_NOT_EQUAL_OP;
                break;
            default:
                throw new HyracksDataException("ClassAd:  Should not reach here");
        }
        if (treeL.getInnerTree() != null && treeR.getInnerTree() != null) {
            Operation newTree = objectPool.operationPool.get();
            Operation.createOperation(op, treeL, treeR, null, newTree);
            tree.setInnerTree(newTree);
        } else {
            tree.setInnerTree(null);
            return false;
        }
        tt = lexer.peekToken();
    }
    return true;
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) Operation(org.apache.asterix.external.classad.Operation) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 9 with ExprTreeHolder

use of org.apache.asterix.external.classad.ExprTreeHolder in project asterixdb by apache.

the class ClassAdParser method parseExpression.

private boolean parseExpression(ExprTreeHolder tree, boolean full) throws IOException {
    TokenType tt;
    if (!parseLogicalORExpression(tree)) {
        return false;
    }
    if ((tt = lexer.peekToken()) == TokenType.LEX_QMARK) {
        lexer.consumeToken();
        ExprTreeHolder treeL = tree;
        ExprTreeHolder treeM = objectPool.mutableExprPool.get();
        ExprTreeHolder treeR = objectPool.mutableExprPool.get();
        parseExpression(treeM);
        if ((tt = lexer.consumeToken()) != TokenType.LEX_COLON) {
            throw new HyracksDataException("expected LEX_COLON, but got " + Lexer.strLexToken(tt));
        }
        parseExpression(treeR);
        if (treeL.getInnerTree() != null && treeM.getInnerTree() != null && treeR.getInnerTree() != null) {
            Operation newTree = objectPool.operationPool.get();
            Operation.createOperation(Operation.OpKind_TERNARY_OP, treeL, treeM, treeR, newTree);
            tree.setInnerTree(newTree);
            return (true);
        }
        tree.setInnerTree(null);
        return false;
    }
    // if a full parse was requested, ensure that input is exhausted
    if (full && (lexer.consumeToken() != TokenType.LEX_END_OF_INPUT)) {
        throw new HyracksDataException("expected LEX_END_OF_INPUT on full parse, but got " + String.valueOf(Lexer.strLexToken(tt)));
    }
    return true;
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) Operation(org.apache.asterix.external.classad.Operation) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 10 with ExprTreeHolder

use of org.apache.asterix.external.classad.ExprTreeHolder in project asterixdb by apache.

the class ClassAdParser method parseNextExpression.

public ExprTree parseNextExpression() throws IOException {
    if (!lexer.wasInitialized()) {
        return null;
    } else {
        ExprTreeHolder expr = objectPool.mutableExprPool.get();
        parseExpression(expr, false);
        ExprTree innerTree = expr.getInnerTree();
        return innerTree;
    }
}
Also used : ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) ExprTree(org.apache.asterix.external.classad.ExprTree)

Aggregations

ExprTreeHolder (org.apache.asterix.external.classad.ExprTreeHolder)25 TokenType (org.apache.asterix.external.classad.Lexer.TokenType)13 Operation (org.apache.asterix.external.classad.Operation)13 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)13 TokenValue (org.apache.asterix.external.classad.TokenValue)5 Value (org.apache.asterix.external.classad.Value)5 ExprList (org.apache.asterix.external.classad.ExprList)2 Literal (org.apache.asterix.external.classad.Literal)2 IOException (java.io.IOException)1 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)1 AMutableNumberFactor (org.apache.asterix.external.classad.AMutableNumberFactor)1 AttributeReference (org.apache.asterix.external.classad.AttributeReference)1 ClassAd (org.apache.asterix.external.classad.ClassAd)1 ExprTree (org.apache.asterix.external.classad.ExprTree)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1