Search in sources :

Example 1 with DoubleQuoteStart

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

the class SimplePredicateParser method parseTokensAndCreateNodes.

/**
     * Parses the tokens and crates the AST nodes.
     * <p/>
     * After the initial parsing of the input (input -> tokens) then we
     * parse again (tokens -> ast).
     * <p/>
     * In this parsing the balance of the blocks is checked, so that each block has a matching
     * start and end token. For example a single quote block, or a function block etc.
     */
protected void parseTokensAndCreateNodes() {
    // we loop the tokens and create a sequence of ast nodes
    // we need to keep a bit of state for keeping track of single and double quotes
    // which need to be balanced and have matching start/end pairs
    SimpleNode lastSingle = null;
    SimpleNode lastDouble = null;
    SimpleNode lastFunction = null;
    AtomicBoolean startSingle = new AtomicBoolean(false);
    AtomicBoolean startDouble = new AtomicBoolean(false);
    AtomicBoolean startFunction = new AtomicBoolean(false);
    LiteralNode imageToken = null;
    for (SimpleToken token : tokens) {
        // break if eol
        if (token.getType().isEol()) {
            break;
        }
        // create a node from the token
        SimpleNode node = createNode(token, startSingle, startDouble, startFunction);
        if (node != null) {
            // keep state of last single/double
            if (node instanceof SingleQuoteStart) {
                lastSingle = node;
            } else if (node instanceof DoubleQuoteStart) {
                lastDouble = node;
            } else if (node instanceof SimpleFunctionStart) {
                lastFunction = node;
            }
            // a new token was created so the current image token need to be added first
            if (imageToken != null) {
                nodes.add(imageToken);
                imageToken = null;
            }
            // and then add the created node
            nodes.add(node);
            // continue to next
            continue;
        }
        // which we need to add together in the same image
        if (imageToken == null) {
            imageToken = new LiteralExpression(token);
        }
        imageToken.addText(token.getText());
    }
    // append any leftover image tokens (when we reached eol)
    if (imageToken != null) {
        nodes.add(imageToken);
    }
    // validate the single, double quote pairs and functions is in balance
    if (startSingle.get()) {
        int index = lastSingle != null ? lastSingle.getToken().getIndex() : 0;
        throw new SimpleParserException("single quote has no ending quote", index);
    }
    if (startDouble.get()) {
        int index = lastDouble != null ? lastDouble.getToken().getIndex() : 0;
        throw new SimpleParserException("double quote has no ending quote", index);
    }
    if (startFunction.get()) {
        // we have a start function, but no ending function
        int index = lastFunction != null ? lastFunction.getToken().getIndex() : 0;
        throw new SimpleParserException("function has no ending token", index);
    }
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LiteralNode(org.apache.camel.language.simple.ast.LiteralNode) LiteralExpression(org.apache.camel.language.simple.ast.LiteralExpression) DoubleQuoteStart(org.apache.camel.language.simple.ast.DoubleQuoteStart) SingleQuoteStart(org.apache.camel.language.simple.ast.SingleQuoteStart) SimpleToken(org.apache.camel.language.simple.types.SimpleToken) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode) SimpleFunctionStart(org.apache.camel.language.simple.ast.SimpleFunctionStart)

Example 2 with DoubleQuoteStart

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

DoubleQuoteStart (org.apache.camel.language.simple.ast.DoubleQuoteStart)2 SimpleFunctionStart (org.apache.camel.language.simple.ast.SimpleFunctionStart)2 SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)2 SingleQuoteStart (org.apache.camel.language.simple.ast.SingleQuoteStart)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BinaryExpression (org.apache.camel.language.simple.ast.BinaryExpression)1 DoubleQuoteEnd (org.apache.camel.language.simple.ast.DoubleQuoteEnd)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 NullExpression (org.apache.camel.language.simple.ast.NullExpression)1 SimpleFunctionEnd (org.apache.camel.language.simple.ast.SimpleFunctionEnd)1 SingleQuoteEnd (org.apache.camel.language.simple.ast.SingleQuoteEnd)1 UnaryExpression (org.apache.camel.language.simple.ast.UnaryExpression)1 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)1 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)1