Search in sources :

Example 1 with ExpressionListContext

use of org.beetl.core.parser.BeetlParser.ExpressionListContext in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseFunExp.

protected FunctionExpression parseFunExp(FunctionCallContext ctx) {
    ExpressionListContext expListCtx = ctx.expressionList();
    Expression[] exps = this.getExprssionList(expListCtx);
    List<VarAttributeContext> vaListCtx = ctx.varAttribute();
    Safe_outputContext soctx = ctx.safe_output();
    Expression safeExp = null;
    boolean hasSafe = false;
    if (soctx != null) {
        safeExp = this.parseSafeOutput(soctx);
        hasSafe = true;
    }
    if (this.pbCtx.isSafeOutput) {
        hasSafe = true;
    }
    VarAttribute[] vs = this.parseVarAttribute(vaListCtx);
    List<TerminalNode> idList = ctx.functionNs().Identifier();
    String nsId = this.getID(idList);
    GrammarToken btToken = new org.beetl.core.statement.GrammarToken(nsId, ctx.start.getLine(), 0);
    // 需要做些特殊处理的函数
    if (safeParameters.contains(nsId)) {
        if (exps.length != 0) {
            Expression one = exps[0];
            if (one instanceof VarRef) {
                // 强制为变量引用增加一个安全输出
                VarRef ref = (VarRef) one;
                if (!ref.hasSafe) {
                    ref.hasSafe = true;
                    ref.safe = null;
                }
            }
        }
    } else if (nsId.equals("has")) {
        if (exps.length != 0) {
            Expression one = exps[0];
            if (one instanceof VarRef) {
                // 强制为变量引用增加一个安全输出
                VarRef ref = (VarRef) one;
                String name = ref.token.text;
                Literal newExp = new Literal(name, ref.token);
                // 将变量引用转化为字符串
                exps[0] = newExp;
            }
        }
    } else if (nsId.equals("debug")) {
        // debug函数传递额外的行数
        Literal l = new Literal(btToken.line, btToken);
        Expression[] newExps = new Expression[exps.length + 2];
        System.arraycopy(exps, 0, newExps, 0, exps.length);
        String[] expStr = this.getExpressionString(expListCtx);
        newExps[newExps.length - 2] = new Literal(expStr, btToken);
        newExps[newExps.length - 1] = l;
        for (int i = 0; i < exps.length; i++) {
            if (!(exps[i] instanceof VarRef)) {
                expStr[i] = null;
            }
        }
        exps = newExps;
    // 可以通过配置查看是否支持debug,2.1再做
    } else if (nsId.equals("decode")) {
        Expression[] newExps = new Expression[exps.length];
        if (newExps.length >= 4) {
            newExps[0] = exps[0];
            newExps[1] = exps[1];
            for (int i = 2; i < exps.length; i++) {
                // 参数改成runtime 执行
                newExps[i] = new ExpressionRuntime(exps[i]);
            }
            exps = newExps;
        } else {
        // 错误的使用了decode函数,不管了,等后面报错吧
        }
    }
    FunctionExpression fe = new FunctionExpression(nsId, exps, vs, hasSafe, safeExp, btToken);
    return fe;
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarAttributeContext(org.beetl.core.parser.BeetlParser.VarAttributeContext) ExpressionListContext(org.beetl.core.parser.BeetlParser.ExpressionListContext) FunctionExpression(org.beetl.core.statement.FunctionExpression) Safe_outputContext(org.beetl.core.parser.BeetlParser.Safe_outputContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) VarAttribute(org.beetl.core.statement.VarAttribute) Literal(org.beetl.core.statement.Literal) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ExpressionRuntime(org.beetl.core.statement.ExpressionRuntime) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 2 with ExpressionListContext

use of org.beetl.core.parser.BeetlParser.ExpressionListContext in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseTag.

protected TagStatement parseTag(FunctionTagCallContext fc) {
    String id = this.getID(fc.functionNs().Identifier());
    ExpressionListContext expListCtx = fc.expressionList();
    List<ExpressionContext> list = null;
    if (expListCtx != null) {
        list = fc.expressionList().expression();
    } else {
        list = Collections.EMPTY_LIST;
    }
    Expression[] expList = this.parseExpressionCtxList(list);
    if (id.equals("htmltagvar")) {
        int line = fc.functionNs().getStart().getLine();
        // 标签具有绑定变量功能
        Literal l = (Literal) expList[2];
        String varList = (String) l.obj;
        String[] vars = varList.split(",");
        // 定义的变量仅仅在标签体内可见
        this.pbCtx.enterBlock();
        VarDefineNode[] varDefine = new VarDefineNode[vars.length];
        for (int i = 0; i < vars.length; i++) {
            VarDefineNode varNode = new VarDefineNode(this.getBTToken(vars[i].trim(), line));
            this.pbCtx.addVarAndPostion(varNode);
            varDefine[i] = varNode;
        }
        BlockContext blockCtx = fc.block();
        Statement block = parseBlock(blockCtx.statement(), blockCtx);
        this.pbCtx.exitBlock();
        TagFactory tf = this.gt.getTagFactory(id);
        if (tf == null) {
            BeetlException ex = new BeetlException(BeetlException.TAG_NOT_FOUND);
            ex.pushToken(this.getBTToken(id, fc.functionNs().getStart().getLine()));
            throw ex;
        }
        TagStatement tag = new TagVarBindingStatement(id, expList, block, varDefine, this.getBTToken(id, line));
        return tag;
    } else {
        BlockContext blockCtx = fc.block();
        Statement block = parseBlock(blockCtx.statement(), blockCtx);
        TagFactory tf = this.gt.getTagFactory(id);
        if (tf == null) {
            BeetlException ex = new BeetlException(BeetlException.TAG_NOT_FOUND);
            ex.pushToken(this.getBTToken(id, fc.functionNs().getStart().getLine()));
            throw ex;
        }
        TagStatement tag = new TagStatement(id, expList, block, this.getBTToken(id, fc.functionNs().getStart().getLine()));
        return tag;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) 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) ExpressionListContext(org.beetl.core.parser.BeetlParser.ExpressionListContext) TagStatement(org.beetl.core.statement.TagStatement) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) Literal(org.beetl.core.statement.Literal) VarDefineNode(org.beetl.core.statement.VarDefineNode) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement)

Aggregations

ExpressionListContext (org.beetl.core.parser.BeetlParser.ExpressionListContext)2 AndExpression (org.beetl.core.statement.AndExpression)2 ArthExpression (org.beetl.core.statement.ArthExpression)2 CompareExpression (org.beetl.core.statement.CompareExpression)2 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)2 Expression (org.beetl.core.statement.Expression)2 FormatExpression (org.beetl.core.statement.FormatExpression)2 FunctionExpression (org.beetl.core.statement.FunctionExpression)2 IncDecExpression (org.beetl.core.statement.IncDecExpression)2 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)2 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)2 Literal (org.beetl.core.statement.Literal)2 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)2 NegExpression (org.beetl.core.statement.NegExpression)2 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)2 OrExpression (org.beetl.core.statement.OrExpression)2 StatementExpression (org.beetl.core.statement.StatementExpression)2 TernaryExpression (org.beetl.core.statement.TernaryExpression)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 BeetlException (org.beetl.core.exception.BeetlException)1