// 弹栈
if (directiveClass == EndDirective.class
|| directiveClass == ElseDirective.class) {
if (directiveStack.isEmpty())
throw new ParseException("Miss #end directive.", directive.getOffset());
BlockDirective blockDirective = ((BlockDirectiveEntry) directiveStack.pop()).popDirective();
if (blockDirective == rootDirective)
throw new ParseException("Miss #end directive.", directive.getOffset());
EndDirective endDirective;
if (directiveClass == ElseDirective.class) {
endDirective = new EndDirective(directive.getOffset());
} else {
endDirective = (EndDirective) directive;
}
blockDirective.setEnd(endDirective);
}
// 设置树
if (directiveClass != EndDirective.class) { // 排除EndDirective
if (directiveStack.isEmpty())
throw new ParseException("Miss #end directive.", directive.getOffset());
((BlockDirectiveEntry) directiveStack.peek()).appendInnerDirective(directive);
}
// 压栈
if (directive instanceof BlockDirective)
directiveStack.push(new BlockDirectiveEntry((BlockDirective) directive));
}
BlockDirective root = (BlockDirective) ((BlockDirectiveEntry) directiveStack.pop()).popDirective();
if (! directiveStack.isEmpty()) { // 后验条件
throw new ParseException("Miss #end directive." + root.getClass().getSimpleName(), root.getOffset());
}
return root;
}