Search in sources :

Example 46 with TemplateException

use of lucee.runtime.exp.TemplateException in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method expressionStatement.

/**
 * List mithilfe des data.CFMLExprTransformer einen Ausruck ein.
 * <br />
 * EBNF:<br />
 * <code>expression ";";</code>
 * @param parent
 * @return Ausdruck
 * @throws TemplateException
 */
private Statement expressionStatement(ExprData data, Body parent) throws TemplateException {
    // first we check if we have a access modifier
    int pos = data.srcCode.getPos();
    int access = -1;
    boolean _final = false;
    if (data.context == CTX_CFC || data.context == CTX_STATIC) {
        if (data.srcCode.forwardIfCurrent("final ")) {
            _final = true;
            comments(data);
        }
        if (data.srcCode.forwardIfCurrent("private ")) {
            access = Component.ACCESS_PRIVATE;
            comments(data);
        } else if (data.srcCode.forwardIfCurrent("package ")) {
            access = Component.ACCESS_PACKAGE;
            comments(data);
        } else if (data.srcCode.forwardIfCurrent("public ")) {
            access = Component.ACCESS_PUBLIC;
            comments(data);
        } else if (data.srcCode.forwardIfCurrent("remote ")) {
            access = Component.ACCESS_REMOTE;
            // comments(data);
            throw new TemplateException(data.srcCode, "access modifier [remote] not supported in this context");
        }
        if (!_final && data.srcCode.forwardIfCurrent("final ")) {
            _final = true;
            comments(data);
        }
    }
    Expression expr = expression(data);
    checkSemiColonLineFeed(data, true, true, false);
    // variable declaration (variable in body)
    if (expr instanceof Variable) {
        Variable v = (Variable) expr;
        if (ASMUtil.isOnlyDataMember(v)) {
            expr = new Assign(v, data.srcCode.getDialect() == CFMLEngine.DIALECT_LUCEE || data.config.getFullNullSupport() ? data.factory.createNull() : data.factory.EMPTY(), data.srcCode.getPosition());
        }
    }
    // if a specific access was defined
    if (access > -1 || _final) {
        if (!(expr instanceof Assign)) {
            data.srcCode.setPos(pos);
            throw new TemplateException(data.srcCode, "invalid syntax, access modifier cannot be used in this context");
        }
        if (access > -1) {
            // this is only supported with the Lucee dialect
            // if(data.srcCode.getDialect()==CFMLEngine.DIALECT_CFML)
            // throw new TemplateException(data.srcCode,
            // "invalid syntax, access modifier cannot be used in this context");
            ((Assign) expr).setAccess(access);
        }
        if (_final)
            ((Assign) expr).setModifier(Member.MODIFIER_FINAL);
    }
    if (expr instanceof FunctionAsExpression)
        return ((FunctionAsExpression) expr).getFunction();
    return new ExpressionAsStatement(expr);
}
Also used : FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Variable(lucee.transformer.expression.var.Variable) TemplateException(lucee.runtime.exp.TemplateException) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) Assign(lucee.transformer.bytecode.expression.var.Assign) ExpressionAsStatement(lucee.transformer.bytecode.statement.ExpressionAsStatement)

Example 47 with TemplateException

use of lucee.runtime.exp.TemplateException in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method closurePart.

