Search in sources :

Example 6 with Expression

use of org.beetl.core.statement.Expression in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseVarRefInLeftExpression.

protected VarRef parseVarRefInLeftExpression(VarRefContext varRef) {
    Expression safeExp = null;
    Safe_outputContext soctx = varRef.safe_output();
    if (soctx != null) {
        throw new BeetlException(BeetlException.ERROR, "语法错,赋值表达式不能使用安全输出");
    }
    List<VarAttributeContext> list = varRef.varAttribute();
    VarAttribute[] vas = this.parseVarAttribute(list);
    // 变量属性,用来收集,暂时未用上
    if (vas.length > 0) {
        VarAttribute first = vas[0];
        if (!(first instanceof VarSquareAttribute || first instanceof VarVirtualAttribute)) {
            pbCtx.setVarAttr(varRef.Identifier().getText(), first.token.text);
        }
    }
    VarRef var = new VarRef(vas, false, null, this.getBTToken(varRef.getText(), varRef.Identifier().getSymbol().getLine()), this.getBTToken(varRef.Identifier().getSymbol()));
    pbCtx.setVarPosition(varRef.Identifier().getText(), var);
    return var;
}
Also used : VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) VarRef(org.beetl.core.statement.VarRef) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) VarAttributeContext(org.beetl.core.parser.BeetlParser.VarAttributeContext) 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)

Example 7 with Expression

use of org.beetl.core.statement.Expression in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseCompareExpression.

protected CompareExpression parseCompareExpression(CompareExpContext ctx) {
    Expression a = this.parseExpress(ctx.expression(0));
    Expression b = this.parseExpress(ctx.expression(1));
    TerminalNode tn = (TerminalNode) ctx.children.get(1);
    short mode = 0;
    if (ctx.EQUAL() != null) {
        mode = 0;
    } else if (ctx.NOT_EQUAL() != null) {
        mode = 1;
    } else if (ctx.LARGE() != null) {
        mode = 2;
    } else if (ctx.LARGE_EQUAL() != null) {
        mode = 3;
    } else if (ctx.LESS() != null) {
        mode = 4;
    } else if (ctx.LESS_EQUAL() != null) {
        mode = 5;
    }
    return new CompareExpression(a, b, mode, this.getBTToken(tn.getSymbol()));
}
Also used : 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) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) CompareExpression(org.beetl.core.statement.CompareExpression)

Example 8 with Expression

use of org.beetl.core.statement.Expression 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 9 with Expression

use of org.beetl.core.statement.Expression in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseTernaryExpression.

protected Expression parseTernaryExpression(TernaryExpContext ctx) {
    Expression cond = this.parseExpress(ctx.expression(0));
    Expression a = this.parseExpress(ctx.expression(1));
    Expression b = null;
    if (ctx.COLON() == null) {
        b = null;
    } else {
        b = this.parseExpress(ctx.expression(2));
    }
    TerminalNode tn = (TerminalNode) ctx.getChild(1);
    return new TernaryExpression(cond, a, b, this.getBTToken(tn.getSymbol()));
}
Also used : TernaryExpression(org.beetl.core.statement.TernaryExpression) 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) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 10 with Expression

use of org.beetl.core.statement.Expression in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseVarRefExpression.

protected Expression parseVarRefExpression(VarRefContext varRef) {
    Expression safeExp = null;
    Safe_outputContext soctx = varRef.safe_output();
    boolean hasSafe = false;
    if (soctx != null) {
        safeExp = this.parseSafeOutput(soctx);
        hasSafe = true;
    }
    if (this.pbCtx.isSafeOutput) {
        hasSafe = true;
    }
    List<VarAttributeContext> list = varRef.varAttribute();
    VarAttribute[] vas = this.parseVarAttribute(list);
    if (vas.length > 0) {
        VarAttribute first = vas[0];
        if (!(first instanceof VarSquareAttribute || first instanceof VarVirtualAttribute)) {
            pbCtx.setVarAttr(varRef.Identifier().getText(), first.token.text);
        }
    }
    VarRef var = new VarRef(vas, hasSafe, safeExp, this.getBTToken(varRef.getText(), varRef.Identifier().getSymbol().getLine()), this.getBTToken(varRef.Identifier().getSymbol()));
    pbCtx.setVarPosition(varRef.Identifier().getText(), var);
    return var;
}
Also used : VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) VarRef(org.beetl.core.statement.VarRef) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) VarAttributeContext(org.beetl.core.parser.BeetlParser.VarAttributeContext) 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)

Aggregations

Expression (org.beetl.core.statement.Expression)24 AndExpression (org.beetl.core.statement.AndExpression)23 ArthExpression (org.beetl.core.statement.ArthExpression)23 CompareExpression (org.beetl.core.statement.CompareExpression)23 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)23 FormatExpression (org.beetl.core.statement.FormatExpression)23 FunctionExpression (org.beetl.core.statement.FunctionExpression)23 IncDecExpression (org.beetl.core.statement.IncDecExpression)23 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)23 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)23 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)23 NegExpression (org.beetl.core.statement.NegExpression)23 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)23 OrExpression (org.beetl.core.statement.OrExpression)23 StatementExpression (org.beetl.core.statement.StatementExpression)23 TernaryExpression (org.beetl.core.statement.TernaryExpression)23 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)10 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)10 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)10 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)7