Search in sources :

Example 21 with Position

use of lucee.transformer.Position in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method __multiAttrStatement.

private final Tag __multiAttrStatement(Body parent, ExprData data, TagLibTag tlt) throws TemplateException {
    if (data.ep == null)
        return null;
    String type = tlt.getName();
    if (data.srcCode.forwardIfCurrent(type) || // lucee dialect support component as alias for class
    (data.srcCode.getDialect() == CFMLEngine.DIALECT_LUCEE && type.equalsIgnoreCase(Constants.LUCEE_COMPONENT_TAG_NAME) && data.srcCode.forwardIfCurrent(Constants.CFML_COMPONENT_TAG_NAME))) {
        boolean isValid = (data.srcCode.isCurrent(' ') || (tlt.getHasBody() && data.srcCode.isCurrent('{')));
        if (!isValid) {
            data.srcCode.setPos(data.srcCode.getPos() - type.length());
            return null;
        }
    } else
        return null;
    Position line = data.srcCode.getPosition();
    TagLibTagScript script = tlt.getScript();
    // TagLibTag tlt = CFMLTransformer.getTLT(data.srcCode,type);
    if (script.getContext() == CTX_CFC)
        data.isCFC = true;
    else if (script.getContext() == CTX_INTERFACE)
        data.isInterface = true;
    // Tag tag=new TagComponent(line);
    Tag tag = getTag(data, parent, tlt, line, null);
    tag.setTagLibTag(tlt);
    tag.setScriptBase(true);
    // add component meta data
    if (data.isCFC) {
        addMetaData(data, tag, IGNORE_LIST_COMPONENT);
    }
    if (data.isInterface) {
        addMetaData(data, tag, IGNORE_LIST_INTERFACE);
    }
    // EvaluatorPool.getPool();
    comments(data);
    // attributes
    // attributes(func,data);
    Attribute[] attrs = attributes(tag, tlt, data, SEMI_BLOCK, data.factory.EMPTY(), script.getRtexpr() ? Boolean.TRUE : Boolean.FALSE, null, false, ',', false);
    for (int i = 0; i < attrs.length; i++) {
        tag.addAttribute(attrs[i]);
    }
    comments(data);
    // body
    if (tlt.getHasBody()) {
        Body body = new BodyBase(data.factory);
        boolean wasSemiColon = statement(data, body, script.getContext());
        if (!wasSemiColon || !tlt.isBodyFree() || body.hasStatements())
            tag.setBody(body);
    } else
        checkSemiColonLineFeed(data, true, true, true);
    tag.setEnd(data.srcCode.getPosition());
    eval(tlt, data, tag);
    return tag;
}
Also used : TagLibTagScript(lucee.transformer.library.tag.TagLibTagScript) Position(lucee.transformer.Position) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase)

Example 22 with Position

use of lucee.transformer.Position in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method returnStatement.

/**
 * Liest ein return Statement ein.
 * <br />
 * EBNF:<br />
 * <code>spaces expressionStatement spaces;</code>
 * @return return Statement
 * @throws TemplateException
 */
private final Return returnStatement(ExprData data) throws TemplateException {
    if (!data.srcCode.forwardIfCurrentAndNoVarExt("return"))
        return null;
    Position line = data.srcCode.getPosition();
    Return rtn;
    comments(data);
    if (checkSemiColonLineFeed(data, false, false, false))
        rtn = new Return(data.factory, line, data.srcCode.getPosition());
    else {
        Expression expr = expression(data);
        checkSemiColonLineFeed(data, true, true, false);
        rtn = new Return(expr, line, data.srcCode.getPosition());
    }
    comments(data);
    return rtn;
}
Also used : Return(lucee.transformer.bytecode.statement.Return) Position(lucee.transformer.Position) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression)

Example 23 with Position

use of lucee.transformer.Position 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 24 with Position

use of lucee.transformer.Position in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method __singleAttrStatement.

