Search in sources :

Example 1 with TokenValue

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

the class ClassAdParser method asterixParseClassAd.

private boolean asterixParseClassAd(ClassAd ad) throws IOException {
    TokenType tt;
    ad.clear();
    lexer.initialize(currentSource);
    if ((tt = lexer.consumeToken()) != TokenType.LEX_OPEN_BOX) {
        handleErrorParsing();
        return false;
    }
    tt = lexer.peekToken();
    TokenValue tv = objectPool.tokenValuePool.get();
    ExprTreeHolder tree = objectPool.mutableExprPool.get();
    while (tt != TokenType.LEX_CLOSE_BOX) {
        // Get the name of the expression
        tv.reset();
        tree.reset();
        tt = lexer.consumeToken(tv);
        if (tt == TokenType.LEX_SEMICOLON) {
            // that was bitten by this.
            continue;
        }
        if (tt != TokenType.LEX_IDENTIFIER) {
            throw new HyracksDataException("while parsing classad:  expected LEX_IDENTIFIER " + " but got " + Lexer.strLexToken(tt));
        }
        // consume the intermediate '='
        if ((tt = lexer.consumeToken()) != TokenType.LEX_BOUND_TO) {
            throw new HyracksDataException("while parsing classad:  expected LEX_BOUND_TO " + " but got " + Lexer.strLexToken(tt));
        }
        int positionBefore = lexer.getLexSource().getPosition();
        isExpr = false;
        // parse the expression
        parseExpression(tree);
        if (tree.getInnerTree() == null) {
            handleErrorParsing();
            throw new HyracksDataException("parse expression returned empty tree");
        }
        if ((!evaluateExpr || keepBoth) && isExpr && positionBefore >= 0) {
            // we will store a string representation of the expression
            int len = lexer.getLexSource().getPosition() - positionBefore - 2;
            // add it as it is to the classAd
            Literal lit = objectPool.literalPool.get();
            Value exprVal = objectPool.valuePool.get();
            exprVal.setStringValue((exprPrefix == null ? "" : exprPrefix) + String.valueOf(lexer.getLexSource().getBuffer(), positionBefore, len) + (exprSuffix == null ? "" : exprSuffix));
            Literal.createLiteral(lit, exprVal, NumberFactor.NO_FACTOR);
            if (!evaluateExpr) {
                ad.insert(tv.getStrValue().toString(), lit);
            } else {
                ad.insert(tv.getStrValue().toString() + exprFieldNameSuffix, lit);
            }
        }
        if (!isExpr || (evaluateExpr)) {
            // insert the attribute into the classad
            if (!ad.insert(tv.getStrValue().toString(), tree)) {
                handleErrorParsing();
                throw new HyracksDataException("Couldn't insert value to classad");
            }
        }
        // the next token must be a ';' or a ']'
        tt = lexer.peekToken();
        if (tt != TokenType.LEX_SEMICOLON && tt != TokenType.LEX_CLOSE_BOX) {
            handleErrorParsing();
            throw new HyracksDataException("while parsing classad:  expected LEX_SEMICOLON or " + "LEX_CLOSE_BOX but got " + Lexer.strLexToken(tt));
        }
        // while the first case accounts for optional beginning semicolons.
        while (tt == TokenType.LEX_SEMICOLON) {
            lexer.consumeToken();
            tt = lexer.peekToken();
        }
    }
    return true;
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) Literal(org.apache.asterix.external.classad.Literal) 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 2 with TokenValue

use of org.apache.asterix.external.classad.TokenValue 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 3 with TokenValue

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

the class ClassAdParser method parsePostfixExpression.

