use of org.exist.xquery.parser.XQueryAST in project exist by eXist-db.
the class NamedFunctionReference method lookupFunction.
public static FunctionCall lookupFunction(Expression self, XQueryContext context, QName funcName, int arity) throws XPathException {
if (Function.BUILTIN_FUNCTION_NS.equals(funcName.getNamespaceURI()) && "concat".equals(funcName.getLocalPart()) && arity < 2) {
throw new XPathException(self, ErrorCodes.XPST0017, "No such function; fn:concat requires at least two arguments");
}
final XQueryAST ast = new XQueryAST();
ast.setLine(self.getLine());
ast.setColumn(self.getColumn());
final List<Expression> args = new ArrayList<>(arity);
for (int i = 0; i < arity; i++) {
args.add(new Function.Placeholder(context));
}
final Expression fun = FunctionFactory.createFunction(context, funcName, ast, null, args, false);
if (fun == null) {
throw new XPathException(self, ErrorCodes.XPST0017, "Function not found: " + funcName);
}
if (fun instanceof FunctionCall) {
if (((FunctionCall) fun).getFunction() == null) {
throw new XPathException(self, ErrorCodes.XPST0017, "Function not found: " + funcName);
}
// clear line and column as it will be misleading. should be set later to point
// to the location from where the function is called.
fun.setLocation(-1, -1);
return (FunctionCall) fun;
} else if (fun instanceof Function) {
final InternalFunctionCall funcCall;
if (fun instanceof InternalFunctionCall) {
funcCall = (InternalFunctionCall) fun;
} else {
funcCall = new InternalFunctionCall((Function) fun);
}
funcCall.setLocation(-1, -1);
return FunctionFactory.wrap(context, funcCall);
} else if (fun instanceof CastExpression) {
final InternalFunctionCall funcCall = new InternalFunctionCall(((CastExpression) fun).toFunction());
funcCall.setLocation(-1, -1);
return FunctionFactory.wrap(context, funcCall);
} else {
throw new XPathException(self, ErrorCodes.XPST0017, "Named function reference should point to a function; found: " + fun.getClass().getName());
}
}
use of org.exist.xquery.parser.XQueryAST in project exist by eXist-db.
the class LoadXQueryModule method addFunctionRefsFromModule.
public static void addFunctionRefsFromModule(final Expression parent, final XQueryContext tempContext, final ValueSequence resultSeq, final Module module) throws XPathException {
final FunctionSignature[] signatures = module.listFunctions();
for (final FunctionSignature signature : signatures) {
if (!signature.isPrivate()) {
if (module.isInternalModule()) {
int arity;
if (signature.isOverloaded()) {
arity = signature.getArgumentTypes().length;
} else {
arity = signature.getArgumentCount();
}
final FunctionDef def = ((InternalModule) module).getFunctionDef(signature.getName(), arity);
final XQueryAST ast = new XQueryAST();
ast.setLine(parent.getLine());
ast.setColumn(parent.getColumn());
final List<Expression> args = new ArrayList<>(arity);
for (int i = 0; i < arity; i++) {
args.add(new Function.Placeholder(tempContext));
}
final Function fn = Function.createFunction(tempContext, ast, module, def);
fn.setArguments(args);
final InternalFunctionCall call = new InternalFunctionCall(fn);
final FunctionCall ref = FunctionFactory.wrap(tempContext, call);
resultSeq.addAll(new FunctionReference(ref));
} else {
final UserDefinedFunction func = ((ExternalModule) module).getFunction(signature.getName(), signature.getArgumentCount(), tempContext);
// could be null if private function
if (func != null) {
// create function reference
final FunctionCall funcCall = new FunctionCall(tempContext, func);
funcCall.setLocation(parent.getLine(), parent.getColumn());
resultSeq.add(new FunctionReference(funcCall));
}
}
}
}
}