private final Statement __singleAttrStatement(Body parent, ExprData data, TagLibTag tlt, boolean allowTwiceAttr) throws TemplateException {
    String tagName = tlt.getName();
    if (data.srcCode.forwardIfCurrent(tagName)) {
        if (!data.srcCode.isCurrent(' ') && !data.srcCode.isCurrent(';')) {
            data.srcCode.setPos(data.srcCode.getPos() - tagName.length());
            return null;
        }
    } else
        return null;
    int pos = data.srcCode.getPos() - tagName.length();
    Position line = data.srcCode.getPosition();
    // TagLibTag tlt = CFMLTransformer.getTLT(data.srcCode,tagName.equals("pageencoding")?"processingdirective":tagName);
    Tag tag = getTag(data, parent, tlt, line, null);
    tag.setScriptBase(true);
    tag.setTagLibTag(tlt);
    comments(data);
    // attribute
    TagLibTagAttr attr = tlt.getScript().getSingleAttr();
    String attrName = null;
    Expression attrValue = null;
    short attrType = ATTR_TYPE_NONE;
    if (attr != null) {
        attrType = attr.getScriptSupport();
        char c = data.srcCode.getCurrent();
        if (ATTR_TYPE_REQUIRED == attrType || (!data.srcCode.isCurrent(';') && ATTR_TYPE_OPTIONAL == attrType)) {
            if (data.srcCode.isCurrent('{')) {
                // this can be only a json string
                int p = data.srcCode.getPos();
                try {
                    attrValue = isSimpleValue(attr.getType()) ? null : json(data, JSON_STRUCT, '{', '}');
                } catch (Throwable t) {
                    ExceptionUtil.rethrowIfNecessary(t);
                    data.srcCode.setPos(p);
                }
            } else
                attrValue = attributeValue(data, tlt.getScript().getRtexpr());
            if (attrValue != null && isOperator(c)) {
                data.srcCode.setPos(pos);
                return null;
            }
        }
    }
    if (attrValue != null) {
        attrName = attr.getName();
        TagLibTagAttr tlta = tlt.getAttribute(attr.getName(), true);
        tag.addAttribute(new Attribute(false, attrName, CastOther.toExpression(attrValue, tlta.getType()), tlta.getType()));
    } else if (ATTR_TYPE_REQUIRED == attrType) {
        data.srcCode.setPos(pos);
        return null;
    }
    // body
    if (tlt.getHasBody()) {
        Body body = new BodyBase(data.factory);
        boolean wasSemiColon = statement(data, body, tlt.getScript().getContext());
        if (!wasSemiColon || !tlt.isBodyFree() || body.hasStatements())
            tag.setBody(body);
    } else
        checkSemiColonLineFeed(data, true, true, true);
    if (tlt.hasTTE())
        data.ep.add(tlt, tag, data.flibs, data.srcCode);
    if (!StringUtil.isEmpty(attrName))
        validateAttributeName(attrName, data.srcCode, new ArrayList<String>(), tlt, new RefBooleanImpl(false), new StringBuffer(), allowTwiceAttr);
    tag.setEnd(data.srcCode.getPosition());
    eval(tlt, data, tag);
    return tag;
}
Also used : TagLibTagAttr(lucee.transformer.library.tag.TagLibTagAttr) Position(lucee.transformer.Position) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) ArrayList(java.util.ArrayList) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase)

Example 25 with Position

use of lucee.transformer.Position in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method cftagStatement.

