Search in sources :

Example 26 with TemplateException

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

the class AbstrCFMLScriptTransformer method block.

/**
 * Liest den Block mit Statements ein.
 * <br />
 * EBNF:<br />
 * <code>"{" spaces {statements} "}" | statement;</code>
 * @param block
 * @return was a block
 * @throws TemplateException
 */
private final boolean block(ExprData data, Body body) throws TemplateException {
    if (!data.srcCode.forwardIfCurrent('{'))
        return false;
    comments(data);
    if (data.srcCode.forwardIfCurrent('}')) {
        return true;
    }
    statements(data, body, false);
    if (!data.srcCode.forwardIfCurrent('}'))
        throw new TemplateException(data.srcCode, "Missing ending [}]");
    return true;
}
Also used : TemplateException(lucee.runtime.exp.TemplateException)

Example 27 with TemplateException

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

the class AbstrCFMLScriptTransformer method attributes.

private final Attribute[] attributes(Tag tag, TagLibTag tlt, ExprData data, EndCondition endCond, Expression defaultValue, Object oAllowExpression, String ignoreAttrReqFor, boolean allowTwiceAttr, char attributeSeparator, boolean allowColonAsNameValueSeparator) throws TemplateException {
    ArrayList<Attribute> attrs = new ArrayList<Attribute>();
    ArrayList<String> ids = new ArrayList<String>();
    while (data.srcCode.isValidIndex()) {
        data.srcCode.removeSpace();
        // if no more attributes break
        if (endCond.isEnd(data))
            break;
        Attribute attr = attribute(tlt, data, ids, defaultValue, oAllowExpression, allowTwiceAttr, allowColonAsNameValueSeparator);
        attrs.add(attr);
        // seperator
        if (attributeSeparator > 0) {
            data.srcCode.removeSpace();
            data.srcCode.forwardIfCurrent(attributeSeparator);
        }
    }
    // not defined attributes
    if (tlt != null) {
        boolean hasAttributeCollection = false;
        Iterator<Attribute> iii = attrs.iterator();
        while (iii.hasNext()) {
            if ("attributecollection".equalsIgnoreCase(iii.next().getName())) {
                hasAttributeCollection = true;
                break;
            }
        }
        int type = tlt.getAttributeType();
        if (type == TagLibTag.ATTRIBUTE_TYPE_FIXED || type == TagLibTag.ATTRIBUTE_TYPE_MIXED) {
            Map<String, TagLibTagAttr> hash = tlt.getAttributes();
            Iterator<Entry<String, TagLibTagAttr>> it = hash.entrySet().iterator();
            Entry<String, TagLibTagAttr> e;
            while (it.hasNext()) {
                e = it.next();
                TagLibTagAttr att = e.getValue();
                if (att.isRequired() && !contains(attrs, att) && att.getDefaultValue() == null && !att.getName().equals(ignoreAttrReqFor)) {
                    if (!hasAttributeCollection)
                        throw new TemplateException(data.srcCode, "attribute " + att.getName() + " is required for statement " + tlt.getName());
                    if (tag != null)
                        tag.addMissingAttribute(att);
                }
            }
        }
    }
    return attrs.toArray(new Attribute[attrs.size()]);
}
Also used : TagLibTagAttr(lucee.transformer.library.tag.TagLibTagAttr) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) TemplateException(lucee.runtime.exp.TemplateException) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry)

Example 28 with TemplateException

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

the class AbstrCFMLScriptTransformer method _propertyStatement.

