use of org.beetl.core.statement.Literal in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseFunExp.
protected FunctionExpression parseFunExp(FunctionCallContext ctx) {
ExpressionListContext expListCtx = ctx.expressionList();
Expression[] exps = this.getExprssionList(expListCtx);
List<VarAttributeContext> vaListCtx = ctx.varAttribute();
Safe_outputContext soctx = ctx.safe_output();
Expression safeExp = null;
boolean hasSafe = false;
if (soctx != null) {
safeExp = this.parseSafeOutput(soctx);
hasSafe = true;
}
if (this.pbCtx.isSafeOutput) {
hasSafe = true;
}
VarAttribute[] vs = this.parseVarAttribute(vaListCtx);
List<TerminalNode> idList = ctx.functionNs().Identifier();
String nsId = this.getID(idList);
GrammarToken btToken = new org.beetl.core.statement.GrammarToken(nsId, ctx.start.getLine(), 0);
// 需要做些特殊处理的函数
if (safeParameters.contains(nsId)) {
if (exps.length != 0) {
Expression one = exps[0];
if (one instanceof VarRef) {
// 强制为变量引用增加一个安全输出
VarRef ref = (VarRef) one;
if (!ref.hasSafe) {
ref.hasSafe = true;
ref.safe = null;
}
}
}
} else if (nsId.equals("has")) {
if (exps.length != 0) {
Expression one = exps[0];
if (one instanceof VarRef) {
// 强制为变量引用增加一个安全输出
VarRef ref = (VarRef) one;
String name = ref.token.text;
Literal newExp = new Literal(name, ref.token);
// 将变量引用转化为字符串
exps[0] = newExp;
}
}
} else if (nsId.equals("debug")) {
// debug函数传递额外的行数
Literal l = new Literal(btToken.line, btToken);
Expression[] newExps = new Expression[exps.length + 2];
System.arraycopy(exps, 0, newExps, 0, exps.length);
String[] expStr = this.getExpressionString(expListCtx);
newExps[newExps.length - 2] = new Literal(expStr, btToken);
newExps[newExps.length - 1] = l;
for (int i = 0; i < exps.length; i++) {
if (!(exps[i] instanceof VarRef)) {
expStr[i] = null;
}
}
exps = newExps;
// 可以通过配置查看是否支持debug,2.1再做
} else if (nsId.equals("decode")) {
Expression[] newExps = new Expression[exps.length];
if (newExps.length >= 4) {
newExps[0] = exps[0];
newExps[1] = exps[1];
for (int i = 2; i < exps.length; i++) {
// 参数改成runtime 执行
newExps[i] = new ExpressionRuntime(exps[i]);
}
exps = newExps;
} else {
// 错误的使用了decode函数,不管了,等后面报错吧
}
}
FunctionExpression fe = new FunctionExpression(nsId, exps, vs, hasSafe, safeExp, btToken);
return fe;
}
use of org.beetl.core.statement.Literal in project beetl2.0 by javamonkey.
the class VarAttributeNodeListener method onEvent.
@Override
public Object onEvent(Event e) {
Stack stack = (Stack) e.getEventTaget();
Object o = stack.peek();
if (o instanceof VarRef) {
VarRef ref = (VarRef) o;
VarAttribute[] attrs = ref.attributes;
for (int i = 0; i < attrs.length; i++) {
GroupTemplate gt = (GroupTemplate) ((Map) stack.get(0)).get("groupTemplate");
VarAttribute attr = attrs[i];
if (attr.getClass() == VarAttribute.class) {
Type type = attr.type;
String name = attr.token != null ? attr.token.text : null;
// 换成速度较快的属性访问类
try {
AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(type.cls, name, gt);
attr.aa = aa;
} catch (BeetlException ex) {
ex.pushToken(attr.token);
throw ex;
}
} else if (attr.getClass() == VarSquareAttribute.class) {
Type type = attr.type;
Class c = type.cls;
if (Map.class.isAssignableFrom(c)) {
attr.setAA(gt.getAttributeAccessFactory().getMapAA());
} else if (List.class.isAssignableFrom(c) || Set.class.isAssignableFrom(c)) {
attr.setAA(gt.getAttributeAccessFactory().getListAA());
} else if (c.isArray()) {
attr.setAA(gt.getAttributeAccessFactory().getArrayAA());
} else {
Expression exp = ((VarSquareAttribute) attr).exp;
if (exp instanceof Literal) {
Literal literal = (Literal) exp;
if (literal.obj instanceof String) {
try {
String attributeName = (String) literal.obj;
AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(c, attributeName, gt);
ref.attributes[i] = new VarSquareAttribute2((VarSquareAttribute) attrs[i], attributeName, aa);
} catch (BeetlException ex) {
ex.pushToken(attr.token);
throw ex;
}
}
}
}
} else if (attr.getClass() == VarVirtualAttribute.class) {
// 对虚拟属性~size做优化
if (attr.token.text.equals("size")) {
// 优化
Class c = attr.type.cls;
if (Map.class.isAssignableFrom(c)) {
ref.attributes[i] = new MapSizeVirtualAttribute((VarVirtualAttribute) attr);
} else if (Collection.class.isAssignableFrom(c)) {
ref.attributes[i] = new CollectionSizeVirtualAttribute((VarVirtualAttribute) attr);
} else if (c.isArray()) {
ref.attributes[i] = new ArraySizeVirtualAttribute((VarVirtualAttribute) attr);
}
}
}
}
}
return null;
}
use of org.beetl.core.statement.Literal in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseLiteralExpress.
protected Expression parseLiteralExpress(LiteralContext ctx) {
ParseTree tree = ctx.getChild(0);
Object value = null;
if (tree instanceof TerminalNode) {
Token node = ((TerminalNode) tree).getSymbol();
String strValue = node.getText();
int type = node.getType();
switch(type) {
case BeetlParser.StringLiteral:
value = getStringValue(strValue);
break;
case BeetlParser.FloatingPointLiteral:
char c = strValue.charAt(strValue.length() - 1);
if (isHighScaleNumber(strValue)) {
String newValue = strValue.substring(0, strValue.length() - 1);
value = new BigDecimal(newValue);
} else {
value = Double.parseDouble(strValue);
}
break;
case BeetlParser.DecimalLiteral:
if (isHighScaleNumber(strValue)) {
String newValue = strValue.substring(0, strValue.length() - 1);
value = new BigDecimal(newValue);
} else {
if (strValue.length() < 10) {
value = Integer.parseInt(strValue);
} else if (strValue.length() > 10) {
value = Long.parseLong(strValue);
} else if (strValue.compareTo("2147483647") > 0) {
value = Long.parseLong(strValue);
} else {
value = Integer.parseInt(strValue);
}
}
break;
case BeetlParser.NULL:
value = null;
break;
}
} else {
BooleanLiteralContext blc = (BooleanLiteralContext) tree;
String strValue = blc.getChild(0).getText();
value = Boolean.parseBoolean(strValue);
}
if (value == null) {
return Literal.NULLLiteral;
} else {
Literal literal = new Literal(value, this.getBTToken(ctx.getStart()));
return literal;
}
}
use of org.beetl.core.statement.Literal in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseTag.
protected TagStatement parseTag(FunctionTagCallContext fc) {
String id = this.getID(fc.functionNs().Identifier());
ExpressionListContext expListCtx = fc.expressionList();
List<ExpressionContext> list = null;
if (expListCtx != null) {
list = fc.expressionList().expression();
} else {
list = Collections.EMPTY_LIST;
}
Expression[] expList = this.parseExpressionCtxList(list);
if (id.equals("htmltagvar")) {
int line = fc.functionNs().getStart().getLine();
// 标签具有绑定变量功能
Literal l = (Literal) expList[2];
String varList = (String) l.obj;
String[] vars = varList.split(",");
// 定义的变量仅仅在标签体内可见
this.pbCtx.enterBlock();
VarDefineNode[] varDefine = new VarDefineNode[vars.length];
for (int i = 0; i < vars.length; i++) {
VarDefineNode varNode = new VarDefineNode(this.getBTToken(vars[i].trim(), line));
this.pbCtx.addVarAndPostion(varNode);
varDefine[i] = varNode;
}
BlockContext blockCtx = fc.block();
Statement block = parseBlock(blockCtx.statement(), blockCtx);
this.pbCtx.exitBlock();
TagFactory tf = this.gt.getTagFactory(id);
if (tf == null) {
BeetlException ex = new BeetlException(BeetlException.TAG_NOT_FOUND);
ex.pushToken(this.getBTToken(id, fc.functionNs().getStart().getLine()));
throw ex;
}
TagStatement tag = new TagVarBindingStatement(id, expList, block, varDefine, this.getBTToken(id, line));
return tag;
} else {
BlockContext blockCtx = fc.block();
Statement block = parseBlock(blockCtx.statement(), blockCtx);
TagFactory tf = this.gt.getTagFactory(id);
if (tf == null) {
BeetlException ex = new BeetlException(BeetlException.TAG_NOT_FOUND);
ex.pushToken(this.getBTToken(id, fc.functionNs().getStart().getLine()));
throw ex;
}
TagStatement tag = new TagStatement(id, expList, block, this.getBTToken(id, fc.functionNs().getStart().getLine()));
return tag;
}
}
Aggregations