private Statement cftagStatement(ExprData data, Body parent) throws TemplateException {
    // that is because cfloop-contition evaluator does not pass this
    if (data.ep == null)
        return null;
    final int start = data.srcCode.getPos();
    // namespace and separator
    final TagLib tagLib = CFMLTransformer.nameSpace(data);
    if (tagLib == null || !tagLib.isCore())
        return null;
    // print.e("namespace:"+tagLib.getNameSpaceAndSeparator());
    // get the name of the tag
    String id = CFMLTransformer.identifier(data.srcCode, false, true);
    if (id == null) {
        data.srcCode.setPos(start);
        return null;
    }
    id = id.toLowerCase();
    String appendix = null;
    TagLibTag tlt = tagLib.getTag(id);
    // get taglib
    if (tlt == null) {
        tlt = tagLib.getAppendixTag(id);
        if (tlt == null) {
            // if(tagLib.getIgnoreUnknowTags()){ if we do this a expression like the following no longer work cfwhatever=1;
            data.srcCode.setPos(start);
            return null;
        // }
        // throw new TemplateException(data.srcCode,"undefined tag ["+tagLib.getNameSpaceAndSeparator()+id+"]");
        }
        appendix = StringUtil.removeStartingIgnoreCase(id, tlt.getName());
    }
    if (tlt.getScript() == null) {
        data.srcCode.setPos(start);
        return null;
    }
    // check for opening bracked or closing semicolon
    comments(data);
    boolean noAttrs = false;
    if (!data.srcCode.forwardIfCurrent('(')) {
        if (checkSemiColonLineFeed(data, false, false, false)) {
            noAttrs = true;
        } else {
            data.srcCode.setPos(start);
            return null;
        }
    }
    Position line = data.srcCode.getPosition();
    // script specific behavior
    short context = CTX_OTHER;
    Boolean allowExpression = Boolean.TRUE;
    {
        TagLibTagScript script = tlt.getScript();
        if (script != null) {
            context = script.getContext();
            // always true for this tags allowExpression=script.getRtexpr()?Boolean.TRUE:Boolean.FALSE;
            if (context == CTX_CFC)
                data.isCFC = true;
            else if (context == CTX_INTERFACE)
                data.isInterface = true;
        }
    }
    Tag tag = getTag(data, parent, tlt, line, null);
    if (appendix != null) {
        tag.setAppendix(appendix);
        tag.setFullname(tlt.getFullName().concat(appendix));
    } else {
        tag.setFullname(tlt.getFullName());
    }
    tag.setTagLibTag(tlt);
    tag.setScriptBase(true);
    // add component meta data
    if (data.isCFC) {
        addMetaData(data, tag, IGNORE_LIST_COMPONENT);
    }
    if (data.isInterface) {
        addMetaData(data, tag, IGNORE_LIST_INTERFACE);
    }
    comments(data);
    // attributes
    Attribute[] attrs = noAttrs ? new Attribute[0] : attributes(tag, tlt, data, BRACKED, data.factory.EMPTY(), allowExpression, null, false, ',', true);
    data.srcCode.forwardIfCurrent(')');
    for (int i = 0; i < attrs.length; i++) {
        tag.addAttribute(attrs[i]);
    }
    comments(data);
    // body
    if (tlt.getHasBody()) {
        Body body = new BodyBase(data.factory);
        boolean wasSemiColon = statement(data, body, context);
        if (!wasSemiColon || !tlt.isBodyFree() || body.hasStatements())
            tag.setBody(body);
    } else
        checkSemiColonLineFeed(data, true, true, true);
    tag.setEnd(data.srcCode.getPosition());
    eval(tlt, data, tag);
    return tag;
}
Also used : TagLibTagScript(lucee.transformer.library.tag.TagLibTagScript) TagLibTag(lucee.transformer.library.tag.TagLibTag) Position(lucee.transformer.Position) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) TagLib(lucee.transformer.library.tag.TagLib) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag) RefBoolean(lucee.commons.lang.types.RefBoolean) ExprBoolean(lucee.transformer.expression.ExprBoolean) CastBoolean(lucee.transformer.bytecode.cast.CastBoolean) LitBoolean(lucee.transformer.expression.literal.LitBoolean) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase)

Aggregations

Position (lucee.transformer.Position)30 TemplateException (lucee.runtime.exp.TemplateException)21 BodyBase (lucee.transformer.bytecode.BodyBase)10 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)10 Expression (lucee.transformer.expression.Expression)10 Body (lucee.transformer.bytecode.Body)9 FunctionBody (lucee.transformer.bytecode.FunctionBody)9 ScriptBody (lucee.transformer.bytecode.ScriptBody)9 TagLibTag (lucee.transformer.library.tag.TagLibTag)6 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)5 Tag (lucee.transformer.bytecode.statement.tag.Tag)5 LitString (lucee.transformer.expression.literal.LitString)5 Variable (lucee.transformer.expression.var.Variable)5 OpVariable (lucee.transformer.bytecode.op.OpVariable)4 TagOther (lucee.transformer.bytecode.statement.tag.TagOther)3 PageRuntimeException (lucee.runtime.exp.PageRuntimeException)2 TransformerException (lucee.transformer.TransformerException)2 OPUnary (lucee.transformer.bytecode.op.OPUnary)2 DoWhile (lucee.transformer.bytecode.statement.DoWhile)2 ComponentTemplateException (lucee.transformer.cfml.script.AbstrCFMLScriptTransformer.ComponentTemplateException)2