Search in sources :

Example 1 with BinaryExpression

use of org.apache.camel.language.simple.ast.BinaryExpression 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 2 with BinaryExpression

use of org.apache.camel.language.simple.ast.BinaryExpression in project camel by apache.

the class SimplePredicateParser method createNode.

/**
     * Creates a node from the given token
     *
     * @param token         the token
     * @param startSingle   state of single quoted blocks
     * @param startDouble   state of double quoted blocks
     * @param startFunction state of function blocks
     * @return the created node, or <tt>null</tt> to let a default node be created instead.
     */
private SimpleNode createNode(SimpleToken token, AtomicBoolean startSingle, AtomicBoolean startDouble, AtomicBoolean startFunction) {
    if (token.getType().isFunctionStart()) {
        startFunction.set(true);
        return new SimpleFunctionStart(token);
    } else if (token.getType().isFunctionEnd()) {
        startFunction.set(false);
        return new SimpleFunctionEnd(token);
    }
    // as we want all the tokens to be literal instead
    if (startFunction.get()) {
        return null;
    }
    // okay so far we also want to support quotes
    if (token.getType().isSingleQuote()) {
        SimpleNode answer;
        boolean start = startSingle.get();
        if (!start) {
            answer = new SingleQuoteStart(token);
        } else {
            answer = new SingleQuoteEnd(token);
        }
        // flip state on start/end flag
        startSingle.set(!start);
        return answer;
    } else if (token.getType().isDoubleQuote()) {
        SimpleNode answer;
        boolean start = startDouble.get();
        if (!start) {
            answer = new DoubleQuoteStart(token);
        } else {
            answer = new DoubleQuoteEnd(token);
        }
        // flip state on start/end flag
        startDouble.set(!start);
        return answer;
    }
    // as we want to only support embedded functions and all other kinds to be literal tokens
    if (startSingle.get() || startDouble.get()) {
        return null;
    }
    // and the special null value as well
    if (token.getType().isUnary()) {
        return new UnaryExpression(token);
    } else if (token.getType().isBinary()) {
        return new BinaryExpression(token);
    } else if (token.getType().isLogical()) {
        return new LogicalExpression(token);
    } else if (token.getType().isNullValue()) {
        return new NullExpression(token);
    }
    // by returning null, we will let the parser determine what to do
    return null;
}
Also used : LogicalExpression(org.apache.camel.language.simple.ast.LogicalExpression) BinaryExpression(org.apache.camel.language.simple.ast.BinaryExpression) DoubleQuoteEnd(org.apache.camel.language.simple.ast.DoubleQuoteEnd) SingleQuoteEnd(org.apache.camel.language.simple.ast.SingleQuoteEnd) DoubleQuoteStart(org.apache.camel.language.simple.ast.DoubleQuoteStart) SingleQuoteStart(org.apache.camel.language.simple.ast.SingleQuoteStart) UnaryExpression(org.apache.camel.language.simple.ast.UnaryExpression) SimpleFunctionEnd(org.apache.camel.language.simple.ast.SimpleFunctionEnd) SimpleFunctionStart(org.apache.camel.language.simple.ast.SimpleFunctionStart) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode) NullExpression(org.apache.camel.language.simple.ast.NullExpression)

Aggregations

BinaryExpression (org.apache.camel.language.simple.ast.BinaryExpression)2 SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)2 Stack (java.util.Stack)1 DoubleQuoteEnd (org.apache.camel.language.simple.ast.DoubleQuoteEnd)1 DoubleQuoteStart (org.apache.camel.language.simple.ast.DoubleQuoteStart)1 LogicalExpression (org.apache.camel.language.simple.ast.LogicalExpression)1 NullExpression (org.apache.camel.language.simple.ast.NullExpression)1 SimpleFunctionEnd (org.apache.camel.language.simple.ast.SimpleFunctionEnd)1 SimpleFunctionStart (org.apache.camel.language.simple.ast.SimpleFunctionStart)1 SingleQuoteEnd (org.apache.camel.language.simple.ast.SingleQuoteEnd)1 SingleQuoteStart (org.apache.camel.language.simple.ast.SingleQuoteStart)1 UnaryExpression (org.apache.camel.language.simple.ast.UnaryExpression)1 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)1