private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();
@Override
public Token go() {
IContext ctx = ctx();
ICodeType curType = ctx.peekCodeType();
if (!curType.allowInternalTypeBlock()) return null;
String remain = ctx.getRemain();
Iterable<ICodeType> types = ctx.getEngine().extensionManager().templateLangs();
for (ICodeType type : types) {
if (type.allowedExternalTypes().contains(curType)) {
String blockStart = type.blockStart();
if (null == blockStart) {
logger.warn("null block start found for lang[%s] inside lang[%s]", type, curType);
continue;
}
Pattern pStart = patterns.get(blockStart);
if (null == pStart) {
pStart = Pattern.compile(blockStart, Pattern.DOTALL);
patterns.put(blockStart, pStart);
}
Matcher m = pStart.matcher(remain);
if (m.matches()) {
ctx.pushCodeType(type);
String matched = m.group(1);
ctx.step(matched.length());
String s = matched;
if (matched.indexOf('@') > -1) {
// process internal template
char ch = s.charAt(0);
s = s.substring(1); //prevent CodeTypeBlockStartSensor to sense it again
String code = ctx.getCodeBuilder().addInlineInclude(s, ctx.currentLine());
if (matched.endsWith("\n")) {
code = code + ";p(\"\\n\");";
}
s = String.format("p('%s');", String.valueOf(ch)) + code + String.format(";__ctx.pushCodeType(%s);", type.newInstanceStr());
} else {