use of org.beetl.core.statement.FunctionExpression 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.FunctionExpression in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseExpress.
protected Expression parseExpress(ExpressionContext ctx) {
if (ctx == null)
return null;
if (ctx instanceof LiteralExpContext) {
return parseLiteralExpress(((LiteralExpContext) ctx).literal());
} else if (ctx instanceof VarRefExpContext) {
return this.parseVarRefExpression(((VarRefExpContext) ctx).varRef());
} else if (ctx instanceof CompareExpContext) {
CompareExpression compare = parseCompareExpression((CompareExpContext) ctx);
if (gt.conf.isStrict) {
throw new MVCStrictException(compare.token);
}
return compare;
} else if (ctx instanceof TernaryExpContext) {
return this.parseTernaryExpression((TernaryExpContext) ctx);
} else if (ctx instanceof MuldivmodExpContext) {
ArthExpression arth = this.parseMuldivmodExpression((MuldivmodExpContext) ctx);
if (gt.conf.isStrict) {
throw new MVCStrictException(arth.token);
}
return arth;
} else if (ctx instanceof AddminExpContext) {
return this.parsePlusMins((AddminExpContext) ctx);
} else if (ctx instanceof ParExpContext) {
ParExpContext par = (ParExpContext) ctx;
return this.parseExpress(par.expression());
} else if (ctx instanceof FunctionCallExpContext) {
FunctionCallExpContext fceCtx = (FunctionCallExpContext) ctx;
FunctionExpression fun = parseFunExp(fceCtx.functionCall());
if (gt.conf.isStrict) {
throw new MVCStrictException(fun.token);
}
return fun;
} else if (ctx instanceof JsonExpContext) {
JsonContext jc = ((JsonExpContext) ctx).json();
return this.parseJson(jc);
} else if (ctx instanceof NativeCallExpContext) {
NativeCallContext ncc = ((NativeCallExpContext) ctx).nativeCall();
NativeCallExpression nativeCall = this.parseNativeCallExpression(ncc);
if (!gt.conf.nativeCall || gt.conf.isStrict) {
throw new NativeNotAllowedException(nativeCall.token);
}
return nativeCall;
} else if (ctx instanceof AndExpContext) {
AndExpContext andCtx = (AndExpContext) ctx;
return this.parseAndExpression(andCtx);
} else if (ctx instanceof OrExpContext) {
OrExpContext orExp = (OrExpContext) ctx;
return this.parseOrExpression(orExp);
} else if (ctx instanceof NotExpContext) {
NotExpContext notCtx = (NotExpContext) ctx;
return this.parseNotExpression(notCtx);
} else if (ctx instanceof NegExpContext) {
NegExpContext negCtx = (NegExpContext) ctx;
return this.parseNegExpression(negCtx);
} else if (ctx instanceof IncDecOneContext) {
IncDecOneContext oneCtx = (IncDecOneContext) ctx;
IncDecExpression exp = this.parseIncDecOneContext(oneCtx);
if (gt.conf.isStrict) {
throw new NativeNotAllowedException(exp.token);
}
return exp;
} else if (ctx instanceof OneIncDecContext) {
OneIncDecContext oneCtx = (OneIncDecContext) ctx;
IncDecExpression exp = this.parseOneIncDecContext(oneCtx);
if (gt.conf.isStrict) {
throw new NativeNotAllowedException(exp.token);
}
return exp;
} else if (ctx instanceof AssignGeneralInExpContext) {
AssignGeneralInExpContext agc = (AssignGeneralInExpContext) ctx;
VarRefAssignExpress vas = this.parseAssingInExp(agc);
return vas;
} else {
throw new UnsupportedOperationException();
}
}
Aggregations