Search in sources :

Example 1 with LiteralNode

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

the class SimpleExpressionParser method parseAndCreateAstModel.

protected void parseAndCreateAstModel() {
    // we loop the tokens and create a sequence of ast nodes
    // counter to keep track of number of functions in the tokens
    AtomicInteger functions = new AtomicInteger();
    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, functions);
        if (node != null) {
            // 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);
    }
}
Also used : LiteralNode(org.apache.camel.language.simple.ast.LiteralNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LiteralExpression(org.apache.camel.language.simple.ast.LiteralExpression) SimpleToken(org.apache.camel.language.simple.types.SimpleToken) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode)

Example 2 with LiteralNode

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

Aggregations

LiteralExpression (org.apache.camel.language.simple.ast.LiteralExpression)2 LiteralNode (org.apache.camel.language.simple.ast.LiteralNode)2 SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)2 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DoubleQuoteStart (org.apache.camel.language.simple.ast.DoubleQuoteStart)1 SimpleFunctionStart (org.apache.camel.language.simple.ast.SimpleFunctionStart)1 SingleQuoteStart (org.apache.camel.language.simple.ast.SingleQuoteStart)1 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)1