Search in sources :

Example 1 with VarRefContext

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

the class AntlrProgramBuilder method parseAssign.

/**
 * 定义变量
 * @param amc
 * @return
 */
protected VarAssignStatement parseAssign(AssignMentContext amc) {
    VarAssignStatement vas = null;
    if (amc instanceof AssignGeneralInStContext) {
        AssignGeneralInStContext agc = (AssignGeneralInStContext) amc;
        ExpressionContext expCtx = agc.generalAssignExp().expression();
        Expression exp = parseExpress(expCtx);
        VarRefContext varRefCtx = agc.generalAssignExp().varRef();
        if (varRefCtx.children.size() == 1) {
            // var a=1;
            Token token = varRefCtx.Identifier().getSymbol();
            vas = new VarAssignStatement(exp, getBTToken(token));
        } else {
            // var a.b=1 since 2.7.0
            VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
            vas = new VarRefAssignStatement(exp, ref);
        }
        return vas;
    } else if (amc instanceof AssignIdContext) {
        AssignIdContext idCtx = (AssignIdContext) amc;
        vas = new VarAssignStatement(Literal.NULLLiteral, getBTToken(idCtx.Identifier().getSymbol()));
        return vas;
    } else if (amc instanceof AssignTemplateVarContext) {
        AssignTemplateVarContext templateVarCtx = (AssignTemplateVarContext) amc;
        BlockContext blockCtx = templateVarCtx.block();
        BlockStatement block = this.parseBlock(blockCtx.statement(), blockCtx);
        ContentBodyExpression bodyExp = new ContentBodyExpression(block, getBTToken(templateVarCtx.Identifier().getSymbol()));
        vas = new VarAssignStatement(bodyExp, getBTToken(templateVarCtx.Identifier().getSymbol()));
    } else {
        throw new RuntimeException("不支持 在 " + amc.start.getLine());
    }
    return vas;
}
Also used : VarRef(org.beetl.core.statement.VarRef) AssignGeneralInStContext(org.beetl.core.parser.BeetlParser.AssignGeneralInStContext) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) BlockStatement(org.beetl.core.statement.BlockStatement) AssignIdContext(org.beetl.core.parser.BeetlParser.AssignIdContext) 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) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) VarRefContext(org.beetl.core.parser.BeetlParser.VarRefContext) AssignTemplateVarContext(org.beetl.core.parser.BeetlParser.AssignTemplateVarContext)

Example 2 with VarRefContext

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

the class AntlrProgramBuilder method parseAssingInExp.

/**
 *  赋值变量
 * @param agc
 * @return
 */
protected VarRefAssignExpress parseAssingInExp(AssignGeneralInExpContext agc) {
    VarRefAssignExpress vas = null;
    ExpressionContext expCtx = agc.generalAssignExp().expression();
    Expression exp = parseExpress(expCtx);
    VarRefContext varRefCtx = agc.generalAssignExp().varRef();
    VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
    vas = new VarRefAssignExpress(exp, ref);
    if (ref.attributes.length == 0) {
        // 变量定义:
        Token token = varRefCtx.Identifier().getSymbol();
        if (pbCtx.hasDefined(token.getText()) != null) {
            registerVar(vas);
            return vas;
        } else {
            BeetlException ex = new BeetlException(BeetlException.VAR_NOT_DEFINED);
            ex.pushToken(this.getBTToken(token));
            throw ex;
        }
    }
    return vas;
}
Also used : VarRef(org.beetl.core.statement.VarRef) BeetlException(org.beetl.core.exception.BeetlException) 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) VarRefContext(org.beetl.core.parser.BeetlParser.VarRefContext) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) VarRefAssignExpress(org.beetl.core.statement.VarRefAssignExpress)

Aggregations

Token (org.antlr.v4.runtime.Token)2 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)2 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)2 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)2 VarRefContext (org.beetl.core.parser.BeetlParser.VarRefContext)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 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