Search in sources :

Example 26 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class AbstrCFMLExprTransformer method string.

/*private Expression variable(Data data) throws TemplateException {
		Expression expr=null;
		
		// Dynamic
		if((expr=dynamic(data))!=null) {
			expr = subDynamic(data,expr);
			data.mode=DYNAMIC;
			return expr;
		} 
		return null;
	}*/
/**
 * Transfomiert einen lierale Zeichenkette.
 * <br />
 * EBNF:<br />
 * <code>("'" {"##"|"''"|"#" impOp "#"| ?-"#"-"'" } "'") |
 *	                 (""" {"##"|""""|"#" impOp "#"| ?-"#"-""" } """);</code>
 * @param data
 * @return CFXD Element
 * @throws TemplateException
 */
protected Expression string(ExprData data) throws TemplateException {
    // check starting character for a string literal
    if (!data.srcCode.isCurrent('"') && !data.srcCode.isCurrent('\''))
        return null;
    Position line = data.srcCode.getPosition();
    // Init Parameter
    char quoter = data.srcCode.getCurrentLower();
    StringBuilder str = new StringBuilder();
    Expression expr = null;
    while (data.srcCode.hasNext()) {
        data.srcCode.next();
        // check sharp
        if (data.srcCode.isCurrent('#')) {
            // Ecaped sharp
            if (data.srcCode.isNext('#')) {
                data.srcCode.next();
                str.append('#');
            } else // get Content of sharp
            {
                data.srcCode.next();
                comments(data);
                Expression inner = assignOp(data);
                comments(data);
                if (!data.srcCode.isCurrent('#'))
                    throw new TemplateException(data.srcCode, "Invalid Syntax Closing [#] not found");
                ExprString exprStr = null;
                if (str.length() != 0) {
                    exprStr = data.factory.createLitString(str.toString(), line, data.srcCode.getPosition());
                    if (expr != null) {
                        expr = data.factory.opString(expr, exprStr);
                    } else
                        expr = exprStr;
                    str = new StringBuilder();
                }
                if (expr == null) {
                    expr = inner;
                } else {
                    expr = data.factory.opString(expr, inner);
                }
            }
        } else // check quoter
        if (data.srcCode.isCurrent(quoter)) {
            // Ecaped sharp
            if (data.srcCode.isNext(quoter)) {
                data.srcCode.next();
                str.append(quoter);
            } else // finsish
            {
                break;
            }
        } else // all other character
        {
            str.append(data.srcCode.getCurrent());
        }
    }
    if (!data.srcCode.forwardIfCurrent(quoter))
        throw new TemplateException(data.srcCode, "Invalid Syntax Closing [" + quoter + "] not found");
    if (expr == null)
        expr = data.factory.createLitString(str.toString(), line, data.srcCode.getPosition());
    else if (str.length() != 0) {
        expr = data.factory.opString(expr, data.factory.createLitString(str.toString(), line, data.srcCode.getPosition()));
    }
    comments(data);
    if (expr instanceof Variable) {
        Variable var = (Variable) expr;
        var.fromHash(true);
    }
    return expr;
}
Also used : OpVariable(lucee.transformer.bytecode.op.OpVariable) Variable(lucee.transformer.expression.var.Variable) Position(lucee.transformer.Position) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TemplateException(lucee.runtime.exp.TemplateException) ExprString(lucee.transformer.expression.ExprString)

Example 27 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method tryStatement.

/**
 * Liest eine try Block ein
 * <br />
 * EBNF:<br />
 * <code>;</code>
 * @return Try Block
 * @throws TemplateException
 */
private final TryCatchFinally tryStatement(ExprData data) throws TemplateException {
    if (!data.srcCode.forwardIfCurrent("try", '{') && !data.srcCode.forwardIfCurrent("try ") && !data.srcCode.forwardIfCurrent("try", '/'))
        return null;
    data.srcCode.previous();
    Body body = new BodyBase(data.factory);
    TryCatchFinally tryCatchFinally = new TryCatchFinally(data.factory, body, data.srcCode.getPosition(), null);
    statement(data, body, CTX_TRY);
    comments(data);
    // catches
    short catchCount = 0;
    while (data.srcCode.forwardIfCurrent("catch", '(')) {
        catchCount++;
        comments(data);
        // type
        int pos = data.srcCode.getPos();
        Position line = data.srcCode.getPosition();
        Expression name = null, type = null;
        StringBuffer sbType = new StringBuffer();
        String id;
        while (true) {
            id = identifier(data, false);
            if (id == null)
                break;
            sbType.append(id);
            data.srcCode.removeSpace();
            if (!data.srcCode.forwardIfCurrent('.'))
                break;
            sbType.append('.');
            data.srcCode.removeSpace();
        }
        if (sbType.length() == 0) {
            type = string(data);
            if (type == null)
                throw new TemplateException(data.srcCode, "a catch statement must begin with the throwing type (query, application ...).");
        } else {
            type = data.factory.createLitString(sbType.toString());
        }
        // name = expression();
        comments(data);
        // name
        if (!data.srcCode.isCurrent(')')) {
            name = expression(data);
        } else {
            data.srcCode.setPos(pos);
            name = expression(data);
            type = data.factory.createLitString("any");
        }
        comments(data);
        Body b = new BodyBase(data.factory);
        try {
            tryCatchFinally.addCatch(type, name, b, line);
        } catch (TransformerException e) {
            throw new TemplateException(data.srcCode, e.getMessage());
        }
        comments(data);
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "invalid catch statement, missing closing )");
        statement(data, b, CTX_CATCH);
        comments(data);
    }
    // finally
    if (finallyStatement(data, tryCatchFinally)) {
        comments(data);
    } else if (catchCount == 0)
        throw new TemplateException(data.srcCode, "a try statement must have at least one catch statement");
    // if(body.isEmpty()) return null;
    tryCatchFinally.setEnd(data.srcCode.getPosition());
    return tryCatchFinally;
}
Also used : Position(lucee.transformer.Position) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TemplateException(lucee.runtime.exp.TemplateException) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase) TransformerException(lucee.transformer.TransformerException) TryCatchFinally(lucee.transformer.bytecode.statement.TryCatchFinally)

