use of org.beetl.core.parser.BeetlParser.BlockContext in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseTryCatch.
protected TryCatchStatement parseTryCatch(TryStContext tryStCtx) {
BlockContext tryBlockCtx = tryStCtx.block(0);
BlockStatement tryPart = (BlockStatement) this.parseBlock(tryBlockCtx.statement(), tryBlockCtx);
BlockStatement catchPart = null;
VarDefineNode errorNode = null;
if (tryStCtx.Catch() != null) {
this.pbCtx.enterBlock();
if (tryStCtx.Identifier() != null) {
Token errorToken = tryStCtx.Identifier().getSymbol();
errorNode = new VarDefineNode(this.getBTToken(errorToken));
this.pbCtx.addVarAndPostion(errorNode);
}
BlockContext catchBlockCtx = tryStCtx.block(1);
catchPart = (BlockStatement) this.parseBlock(catchBlockCtx.statement(), catchBlockCtx);
this.pbCtx.exitBlock();
}
TryCatchStatement statement = new TryCatchStatement(tryPart, catchPart, errorNode, this.getBTToken(tryStCtx.Try().getSymbol()));
return statement;
}
use of org.beetl.core.parser.BeetlParser.BlockContext in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseAssign.
/**
* 定义变量
* @param amc
* @return
*/
protected VarAssignStatement parseAssign(AssignMentContext amc) {
VarAssignStatement vas = null;
if (amc instanceof AssignGeneralInStContext) {
AssignGeneralInStContext agc = (AssignGeneralInStContext) amc;
ExpressionContext expCtx = agc.generalAssignExp().expression();
Expression exp = parseExpress(expCtx);
VarRefContext varRefCtx = agc.generalAssignExp().varRef();
if (varRefCtx.children.size() == 1) {
// var a=1;
Token token = varRefCtx.Identifier().getSymbol();
vas = new VarAssignStatement(exp, getBTToken(token));
} else {
// var a.b=1 since 2.7.0
VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
vas = new VarRefAssignStatement(exp, ref);
}
return vas;
} else if (amc instanceof AssignIdContext) {
AssignIdContext idCtx = (AssignIdContext) amc;
vas = new VarAssignStatement(Literal.NULLLiteral, getBTToken(idCtx.Identifier().getSymbol()));
return vas;
} else if (amc instanceof AssignTemplateVarContext) {
AssignTemplateVarContext templateVarCtx = (AssignTemplateVarContext) amc;
BlockContext blockCtx = templateVarCtx.block();
BlockStatement block = this.parseBlock(blockCtx.statement(), blockCtx);
ContentBodyExpression bodyExp = new ContentBodyExpression(block, getBTToken(templateVarCtx.Identifier().getSymbol()));
vas = new VarAssignStatement(bodyExp, getBTToken(templateVarCtx.Identifier().getSymbol()));
} else {
throw new RuntimeException("不支持 在 " + amc.start.getLine());
}
return vas;
}
use of org.beetl.core.parser.BeetlParser.BlockContext in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseAjax.
protected AjaxStatement parseAjax(AjaxStContext ajaxCtx) {
GrammarToken token = null;
String flag = "render";
List<TerminalNode> nodes = ajaxCtx.Identifier();
if (nodes.size() == 1) {
token = this.getBTToken(nodes.get(0).getSymbol());
} else {
token = this.getBTToken(nodes.get(1).getSymbol());
flag = nodes.get(0).getSymbol().getText();
if (!(flag.equals("render") || flag.equals("norender"))) {
BeetlException be = new BeetlException(BeetlException.AJAX_PROPERTY_ERROR, "expect render or norender ,but " + flag);
be.pushToken(token);
throw be;
}
}
BlockContext blockCtx = ajaxCtx.block();
BlockStatement block = (BlockStatement) this.parseBlock(blockCtx.statement(), blockCtx);
AjaxStatement ajaxStat = new AjaxStatement(block, token, flag.equals("render"));
if (this.data.ajaxs == null) {
this.data.ajaxs = new HashMap<String, AjaxStatement>();
}
String anchor = ajaxStat.token.text;
if (this.data.ajaxs.containsKey(anchor)) {
GrammarToken lastToken = this.data.ajaxs.get(anchor).token;
BeetlException ex = new BeetlException(BeetlException.AJAX_ALREADY_DEFINED, "已经在第" + lastToken.line + "行定义");
ex.pushToken(token);
throw ex;
}
this.data.ajaxs.put(anchor, ajaxStat);
return ajaxStat;
}
use of org.beetl.core.parser.BeetlParser.BlockContext 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