Search in sources :

Example 1 with ForEach

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

the class AbstrCFMLScriptTransformer method forStatement.

/**
 * Liest ein for Statement ein.
 * <br />
 * EBNF:<br />
 * <code>expression spaces ";" spaces condition spaces ";" spaces expression spaces ")" spaces block;</code>
 * @return for Statement
 * @throws TemplateException
 */
private final Statement forStatement(ExprData data) throws TemplateException {
    int pos = data.srcCode.getPos();
    // id
    String id = variableDec(data, false);
    if (id == null) {
        data.srcCode.setPos(pos);
        return null;
    }
    if (id.equalsIgnoreCase("for")) {
        id = null;
        data.srcCode.removeSpace();
        if (!data.srcCode.forwardIfCurrent('(')) {
            data.srcCode.setPos(pos);
            return null;
        }
    } else {
        data.srcCode.removeSpace();
        if (!data.srcCode.forwardIfCurrent(':')) {
            data.srcCode.setPos(pos);
            return null;
        }
        data.srcCode.removeSpace();
        if (!data.srcCode.forwardIfCurrent("for", '(')) {
            data.srcCode.setPos(pos);
            return null;
        }
    }
    // if(!data.srcCode.forwardIfCurrent("for",'('))
    // return null;
    Expression left = null;
    Body body = new BodyBase(data.factory);
    Position line = data.srcCode.getPosition();
    comments(data);
    if (!data.srcCode.isCurrent(';')) {
        // left
        left = expression(data);
        comments(data);
    }
    // middle for
    if (data.srcCode.forwardIfCurrent(';')) {
        Expression cont = null;
        Expression update = null;
        // condition
        comments(data);
        if (!data.srcCode.isCurrent(';')) {
            cont = condition(data);
            comments(data);
        }
        // middle
        if (!data.srcCode.forwardIfCurrent(';'))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement");
        // update
        comments(data);
        if (!data.srcCode.isCurrent(')')) {
            update = expression(data);
            comments(data);
        }
        // start )
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement, for statement must end with a [)]");
        // ex block
        statement(data, body, CTX_FOR);
        return new For(data.factory, left, cont, update, body, line, data.srcCode.getPosition(), id);
    } else // middle foreach
    if (data.srcCode.forwardIfCurrent("in")) {
        // condition
        comments(data);
        Expression value = expression(data);
        comments(data);
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement, for statement must end with a [)]");
        // ex block
        statement(data, body, CTX_FOR);
        if (!(left instanceof Variable))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement, left value is invalid");
        // throw new TemplateException(data.srcCode,"invalid syntax in for statement, right value is invalid");
        return new ForEach((Variable) left, value, body, line, data.srcCode.getPosition(), id);
    } else
        throw new TemplateException(data.srcCode, "invalid syntax in for statement");
}
Also used : Variable(lucee.transformer.expression.var.Variable) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) Position(lucee.transformer.Position) TemplateException(lucee.runtime.exp.TemplateException) For(lucee.transformer.bytecode.statement.For) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase) ForEach(lucee.transformer.bytecode.statement.ForEach)

Aggregations

TemplateException (lucee.runtime.exp.TemplateException)1 Position (lucee.transformer.Position)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 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)1 For (lucee.transformer.bytecode.statement.For)1 ForEach (lucee.transformer.bytecode.statement.ForEach)1 Expression (lucee.transformer.expression.Expression)1 Variable (lucee.transformer.expression.var.Variable)1