Example 28 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method caseStatement.

/**
 * Liest ein Case Statement ein
 * @return case Statement
 * @throws TemplateException
 */
private final boolean caseStatement(ExprData data, Switch swit) throws TemplateException {
    if (!data.srcCode.forwardIfCurrentAndNoWordAfter("case"))
        return false;
    // int line=data.srcCode.getLine();
    comments(data);
    Expression expr = super.expression(data);
    comments(data);
    if (!data.srcCode.forwardIfCurrent(':'))
        throw new TemplateException(data.srcCode, "case body must start with [:]");
    Body body = new BodyBase(data.factory);
    switchBlock(data, body);
    swit.addCase(expr, body);
    return true;
}
Also used : FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TemplateException(lucee.runtime.exp.TemplateException) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase)

Example 29 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method switchStatement.

/**
 * Liest ein switch Statment ein
 * @return switch Statement
 * @throws TemplateException
 */
private final Switch switchStatement(ExprData data) throws TemplateException {
    if (!data.srcCode.forwardIfCurrent("switch", '('))
        return null;
    Position line = data.srcCode.getPosition();
    comments(data);
    Expression expr = super.expression(data);
    comments(data);
    // end )
    if (!data.srcCode.forwardIfCurrent(')'))
        throw new TemplateException(data.srcCode, "switch statement must end with a [)]");
    comments(data);
    if (!data.srcCode.forwardIfCurrent('{'))
        throw new TemplateException(data.srcCode, "switch statement must have a starting  [{]");
    Switch swit = new Switch(expr, line, null);
    // cases
    // Node child=null;
    comments(data);
    while (caseStatement(data, swit)) {
        comments(data);
    }
    // default
    if (defaultStatement(data, swit)) {
        comments(data);
    }
    while (caseStatement(data, swit)) {
        comments(data);
    }
    // }
    if (!data.srcCode.forwardIfCurrent('}'))
        throw new TemplateException(data.srcCode, "invalid construct in switch statement");
    swit.setEnd(data.srcCode.getPosition());
    return swit;
}
Also used : Switch(lucee.transformer.bytecode.statement.Switch) Position(lucee.transformer.Position) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TemplateException(lucee.runtime.exp.TemplateException)

Example 30 with Expression

use of lucee.transformer.expression.Expression in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method _paramStatement.

