Package xbird.xquery.func

Examples of xbird.xquery.func.UserFunction


        if(f_itor.hasNext()) {
            attrFeed("function decls");
            indent++;
            do {
                lineFeed();
                UserFunction f = f_itor.next();
                f.visit(this, ctxt);
            } while(f_itor.hasNext());
            indent--;
        }

        final XQExpression body = module.getExpression();
View Full Code Here


    public static UserFunction createUserFunction(Module declaredModule, QualifiedName funcName, List<ParametricVariable> parameters, Type returnType)
            throws XQueryException {
        if(declaredModule.lookupFunction(funcName, parameters) != null) {
            throw new SyntaxError("err:XPST0017");
        }
        final UserFunction func = new UserFunction(declaredModule, funcName, parameters, returnType);
        declaredModule.declareLocalFunction(func);
        return func;
    }
View Full Code Here

        if(params.isEmpty()) {
            return body;
        } else {
            // Note that create function always generates new body expression for each call,
            // and must care about parameters reference.           
            UserFunction newuf = ObjectUtils.deepCopy(func);
            List<ParametricVariable> newParams = newuf.getParameters();
            final int psize = newParams.size();
            for(int i = 0; i < psize; i++) {
                ParametricVariable p = newParams.get(i);
                Type implicitParamType = p.getType();
                XQExpression arg = argv.get(i);
                Type argType = arg.getType();
                if(implicitParamType != Untyped.UNTYPED && !implicitParamType.accepts(argType)) {
                    // type promotion is required for arguments
                    p.setValue(new TypePromotedExpr(arg, implicitParamType));
                } else {
                    p.setValue(arg);
                }
            }
            XQExpression newbody = newuf.getBodyExpression();
            statEnv.setFunctionManager(funcMgr);
            if(LOG.isDebugEnabled()) {
                LOG.debug("Inlined the function '" + QNameUtil.toLexicalForm(newuf.getName()) + "'");
            }
            return newbody;
        }
    }
View Full Code Here

    }

    @Override
    public Sequence<? extends Item> eval(Sequence<? extends Item> contextSeq, DynamicContext dynEnv)
            throws XQueryException {
        UserFunction uf = this.getFunction();
        List<ParametricVariable> params = uf.getParameters();
        int paramArity = _params.size();
        int argArity = params.size();
        assert (paramArity == argArity);
        FunctionCallContext recEnv = new FunctionCallContext(dynEnv);
        for(int i = 0; i < paramArity; i++) {
            ParametricVariable paramVar = params.get(i);
            XQExpression argExpr = _params.get(i);
            Sequence argValue = argExpr.eval(contextSeq, dynEnv);
            paramVar.allocateResult(argValue, recEnv);
        }
        Sequence<? extends Item> result = uf.eval(contextSeq, ValueSequence.EMPTY_SEQUENCE, recEnv);
        if(LOG.isDebugEnabled()) {
            LOG.debug("Invoke RecursiveCall: " + QNameUtil.toLexicalForm(getFuncName()));
        }
        return result;
    }
View Full Code Here

    @Override
    public XQExpression staticAnalysis(StaticContext statEnv) throws XQueryException {
        if(!_analyzed) {
            this._analyzed = true;
            UserFunction udf = getUserFunction(statEnv);
            if(!_inlined) {
                // analyze body
                XQExpression body = ExpressionFactory.inlineFunction(_functionManager, udf, _params, statEnv);
                XQExpression analyzed = body.staticAnalysis(statEnv);
                XQExpression typePromoted = new TypePromotedExpr(analyzed, udf.getReturnType());
                return typePromoted;
            } else {
                // analyze params
                for(int i = 0; i < _params.size(); i++) {
                    final XQExpression p = _params.get(i);
                    _params.set(i, p.staticAnalysis(statEnv));
                }
                this._type = udf.getReturnType();
            }
        }
        return this;
    }
View Full Code Here

        }
        if(!(func instanceof UserFunction)) {
            throw new IllegalStateException("function must be UserFunction, but was '"
                    + func.getName() + '\'');
        }
        UserFunction udf = ((UserFunction) func);
        return udf;
    }
