Search in sources :

Example 6 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class SimplePredicateParser method binaryOperator.

protected boolean binaryOperator() {
    if (accept(TokenType.binaryOperator)) {
        // remember the binary operator
        BinaryOperatorType operatorType = BinaryOperatorType.asOperator(token.getText());
        nextToken();
        // there should be at least one whitespace after the operator
        expectAndAcceptMore(TokenType.whiteSpace);
        // okay a binary operator may not support all kind if preceding parameters, so we need to limit this
        BinaryOperatorType.ParameterType[] types = BinaryOperatorType.supportedParameterTypes(operatorType);
        // based on the parameter types the binary operator support, we need to set this state into
        // the following booleans so we know how to proceed in the grammar
        boolean literalWithFunctionsSupported = false;
        boolean literalSupported = false;
        boolean functionSupported = false;
        boolean numericSupported = false;
        boolean booleanSupported = false;
        boolean nullSupported = false;
        if (types == null || types.length == 0) {
            literalWithFunctionsSupported = true;
            // favor literal with functions over literals without functions
            literalSupported = false;
            functionSupported = true;
            numericSupported = true;
            booleanSupported = true;
            nullSupported = true;
        } else {
            for (BinaryOperatorType.ParameterType parameterType : types) {
                literalSupported |= parameterType.isLiteralSupported();
                literalWithFunctionsSupported |= parameterType.isLiteralWithFunctionSupport();
                functionSupported |= parameterType.isFunctionSupport();
                nullSupported |= parameterType.isNumericValueSupported();
                booleanSupported |= parameterType.isBooleanValueSupported();
                nullSupported |= parameterType.isNullValueSupported();
            }
        }
        //CHECKSTYLE:OFF
        if ((literalWithFunctionsSupported && singleQuotedLiteralWithFunctionsText()) || (literalWithFunctionsSupported && doubleQuotedLiteralWithFunctionsText()) || (literalSupported && singleQuotedLiteralText()) || (literalSupported && doubleQuotedLiteralText()) || (functionSupported && functionText()) || (numericSupported && numericValue()) || (booleanSupported && booleanValue()) || (nullSupported && nullValue())) {
            // then after the right hand side value, there should be a whitespace if there is more tokens
            nextToken();
            if (!token.getType().isEol()) {
                expect(TokenType.whiteSpace);
            }
        } else {
            throw new SimpleParserException("Binary operator " + operatorType + " does not support token " + token, token.getIndex());
        }
        //CHECKSTYLE:ON
        return true;
    }
    return false;
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) BinaryOperatorType(org.apache.camel.language.simple.types.BinaryOperatorType)

Example 7 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class UnaryExpression method createExpression.

@Override
public Expression createExpression(String expression) {
    ObjectHelper.notNull(left, "left node", this);
    final Expression leftExp = left.createExpression(expression);
    if (operator == UnaryOperatorType.INC) {
        return createIncExpression(leftExp);
    } else if (operator == UnaryOperatorType.DEC) {
        return createDecExpression(leftExp);
    }
    throw new SimpleParserException("Unknown unary operator " + operator, token.getIndex());
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) Expression(org.apache.camel.Expression)

Example 8 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class SimplePredicateParser method logicalOperator.

protected boolean logicalOperator() {
    if (accept(TokenType.logicalOperator)) {
        // remember the logical operator
        LogicalOperatorType operatorType = LogicalOperatorType.asOperator(token.getText());
        nextToken();
        // there should be at least one whitespace after the operator
        expectAndAcceptMore(TokenType.whiteSpace);
        // then we expect either some quoted text, another function, or a numeric, boolean or null value
        if (singleQuotedLiteralWithFunctionsText() || doubleQuotedLiteralWithFunctionsText() || functionText() || numericValue() || booleanValue() || nullValue()) {
            // then after the right hand side value, there should be a whitespace if there is more tokens
            nextToken();
            if (!token.getType().isEol()) {
                expect(TokenType.whiteSpace);
            }
        } else {
            throw new SimpleParserException("Logical operator " + operatorType + " does not support token " + token, token.getIndex());
        }
        return true;
    }
    return false;
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) LogicalOperatorType(org.apache.camel.language.simple.types.LogicalOperatorType)

Example 9 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class SimplePredicateParser method prepareBinaryExpressions.

/**
     * Prepares binary expressions.
     * <p/>
     * This process prepares the binary expressions in the AST. This is done
     * by linking the binary operator with both the right and left hand side
     * nodes, to have the AST graph updated and prepared properly.
     * <p/>
     * So when the AST node is later used to create the {@link Predicate}s
     * to be used by Camel then the AST graph has a linked and prepared
     * graph of nodes which represent the input expression.
     */