private Tag _paramStatement(ExprData data, Body parent) throws TemplateException {
    if (!data.srcCode.forwardIfCurrent("param "))
        return null;
    Position line = data.srcCode.getPosition();
    TagLibTag tlt = CFMLTransformer.getTLT(data.srcCode, "param", data.config.getIdentification());
    TagParam param = new TagParam(data.factory, line, null);
    // type
    boolean hasType = false;
    boolean hasName = false;
    int pos = data.srcCode.getPos();
    // first 2 arguments can be type/name directly
    String tmp = variableDec(data, true);
    do {
        if (!StringUtil.isEmpty(tmp)) {
            TagLibTagAttr attr = tlt.getAttribute(tmp.toLowerCase(), true);
            // name is not a defined attribute
            if (attr == null) {
                comments(data);
                // it could be a name followed by default value
                if (data.srcCode.forwardIfCurrent('=')) {
                    comments(data);
                    Expression v = attributeValue(data, true);
                    param.addAttribute(new Attribute(false, "name", data.factory.createLitString(tmp), "string"));
                    param.addAttribute(new Attribute(false, "default", v, "string"));
                    hasName = true;
                    // if we had a value this was already name
                    break;
                }
                // can be type or name
                int pos2 = data.srcCode.getPos();
                // first could be type, followed by name
                comments(data);
                String tmp2 = variableDec(data, true);
                if (!StringUtil.isEmpty(tmp2)) {
                    attr = tlt.getAttribute(tmp2.toLowerCase(), true);
                    if (attr == null) {
                        param.addAttribute(new Attribute(false, "name", data.factory.createLitString(tmp2), "string"));
                        param.addAttribute(new Attribute(false, "type", data.factory.createLitString(tmp), "string"));
                        if (data.srcCode.forwardIfCurrent('=')) {
                            Expression v = attributeValue(data, true);
                            param.addAttribute(new Attribute(false, "default", v, "string"));
                        }
                        hasName = true;
                        hasType = true;
                        break;
                    }
                }
                param.addAttribute(new Attribute(false, "name", data.factory.createLitString(tmp), "string"));
                data.srcCode.setPos(pos2);
                hasName = true;
            } else
                data.srcCode.setPos(pos);
        } else
            data.srcCode.setPos(pos);
    } while (false);
    // folgend wird tlt extra nicht uebergeben, sonst findet pruefung statt
    Attribute[] attrs = attributes(param, tlt, data, SEMI, data.factory.NULL(), Boolean.TRUE, "name", true, ',', false);
    checkSemiColonLineFeed(data, true, true, true);
    param.setTagLibTag(tlt);
    param.setScriptBase(true);
    Attribute attr;
    // first fill all regular attribute -> name="value"
    boolean hasDynamic = false;
    for (int i = attrs.length - 1; i >= 0; i--) {
        attr = attrs[i];
        if (!attr.getValue().equals(data.factory.NULL())) {
            if (attr.getName().equalsIgnoreCase("name")) {
                hasName = true;
                param.addAttribute(attr);
            } else if (attr.getName().equalsIgnoreCase("type")) {
                hasType = true;
                param.addAttribute(attr);
            } else if (attr.isDynamicType()) {
                hasName = true;
                if (hasDynamic)
                    throw attrNotSupported(data.srcCode, tlt, attr.getName());
                hasDynamic = true;
                param.addAttribute(new Attribute(false, "name", data.factory.createLitString(attr.getName()), "string"));
                param.addAttribute(new Attribute(false, "default", attr.getValue(), "any"));
            } else
                param.addAttribute(attr);
        }
    }
    // now fill name named attributes -> attr1 attr2
    String first = null, second = null;
    for (int i = 0; i < attrs.length; i++) {
        attr = attrs[i];
        if (attr.getValue().equals(data.factory.NULL())) {
            // type
            if (first == null && (!hasName || !hasType)) {
                first = attr.getName();
            } else // name
            if (second == null && !hasName && !hasType) {
                second = attr.getName();
            } else // attr with no value
            {
                attr = new Attribute(true, attr.getName(), data.factory.EMPTY(), "string");
                param.addAttribute(attr);
            }
        }
    }
    if (first != null) {
        if (second != null) {
            hasName = true;
            hasType = true;
            if (hasDynamic)
                throw attrNotSupported(data.srcCode, tlt, first);
            hasDynamic = true;
            param.addAttribute(new Attribute(false, "name", data.factory.createLitString(second), "string"));
            param.addAttribute(new Attribute(false, "type", data.factory.createLitString(first), "string"));
        } else {
            param.addAttribute(new Attribute(false, hasName ? "type" : "name", data.factory.createLitString(first), "string"));
            hasName = true;
        }
    }
    if (!hasName)
        throw new TemplateException(data.srcCode, "missing name declaration for param");
    param.setEnd(data.srcCode.getPosition());
    return param;
}
Also used : TagLibTagAttr(lucee.transformer.library.tag.TagLibTagAttr) TagLibTag(lucee.transformer.library.tag.TagLibTag) TagParam(lucee.transformer.bytecode.statement.tag.TagParam) Position(lucee.transformer.Position) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) TemplateException(lucee.runtime.exp.TemplateException)

Aggregations

Expression (lucee.transformer.expression.Expression)74 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)34 LitString (lucee.transformer.expression.literal.LitString)25 TemplateException (lucee.runtime.exp.TemplateException)19 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)16 Variable (lucee.transformer.expression.var.Variable)16 Body (lucee.transformer.bytecode.Body)11 ExprString (lucee.transformer.expression.ExprString)11 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)11 Position (lucee.transformer.Position)10 OpVariable (lucee.transformer.bytecode.op.OpVariable)10 LitBoolean (lucee.transformer.expression.literal.LitBoolean)8 Literal (lucee.transformer.expression.literal.Literal)8 ArrayList (java.util.ArrayList)7 TransformerException (lucee.transformer.TransformerException)7 BodyBase (lucee.transformer.bytecode.BodyBase)7 Statement (lucee.transformer.bytecode.Statement)7 Argument (lucee.transformer.bytecode.expression.var.Argument)7 PrintOut (lucee.transformer.bytecode.statement.PrintOut)6 FunctionBody (lucee.transformer.bytecode.FunctionBody)5