Search in sources :

Example 1 with Literal

use of org.beetl.core.statement.Literal 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 Literal

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

the class VarAttributeNodeListener method onEvent.

@Override
public Object onEvent(Event e) {
    Stack stack = (Stack) e.getEventTaget();
    Object o = stack.peek();
    if (o instanceof VarRef) {
        VarRef ref = (VarRef) o;
        VarAttribute[] attrs = ref.attributes;
        for (int i = 0; i < attrs.length; i++) {
            GroupTemplate gt = (GroupTemplate) ((Map) stack.get(0)).get("groupTemplate");
            VarAttribute attr = attrs[i];
            if (attr.getClass() == VarAttribute.class) {
                Type type = attr.type;
                String name = attr.token != null ? attr.token.text : null;
                // 换成速度较快的属性访问类
                try {
                    AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(type.cls, name, gt);
                    attr.aa = aa;
                } catch (BeetlException ex) {
                    ex.pushToken(attr.token);
                    throw ex;
                }
            } else if (attr.getClass() == VarSquareAttribute.class) {
                Type type = attr.type;
                Class c = type.cls;
                if (Map.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getMapAA());
                } else if (List.class.isAssignableFrom(c) || Set.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getListAA());
                } else if (c.isArray()) {
                    attr.setAA(gt.getAttributeAccessFactory().getArrayAA());
                } else {
                    Expression exp = ((VarSquareAttribute) attr).exp;
                    if (exp instanceof Literal) {
                        Literal literal = (Literal) exp;
                        if (literal.obj instanceof String) {
                            try {
                                String attributeName = (String) literal.obj;
                                AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(c, attributeName, gt);
                                ref.attributes[i] = new VarSquareAttribute2((VarSquareAttribute) attrs[i], attributeName, aa);
                            } catch (BeetlException ex) {
                                ex.pushToken(attr.token);
                                throw ex;
                            }
                        }
                    }
                }
            } else if (attr.getClass() == VarVirtualAttribute.class) {
                // 对虚拟属性~size做优化
                if (attr.token.text.equals("size")) {
                    // 优化
                    Class c = attr.type.cls;
                    if (Map.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new MapSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (Collection.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new CollectionSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (c.isArray()) {
                        ref.attributes[i] = new ArraySizeVirtualAttribute((VarVirtualAttribute) attr);
                    }
                }
            }
        }
    }
    return null;
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) Set(java.util.Set) GroupTemplate(org.beetl.core.GroupTemplate) AttributeAccess(org.beetl.core.om.AttributeAccess) Stack(java.util.Stack) Type(org.beetl.core.statement.Type) Expression(org.beetl.core.statement.Expression) VarAttribute(org.beetl.core.statement.VarAttribute) Literal(org.beetl.core.statement.Literal) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Example 3 with Literal

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

the class AntlrProgramBuilder method parseLiteralExpress.

protected Expression parseLiteralExpress(LiteralContext ctx) {
    ParseTree tree = ctx.getChild(0);
    Object value = null;
    if (tree instanceof TerminalNode) {
        Token node = ((TerminalNode) tree).getSymbol();
        String strValue = node.getText();
        int type = node.getType();
        switch(type) {
            case BeetlParser.StringLiteral:
                value = getStringValue(strValue);
                break;
            case BeetlParser.FloatingPointLiteral:
                char c = strValue.charAt(strValue.length() - 1);
                if (isHighScaleNumber(strValue)) {
                    String newValue = strValue.substring(0, strValue.length() - 1);
                    value = new BigDecimal(newValue);
                } else {
                    value = Double.parseDouble(strValue);
                }
                break;
            case BeetlParser.DecimalLiteral:
                if (isHighScaleNumber(strValue)) {
                    String newValue = strValue.substring(0, strValue.length() - 1);
                    value = new BigDecimal(newValue);
                } else {
                    if (strValue.length() < 10) {
                        value = Integer.parseInt(strValue);
                    } else if (strValue.length() > 10) {
                        value = Long.parseLong(strValue);
                    } else if (strValue.compareTo("2147483647") > 0) {
                        value = Long.parseLong(strValue);
                    } else {
                        value = Integer.parseInt(strValue);
                    }
                }
                break;
            case BeetlParser.NULL:
                value = null;
                break;
        }
    } else {
        BooleanLiteralContext blc = (BooleanLiteralContext) tree;
        String strValue = blc.getChild(0).getText();
        value = Boolean.parseBoolean(strValue);
    }
    if (value == null) {
        return Literal.NULLLiteral;
    } else {
        Literal literal = new Literal(value, this.getBTToken(ctx.getStart()));
        return literal;
    }
}
Also used : BooleanLiteralContext(org.beetl.core.parser.BeetlParser.BooleanLiteralContext) Literal(org.beetl.core.statement.Literal) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) BigDecimal(java.math.BigDecimal)

Example 4 with Literal

use of org.beetl.core.statement.Literal 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

Literal (org.beetl.core.statement.Literal)4 Expression (org.beetl.core.statement.Expression)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 BeetlException (org.beetl.core.exception.BeetlException)2 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 FormatExpression (org.beetl.core.statement.FormatExpression)2 FunctionExpression (org.beetl.core.statement.FunctionExpression)2 GrammarToken (org.beetl.core.statement.GrammarToken)2 IncDecExpression (org.beetl.core.statement.IncDecExpression)2 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)2 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)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