Search in sources :

Example 1 with TryCatchFinally

use of lucee.transformer.bytecode.statement.TryCatchFinally in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method tryStatement.

/**
 * Liest eine try Block ein
 * <br />
 * EBNF:<br />
 * <code>;</code>
 * @return Try Block
 * @throws TemplateException
 */
private final TryCatchFinally tryStatement(ExprData data) throws TemplateException {
    if (!data.srcCode.forwardIfCurrent("try", '{') && !data.srcCode.forwardIfCurrent("try ") && !data.srcCode.forwardIfCurrent("try", '/'))
        return null;
    data.srcCode.previous();
    Body body = new BodyBase(data.factory);
    TryCatchFinally tryCatchFinally = new TryCatchFinally(data.factory, body, data.srcCode.getPosition(), null);
    statement(data, body, CTX_TRY);
    comments(data);
    // catches
    short catchCount = 0;
    while (data.srcCode.forwardIfCurrent("catch", '(')) {
        catchCount++;
        comments(data);
        // type
        int pos = data.srcCode.getPos();
        Position line = data.srcCode.getPosition();
        Expression name = null, type = null;
        StringBuffer sbType = new StringBuffer();
        String id;
        while (true) {
            id = identifier(data, false);
            if (id == null)
                break;
            sbType.append(id);
            data.srcCode.removeSpace();
            if (!data.srcCode.forwardIfCurrent('.'))
                break;
            sbType.append('.');
            data.srcCode.removeSpace();
        }
        if (sbType.length() == 0) {
            type = string(data);
            if (type == null)
                throw new TemplateException(data.srcCode, "a catch statement must begin with the throwing type (query, application ...).");
        } else {
            type = data.factory.createLitString(sbType.toString());
        }
        // name = expression();
        comments(data);
        // name
        if (!data.srcCode.isCurrent(')')) {
            name = expression(data);
        } else {
            data.srcCode.setPos(pos);
            name = expression(data);
            type = data.factory.createLitString("any");
        }
        comments(data);
        Body b = new BodyBase(data.factory);
        try {
            tryCatchFinally.addCatch(type, name, b, line);
        } catch (TransformerException e) {
            throw new TemplateException(data.srcCode, e.getMessage());
        }
        comments(data);
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "invalid catch statement, missing closing )");
        statement(data, b, CTX_CATCH);
        comments(data);
    }
    // finally
    if (finallyStatement(data, tryCatchFinally)) {
        comments(data);
    } else if (catchCount == 0)
        throw new TemplateException(data.srcCode, "a try statement must have at least one catch statement");
    // if(body.isEmpty()) return null;
    tryCatchFinally.setEnd(data.srcCode.getPosition());
    return tryCatchFinally;
}
Also used : Position(lucee.transformer.Position) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TemplateException(lucee.runtime.exp.TemplateException) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase) TransformerException(lucee.transformer.TransformerException) TryCatchFinally(lucee.transformer.bytecode.statement.TryCatchFinally)

Example 2 with TryCatchFinally

use of lucee.transformer.bytecode.statement.TryCatchFinally in project Lucee by lucee.

the class Retry method getAncestorCatch.

public static Statement getAncestorCatch(TagLib tagLib, Statement stat) {
    String name = tagLib.getNameSpaceAndSeparator() + "catch";
    Tag tag;
    Statement parent = stat;
    while (true) {
        parent = parent.getParent();
        if (parent == null)
            return null;
        if (parent instanceof Tag) {
            tag = (Tag) parent;
            if (tag.getFullname().equalsIgnoreCase(name))
                return tag;
        } else if (parent instanceof TryCatchFinally)
            return parent;
    }
}
Also used : Statement(lucee.transformer.bytecode.Statement) Tag(lucee.transformer.bytecode.statement.tag.Tag) TagLibTag(lucee.transformer.library.tag.TagLibTag) TryCatchFinally(lucee.transformer.bytecode.statement.TryCatchFinally)

Aggregations

TryCatchFinally (lucee.transformer.bytecode.statement.TryCatchFinally)2 TemplateException (lucee.runtime.exp.TemplateException)1 Position (lucee.transformer.Position)1 TransformerException (lucee.transformer.TransformerException)1 Body (lucee.transformer.bytecode.Body)1 BodyBase (lucee.transformer.bytecode.BodyBase)1 FunctionBody (lucee.transformer.bytecode.FunctionBody)1 ScriptBody (lucee.transformer.bytecode.ScriptBody)1 Statement (lucee.transformer.bytecode.Statement)1 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)1 Tag (lucee.transformer.bytecode.statement.tag.Tag)1 Expression (lucee.transformer.expression.Expression)1 TagLibTag (lucee.transformer.library.tag.TagLibTag)1