use of org.beetl.core.statement.ASTNode in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseBlock.
protected BlockStatement parseBlock(List list, ParserRuleContext ctx) {
pbCtx.enterBlock();
ASTNode[] statements = new ASTNode[list.size()];
List<Statement> nodes = new ArrayList<Statement>();
boolean hasGoto = false;
for (int i = 0; i < statements.length; i++) {
nodes.add(parseStatment((ParserRuleContext) list.get(i)));
}
BlockStatement block = new BlockStatement(nodes.toArray(new Statement[0]), this.getBTToken(ctx.getStart()));
this.checkGoto(block);
pbCtx.exitBlock();
return block;
}
use of org.beetl.core.statement.ASTNode in project beetl2.0 by javamonkey.
the class VarDescrption method anzlyszeGlobal.
public void anzlyszeGlobal() {
int index = 0;
for (Entry<String, VarDescrption> entry : globalVar.entrySet()) {
globalIndexMap.put(entry.getKey(), index);
VarDescrption vd = entry.getValue();
String[] attrs = vd.attrList.toArray(new String[0]);
globaVarAttr.put(entry.getKey(), attrs);
for (ASTNode node : vd.where) {
((IVarIndex) node).setVarIndex(index);
}
index++;
}
}
use of org.beetl.core.statement.ASTNode in project beetl2.0 by javamonkey.
the class StatementParser method exec.
protected void exec(Object astNode, Class[] matchClasses, Stack stack) {
stack.push(astNode);
Class astNodeClass = astNode.getClass();
Field[] fields = astNodeClass.getFields();
for (Field f : fields) {
if (f.getModifiers() != Modifier.PUBLIC)
continue;
Class target = null;
Class c = f.getType();
if (c.isArray()) {
target = c.getComponentType();
} else {
target = c;
}
// 只解析含有ASTNode的字段
if (!ASTNode.class.isAssignableFrom(target))
continue;
Object values;
try {
// 需要判断的节点
values = f.get(astNode);
if (values == null)
continue;
// 具体类型
Class target2 = values.getClass();
if (target2.isArray()) {
Object[] array = (Object[]) values;
if (array.length == 0)
continue;
for (int i = 0; i < array.length; i++) {
Object item = array[i];
if (item == null)
continue;
Class target3 = item.getClass();
if (match(target3, matchClasses)) {
stack.push(item);
Listener ls = this.listeners.get(target3);
NodeEvent e = new NodeEvent(stack);
Object newASTNode = ls.onEvent(e);
if (newASTNode != null) {
stack.pop();
stack.push(newASTNode);
// 替换原有节点
array[i] = newASTNode;
item = newASTNode;
}
// 继续遍历子节点
this.exec(item, matchClasses, stack);
stack.pop();
} else {
ASTNode node = (ASTNode) item;
this.exec(node, matchClasses, stack);
}
}
} else {
if (match(target2, matchClasses)) {
stack.push(values);
Listener ls = this.listeners.get(target2);
NodeEvent e = new NodeEvent(stack);
Object newASTNode = ls.onEvent(e);
if (newASTNode != null) {
stack.pop();
stack.push(newASTNode);
// 替换原有节点
try {
f.set(astNode, newASTNode);
} catch (Exception ex) {
BeetlException be = new BeetlException(BeetlException.ERROR, "替换ASTNode错", ex);
be.pushToken(((ASTNode) newASTNode).token);
throw be;
}
values = newASTNode;
}
this.exec(values, matchClasses, stack);
stack.pop();
continue;
} else {
ASTNode node = (ASTNode) values;
this.exec(node, matchClasses, stack);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
stack.pop();
}
use of org.beetl.core.statement.ASTNode in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseVarDeclareList.
private VarAssignStatementSeq parseVarDeclareList(VarDeclareListContext ctx) {
List<AssignMentContext> list = ctx.assignMent();
List<ASTNode> listNode = new ArrayList<ASTNode>();
for (AssignMentContext amc : list) {
VarAssignStatement vas = this.parseAssign(amc);
listNode.add(vas);
if (!(vas instanceof VarRefAssignStatement)) {
// 如果是临时变量定义
this.registerNewVar(vas);
}
}
VarAssignStatementSeq seq = new VarAssignStatementSeq(listNode.toArray(new Statement[0]), null);
return seq;
}
use of org.beetl.core.statement.ASTNode in project beetl2.0 by javamonkey.
the class VarDescrption method anzlysze.
private void anzlysze(BlockEnvContext block, int nextIndex, boolean isRoot) {
for (Entry<String, VarDescrption> entry : block.vars.entrySet()) {
VarDescrption vd = entry.getValue();
// if (!vd.where.isEmpty()) {
for (ASTNode node : vd.where) {
((IVarIndex) node).setVarIndex(nextIndex);
if (isRoot) {
this.rootIndexMap.put(vd.getVarName(), nextIndex);
}
}
nextIndex++;
// }
}
varIndexSize = Math.max(varIndexSize, nextIndex);
for (BlockEnvContext subBlock : block.blockList) {
anzlysze(subBlock, nextIndex, false);
int inc = subBlock.vars.size();
varIndexSize = Math.max(varIndexSize, nextIndex + inc);
}
}
Aggregations