private void prepareBinaryExpressions() {
    Stack<SimpleNode> stack = new Stack<SimpleNode>();
    SimpleNode left = null;
    for (int i = 0; i < nodes.size(); i++) {
        if (left == null) {
            left = i > 0 ? nodes.get(i - 1) : null;
        }
        SimpleNode token = nodes.get(i);
        SimpleNode right = i < nodes.size() - 1 ? nodes.get(i + 1) : null;
        if (token instanceof BinaryExpression) {
            BinaryExpression binary = (BinaryExpression) token;
            // remember the binary operator
            String operator = binary.getOperator().toString();
            if (left == null) {
                throw new SimpleParserException("Binary operator " + operator + " has no left hand side token", token.getToken().getIndex());
            }
            if (!binary.acceptLeftNode(left)) {
                throw new SimpleParserException("Binary operator " + operator + " does not support left hand side token " + left.getToken(), token.getToken().getIndex());
            }
            if (right == null) {
                throw new SimpleParserException("Binary operator " + operator + " has no right hand side token", token.getToken().getIndex());
            }
            if (!binary.acceptRightNode(right)) {
                throw new SimpleParserException("Binary operator " + operator + " does not support right hand side token " + right.getToken(), token.getToken().getIndex());
            }
            // pop previous as we need to replace it with this binary operator
            stack.pop();
            stack.push(token);
            // advantage after the right hand side
            i++;
            // this token is now the left for the next loop
            left = token;
        } else {
            // clear left
            left = null;
            stack.push(token);
        }
    }
    nodes.clear();
    nodes.addAll(stack);
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) BinaryExpression(org.apache.camel.language.simple.ast.BinaryExpression) Stack(java.util.Stack) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode)

Example 10 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class BinaryExpression method createExpression.

@Override
public Expression createExpression(String expression) {
    ObjectHelper.notNull(left, "left node", this);
    ObjectHelper.notNull(right, "right node", this);
    final Expression leftExp = left.createExpression(expression);
    final Expression rightExp = right.createExpression(expression);
    if (operator == BinaryOperatorType.EQ) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isEqualTo(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.EQ_IGNORE) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isEqualToIgnoreCase(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.GT) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isGreaterThan(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.GTE) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isGreaterThanOrEqualTo(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.LT) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isLessThan(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.LTE) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isLessThanOrEqualTo(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.NOT_EQ) {
        return createExpression(leftExp, rightExp, PredicateBuilder.isNotEqualTo(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.CONTAINS) {
        return createExpression(leftExp, rightExp, PredicateBuilder.contains(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.NOT_CONTAINS) {
        return createExpression(leftExp, rightExp, PredicateBuilder.not(PredicateBuilder.contains(leftExp, rightExp)));
    } else if (operator == BinaryOperatorType.IS || operator == BinaryOperatorType.NOT_IS) {
        return createIsExpression(expression, leftExp, rightExp);
    } else if (operator == BinaryOperatorType.REGEX || operator == BinaryOperatorType.NOT_REGEX) {
        return createRegexExpression(leftExp, rightExp);
    } else if (operator == BinaryOperatorType.IN || operator == BinaryOperatorType.NOT_IN) {
        return createInExpression(leftExp, rightExp);
    } else if (operator == BinaryOperatorType.RANGE || operator == BinaryOperatorType.NOT_RANGE) {
        return createRangeExpression(expression, leftExp, rightExp);
    } else if (operator == BinaryOperatorType.STARTS_WITH) {
        return createExpression(leftExp, rightExp, PredicateBuilder.startsWith(leftExp, rightExp));
    } else if (operator == BinaryOperatorType.ENDS_WITH) {
        return createExpression(leftExp, rightExp, PredicateBuilder.endsWith(leftExp, rightExp));
    }
    throw new SimpleParserException("Unknown binary operator " + operator, token.getIndex());
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) Expression(org.apache.camel.Expression)

Aggregations

SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)12 Expression (org.apache.camel.Expression)5 SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)5 Stack (java.util.Stack)4 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Exchange (org.apache.camel.Exchange)1 BinaryExpression (org.apache.camel.language.simple.ast.BinaryExpression)1 Block (org.apache.camel.language.simple.ast.Block)1 BlockEnd (org.apache.camel.language.simple.ast.BlockEnd)1 BlockStart (org.apache.camel.language.simple.ast.BlockStart)1 DoubleQuoteStart (org.apache.camel.language.simple.ast.DoubleQuoteStart)1 LiteralExpression (org.apache.camel.language.simple.ast.LiteralExpression)1 LiteralNode (org.apache.camel.language.simple.ast.LiteralNode)1 LogicalExpression (org.apache.camel.language.simple.ast.LogicalExpression)1 SimpleFunctionStart (org.apache.camel.language.simple.ast.SimpleFunctionStart)1 SingleQuoteStart (org.apache.camel.language.simple.ast.SingleQuoteStart)1 UnaryExpression (org.apache.camel.language.simple.ast.UnaryExpression)1 BinaryOperatorType (org.apache.camel.language.simple.types.BinaryOperatorType)1