Search in sources :

Example 1 with ASTNode

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;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) StaticTextByteASTNode(org.beetl.core.statement.StaticTextByteASTNode) ASTNode(org.beetl.core.statement.ASTNode) StaticTextASTNode(org.beetl.core.statement.StaticTextASTNode) ArrayList(java.util.ArrayList) BlockStatement(org.beetl.core.statement.BlockStatement)

Example 2 with ASTNode

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++;
    }
}
Also used : IVarIndex(org.beetl.core.statement.IVarIndex) ASTNode(org.beetl.core.statement.ASTNode)

Example 3 with ASTNode

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();
}
Also used : Field(java.lang.reflect.Field) BeetlException(org.beetl.core.exception.BeetlException) Listener(org.beetl.core.Listener) ASTNode(org.beetl.core.statement.ASTNode) BeetlException(org.beetl.core.exception.BeetlException)

Example 4 with ASTNode

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;
}
Also used : VarAssignStatementSeq(org.beetl.core.statement.VarAssignStatementSeq) AssignMentContext(org.beetl.core.parser.BeetlParser.AssignMentContext) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) StaticTextByteASTNode(org.beetl.core.statement.StaticTextByteASTNode) ASTNode(org.beetl.core.statement.ASTNode) StaticTextASTNode(org.beetl.core.statement.StaticTextASTNode) ArrayList(java.util.ArrayList) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement)

Example 5 with ASTNode

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);
    }
}
Also used : IVarIndex(org.beetl.core.statement.IVarIndex) ASTNode(org.beetl.core.statement.ASTNode)

Aggregations

ASTNode (org.beetl.core.statement.ASTNode)6 IVarIndex (org.beetl.core.statement.IVarIndex)3 ArrayList (java.util.ArrayList)2 AjaxStatement (org.beetl.core.statement.AjaxStatement)2 BlockStatement (org.beetl.core.statement.BlockStatement)2 BreakStatement (org.beetl.core.statement.BreakStatement)2 ContinueStatement (org.beetl.core.statement.ContinueStatement)2 DirectiveStatement (org.beetl.core.statement.DirectiveStatement)2 EndStatement (org.beetl.core.statement.EndStatement)2 ForStatement (org.beetl.core.statement.ForStatement)2 GeneralForStatement (org.beetl.core.statement.GeneralForStatement)2 IfStatement (org.beetl.core.statement.IfStatement)2 ReturnStatement (org.beetl.core.statement.ReturnStatement)2 SelectStatement (org.beetl.core.statement.SelectStatement)2 Statement (org.beetl.core.statement.Statement)2 StaticTextASTNode (org.beetl.core.statement.StaticTextASTNode)2 StaticTextByteASTNode (org.beetl.core.statement.StaticTextByteASTNode)2 SwitchStatement (org.beetl.core.statement.SwitchStatement)2 TagStatement (org.beetl.core.statement.TagStatement)2 TagVarBindingStatement (org.beetl.core.statement.TagVarBindingStatement)2