Search in sources :

Example 6 with SimpleNode

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

the class SimplePredicateParser method createPredicates.

/**
     * Creates the {@link Predicate}s from the AST nodes.
     *
     * @return the created {@link Predicate}s, is never <tt>null</tt>.
     */
private List<Predicate> createPredicates() {
    List<Predicate> answer = new ArrayList<Predicate>();
    for (SimpleNode node : nodes) {
        Expression exp = node.createExpression(expression);
        if (exp != null) {
            Predicate predicate = ExpressionToPredicateAdapter.toPredicate(exp);
            answer.add(predicate);
        }
    }
    return answer;
}
Also used : Expression(org.apache.camel.Expression) NullExpression(org.apache.camel.language.simple.ast.NullExpression) LogicalExpression(org.apache.camel.language.simple.ast.LogicalExpression) LiteralExpression(org.apache.camel.language.simple.ast.LiteralExpression) BinaryExpression(org.apache.camel.language.simple.ast.BinaryExpression) UnaryExpression(org.apache.camel.language.simple.ast.UnaryExpression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode)

Example 7 with SimpleNode

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

the class BaseSimpleParser method prepareUnaryExpressions.

/**
     * Prepares unary expressions.
     * <p/>
     * This process prepares the unary expressions in the AST. This is done
     * by linking the unary operator with the left hand side node,
     * to have the AST graph updated and prepared properly.
     * <p/>
     * So when the AST node is later used to create the {@link org.apache.camel.Predicate}s
     * or {@link org.apache.camel.Expression}s to be used by Camel then the AST graph
     * has a linked and prepared graph of nodes which represent the input expression.
     */
protected void prepareUnaryExpressions() {
    Stack<SimpleNode> stack = new Stack<SimpleNode>();
    for (SimpleNode node : nodes) {
        if (node instanceof UnaryExpression) {
            UnaryExpression token = (UnaryExpression) node;
            // remember the logical operator
            String operator = token.getOperator().toString();
            SimpleNode previous = stack.isEmpty() ? null : stack.pop();
            if (previous == null) {
                throw new SimpleParserException("Unary operator " + operator + " has no left hand side token", token.getToken().getIndex());
            } else {
                token.acceptLeft(previous);
            }
        }
        stack.push(node);
    }
    // replace nodes from the stack
    nodes.clear();
    nodes.addAll(stack);
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) UnaryExpression(org.apache.camel.language.simple.ast.UnaryExpression) Stack(java.util.Stack) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode)

Example 8 with SimpleNode

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

SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)8 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)5 Stack (java.util.Stack)4 BinaryExpression (org.apache.camel.language.simple.ast.BinaryExpression)3 LiteralExpression (org.apache.camel.language.simple.ast.LiteralExpression)3 LogicalExpression (org.apache.camel.language.simple.ast.LogicalExpression)3 UnaryExpression (org.apache.camel.language.simple.ast.UnaryExpression)3 ArrayList (java.util.ArrayList)2 DoubleQuoteStart (org.apache.camel.language.simple.ast.DoubleQuoteStart)2 LiteralNode (org.apache.camel.language.simple.ast.LiteralNode)2 NullExpression (org.apache.camel.language.simple.ast.NullExpression)2 SimpleFunctionStart (org.apache.camel.language.simple.ast.SimpleFunctionStart)2 SingleQuoteStart (org.apache.camel.language.simple.ast.SingleQuoteStart)2 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Expression (org.apache.camel.Expression)1 Predicate (org.apache.camel.Predicate)1 Block (org.apache.camel.language.simple.ast.Block)1 BlockEnd (org.apache.camel.language.simple.ast.BlockEnd)1