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);
}
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;
}
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;
}
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");
}
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);
}
}*/
}
Aggregations