@Override
protected final Function closurePart(ExprData data, String id, int access, int modifier, String rtnType, Position line, boolean closure) throws TemplateException {
    Body body = new FunctionBody(data.factory);
    Function func = closure ? new Closure(data.root, id, access, modifier, rtnType, body, line, null) : new FunctionImpl(data.root, id, access, modifier, rtnType, body, line, null);
    comments(data);
    if (!data.srcCode.forwardIfCurrent('('))
        throw new TemplateException(data.srcCode, "invalid syntax in function head, missing begin [(]");
    // arguments
    ArrayList<Argument> args = getScriptFunctionArguments(data);
    for (Argument arg : args) {
        func.addArgument(arg.getName(), arg.getType(), arg.getRequired(), arg.getDefaultValue(), arg.isPassByReference(), arg.getDisplayName(), arg.getHint(), arg.getMetaData());
    }
    // end )
    comments(data);
    if (!data.srcCode.forwardIfCurrent(')'))
        throw new TemplateException(data.srcCode, "invalid syntax in function head, missing ending [)]");
    // doc comment
    if (data.docComment != null) {
        func.setHint(data.factory, data.docComment.getHint());
        // params
        /*Map<String, Attribute> params = data.docComment.getParams();
			Iterator<Attribute> it = params.values().iterator();
			Attribute attr;
			String name;
			while(it.hasNext()){
				attr=it.next();
				name=attr.getName();
			}*/
        func.setMetaData(data.docComment.getParams());
        data.docComment = null;
    }
    comments(data);
    // attributes
    Attribute[] attrs = attributes(null, null, data, SEMI_BLOCK, data.factory.EMPTY(), Boolean.TRUE, null, false, NO_ATTR_SEP, true);
    for (int i = 0; i < attrs.length; i++) {
        func.addAttribute(attrs[i]);
    }
    // body
    boolean oldInsideFunction = data.insideFunction;
    data.insideFunction = true;
    try {
        // ex block
        statement(data, body, CTX_FUNCTION);
    } finally {
        data.insideFunction = oldInsideFunction;
    }
    func.setEnd(data.srcCode.getPosition());
    if (closure)
        comments(data);
    return func;
}
Also used : Function(lucee.transformer.bytecode.statement.udf.Function) CFFunction(lucee.runtime.functions.system.CFFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) Closure(lucee.transformer.bytecode.statement.udf.Closure) Argument(lucee.transformer.bytecode.statement.Argument) TemplateException(lucee.runtime.exp.TemplateException) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) FunctionBody(lucee.transformer.bytecode.FunctionBody) FunctionImpl(lucee.transformer.bytecode.statement.udf.FunctionImpl) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody)

Example 48 with TemplateException

use of lucee.runtime.exp.TemplateException in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method funcStatement.

/**
 * Liest ein function Statement ein.
 * <br />
 * EBNF:<br />
 * <code>identifier spaces "(" spaces identifier spaces {"," spaces identifier spaces} ")" spaces block;</code>
 * @return function Statement
 * @throws TemplateException
 */
