use of org.beetl.core.statement.NativeCallExpression in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseNativeCallExpression.
protected NativeCallExpression parseNativeCallExpression(NativeCallContext ncc) {
NativeCallExpression nativeExp = null;
List<ParseTree> list = ncc.children;
// nativeCall: nativeVarRefChain (nativeMethod|nativeArray| PERIOD nativeVarRefChain)*;
NativeVarRefChainContext first = (NativeVarRefChainContext) list.get(0);
List<TerminalNode> ids = first.Identifier();
StringBuilder clsSb = new StringBuilder();
// 是类静态调用还是实例调用
boolean isCls = false;
int i = 0;
for (; i < ids.size(); i++) {
String text = ids.get(i).getText();
char c = text.charAt(0);
if (c >= 'A' && c <= 'Z') {
clsSb.append(text);
isCls = true;
break;
} else {
clsSb.append(text).append(".");
}
}
ClassNode clsNode = null;
InstanceNode insNode = null;
if (isCls) {
clsNode = new ClassNode(clsSb.toString());
// 指向下一个属性或者静态方法
i++;
} else {
// 变量的属性引用,回到第一个,构造一个变量
String varName = ids.get(0).getText();
VarRef ref = new VarRef(new VarAttribute[0], false, null, this.getBTToken(varName, ncc.start.getLine()));
this.pbCtx.setVarPosition(varName, ref);
insNode = new InstanceNode(ref);
i = 1;
}
List<NativeNode> nativeList = new ArrayList<NativeNode>();
for (int j = i; j < ids.size(); j++) {
// 剩下的是属性
NativeAtrributeNode attribute = new NativeAtrributeNode(ids.get(j).getText());
nativeList.add(attribute);
}
for (int z = 1; z < list.size(); z++) {
ParseTree tree = list.get(z);
if (tree instanceof NativeMethodContext) {
NativeMethodContext methodCtx = (NativeMethodContext) tree;
NativeMethodNode methodNode = null;
String method = null;
NativeNode lastNode = nativeList.get(nativeList.size() - 1);
if (lastNode instanceof NativeAtrributeNode) {
method = ((NativeAtrributeNode) lastNode).attribute;
//
nativeList.remove(nativeList.size() - 1);
} else {
String msg = null;
if (lastNode instanceof NativeArrayNode) {
msg = "[]()";
} else {
msg = "()()";
}
BeetlException ex = new BeetlException(BeetlException.PARSER_NATIVE_ERROR, msg);
ex.pushToken(this.getBTToken(methodCtx.getStart()));
throw ex;
}
// 解析参数
List<ExpressionContext> expCtxList = methodCtx.expression();
Expression[] exp = this.parseExpressionCtxList(expCtxList);
methodNode = new NativeMethodNode(method, exp);
nativeList.add(methodNode);
} else if (tree instanceof NativeArrayContext) {
ExpressionContext expCtx = ((NativeArrayContext) tree).expression();
Expression exp = this.parseExpress(expCtx);
NativeArrayNode arrayNode = new NativeArrayNode(exp);
nativeList.add(arrayNode);
} else if (tree instanceof NativeVarRefChainContext) {
List<TerminalNode> nodes = ((NativeVarRefChainContext) tree).Identifier();
for (TerminalNode node : nodes) {
NativeAtrributeNode attributeNode = new NativeAtrributeNode(node.getText());
nativeList.add(attributeNode);
}
} else {
// 其他节点,这段语法写的不好,造成解析困难,但先这样了
continue;
}
}
NativeNode[] chain = nativeList.toArray(new NativeNode[0]);
if (clsNode != null) {
nativeExp = new NativeCallExpression(clsNode, chain, this.getBTToken(ncc.start));
} else {
nativeExp = new NativeCallExpression(insNode, chain, this.getBTToken(ncc.start));
}
return nativeExp;
}
use of org.beetl.core.statement.NativeCallExpression 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