Search in sources :

Example 1 with BlockStart

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

the class BaseSimpleParser method prepareBlocks.

/**
     * Prepares blocks, such as functions, single or double quoted texts.
     * <p/>
     * This process prepares the {@link Block}s in the AST. This is done
     * by linking child {@link SimpleNode nodes} which are within the start and end of the blocks,
     * as child to the given block. This is done 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 prepareBlocks() {
    List<SimpleNode> answer = new ArrayList<SimpleNode>();
    Stack<Block> stack = new Stack<Block>();
    for (SimpleNode token : nodes) {
        if (token instanceof BlockStart) {
            // a new block is started, so push on the stack
            stack.push((Block) token);
        } else if (token instanceof BlockEnd) {
            // end block is just an abstract mode, so we should not add it
            if (stack.isEmpty()) {
                throw new SimpleParserException(token.getToken().getType().getType() + " has no matching start token", token.getToken().getIndex());
            }
            Block top = stack.pop();
            // if there is a block on the stack then it should accept the child token
            Block block = stack.isEmpty() ? null : stack.peek();
            if (block != null) {
                if (!block.acceptAndAddNode(top)) {
                    throw new SimpleParserException(block.getToken().getType() + " cannot accept " + token.getToken().getType(), token.getToken().getIndex());
                }
            } else {
                // no block, so add to answer
                answer.add(top);
            }
        } else {
            // if there is a block on the stack then it should accept the child token
            Block block = stack.isEmpty() ? null : stack.peek();
            if (block != null) {
                if (!block.acceptAndAddNode(token)) {
                    throw new SimpleParserException(block.getToken().getType() + " cannot accept " + token.getToken().getType(), token.getToken().getIndex());
                }
            } else {
                // no block, so add to answer
                answer.add(token);
            }
        }
    }
    // replace nodes from the stack
    nodes.clear();
    nodes.addAll(answer);
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) BlockStart(org.apache.camel.language.simple.ast.BlockStart) BlockEnd(org.apache.camel.language.simple.ast.BlockEnd) ArrayList(java.util.ArrayList) Block(org.apache.camel.language.simple.ast.Block) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode) Stack(java.util.Stack)

Aggregations

ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)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 SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)1 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)1