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");
}
Aggregations