@Override
public void process(Template template, TemplateReader in, String left,
String right) {
StringBuilder text = new StringBuilder(64);
Engine engine = template.getEngine();
boolean debug = engine.isDebug();
next: while (true) {
String line;
try {
line = in.readLine();
} catch (IOException e) {
// 出现异常的概率极低
throw new RuntimeException("数据读入异常");
}
if (line == null) {
// 文档已经结束, 如果不是文档函数本身, 则块状函数没有正常结束
String name = getName();
if (name == null) {
try {
addStatement(new TextStatement(text.toString()));
} catch (ParseException e) {
in.addMessage(e);
}
} else {
in.addMessage("没有找到" + name + "的结束标签");
}
return;
}
try {
// 对smarty标签进行词法分析
Object[] words = Analyzer.lexical(line, left, right);
if (words != null) {
int size = words.length;
int wordSize = 2;
for (int i = 2; i < size;) {
words[wordSize++] = words[i];
if (words[i] instanceof ObjectExpression) {
try {
i = Analyzer.mergeModifier(engine, template, words, i + 1,
size, (ObjectExpression) words[i]);
} catch (ParseException e) {
in.addMessage(e);
continue next;
}
} else {
i++;
}
}
int leftStart = (Integer) words[0];
int rightEnd = (Integer) words[1];
text.append(line.substring(0, leftStart));
// 将当前标签至上一个标签之间的内容设置成文本节点
addStatement(new TextStatement(text.toString()));
in.move(leftStart);
text.setLength(0);
in.unread(line.substring(rightEnd));
if (wordSize > 2) {
Object word2 = words[2];
// 区块函数的结束标签
if (Operation.C_DIV == word2) {
// 结束标签内不能有参数, 必须是{/[NAME]}的方式,
// 并且结束标签必须与当前块函数相同
String name = getName();
Object word3 = words[3];
if ((wordSize == 4) && word3.equals(name)) {
return;
}
if (name == null) {
in.addMessage("多余的结束标签");
} else {
in.addMessage("错误的结束标签");
}
} else {
// 结束标签没有实质上的处理, 不标记行号,
// 只要不是结束标签就要标记行号
if (debug && in.isNewline()) {
addStatement(new DebugStatement(in.getLineNumber()));
}
// smarty的注释语法是两头均为'*'号
if (Operation.C_MUL == word2) {
if (wordSize == 3 || Operation.C_MUL != words[wordSize - 1]) {
in.addMessage("错误的注释语法");
}
} else if (word2 instanceof IExpression) {
// 如果第一个词是对象表达式, 则是输出语句
addStatement(new PrintStatement(wordSize > 3 ? Analyzer
.mergeExpression(words, 2, wordSize, Operation.OBJECT)
: (IExpression) word2));
} else if (word2 instanceof String) {
String word = (String) word2;
// 首个词是字符串, 是函数开始语句
IFunction function = (IFunction) engine.createNode(word, true);
function.init(template, word);
function.syntax(template, words, wordSize);
if (function.setParent(this)) {
addStatement(function);
}