private final Tag _propertyStatement(ExprData data, Body parent) throws TemplateException {
    if (data.context != CTX_CFC || !data.srcCode.forwardIfCurrent("property "))
        return null;
    Position line = data.srcCode.getPosition();
    TagLibTag tlt = CFMLTransformer.getTLT(data.srcCode, "property", data.config.getIdentification());
    Tag property = new TagOther(data.factory, line, null);
    addMetaData(data, property, IGNORE_LIST_PROPERTY);
    boolean hasName = false, hasType = false;
    int pos = data.srcCode.getPos();
    String tmp = variableDec(data, true);
    if (!StringUtil.isEmpty(tmp)) {
        if (tmp.indexOf('.') != -1) {
            property.addAttribute(new Attribute(false, "type", data.factory.createLitString(tmp), "string"));
            hasType = true;
        } else {
            data.srcCode.setPos(pos);
        }
    } else
        data.srcCode.setPos(pos);
    // folgend wird tlt extra nicht uebergeben, sonst findet pruefung statt
    Attribute[] attrs = attributes(property, tlt, data, SEMI, data.factory.NULL(), Boolean.FALSE, "name", true, NO_ATTR_SEP, false);
    checkSemiColonLineFeed(data, true, true, false);
    property.setTagLibTag(tlt);
    property.setScriptBase(true);
    Attribute attr;
    // first fill all regular attribute -> name="value"
    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;
            } else if (attr.getName().equalsIgnoreCase("type")) {
                hasType = true;
            }
            property.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) || !hasName)) {
                first = attr.getNameOC();
            } else // name
            if (second == null && !hasName && !hasType) {
                second = attr.getNameOC();
            } else // attr with no value
            {
                attr = new Attribute(true, attr.getName(), data.factory.EMPTY(), "string");
                property.addAttribute(attr);
            }
        }
    }
    if (first != null) {
        hasName = true;
        if (second != null) {
            hasType = true;
            property.addAttribute(new Attribute(false, "name", data.factory.createLitString(second), "string"));
            property.addAttribute(new Attribute(false, "type", data.factory.createLitString(first), "string"));
        } else {
            property.addAttribute(new Attribute(false, "name", data.factory.createLitString(first), "string"));
        }
    }
    if (!hasType) {
        property.addAttribute(new Attribute(false, "type", data.factory.createLitString("any"), "string"));
    }
    if (!hasName)
        throw new TemplateException(data.srcCode, "missing name declaration for property");
    /*Tag property=new TagBase(line);
		property.setTagLibTag(tlt);
		property.addAttribute(new Attribute(false,"name",data.factory.createLitString(name),"string"));
		property.addAttribute(new Attribute(false,"type",data.factory.createLitString(type),"string"));
		*/
    property.setEnd(data.srcCode.getPosition());
    return property;
}
Also used : TagLibTag(lucee.transformer.library.tag.TagLibTag) Position(lucee.transformer.Position) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) TemplateException(lucee.runtime.exp.TemplateException) TagOther(lucee.transformer.bytecode.statement.tag.TagOther) TagLibTag(lucee.transformer.library.tag.TagLibTag) Tag(lucee.transformer.bytecode.statement.tag.Tag)

Example 29 with TemplateException

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

the class AbstrCFMLScriptTransformer method whileStatement.

/**
 * Liest ein while Statement ein.
 * <br />
 * EBNF:<br />
 * <code>spaces condition spaces ")" spaces block;</code>
 * @return while Statement
 * @throws TemplateException
 */
private final While whileStatement(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("while")) {
        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("while", '(')) {
            data.srcCode.setPos(pos);
            return null;
        }
    }
    Position line = data.srcCode.getPosition();
    Body body = new BodyBase(data.factory);
    While whil = new While(condition(data), body, line, null, id);
    if (!data.srcCode.forwardIfCurrent(')'))
        throw new TemplateException(data.srcCode, "while statement must end with a [)]");
    statement(data, body, CTX_WHILE);
    whil.setEnd(data.srcCode.getPosition());
    return whil;
}
Also used : Position(lucee.transformer.Position) TemplateException(lucee.runtime.exp.TemplateException) DoWhile(lucee.transformer.bytecode.statement.DoWhile) While(lucee.transformer.bytecode.statement.While) Body(lucee.transformer.bytecode.Body) ScriptBody(lucee.transformer.bytecode.ScriptBody) FunctionBody(lucee.transformer.bytecode.FunctionBody) BodyBase(lucee.transformer.bytecode.BodyBase)

Example 30 with TemplateException

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

the class AbstrCFMLScriptTransformer method getScriptFunctionArguments.

