Search in sources :

Example 51 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportInputMismatch.

protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e) {
    Token t1 = recognizer.getInputStream().LT(-1);
    String msg = "缺少输入在 " + getTokenErrorDisplay(t1) + " 后面, 期望 " + e.getExpectedTokens().toString(recognizer.getTokenNames());
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg, e);
    // exception.token = this.getGrammarToken(e.getOffendingToken());
    exception.pushToken(this.getGrammarToken(t1));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Example 52 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportMissingToken.

protected void reportMissingToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    // Token t = recognizer.getCurrentToken();
    Token t = recognizer.getTokenStream().LT(-1);
    IntervalSet expecting = getExpectedTokens(recognizer);
    String expect = expecting.toString(recognizer.getTokenNames());
    if (expects.containsKey(expect)) {
        expect = expects.get(expect);
    }
    if (expect.equals("'>>'")) {
        expect = "'模板的占位结束符号'";
    }
    String tokenStr = getTokenErrorDisplay(t);
    String msg = "缺少输入 " + expect + " 在 " + tokenStr + " 后面";
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
    exception.pushToken(this.getGrammarToken(t));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Example 53 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class FormatExpression method evaluateValue.

public Object evaluateValue(Object o, Context ctx) {
    Format format = null;
    if (name != null) {
        format = ctx.gt.getFormat(name);
    } else {
        if (o == null)
            return null;
        format = ctx.gt.getDefaultFormat(o.getClass());
    }
    if (format == null) {
        BeetlException ex = new BeetlException(BeetlException.FORMAT_NOT_FOUND);
        ex.pushToken(token);
        throw ex;
    }
    try {
        if (format instanceof ContextFormat) {
            return ((ContextFormat) format).format(o, pattern, ctx);
        } else {
            return format.format(o, pattern);
        }
    } catch (Exception e) {
        BeetlException ex = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用格式化函数抛出异常", e);
        ex.pushToken(token);
        throw ex;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Format(org.beetl.core.Format) ContextFormat(org.beetl.core.ContextFormat) ContextFormat(org.beetl.core.ContextFormat) BeetlException(org.beetl.core.exception.BeetlException)

Example 54 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class FunctionExpression method evaluate.

public Object evaluate(Context ctx) {
    Function fn = ctx.gt.getFunction(name);
    if (fn == null) {
        // 检查html实现
        Resource resource = getResource(ctx.gt, name);
        if (resource.getResourceLoader().exist(resource.getId())) {
            fn = new FileFunctionWrapper(resource.getId());
        } else {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_NOT_FOUND);
            ex.pushToken(token);
            throw ex;
        }
    }
    Object[] paras = new Object[exps.length];
    for (int i = 0; i < paras.length; i++) {
        paras[i] = exps[i].evaluate(ctx);
    }
    Object value = null;
    try {
        value = fn.call(paras, ctx);
    } catch (BeetlException ex) {
        ex.pushToken(token);
        throw ex;
    } catch (RuntimeException ex) {
        BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用方法出错 " + name, ex);
        be.pushToken(this.token);
        throw be;
    }
    Object ret = null;
    if (vas == null) {
        ret = value;
    } else {
        for (VarAttribute attr : vas) {
            try {
                value = attr.evaluate(ctx, value);
            } catch (BeetlException ex) {
                ex.pushToken(attr.token);
                throw ex;
            } catch (RuntimeException ex) {
                BeetlException be = new BeetlException(BeetlException.ATTRIBUTE_INVALID, "属性访问出错", ex);
                be.pushToken(attr.token);
                throw be;
            }
            if (value == null) {
                if (hasSafe) {
                    return safeExp == null ? null : safeExp.evaluate(ctx);
                } else {
                    BeetlException be = new BeetlException(BeetlException.ERROR, "空指针 ");
                    be.pushToken(attr.token);
                    throw be;
                }
            }
        }
        ret = value;
    }
    if (ret == null && hasSafe) {
        return safeExp == null ? null : safeExp.evaluate(ctx);
    } else {
        return ret;
    }
}
Also used : Function(org.beetl.core.Function) FileFunctionWrapper(org.beetl.core.fun.FileFunctionWrapper) BeetlException(org.beetl.core.exception.BeetlException) Resource(org.beetl.core.Resource)

Example 55 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class VarRefAssignExpress method evaluate.

public Object evaluate(Context ctx) {
    Object value = exp.evaluate(ctx);
    if (lastVarAttribute == null) {
        ctx.vars[varIndex] = value;
        return value;
    }
    Object obj = varRef.evaluateUntilLast(ctx);
    Object key = null;
    if (lastVarAttribute instanceof VarSquareAttribute) {
        key = (((VarSquareAttribute) lastVarAttribute).exp).evaluate(ctx);
    } else {
        key = lastVarAttribute.name;
    }
    try {
        ObjectAA.defaultObjectAA().setValue(obj, key, value);
    } catch (ClassCastException ex) {
        BeetlException bx = new BeetlException(BeetlException.ATTRIBUTE_INVALID, ex);
        bx.pushToken(lastVarAttribute.token);
        throw bx;
    } catch (BeetlException be) {
        be.pushToken(lastVarAttribute.token);
        throw be;
    }
    return value;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Aggregations

BeetlException (org.beetl.core.exception.BeetlException)60 GrammarToken (org.beetl.core.statement.GrammarToken)10 IOException (java.io.IOException)7 BeetlParserException (org.beetl.core.exception.BeetlParserException)6 Expression (org.beetl.core.statement.Expression)6 AndExpression (org.beetl.core.statement.AndExpression)5 ArthExpression (org.beetl.core.statement.ArthExpression)5 CompareExpression (org.beetl.core.statement.CompareExpression)5 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)5 FormatExpression (org.beetl.core.statement.FormatExpression)5 FunctionExpression (org.beetl.core.statement.FunctionExpression)5 IncDecExpression (org.beetl.core.statement.IncDecExpression)5 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)5 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)5 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)5 NegExpression (org.beetl.core.statement.NegExpression)5 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)5 OrExpression (org.beetl.core.statement.OrExpression)5 StatementExpression (org.beetl.core.statement.StatementExpression)5 TernaryExpression (org.beetl.core.statement.TernaryExpression)5