Search in sources :

Example 1 with BooleanLiteralContext

use of org.beetl.core.parser.BeetlParser.BooleanLiteralContext in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseLiteralExpress.

protected Expression parseLiteralExpress(LiteralContext ctx) {
    ParseTree tree = ctx.getChild(0);
    Object value = null;
    if (tree instanceof TerminalNode) {
        Token node = ((TerminalNode) tree).getSymbol();
        String strValue = node.getText();
        int type = node.getType();
        switch(type) {
            case BeetlParser.StringLiteral:
                value = getStringValue(strValue);
                break;
            case BeetlParser.FloatingPointLiteral:
                char c = strValue.charAt(strValue.length() - 1);
                if (isHighScaleNumber(strValue)) {
                    String newValue = strValue.substring(0, strValue.length() - 1);
                    value = new BigDecimal(newValue);
                } else {
                    value = Double.parseDouble(strValue);
                }
                break;
            case BeetlParser.DecimalLiteral:
                if (isHighScaleNumber(strValue)) {
                    String newValue = strValue.substring(0, strValue.length() - 1);
                    value = new BigDecimal(newValue);
                } else {
                    if (strValue.length() < 10) {
                        value = Integer.parseInt(strValue);
                    } else if (strValue.length() > 10) {
                        value = Long.parseLong(strValue);
                    } else if (strValue.compareTo("2147483647") > 0) {
                        value = Long.parseLong(strValue);
                    } else {
                        value = Integer.parseInt(strValue);
                    }
                }
                break;
            case BeetlParser.NULL:
                value = null;
                break;
        }
    } else {
        BooleanLiteralContext blc = (BooleanLiteralContext) tree;
        String strValue = blc.getChild(0).getText();
        value = Boolean.parseBoolean(strValue);
    }
    if (value == null) {
        return Literal.NULLLiteral;
    } else {
        Literal literal = new Literal(value, this.getBTToken(ctx.getStart()));
        return literal;
    }
}
Also used : BooleanLiteralContext(org.beetl.core.parser.BeetlParser.BooleanLiteralContext) Literal(org.beetl.core.statement.Literal) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)1 Token (org.antlr.v4.runtime.Token)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 BooleanLiteralContext (org.beetl.core.parser.BeetlParser.BooleanLiteralContext)1 GrammarToken (org.beetl.core.statement.GrammarToken)1 Literal (org.beetl.core.statement.Literal)1