Search in sources :

Example 6 with GrammarToken

use of org.beetl.core.statement.GrammarToken in project beetl2.0 by javamonkey.

the class LineStatus method readHTMLTagEnd.

public void readHTMLTagEnd() throws HTMLTagParserException {
    String tagName = null;
    try {
        HTMLTagParser html = new HTMLTagParser(cs, index, htmlTagBindingAttribute, false);
        html.parser();
        tagName = html.getTagName();
        if (htmlTagStack.empty()) {
            throw new RuntimeException("解析html tag出错");
        }
        String lastTag = (String) this.htmlTagStack.peek();
        if (tagName.equals(lastTag)) {
            this.htmlTagStack.pop();
            sb.append("}");
        } else {
            throw new RuntimeException("解析html tag出错,期望匹配标签" + lastTag);
        }
        this.index = html.getIndex();
        status = 1;
        this.lineStatus.setStatment();
    } catch (RuntimeException re) {
        if (tagName == null) {
            tagName = "未知标签";
        }
        GrammarToken token = GrammarToken.createToken(tagName, this.totalLineCount + 1);
        HTMLTagParserException ex = new HTMLTagParserException(re.getMessage());
        ex.pushToken(token);
        // ex.token = token;
        ex.line = totalLineCount + 1;
        throw ex;
    }
}
Also used : HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 7 with GrammarToken

use of org.beetl.core.statement.GrammarToken in project beetl2.0 by javamonkey.

the class LineStatus method readHTMLTagBegin.

public void readHTMLTagBegin() throws HTMLTagParserException {
    String tagName = null;
    try {
        StringBuilder script = new StringBuilder();
        HTMLTagParser html = new HTMLTagParser(cs, index, htmlTagBindingAttribute, true);
        html.parser();
        if (html.hasVarBinding) {
            script.append("htmltagvar");
        } else {
            script.append("htmltag");
        }
        tagName = html.getTagName();
        script.append("('").append(tagName).append("',");
        Map<String, String> map = html.getExpMap();
        Map<String, Character> quat = html.getQuatMap();
        if (map.size() != 0) {
            script.append("{");
        }
        for (Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (html.crKey.contains(key)) {
                script.append(this.lineSeparator);
            }
            script.append(key).append(":");
            String attrValue = this.parseAttr(quat.get(key), value);
            script.append(attrValue);
            script.append(",");
        }
        script.setLength(script.length() - 1);
        if (map.size() != 0) {
            script.append("}");
        }
        if (html.hasVarBinding) {
            if (map.size() == 0) {
                // 保持三个参数,第一个为标签函数名,第二个为属性,第三个为申明的变量
                script.append(",{}");
            }
            if (html.varBidingStr.trim().length() == 0) {
                String defaultVarName = null;
                int index = tagName.lastIndexOf(":");
                if (index == -1) {
                    defaultVarName = tagName;
                } else {
                    defaultVarName = tagName.substring(index + 1);
                }
                script.append(",").append("'").append(defaultVarName).append("'");
            } else {
                script.append(",").append("'").append(html.varBidingStr).append("'");
            }
        }
        script.append("){");
        if (html.isEmptyTag()) {
            script.append("}");
        } else {
            htmlTagStack.push(tagName);
        }
        sb.append(script);
        this.index = html.getIndex();
        status = 1;
        this.lineStatus.setStatment();
    } catch (RuntimeException re) {
        if (tagName == null) {
            tagName = "未知标签";
        }
        GrammarToken token = GrammarToken.createToken(tagName, this.totalLineCount + 1);
        HTMLTagParserException ex = new HTMLTagParserException(re.getMessage());
        ex.pushToken(token);
        // todo
        // ex.token = token;
        ex.line = totalLineCount + 1;
        throw ex;
    }
}
Also used : HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 8 with GrammarToken

use of org.beetl.core.statement.GrammarToken in project beetl2.0 by javamonkey.

the class ALU method UnsupportedTypeException.

private static RuntimeException UnsupportedTypeException(final Object o1, final Object o2, final ASTNode node1, final ASTNode node2, String type) {
    BeetlException ex = new BeetlException(BeetlException.EXPRESSION_NOT_COMPATIBLE, o1.getClass() + type + o2.getClass());
    GrammarToken token = GrammarToken.createToken(node1.token.text + type + node2.token.text, node1.token.line);
    ex.pushToken(token);
    throw ex;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 9 with GrammarToken

use of org.beetl.core.statement.GrammarToken in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method registerNewVar.

// 纪录一个新变量
protected void registerNewVar(ASTNode vas) {
    if (pbCtx.hasDefined(vas.token.text) != null) {
        GrammarToken token = pbCtx.hasDefined(vas.token.text);
        BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "已经在第" + token.line + "行定义");
        ex.pushToken(vas.token);
        throw ex;
    }
    pbCtx.addVar(vas.token.text);
    pbCtx.setVarPosition(vas.token.text, vas);
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 10 with GrammarToken

use of org.beetl.core.statement.GrammarToken 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;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) BlockStatement(org.beetl.core.statement.BlockStatement) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) GrammarToken(org.beetl.core.statement.GrammarToken) AjaxStatement(org.beetl.core.statement.AjaxStatement)

Aggregations

GrammarToken (org.beetl.core.statement.GrammarToken)13 BeetlException (org.beetl.core.exception.BeetlException)6 HTMLTagParserException (org.beetl.core.exception.HTMLTagParserException)4 IncDecExpression (org.beetl.core.statement.IncDecExpression)4 AjaxStatement (org.beetl.core.statement.AjaxStatement)3 StringReader (java.io.StringReader)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 AndExpression (org.beetl.core.statement.AndExpression)2 ArthExpression (org.beetl.core.statement.ArthExpression)2 BlockStatement (org.beetl.core.statement.BlockStatement)2 CompareExpression (org.beetl.core.statement.CompareExpression)2 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)2 Expression (org.beetl.core.statement.Expression)2 FormatExpression (org.beetl.core.statement.FormatExpression)2 FunctionExpression (org.beetl.core.statement.FunctionExpression)2 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)2 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)2 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)2 NegExpression (org.beetl.core.statement.NegExpression)2 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)2