private final Statement funcStatement(ExprData data, Body parent) throws TemplateException {
    int pos = data.srcCode.getPos();
    // read 5 tokens (returntype,access modifier,"abstract|final|static","function", function name)
    String str = variableDec(data, false);
    // if there is no token at all we have no function
    if (str == null) {
        data.srcCode.setPos(pos);
        return null;
    }
    comments(data);
    String[] tokens = new String[] { str, null, null, null, null };
    tokens[1] = variableDec(data, false);
    comments(data);
    if (tokens[1] != null) {
        tokens[2] = variableDec(data, false);
        comments(data);
        if (tokens[2] != null) {
            tokens[3] = variableDec(data, false);
            comments(data);
            if (tokens[3] != null) {
                tokens[4] = identifier(data, false);
                comments(data);
            }
        }
    }
    // function name
    String functionName = null;
    for (int i = tokens.length - 1; i >= 0; i--) {
        // first from right is the function name
        if (tokens[i] != null) {
            functionName = tokens[i];
            tokens[i] = null;
            break;
        }
    }
    if (functionName == null || functionName.indexOf(',') != -1 || functionName.indexOf('[') != -1) {
        data.srcCode.setPos(pos);
        return null;
    }
    // throw new TemplateException(data.srcCode, "invalid syntax");
    String returnType = null;
    // search for "function"
    boolean hasOthers = false, first = true;
    for (int i = tokens.length - 1; i >= 0; i--) {
        if ("function".equalsIgnoreCase(tokens[i])) {
            // if it is the first "function" (from right) and we had already something else, the syntax is broken!
            if (hasOthers && first)
                throw new TemplateException(data.srcCode, "invalid syntax");
            else // we already have a return type,so this is the 3th "function"!
            if (returnType != null)
                throw new TemplateException(data.srcCode, "invalid syntax");
            else if (!first)
                returnType = tokens[i];
            first = false;
            tokens[i] = null;
        } else if (tokens[i] != null) {
            hasOthers = true;
        }
    }
    // no "function" found
    if (first) {
        data.srcCode.setPos(pos);
        return null;
    }
    // access modifier
    int _access, access = -1;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null && (_access = ComponentUtil.toIntAccess(tokens[i], -1)) != -1) {
            // we already have an access modifier
            if (access != -1) {
                // we already have a return type
                if (returnType != null)
                    throw new TemplateException(data.srcCode, "invalid syntax");
                returnType = tokens[i];
            } else
                access = _access;
            tokens[i] = null;
        }
    }
    // no access defined
    if (access == -1)
        access = Component.ACCESS_PUBLIC;
    // Non access modifier
    int _modifier, modifier = Component.MODIFIER_NONE;
    boolean isStatic = false;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null) {
            _modifier = ComponentUtil.toModifier(tokens[i], Component.MODIFIER_NONE, Component.MODIFIER_NONE);
            // abstract|final
            if (_modifier != Component.MODIFIER_NONE) {
                // we already have an Non access modifier
                if (modifier != Component.MODIFIER_NONE || isStatic) {
                    // we already have a return type
                    if (returnType != null)
                        throw new TemplateException(data.srcCode, "invalid syntax");
                    returnType = tokens[i];
                } else
                    modifier = _modifier;
                tokens[i] = null;
            } else // static
            if (tokens[i].equalsIgnoreCase("static")) {
                // we already have an Non access modifier
                if (modifier != Component.MODIFIER_NONE || isStatic) {
                    // we already have a return type
                    if (returnType != null)
                        throw new TemplateException(data.srcCode, "invalid syntax");
                    returnType = tokens[i];
                } else
                    isStatic = true;
                tokens[i] = null;
            }
        }
    }
    // return type
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null) {
            if (returnType != null)
                throw new TemplateException(data.srcCode, "invalid syntax");
            returnType = tokens[i];
        }
    }
    Position line = data.srcCode.getPosition();
    // Name
    if (!data.isCFC && !data.isInterface) {
        FunctionLibFunction flf = getFLF(data, functionName);
        try {
            if (flf != null && flf.getFunctionClassDefinition().getClazz() != CFFunction.class) {
                PageSource ps = null;
                if (data.srcCode instanceof PageSourceCode) {
                    ps = ((PageSourceCode) data.srcCode).getPageSource();
                }
                String path = null;
                if (ps != null) {
                    path = ps.getDisplayPath();
                    path = path.replace('\\', '/');
                }
                if (// TODO make better
                path == null || path.indexOf("/library/function/") == -1)
                    throw new TemplateException(data.srcCode, "The name [" + functionName + "] is already used by a built in Function");
            }
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            throw new PageRuntimeException(Caster.toPageException(t));
        }
    }
    Function res = closurePart(data, functionName, access, modifier, returnType, line, false);
    if (isStatic) {
        if (data.context == CTX_INTERFACE)
            throw new TemplateException(data.srcCode, "static functions are not allowed within the interface body");
        TagOther tag = createStaticTag(data, res.getStart());
        tag.getBody().addStatement(res);
        return tag;
    }
    return res;
}
Also used : CFFunction(lucee.runtime.functions.system.CFFunction) PageSourceCode(lucee.transformer.util.PageSourceCode) TemplateException(lucee.runtime.exp.TemplateException) Position(lucee.transformer.Position) TagOther(lucee.transformer.bytecode.statement.tag.TagOther) PageSource(lucee.runtime.PageSource) Function(lucee.transformer.bytecode.statement.udf.Function) CFFunction(lucee.runtime.functions.system.CFFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) PageRuntimeException(lucee.runtime.exp.PageRuntimeException)

Example 49 with TemplateException

use of lucee.runtime.exp.TemplateException in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method forStatement.

/**
 * Liest ein for Statement ein.
 * <br />
 * EBNF:<br />
 * <code>expression spaces ";" spaces condition spaces ";" spaces expression spaces ")" spaces block;</code>
 * @return for Statement
 * @throws TemplateException
 */
