return new SegmentCode(klass, source, ctx);
}
@Override
public Code visitConstant(ConstantContext ctx) {
Token token = ((TerminalNode) ctx.getChild(0)).getSymbol();
String text = token.getText();
switch (token.getType()) {
case JetTemplateParser.STRING_DOUBLE:
return new SegmentCode(String.class, text, ctx);
case JetTemplateParser.STRING_SINGLE:
text = StringEscapeUtils.asCanonicalJavaString(text);
return new SegmentCode(String.class, text, ctx);
case JetTemplateParser.INTEGER:
case JetTemplateParser.INTEGER_HEX:
case JetTemplateParser.FLOATING_POINT:
Class<?> klass;
if (text.endsWith("l") || text.endsWith("L")) {
klass = Long.TYPE;
} else if (text.endsWith("f") || text.endsWith("F")) {
klass = Float.TYPE;
} else if (text.endsWith("d") || text.endsWith("D")) {
klass = Double.TYPE;
} else if (token.getType() == JetTemplateParser.FLOATING_POINT) {
klass = Double.TYPE; // 浮点数默认是double
} else {
klass = Integer.TYPE;
}
return new SegmentCode(klass, text, ctx);
case JetTemplateParser.KEYWORD_TRUE:
return new SegmentCode(Boolean.TYPE, text, ctx);
case JetTemplateParser.KEYWORD_FALSE:
return new SegmentCode(Boolean.TYPE, text, ctx);
case JetTemplateParser.KEYWORD_NULL:
return new SegmentCode(TypedKlass.NULL, text, ctx);
default:
throw reportError("Unexpected token type :" + token.getType(), ctx);
}
}