View Full Code Here

    }

    public Sequence<? extends Item> eval(Sequence<? extends Item> contextSeq, DynamicContext dynEnv)
            throws XQueryException {
        assert (dynEnv != null);
        UserFunction udf = getUserFunction(dynEnv.getStaticContext());
        List<ParametricVariable> params = udf.getParameters();
        int paramArity = _params.size();
        int argArity = params.size();
        assert (paramArity == argArity);
        FunctionCallContext recEnv = new FunctionCallContext(dynEnv);
        for(int i = 0; i < paramArity; i++) {
            ParametricVariable paramVar = params.get(i);
            XQExpression argExpr = _params.get(i);
            paramVar.setValue(argExpr); // for the static analzing
            Sequence argValue = argExpr.eval(contextSeq, dynEnv);
            paramVar.allocateResult(argValue, recEnv);
        }
        XQExpression body = udf.getBodyExpression();
        // lazy function may not be analyzed TODO REVIEWME
        XQExpression analyzed = body.staticAnalysis(dynEnv.getStaticContext());
        if(LOG.isDebugEnabled()) {
            LOG.debug("Invoke LazyFunctionCall: " + QNameUtil.toLexicalForm(getFuncName()));
        }
View Full Code Here

    /************************************************************************
    [23] FunctionDecl ::= <"declare" "function"> <QName "("> ParamList? (")" | (<")" "as"> SequenceType)) (EnclosedExpr | "external")
    [26] EnclosedExpr ::= "{" Expr "}"
    ************************************************************************/
    final public void parseFunctionDecl() throws ParseException, XQueryException {
        final UserFunction func;
        final QualifiedName funcName;
        List<ParametricVariable> paramList = Collections.<ParametricVariable> emptyList();
        Type returnType = Untyped.UNTYPED;
        final XQExpression funcBody;
        currentModule.pushVarScope();
        currentToken = jj_consume_token(DefineFunction);
        currentToken = jj_consume_token(QNameLpar);
        funcName = QNameUtil.parse(currentToken.image.substring(0, currentToken.image.length() - 1).trim(), namespaceContext, staticContext.getDefaultFunctionNamespace());
        switch(jj_nt.kind) {
            case VariableIndicator:
                paramList = parseParamList();
                break;
            default:
                jj_la1[24] = jj_gen;
                ;
        }
        switch(jj_nt.kind) {
            case Rpar:
                currentToken = jj_consume_token(Rpar);
                break;
            case RparAs:
                currentToken = jj_consume_token(RparAs);
                returnType = parseSequenceType();
                break;
            default:
                jj_la1[25] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        final String nsuri = funcName.getNamespaceURI();
        if(isLibraryModule) {
            final String tn = currentModule.getNamespace();
            if(tn == null) { // sanity check
                error("err:XQST0046");
            }
            if(tn.equals(nsuri) == false) {
                error("err:XQST0048");
            }
        } else {
            UserFunction.checkAsPredefinedFunction(nsuri);
        }
        func = ExpressionFactory.createUserFunction(currentModule, funcName, paramList, returnType);
        switch(jj_nt.kind) {
            case LbraceExprEnclosure:
                currentToken = jj_consume_token(LbraceExprEnclosure);
                funcBody = parseExpr();
                func.setBodyExpression(funcBody);
                currentToken = jj_consume_token(Rbrace);
                break;
            case External:
                currentToken = jj_consume_token(External);
                func.setExternal(true);
                break;
            default:
                jj_la1[26] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
View Full Code Here

    /************************************************************************
     [23] FunctionDecl ::= <"declare" "function"> <QName "("> ParamList? (")" | (<")" "as"> SequenceType)) (EnclosedExpr | "external")
     [26] EnclosedExpr ::= "{" Expr "}"
     ************************************************************************/
    final public void parseFunctionDecl() throws ParseException, XQueryException {
        final UserFunction func;
        final QualifiedName funcName;
        List<ParametricVariable> paramList = Collections.<ParametricVariable> emptyList();
        Type returnType = Untyped.UNTYPED;
        final XQExpression funcBody;
        currentModule.pushVarScope();
        currentToken = jj_consume_token(DefineFunction);
        currentToken = jj_consume_token(QNameLpar);
        funcName = QNameUtil.parse(currentToken.image.substring(0, currentToken.image.length() - 1).trim(), namespaceContext, staticContext.getDefaultFunctionNamespace());
        switch(jj_nt.kind) {
            case VariableIndicator:
                paramList = parseParamList();
                break;
            default:
                jj_la1[24] = jj_gen;
                ;
        }
        switch(jj_nt.kind) {
            case Rpar:
                currentToken = jj_consume_token(Rpar);
                break;
            case RparAs:
                currentToken = jj_consume_token(RparAs);
                returnType = parseSequenceType();
                break;
            default:
                jj_la1[25] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        final String nsuri = funcName.getNamespaceURI();
        if(isLibraryModule) {
            final String tn = currentModule.getNamespace();
            if(tn == null) { // sanity check
                error("err:XQST0046");
            }
            if(tn.equals(nsuri) == false) {
                error("err:XQST0048");
            }
        } else {
            UserFunction.checkAsPredefinedFunction(nsuri);
        }
        func = ExpressionFactory.createUserFunction(currentModule, funcName, paramList, returnType);
        switch(jj_nt.kind) {
            case LbraceExprEnclosure:
                currentToken = jj_consume_token(LbraceExprEnclosure);
                funcBody = parseExpr();
                func.setBodyExpression(funcBody);
                currentToken = jj_consume_token(Rbrace);
                break;
            case External:
                currentToken = jj_consume_token(External);
                func.setExternal(true);
                break;
            default:
                jj_la1[26] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
View Full Code Here

TOP

Related Classes of xbird.xquery.func.UserFunction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.