Search in sources :

Example 11 with Expression

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

the class Output method addEncodeToChildren.

private void addEncodeToChildren(Iterator it, Expression encodeForValue, FunctionLibFunction encodeForBIF) {
    Statement stat;
    while (it.hasNext()) {
        stat = (Statement) it.next();
        if (stat instanceof PrintOut) {
            PrintOut printOut = ((PrintOut) stat);
            Expression e = removeCastString(printOut.getExpr());
            if (!(e instanceof Literal)) {
                if (e instanceof Variable) {
                    Member member = ((Variable) e).getFirstMember();
                    if (member instanceof BIF) {
                        BIF bif = (BIF) member;
                        String cn = bif.getClassDefinition().getClassName();
                        if (cn.startsWith(ENCODE_FOR) || cn.equals(ESAPIEncode.class.getName())) {
                            continue;
                        }
                    }
                }
                printOut.setEncodeFor(encodeForValue);
            }
        } else if (stat instanceof Tag) {
            Body b = ((Tag) stat).getBody();
            if (b != null)
                addEncodeToChildren(b.getStatements().iterator(), encodeForValue, encodeForBIF);
        } else if (stat instanceof Body) {
            addEncodeToChildren(((Body) stat).getStatements().iterator(), encodeForValue, encodeForBIF);
        }
    }
}
Also used : Variable(lucee.transformer.expression.var.Variable) PrintOut(lucee.transformer.bytecode.statement.PrintOut) Expression(lucee.transformer.expression.Expression) Statement(lucee.transformer.bytecode.Statement) Literal(lucee.transformer.expression.literal.Literal) CastString(lucee.transformer.bytecode.cast.CastString) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) Member(lucee.transformer.expression.var.Member) Body(lucee.transformer.bytecode.Body) BIF(lucee.transformer.bytecode.expression.var.BIF)

Example 12 with Expression

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

the class AbstrCFMLExprTransformer method assignOp.

/**
 * Transfomiert Zuweisungs Operation.
 * <br />
 * EBNF:<br />
 * <code>eqvOp ["=" spaces assignOp];</code>
 * @return CFXD Element
 * @throws TemplateException
 */
protected Expression assignOp(ExprData data) throws TemplateException {
    Expression expr = conditionalOp(data);
    if (data.srcCode.forwardIfCurrent('=')) {
        comments(data);
        if (data.mode == STATIC)
            expr = new DynAssign(expr, assignOp(data));
        else {
            if (expr instanceof Variable) {
                Expression value = assignOp(data);
                expr = new Assign((Variable) expr, value, data.srcCode.getPosition());
            } else if (expr instanceof Null) {
                Variable var = ((Null) expr).toVariable();
                Expression value = assignOp(data);
                expr = new Assign(var, value, data.srcCode.getPosition());
            } else
                throw new TemplateException(data.srcCode, "invalid assignment left-hand side (" + expr.getClass().getName() + ")");
        }
    }
    // patch for test()();  only works at the end of an expression!
    comments(data);
    while (data.srcCode.isCurrent('(')) {
        comments(data);
        Call call = new Call(expr);
        getFunctionMemberAttrs(data, null, false, call, null);
        call.setEnd(data.srcCode.getPosition());
        comments(data);
        expr = call;
    }
    return expr;
}
Also used : Call(lucee.transformer.bytecode.expression.var.Call) Null(lucee.transformer.bytecode.literal.Null) OpVariable(lucee.transformer.bytecode.op.OpVariable) Variable(lucee.transformer.expression.var.Variable) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TemplateException(lucee.runtime.exp.TemplateException) DynAssign(lucee.transformer.bytecode.expression.var.DynAssign) Assign(lucee.transformer.bytecode.expression.var.Assign) DynAssign(lucee.transformer.bytecode.expression.var.DynAssign)

Example 13 with Expression

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

the class AbstrCFMLExprTransformer method dynamic.

/**
 * Liest den folgenden idetifier ein und prueft ob dieser ein boolscher Wert ist.
 * Im Gegensatz zu CFMX wird auch "yes" und "no" als bolscher <wert akzeptiert,
 * was bei CFMX nur beim Umwandeln einer Zeichenkette zu einem boolschen Wert der Fall ist.<br />
 * Wenn es sich um keinen bolschen Wert handelt wird der folgende Wert eingelesen mit seiner ganzen Hirarchie.
 * <br />
 * EBNF:<br />
 * <code>"true" | "false" | "yes" | "no" | startElement {("." identifier | "[" structElement "]" )[function] };</code>
 * @return CFXD Element
 * @throws TemplateException
 */