// PostfixExpression .= PrimaryExpression
// | PostfixExpression '.' Identifier
// | PostfixExpression '[' Expression ']'
private boolean parsePostfixExpression(ExprTreeHolder tree) throws IOException {
    TokenType tt;
    if (!parsePrimaryExpression(tree)) {
        return false;
    }
    while ((tt = lexer.peekToken()) == TokenType.LEX_OPEN_BOX || tt == TokenType.LEX_SELECTION) {
        ExprTreeHolder treeL = tree;
        ExprTreeHolder treeR = objectPool.mutableExprPool.get();
        TokenValue tv = objectPool.tokenValuePool.get();
        lexer.consumeToken();
        if (tt == TokenType.LEX_OPEN_BOX) {
            // subscript operation
            parseExpression(treeR);
            if (treeL.getInnerTree() != null && treeR.getInnerTree() != null) {
                Operation newTree = objectPool.operationPool.get();
                Operation.createOperation(Operation.OpKind_SUBSCRIPT_OP, treeL, treeR, null, newTree);
                if (lexer.consumeToken() == TokenType.LEX_CLOSE_BOX) {
                    tree.setInnerTree(newTree);
                    continue;
                }
            }
            tree.setInnerTree(null);
            return false;
        } else if (tt == TokenType.LEX_SELECTION) {
            // field selection operation
            if ((tt = lexer.consumeToken(tv)) != TokenType.LEX_IDENTIFIER) {
                throw new HyracksDataException("second argument of selector must be an " + "identifier (got" + String.valueOf(Lexer.strLexToken(tt)) + ")");
            }
            AttributeReference newTree = objectPool.attrRefPool.get();
            AttributeReference.createAttributeReference(treeL, tv.getStrValue(), false, newTree);
            tree.setInnerTree(newTree);
        }
    }
    return true;
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) AttributeReference(org.apache.asterix.external.classad.AttributeReference) Operation(org.apache.asterix.external.classad.Operation) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) TokenValue(org.apache.asterix.external.classad.TokenValue)

Example 4 with TokenValue

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

the class ClassAdParser method parseClassAd.

public boolean parseClassAd(ClassAd ad, boolean full) throws IOException {
    TokenType tt;
    ad.clear();
    if ((tt = lexer.consumeToken()) != TokenType.LEX_OPEN_BOX) {
        return false;
    }
    tt = lexer.peekToken();
    TokenValue tv = objectPool.tokenValuePool.get();
    ExprTreeHolder tree = objectPool.mutableExprPool.get();
    while (tt != TokenType.LEX_CLOSE_BOX) {
        // Get the name of the expression
        tv.reset();
        tree.reset();
        tt = lexer.consumeToken(tv);
        if (tt == TokenType.LEX_SEMICOLON) {
            // that was bitten by this.
            continue;
        }
        if (tt != TokenType.LEX_IDENTIFIER) {
            throw new HyracksDataException("while parsing classad:  expected LEX_IDENTIFIER " + " but got " + Lexer.strLexToken(tt));
        }
        // consume the intermediate '='
        if ((tt = lexer.consumeToken()) != TokenType.LEX_BOUND_TO) {
            throw new HyracksDataException("while parsing classad:  expected LEX_BOUND_TO " + " but got " + Lexer.strLexToken(tt));
        }
        isExpr = false;
        parseExpression(tree);
        if (tree.getInnerTree() == null) {
            throw new HyracksDataException("parse expression returned empty tree");
        }
        // insert the attribute into the classad
        if (!ad.insert(tv.getStrValue().toString(), tree)) {
            throw new HyracksDataException("Couldn't insert value to classad");
        }
        // the next token must be a ';' or a ']'
        tt = lexer.peekToken();
        if (tt != TokenType.LEX_SEMICOLON && tt != TokenType.LEX_CLOSE_BOX) {
            throw new HyracksDataException("while parsing classad:  expected LEX_SEMICOLON or " + "LEX_CLOSE_BOX but got " + Lexer.strLexToken(tt));
        }
        // while the first case accounts for optional beginning semicolons.
        while (tt == TokenType.LEX_SEMICOLON) {
            lexer.consumeToken();
            tt = lexer.peekToken();
        }
    }
    lexer.consumeToken();
    // if a full parse was requested, ensure that input is exhausted
    if (full && (lexer.consumeToken() != TokenType.LEX_END_OF_INPUT)) {
        throw new HyracksDataException("while parsing classad:  expected LEX_END_OF_INPUT for " + "full parse but got " + Lexer.strLexToken(tt));
    }
    return true;
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) TokenValue(org.apache.asterix.external.classad.TokenValue)

Aggregations

ExprTreeHolder (org.apache.asterix.external.classad.ExprTreeHolder)4 TokenType (org.apache.asterix.external.classad.Lexer.TokenType)4 TokenValue (org.apache.asterix.external.classad.TokenValue)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)4 Value (org.apache.asterix.external.classad.Value)2 AttributeReference (org.apache.asterix.external.classad.AttributeReference)1 ClassAd (org.apache.asterix.external.classad.ClassAd)1 ExprList (org.apache.asterix.external.classad.ExprList)1 Literal (org.apache.asterix.external.classad.Literal)1 Operation (org.apache.asterix.external.classad.Operation)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1