private final Statement forStatement(ExprData data) throws TemplateException {
    int pos = data.srcCode.getPos();
    // id
    String id = variableDec(data, false);
    if (id == null) {
        data.srcCode.setPos(pos);
        return null;
    }
    if (id.equalsIgnoreCase("for")) {
        id = null;
        data.srcCode.removeSpace();
        if (!data.srcCode.forwardIfCurrent('(')) {
            data.srcCode.setPos(pos);
            return null;
        }
    } else {
        data.srcCode.removeSpace();
        if (!data.srcCode.forwardIfCurrent(':')) {
            data.srcCode.setPos(pos);
            return null;
        }
        data.srcCode.removeSpace();
        if (!data.srcCode.forwardIfCurrent("for", '(')) {
            data.srcCode.setPos(pos);
            return null;
        }
    }
    // if(!data.srcCode.forwardIfCurrent("for",'('))
    // return null;
    Expression left = null;
    Body body = new BodyBase(data.factory);
    Position line = data.srcCode.getPosition();
    comments(data);
    if (!data.srcCode.isCurrent(';')) {
        // left
        left = expression(data);
        comments(data);
    }
    // middle for
    if (data.srcCode.forwardIfCurrent(';')) {
        Expression cont = null;
        Expression update = null;
        // condition
        comments(data);
        if (!data.srcCode.isCurrent(';')) {
            cont = condition(data);
            comments(data);
        }
        // middle
        if (!data.srcCode.forwardIfCurrent(';'))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement");
        // update
        comments(data);
        if (!data.srcCode.isCurrent(')')) {
            update = expression(data);
            comments(data);
        }
        // start )
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement, for statement must end with a [)]");
        // ex block
        statement(data, body, CTX_FOR);
        return new For(data.factory, left, cont, update, body, line, data.srcCode.getPosition(), id);
    } else // middle foreach
    if (data.srcCode.forwardIfCurrent("in")) {
        // condition
        comments(data);
        Expression value = expression(data);
        comments(data);
        if (!data.srcCode.forwardIfCurrent(')'))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement, for statement must end with a [)]");
        // ex block
        statement(data, body, CTX_FOR);
        if (!(left instanceof Variable))
            throw new TemplateException(data.srcCode, "invalid syntax in for statement, left value is invalid");
        // throw new TemplateException(data.srcCode,"invalid syntax in for statement, right value is invalid");
        return new ForEach((Variable) left, value, body, line, data.srcCode.getPosition(), id);
    } else
        throw new TemplateException(data.srcCode, "invalid syntax in for statement");
}
Also used : Variable(lucee.transformer.expression.var.Variable) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) Position(lucee.transformer.Position) TemplateException(lucee.runtime.exp.TemplateException) For(lucee.transformer.bytecode.statement.For) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase) ForEach(lucee.transformer.bytecode.statement.ForEach)

Example 50 with TemplateException

use of lucee.runtime.exp.TemplateException in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method getTag.

private final Tag getTag(ExprData data, Body parent, TagLibTag tlt, Position start, Position end) throws TemplateException {
    try {
        Tag tag = tlt.getTag(data.factory, start, end);
        tag.setParent(parent);
        return tag;
    } catch (TagLibException e) {
        throw new TemplateException(data.srcCode, e);
    }
/*if(StringUtil.isEmpty(tlt.getTttClassName()))tag= new TagBase(line);
		else {
			try {
				Class<Tag> clazz = ClassUtil.loadClass(tlt.getTttClassName());
				Constructor<Tag> constr = clazz.getConstructor(new Class[]{Position.class});
				tag = constr.newInstance(new Object[]{line});
				
			} 
			catch (Exception e) {
            SystemOut.printDate(e);
				tag= new TagBase(line);
			}
		}*/
}
Also used : TemplateException(lucee.runtime.exp.TemplateException) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) TagLibException(lucee.transformer.library.tag.TagLibException)

Aggregations

TemplateException (lucee.runtime.exp.TemplateException)55 Position (lucee.transformer.Position)21 Expression (lucee.transformer.expression.Expression)18 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)15 LitString (lucee.transformer.expression.literal.LitString)14 ScriptBody (lucee.transformer.bytecode.ScriptBody)9 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)9 Body (lucee.transformer.bytecode.Body)8 BodyBase (lucee.transformer.bytecode.BodyBase)8 FunctionBody (lucee.transformer.bytecode.FunctionBody)8 ComponentTemplateException (lucee.transformer.cfml.script.AbstrCFMLScriptTransformer.ComponentTemplateException)8 Variable (lucee.transformer.expression.var.Variable)8 ExprString (lucee.transformer.expression.ExprString)7 TagLibTag (lucee.transformer.library.tag.TagLibTag)6 PageSourceCode (lucee.transformer.util.PageSourceCode)6 ArrayList (java.util.ArrayList)5 OpVariable (lucee.transformer.bytecode.op.OpVariable)5 TagLibTagAttr (lucee.transformer.library.tag.TagLibTagAttr)5 Resource (lucee.commons.io.res.Resource)4 EvaluatorException (lucee.transformer.cfml.evaluator.EvaluatorException)4