private Expression dynamic(ExprData data) throws TemplateException {
    // Die Implementation weicht ein wenig von der Grammatik ab,
    // aber nicht in der Logik sondern rein wie es umgesetzt wurde.
    // get First Element of the Variable
    Position line = data.srcCode.getPosition();
    Identifier id = identifier(data, false, true);
    if (id == null) {
        if (!data.srcCode.forwardIfCurrent('('))
            return null;
        comments(data);
        Expression expr = assignOp(data);
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "Invalid Syntax Closing [)] not found");
        comments(data);
        // subDynamic(expr);
        return expr;
    }
    Variable var;
    comments(data);
    // Boolean constant
    if (id.getString().equalsIgnoreCase("TRUE")) {
        // || name.equals("YES"))	{
        comments(data);
        return id.getFactory().createLitBoolean(true, line, data.srcCode.getPosition());
    } else if (id.getString().equalsIgnoreCase("FALSE")) {
        // || name.equals("NO"))	{
        comments(data);
        return id.getFactory().createLitBoolean(false, line, data.srcCode.getPosition());
    } else if ((data.srcCode.getDialect() != CFMLEngine.DIALECT_CFML || data.config.getFullNullSupport()) && id.getString().equalsIgnoreCase("NULL")) {
        comments(data);
        return id.getFactory().createNull(line, data.srcCode.getPosition());
    }
    // Extract Scope from the Variable
    var = startElement(data, id, line);
    var.setStart(line);
    var.setEnd(data.srcCode.getPosition());
    return var;
}
Also used : Identifier(lucee.transformer.bytecode.literal.Identifier) 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)

Example 14 with Expression

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

the class TagFunction method _writeOut.

public void _writeOut(BytecodeContext bc, int type) throws TransformerException {
    // private static final Expression EMPTY = LitString.toExprString("");
    Body functionBody = new BodyBase(bc.getFactory());
    RefBoolean isStatic = new RefBooleanImpl();
    Function func = createFunction(bc.getPage(), functionBody, isStatic, bc.getOutput());
    // ScriptBody sb=new ScriptBody(bc.getFactory());
    func.setParent(getParent());
    List<Statement> statements = getBody().getStatements();
    Statement stat;
    Tag tag;
    // suppress WS between cffunction and the last cfargument
    Tag last = null;
    if (bc.getSupressWSbeforeArg()) {
        // check if there is a cfargument at all
        Iterator<Statement> it = statements.iterator();
        while (it.hasNext()) {
            stat = it.next();
            if (stat instanceof Tag) {
                tag = (Tag) stat;
                if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Argument")) {
                    last = tag;
                }
            }
        }
        // check if there are only literal WS printouts
        if (last != null) {
            it = statements.iterator();
            while (it.hasNext()) {
                stat = it.next();
                if (stat == last)
                    break;
                if (stat instanceof PrintOut) {
                    PrintOut po = (PrintOut) stat;
                    Expression expr = po.getExpr();
                    if (!(expr instanceof LitString) || !StringUtil.isWhiteSpace(((LitString) expr).getString())) {
                        last = null;
                        break;
                    }
                }
            }
        }
    }
    Iterator<Statement> it = statements.iterator();
    boolean beforeLastArgument = last != null;
    while (it.hasNext()) {
        stat = it.next();
        if (beforeLastArgument) {
            if (stat == last) {
                beforeLastArgument = false;
            } else if (stat instanceof PrintOut) {
                PrintOut po = (PrintOut) stat;
                Expression expr = po.getExpr();
                if (expr instanceof LitString) {
                    LitString ls = (LitString) expr;
                    if (StringUtil.isWhiteSpace(ls.getString()))
                        continue;
                }
            }
        }
        if (stat instanceof Tag) {
            tag = (Tag) stat;
            if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Argument")) {
                addArgument(func, tag);
                continue;
            }
        }
        functionBody.addStatement(stat);
    }
    func._writeOut(bc, type);
}
Also used : RefBoolean(lucee.commons.lang.types.RefBoolean) Statement(lucee.transformer.bytecode.Statement) Function(lucee.transformer.bytecode.statement.udf.Function) IFunction(lucee.transformer.bytecode.statement.IFunction) LitString(lucee.transformer.expression.literal.LitString) PrintOut(lucee.transformer.bytecode.statement.PrintOut) Expression(lucee.transformer.expression.Expression) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl) Body(lucee.transformer.bytecode.Body) BodyBase(lucee.transformer.bytecode.BodyBase)

