use of org.beetl.core.exception.BeetlException 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.exception.BeetlException in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method getClassType.
private Type getClassType(ClassOrInterfaceTypeContext ctx) {
List<TerminalNode> list = ctx.Identifier1();
String className = this.getID(list);
Class cls = gt.loadClassBySimpleName(className);
if (cls == null) {
BeetlException ex = new BeetlException(BeetlException.TYPE_SEARCH_ERROR, className);
ex.pushToken(this.getBTToken(ctx.getStart()));
throw ex;
}
Type classType = new Type(cls);
TypeArgumentsContext typeArgCtx = ctx.typeArguments();
if (typeArgCtx != null) {
List<TypeArgumentContext> listType = typeArgCtx.typeArgument();
if (listType != null) {
Type[] subType = new Type[listType.size()];
int i = 0;
for (TypeArgumentContext typeCtx : listType) {
ClassOrInterfaceTypeContext child = typeCtx.classOrInterfaceType();
Type type = this.getClassType(child);
subType[i++] = type;
}
classType.types = subType;
}
}
return classType;
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseForSt.
protected Statement parseForSt(ForStContext ctx) {
pbCtx.enterBlock();
// break,continue语句到此为止
pbCtx.current.canStopContinueBreakFlag = true;
StatementContext forContext = ctx.statement(0);
StatementContext elseContext = null;
ForControlContext forTypeCtx = ctx.forControl();
if (forTypeCtx.forInControl() != null) {
// for(a in list)...
ForInControlContext forCtx = forTypeCtx.forInControl();
VarDefineNode forVar = new VarDefineNode(this.getBTToken(forCtx.Identifier().getSymbol()));
if (pbCtx.hasDefined(forVar.token.text) != null) {
GrammarToken token = pbCtx.hasDefined(forVar.token.text);
BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "已经在第" + token.line + "行定义");
ex.pushToken(forVar.token);
throw ex;
}
VarDefineNode loopStatusVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier().getSymbol().getText() + "LP", forCtx.Identifier().getSymbol().getLine(), 0));
if (pbCtx.hasDefined(loopStatusVar.token.text) != null) {
GrammarToken token = pbCtx.hasDefined(loopStatusVar.token.text);
BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "For循环隐含变量,已经在第" + token.line + "行定义");
ex.pushToken(loopStatusVar.token);
throw ex;
}
pbCtx.addVarAndPostion(forVar);
pbCtx.addVarAndPostion(loopStatusVar);
// //beetl.2 兼容
// VarDefineNode indexVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier()
// .getSymbol().getText()
// + "_index", forCtx.Identifier().getSymbol().getLine(), 0));
//
// VarDefineNode sizeVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier()
// .getSymbol().getText()
// + "_size", forCtx.Identifier().getSymbol().getLine(), 0));
//
// pbCtx.addVarAndPostion(indexVar);
//
// pbCtx.addVarAndPostion(sizeVar);
Expression exp = this.parseExpress(forCtx.expression());
Statement forPart = this.parseStatment(forContext);
// elsefor
Statement elseForPart = null;
if (ctx.Elsefor() != null) {
elseContext = ctx.statement(1);
elseForPart = this.parseStatment(elseContext);
}
boolean hasSafe = false;
if (exp instanceof VarRef) {
VarRef varRef = (VarRef) exp;
hasSafe = varRef.hasSafe;
}
if (pbCtx.isSafeOutput) {
hasSafe = true;
}
ForStatement forStatement = new ForStatement(forVar, exp, hasSafe, forPart, elseForPart, forVar.token);
this.checkGoto(forStatement);
pbCtx.exitBlock();
return forStatement;
} else {
GeneralForControlContext forCtx = forTypeCtx.generalForControl();
Expression[] initExp = null;
VarAssignStatementSeq varInitSeq = null;
Expression condtion = null;
Expression[] updateExp = null;
if (forCtx.forInit() != null) {
ForInitContext forInitCtx = forCtx.forInit();
if (forInitCtx.Var() == null) {
// for( a=1,b=3;
List<ExpressionContext> list = forInitCtx.expressionList().expression();
initExp = this.parseExpressionCtxList(list);
} else {
// for( var a=1,b=3;
VarDeclareListContext varDeclare = forInitCtx.varDeclareList();
varInitSeq = this.parseVarDeclareList(varDeclare);
}
}
if (forCtx.expression() != null) {
condtion = this.parseExpress(forCtx.expression());
}
if (forCtx.forUpdate() != null) {
ForUpdateContext updateCtx = forCtx.forUpdate();
List<ExpressionContext> list = updateCtx.expressionList().expression();
updateExp = this.parseExpressionCtxList(list);
}
Statement forPart = this.parseStatment(forContext);
// elsefor
Statement elseForPart = null;
if (ctx.Elsefor() != null) {
elseContext = ctx.statement(1);
elseForPart = this.parseStatment(elseContext);
}
String str = forTypeCtx.getText();
GeneralForStatement forStat = new GeneralForStatement(varInitSeq, initExp, condtion, updateExp, forPart, elseForPart, this.getBTToken(str, forTypeCtx.start.getLine()));
pbCtx.exitBlock();
return forStat;
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseVarRefInLeftExpression.
protected VarRef parseVarRefInLeftExpression(VarRefContext varRef) {
Expression safeExp = null;
Safe_outputContext soctx = varRef.safe_output();
if (soctx != null) {
throw new BeetlException(BeetlException.ERROR, "语法错,赋值表达式不能使用安全输出");
}
List<VarAttributeContext> list = varRef.varAttribute();
VarAttribute[] vas = this.parseVarAttribute(list);
// 变量属性,用来收集,暂时未用上
if (vas.length > 0) {
VarAttribute first = vas[0];
if (!(first instanceof VarSquareAttribute || first instanceof VarVirtualAttribute)) {
pbCtx.setVarAttr(varRef.Identifier().getText(), first.token.text);
}
}
VarRef var = new VarRef(vas, false, null, this.getBTToken(varRef.getText(), varRef.Identifier().getSymbol().getLine()), this.getBTToken(varRef.Identifier().getSymbol()));
pbCtx.setVarPosition(varRef.Identifier().getText(), var);
return var;
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class GroupTemplate method loadTemplate.
private Program loadTemplate(Resource res, boolean isTextTemplate) {
Transformator sf = null;
try {
Reader reader = res.openReader();
if (isTextTemplate) {
sf = new Transformator(conf.placeholderStart, conf.placeholderEnd, conf.statementStart, conf.statementEnd);
} else {
if (conf.isHasFunctionLimiter()) {
sf = new Transformator(conf.placeholderStart, conf.placeholderEnd, conf.functionLimiterStart, conf.functionLimiterEnd);
} else {
sf = new Transformator(conf.placeholderStart, conf.placeholderEnd, conf.statementStart, conf.statementEnd);
}
}
if (this.conf.isHtmlTagSupport()) {
sf.enableHtmlTagSupport(conf.getHtmlTagStart(), conf.getHtmlTagEnd(), this.conf.getHtmlTagBindingAttribute());
}
Reader scriptReader;
scriptReader = sf.transform(reader);
Program program = engine.createProgram(res, scriptReader, sf.textMap, sf.lineSeparator, this);
return program;
} catch (HTMLTagParserException e) {
ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, sf.lineSeparator);
ep.setException(e);
e.pushResource(res);
return ep;
} catch (IOException e) {
ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, sf.lineSeparator);
BeetlException ex = new BeetlException(BeetlException.TEMPLATE_LOAD_ERROR);
ex.pushResource(res);
ep.setException(ex);
return ep;
} catch (BeetlException ex) {
ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, sf != null ? sf.lineSeparator : null);
ex.pushResource(res);
ep.setException(ex);
return ep;
}
}
Aggregations