@Override
public ArrayList<Argument> getScriptFunctionArguments(ExprData data) throws TemplateException {
    // arguments
    LitBoolean passByRef;
    Expression displayName;
    Expression hint;
    Map<String, Attribute> meta;
    String _name;
    ArrayList<Argument> result = new ArrayList<Argument>();
    do {
        comments(data);
        // finish
        if (data.srcCode.isCurrent(')'))
            break;
        // attribute
        // name
        // String idName=identifier(data,false,true);
        boolean required = false;
        String idName = variableDec(data, false);
        // required
        if ("required".equalsIgnoreCase(idName)) {
            comments(data);
            String idName2 = variableDec(data, false);
            if (idName2 != null) {
                idName = idName2;
                required = true;
            }
            if (idName == null)
                throw new TemplateException(data.srcCode, "invalid argument definition");
        }
        String typeName = "any";
        if (idName == null)
            throw new TemplateException(data.srcCode, "invalid argument definition");
        comments(data);
        if (!data.srcCode.isCurrent(')') && !data.srcCode.isCurrent('=') && !data.srcCode.isCurrent(':') && !data.srcCode.isCurrent(',')) {
            typeName = idName.toLowerCase();
            // MUST was upper case before, is this a problem?
            idName = identifier(data, false);
        } else if (idName.indexOf('.') != -1 || idName.indexOf('[') != -1) {
            throw new TemplateException(data.srcCode, "invalid argument name [" + idName + "] definition");
        }
        if (idName == null)
            throw new TemplateException(data.srcCode, "invalid argument definition");
        comments(data);
        Expression defaultValue;
        if (data.srcCode.isCurrent('=') || data.srcCode.isCurrent(':')) {
            data.srcCode.next();
            comments(data);
            defaultValue = expression(data);
        } else
            defaultValue = null;
        // assign meta data defined in doc comment
        passByRef = data.factory.TRUE();
        displayName = data.factory.EMPTY();
        hint = data.factory.EMPTY();
        meta = null;
        if (data.docComment != null) {
            Map<String, Attribute> params = data.docComment.getParams();
            Attribute[] attrs = params.values().toArray(new Attribute[params.size()]);
            Attribute attr;
            String name;
            for (int i = 0; i < attrs.length; i++) {
                attr = attrs[i];
                name = attr.getName();
                // hint
                if (idName.equalsIgnoreCase(name) || name.equalsIgnoreCase(idName + ".hint")) {
                    hint = data.factory.toExprString(attr.getValue());
                    params.remove(name);
                }
                // meta
                if (StringUtil.startsWithIgnoreCase(name, idName + ".")) {
                    if (name.length() > idName.length() + 1) {
                        if (meta == null)
                            meta = new HashMap<String, Attribute>();
                        _name = name.substring(idName.length() + 1);
                        meta.put(_name, new Attribute(attr.isDynamicType(), _name, attr.getValue(), attr.getType()));
                    }
                    params.remove(name);
                }
            }
        }
        // argument attributes
        Attribute[] _attrs = attributes(null, null, data, COMMA_ENDBRACKED, data.factory.EMPTY(), Boolean.TRUE, null, false, NO_ATTR_SEP, true);
        Attribute _attr;
        if (!ArrayUtil.isEmpty(_attrs)) {
            if (meta == null)
                meta = new HashMap<String, Attribute>();
            for (int i = 0; i < _attrs.length; i++) {
                _attr = _attrs[i];
                meta.put(_attr.getName(), _attr);
            }
        }
        result.add(new Argument(data.factory.createLitString(idName), data.factory.createLitString(typeName), data.factory.createLitBoolean(required), defaultValue, passByRef, displayName, hint, meta));
        comments(data);
    } while (data.srcCode.forwardIfCurrent(','));
    return result;
}
Also used : Argument(lucee.transformer.bytecode.statement.Argument) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) TemplateException(lucee.runtime.exp.TemplateException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LitBoolean(lucee.transformer.expression.literal.LitBoolean) FunctionAsExpression(lucee.transformer.bytecode.expression.FunctionAsExpression) Expression(lucee.transformer.expression.Expression)

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