Example 15 with Expression

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

the class TagGroupUtil method writeOutTypeQuery.

public static void writeOutTypeQuery(final TagGroup tag, BytecodeContext bc) throws TransformerException {
    final GeneratorAdapter adapter = bc.getAdapter();
    tag.setNumberIterator(adapter.newLocal(NUMBER_ITERATOR));
    boolean isOutput = tag.getType() == TagGroup.TAG_OUTPUT;
    ParseBodyVisitor pbv = isOutput ? new ParseBodyVisitor() : null;
    if (isOutput)
        pbv.visitBegin(bc);
    // Query query=pc.getQuery(@query);
    tag.setQuery(adapter.newLocal(Types.QUERY));
    adapter.loadArg(0);
    Expression val = tag.getAttribute("query").getValue();
    val.writeOut(bc, Expression.MODE_REF);
    if (val instanceof LitString)
        adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_QUERY_STRING);
    else
        adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_QUERY_OBJ);
    adapter.storeLocal(tag.getQuery());
    tag.setPID(adapter.newLocal(Types.INT_VALUE));
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_ID);
    adapter.storeLocal(tag.getPID());
    // int startAt=query.getCurrentrow();
    final int startAt = adapter.newLocal(Types.INT_VALUE);
    adapter.loadLocal(tag.getQuery());
    adapter.loadLocal(tag.getPID());
    // adapter.loadArg(0);
    // adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_ID);
    adapter.invokeInterface(Types.QUERY, TagLoop.GET_CURRENTROW_1);
    adapter.storeLocal(startAt);
    // if(query.getRecordcount()>0) {
    DecisionIntVisitor div = new DecisionIntVisitor();
    div.visitBegin();
    adapter.loadLocal(tag.getQuery());
    adapter.invokeInterface(Types.QUERY, GET_RECORDCOUNT);
    div.visitGT();
    adapter.push(0);
    div.visitEnd(bc);
    Label ifRecCount = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, ifRecCount);
    // startrow
    int from = adapter.newLocal(Types.INT_VALUE);
    Attribute attrStartRow = tag.getAttribute("startrow");
    if (attrStartRow != null) {
        // NumberRange.range(@startrow,1)
        // attrStartRow.getValue().writeOut(bc, Expression.MODE_VALUE);
        bc.getFactory().toExprInt(attrStartRow.getValue()).writeOut(bc, Expression.MODE_VALUE);
        // adapter.visitInsn(Opcodes.D2I);
        adapter.push(1);
        adapter.invokeStatic(Types.NUMBER_RANGE, RANGE);
    // adapter.visitInsn(Opcodes.D2I);
    } else {
        adapter.push(1);
    }
    adapter.storeLocal(from);
    // numberIterator
    adapter.loadLocal(from);
    adapter.loadLocal(tag.getQuery());
    adapter.invokeInterface(Types.QUERY, GET_RECORDCOUNT);
    // adapter.visitInsn(Opcodes.I2D);
    Attribute attrMaxRow = tag.getAttribute("maxrows");
    Attribute attrEndRow = tag.getAttribute("endrow");
    if (attrMaxRow != null) {
        bc.getFactory().toExprInt(attrMaxRow.getValue()).writeOut(bc, Expression.MODE_VALUE);
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_MAX);
    } else if (attrEndRow != null) {
        bc.getFactory().toExprInt(attrEndRow.getValue()).writeOut(bc, Expression.MODE_VALUE);
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_END);
    } else {
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_2);
    }
    adapter.storeLocal(tag.getNumberIterator());
    // Group
    Attribute attrGroup = tag.getAttribute("group");
    Attribute attrGroupCS = tag.getAttribute("groupcasesensitive");
    tag.setGroup(adapter.newLocal(Types.STRING));
    final int groupCaseSensitive = adapter.newLocal(Types.BOOLEAN_VALUE);
    if (attrGroup != null) {
        attrGroup.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.storeLocal(tag.getGroup());
        if (attrGroupCS != null)
            attrGroupCS.getValue().writeOut(bc, Expression.MODE_VALUE);
        else
            adapter.push(false);
        adapter.storeLocal(groupCaseSensitive);
    }
    // pc.us().addQuery(query);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, US);
    adapter.loadLocal(tag.getQuery());
    adapter.invokeInterface(UNDEFINED, ADD_QUERY);
    // current
    final int current = adapter.newLocal(Types.INT_VALUE);
    adapter.loadLocal(from);
    adapter.push(1);
    adapter.visitInsn(Opcodes.ISUB);
    adapter.storeLocal(current);
    // Try
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            // query.reset();
            // query.go(startAt);
            adapter.loadLocal(tag.getQuery());
            adapter.loadLocal(startAt);
            adapter.loadLocal(tag.getPID());
            // adapter.loadArg(0);
            // adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_ID);
            adapter.invokeInterface(Types.QUERY, TagLoop.GO);
            adapter.pop();
            // pc.us().removeQuery();
            adapter.loadArg(0);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, US);
            adapter.invokeInterface(UNDEFINED, REMOVE_QUERY);
            // NumberIterator.release(ni);
            adapter.loadLocal(tag.getNumberIterator());
            adapter.invokeStatic(NUMBER_ITERATOR, REALEASE);
        }
    }, null);
    tfv.visitTryBegin(bc);
    WhileVisitor wv = new WhileVisitor();
    if (tag instanceof TagLoop)
        ((TagLoop) tag).setLoopVisitor(wv);
    wv.visitBeforeExpression(bc);
    // while(ni.isValid()) {
    adapter.loadLocal(tag.getNumberIterator());
    adapter.loadLocal(current);
    adapter.push(1);
    adapter.visitInsn(Opcodes.IADD);
    adapter.invokeVirtual(NUMBER_ITERATOR, IS_VALID_1);
    wv.visitAfterExpressionBeforeBody(bc);
    // if(!query.go(ni.current()))break;
    adapter.loadLocal(tag.getQuery());
    adapter.loadLocal(tag.getNumberIterator());
    adapter.invokeVirtual(NUMBER_ITERATOR, CURRENT);
    adapter.loadLocal(tag.getPID());
    adapter.invokeInterface(Types.QUERY, TagLoop.GO);
    NotVisitor.visitNot(bc);
    Label _if = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, _if);
    wv.visitBreak(bc);
    adapter.visitLabel(_if);
    if (attrGroup != null) {
        // NumberIterator oldNi=numberIterator;
        int oldNi = adapter.newLocal(NUMBER_ITERATOR);
        adapter.loadLocal(tag.getNumberIterator());
        adapter.storeLocal(oldNi);
        // numberIterator=NumberIterator.load(ni,query,group,grp_case);
        adapter.loadArg(0);
        adapter.loadLocal(tag.getNumberIterator());
        adapter.loadLocal(tag.getQuery());
        adapter.loadLocal(tag.getGroup());
        adapter.loadLocal(groupCaseSensitive);
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_5);
        adapter.storeLocal(tag.getNumberIterator());
        // current=oldNi.current();
        adapter.loadLocal(oldNi);
        adapter.invokeVirtual(NUMBER_ITERATOR, CURRENT);
        adapter.storeLocal(current);
        tag.getBody().writeOut(bc);
        // tmp(adapter,current);
        // NumberIterator.release(ni);
        adapter.loadLocal(tag.getNumberIterator());
        adapter.invokeStatic(NUMBER_ITERATOR, REALEASE);
        // numberIterator=oldNi;
        adapter.loadLocal(oldNi);
        adapter.storeLocal(tag.getNumberIterator());
    } else {
        // current=ni.current();
        adapter.loadLocal(tag.getNumberIterator());
        adapter.invokeVirtual(NUMBER_ITERATOR, CURRENT);
        adapter.storeLocal(current);
        tag.getBody().writeOut(bc);
    }
    // ni.setCurrent(current+1);
    /*adapter.loadLocal(tag.getNumberIterator());
					adapter.loadLocal(current);
					adapter.push(1);
					adapter.visitInsn(Opcodes.IADD);
					adapter.invokeVirtual(NUMBER_ITERATOR, SET_CURRENT);*/
    wv.visitAfterBody(bc, tag.getEnd());
    tfv.visitTryEnd(bc);
    adapter.visitLabel(ifRecCount);
    if (isOutput)
        pbv.visitEnd(bc);
}
Also used : ParseBodyVisitor(lucee.transformer.bytecode.visitor.ParseBodyVisitor) Label(org.objectweb.asm.Label) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) DecisionIntVisitor(lucee.transformer.bytecode.visitor.DecisionIntVisitor) WhileVisitor(lucee.transformer.bytecode.visitor.WhileVisitor) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

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