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);
}
Aggregations