Example #1
0
 /**
  * Creates a function literal for a function that was not yet encountered while parsing.
  *
  * @param nm function name
  * @param ar function arity
  * @param ctx query context
  * @param ii input info
  * @return function literal
  * @throws QueryException query exception
  */
 public static FuncLit unknown(
     final QNm nm, final long ar, final QueryContext ctx, final InputInfo ii)
     throws QueryException {
   final VarScope scp = new VarScope();
   final FuncType temp = FuncType.arity((int) ar);
   final Var[] arg = new Var[(int) ar];
   final Expr[] refs = temp.args(arg, ctx, scp, ii);
   final TypedFunc call = ctx.funcs.getFuncRef(nm, refs, ctx.sc, ii);
   return new FuncLit(nm, arg, call.fun, null, scp, ctx.sc, ii);
 }
Example #2
0
  /**
   * Adds and returns a user-defined function that has not been defined yet.
   *
   * @param name name of the function
   * @param args optional arguments
   * @param ii input info
   * @return function instance
   * @throws QueryException query exception
   */
  TypedFunc add(final QNm name, final Expr[] args, final InputInfo ii) throws QueryException {

    // add function call for function that has not been declared yet
    final int al = args.length;
    final UserFunc uf = new UserFunc(ii, name, new Var[al], null, null, false);
    final UserFuncCall call = add(ii, name, add(uf, ii), args);
    final FuncType type = FuncType.arity(al);
    return new TypedFunc(call, type);
  }
Example #3
0
  /**
   * Returns the specified function.
   *
   * @param name name of the function
   * @param args optional arguments
   * @param ii input info
   * @return function instance
   */
  TypedFunc get(final QNm name, final Expr[] args, final InputInfo ii) {
    final int id = indexOf(name, args);
    if (id == -1) return null;

    // function has already been declared
    final UserFuncCall call = add(ii, funcs[id].name, id, args);
    final FuncType type = FuncType.get(funcs[id]);
    return new TypedFunc(call, type);
  }
Example #4
0
 /**
  * Constructor.
  *
  * @param nm function name
  * @param arg formal parameters
  * @param fn function body
  * @param ft function type
  * @param scp variable scope
  * @param sctx static context
  * @param ii input info
  */
 public FuncLit(
     final QNm nm,
     final Var[] arg,
     final Expr fn,
     final FuncType ft,
     final VarScope scp,
     final StaticContext sctx,
     final InputInfo ii) {
   super(ii, fn);
   name = nm;
   args = arg;
   check = ft == null;
   type = (ft == null ? FuncType.arity(args.length) : ft).seqType();
   scope = scp;
   sc = sctx;
 }
Example #5
0
  /**
   * Returns an instance of a with the specified name and number of arguments, or {@code null}.
   *
   * @param name name of the function
   * @param args optional arguments
   * @param dyn compile-/run-time flag
   * @param ctx query context
   * @param ii input info
   * @return function instance
   * @throws QueryException query exception
   */
  public static TypedFunc get(
      final QNm name,
      final Expr[] args,
      final boolean dyn,
      final QueryContext ctx,
      final InputInfo ii)
      throws QueryException {

    // get namespace and local name
    // parse data type constructors
    if (eq(name.uri(), XSURI)) {
      final byte[] ln = name.local();
      final AtomType type = AtomType.find(name, false);
      if (type == null) {
        final Levenshtein ls = new Levenshtein();
        for (final AtomType t : AtomType.values()) {
          if (t.par != null
              && t != AtomType.NOT
              && t != AtomType.AAT
              && t != AtomType.BIN
              && ls.similar(lc(ln), lc(t.string()), 0))
            FUNSIMILAR.thrw(ii, name.string(), t.string());
        }
      }
      // no constructor function found, or abstract type specified
      if (type == null || type == AtomType.NOT || type == AtomType.AAT) {
        FUNCUNKNOWN.thrw(ii, name.string());
      }

      if (args.length != 1) FUNCTYPE.thrw(ii, name.string());
      final SeqType to = SeqType.get(type, Occ.ZERO_ONE);
      return TypedFunc.constr(new Cast(ii, args[0], to), to);
    }

    // pre-defined functions
    final StandardFunc fun = Functions.get().get(name, args, ii);
    if (fun != null) {
      if (!ctx.sc.xquery3 && fun.xquery3()) FEATURE30.thrw(ii);
      for (final Function f : Function.UPDATING) {
        if (fun.sig == f) {
          ctx.updating(true);
          break;
        }
      }
      return new TypedFunc(fun, fun.sig.type(args.length));
    }

    // user-defined function
    final TypedFunc tf = ctx.funcs.get(name, args, ii);
    if (tf != null) return tf;

    // Java function (only allowed with administrator permissions)
    final JavaMapping jf = JavaMapping.get(name, args, ctx, ii);
    if (jf != null) return TypedFunc.java(jf);

    // add user-defined function that has not been declared yet
    if (!dyn && FuncType.find(name) == null) return ctx.funcs.add(name, args, ii, ctx);

    // no function found
    return null;
  }