code:
for (String expr : code) {
Long lineNumber = iterLineNumber.hasNext() ? iterLineNumber.next() : null;
BalancedTokenizer tokenizer =
new BalancedTokenizer(new StringReader(expr));
int currentLevel = 0;
int newLevel = 0;
StringBuilder buffer = new StringBuilder();
StringBuilder oldBuffer = null;
String oldName = null;
int oldContext = -1;
while (tokenizer.hasNext()) {
Token token = tokenizer.next();
newLevel = tokenizer.getCurrentLevel();
StringBuilder currentBuffer = oldBuffer != null ? oldBuffer : buffer;
/* Unexpected ';' error. */
if (newLevel == 0 && token.text.equals(";")) {
diagnosticManager.error("'\"' expected",
expr, token.offset, token.offset, token.offset,
sourceInfo);
parsed = false;
continue code;
}
/* old expressions. */
if (oldBuffer != null) {
if (newLevel == oldContext) {
/* End of old expression. */
String oldExpr = oldBuffer.toString();
oldParameters.add(
new VariableModel(ElementKind.PARAMETER, oldName,
new ClassName("java/lang/Object")));
oldParametersCode.add(oldExpr);
oldParametersLineNumbers.add(lineNumber);
/* Pad buffer (for error reporting purposes). */
buffer.append("( ");
/* Replace old expression in original expression. */
buffer.append(JavaUtils.BEGIN_GENERATED_CODE);
buffer.append(MAGIC_CAST_METHOD);
buffer.append("(");
buffer.append(oldName);
buffer.append(", ");
buffer.append("true ? null : ");
buffer.append(JavaUtils.END_GENERATED_CODE);
buffer.append(oldExpr);
buffer.append(JavaUtils.BEGIN_GENERATED_CODE);
buffer.append(")");
buffer.append(JavaUtils.END_GENERATED_CODE);
/* Pad buffer (for error reporting purposes). */
buffer.append(")");
/* Exit old context. */
oldBuffer = null;
oldContext = -1;
} else {
switch (token.kind) {
case WORD:
if (token.text.equals("old")) {
diagnosticManager.error("nested old expression",
expr, token.offset, token.offset, token.offset,
sourceInfo);
parsed = false;
continue code;
}
oldBuffer.append(token.text);
break;
default:
transformCommon(oldBuffer, tokenizer, token);
}
}
} else {
switch (token.kind) {
case WORD:
if (acceptOld && token.text.equals("old")) {
/* Start of old expression. */
Token afterOld = null;
if (!tokenizer.hasNext()
|| !((afterOld = tokenizer.next()).text.equals("(")
|| (afterOld.kind == TokenKind.SPACE
&& tokenizer.hasNext()
&& tokenizer.next().text.equals("(")))) {
int errorPos = afterOld != null ? afterOld.offset
: tokenizer.getCurrentOffset();
diagnosticManager.error("'(' expected",
expr, errorPos, errorPos, errorPos,
sourceInfo);
parsed = false;
continue code;
}
/* Compute new old variable name. */
oldName = JavaUtils.OLD_VARIABLE_PREFIX + oldId++;
/* Enter old context. */
if (afterOld.kind == TokenKind.SPACE) {
oldBuffer = new StringBuilder(afterOld.text);
} else {
oldBuffer = new StringBuilder();
}
oldContext = currentLevel;
break;
}
/* Fall through. */
default:
transformCommon(buffer, tokenizer, token);
}
}
currentLevel = newLevel;
}
/* Parse errors. */
if (tokenizer.hasErrors()) {
int errorPos = tokenizer.getCurrentOffset();
diagnosticManager.error(tokenizer.getErrorMessage(),
expr, errorPos, errorPos, errorPos, sourceInfo);
parsed = false;
continue code;
}
newCode.add(buffer.toString());