Example #1
0
        private object ProcessDirectConstructor(Notation notation, Symbol sym)
        {
            Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.DirElemConstructor,
                Descriptor.DirCommentConstructor, Descriptor.DirPIConstructor });
            if (recs.Length > 0)
            {
                switch (recs[0].descriptor)
                {
                    case Descriptor.DirElemConstructor:
                        {
                            bool mapping = false;                            
                            object builder = ATOM.Create("b");
                            List<object> stmt = new List<object>();
                            stmt.Add(Funcs.Progn);
                            WriteDirElemConstructor(notation, recs[0], builder, stmt, ref mapping);
                            stmt.Add(Lisp.List(ID.CreateNavigator, builder));                            
                            object body = Lisp.List(stmt.ToArray());
                            object res;
                            if (mapping)
                                res = new XQueryLET(_context, builder, XQuerySequenceType.Item,
                                    Lisp.Cons(ID.CreateBuilder), new XQueryExpr(_context, new object[] { body }), false);
                            else
                                res = Lisp.List(Funcs.Let1, Lisp.Cons(Lisp.List(builder, Lisp.Cons(ID.CreateBuilder))), body);
                            Notation.Record[] recs1 = notation.Select(sym, Descriptor.MappingExpr, 1);
                            if (recs1.Length > 0) // Mapping extension support
                            {
                                if (!mapping)
                                    res = new XQueryExpr(_context, new object[] { res });
                                return new XQueryMapping(_context, ProcessPathExpr(notation, recs1[0].Arg0), 
                                    (XQueryExprBase)res, true).ToLispFunction();
                            }
                            else
                            {
                                if (mapping)
                                    return ((XQueryExprBase)res).ToLispFunction();
                                return res;
                            }
                        }

                    case Descriptor.DirCommentConstructor:
                        {
                            Literal lit = (Literal)recs[0].Arg0;
                            return Lisp.List(ID.CreateNavigator, Lisp.List(ID.WriteComment, Lisp.Cons(ID.CreateBuilder), lit.Data));
                        }

                    case Descriptor.DirPIConstructor:
                        {
                            Literal name = (Literal)recs[0].Arg0;
                            Literal data = (Literal)recs[0].Arg1;
                            return Lisp.List(ID.CreateNavigator, Lisp.List(ID.WritePi, Lisp.Cons(ID.CreateBuilder), name.Data,
                                data != null ? data.Data : String.Empty));
                        }

                    default:
                        throw new InvalidOperationException();
                }
            }
            else
                return ProcessComputedConstructor(notation, sym);
        }
Example #2
0
        private object ProcessFLORExpr(Notation notation, Notation.Record rec)
        {                        
            int stack_pos = _varTable.BeginFrame();
            Symbol[] arr = Lisp.ToArray<Symbol>(rec.args[0]);
            List<FLWORItem> flworItems = new List<FLWORItem>();
            bool stable = false;
            for (int k = 0; k < arr.Length; k++)
            {
                Notation.Record[] recs = notation.Select(arr[k]);
                switch (recs[0].descriptor)
                {
                    case Descriptor.For:
                        {
                            Symbol[] arr2 = Lisp.ToArray<Symbol>(recs[0].args[0]);
                            for (int s = 0; s < arr2.Length; s++)
                            {
                                Notation.Record[] recs2 = notation.Select(arr2[s], 
                                    Descriptor.ForClauseOperator, 4);
                                if (recs.Length > 0)
                                {
                                    FLWORItem item = new FLWORItem();
                                    item.desc = Descriptor.For;
                                    if (notation.Flag(arr[k], Descriptor.Parallel))
                                        item.parallel = true;
                                    VarName name = (VarName)recs2[0].Arg0;
                                    item.var = ProcessVarName(name);
                                    item.assignExpr = ProcessExprSingle(notation, recs2[0].Arg3);
                                    item.convert = true;
                                    if (recs2[0].Arg1 == null)
                                    {
                                        XQuerySequenceType type = EvalExprType(item.assignExpr);
                                        if (type.TypeCode != XmlTypeCode.Item)
                                        {
                                            item.varType = new XQuerySequenceType(type.TypeCode, XmlTypeCardinality.One);
                                            item.convert = false;
                                        }
                                        else
                                            item.varType = XQuerySequenceType.Item;
                                    }
                                    else
                                        item.varType = ProcessTypeDecl(notation, recs2[0].Arg1);
                                    item.pos = null;
                                    if (recs2[0].Arg2 != null)
                                    {
                                        VarName posname = (VarName)recs2[0].Arg2;
                                        if (posname.Name == name.Name)
                                            throw new XQueryException(Properties.Resources.XQST0089, posname.Name);
                                        item.pos = ProcessVarName(posname);
                                    }                                    
                                    _varTable.PushVar(item.var, item.varType);
                                    if (item.pos != null)
                                        _varTable.PushVar(item.pos, new XQuerySequenceType(XmlTypeCode.Integer));
                                    flworItems.Add(item);
                                }
                            }
                        }
                        break;

                    case Descriptor.Let:
                        {
                            Symbol[] arr2 = Lisp.ToArray<Symbol>(recs[0].args[0]);
                            for (int s = 0; s < arr2.Length; s++)
                            {
                                Notation.Record[] recs2 = notation.Select(arr2[s],
                                    Descriptor.LetClauseOperator, 3);
                                if (recs.Length > 0)
                                {
                                    FLWORItem item = new FLWORItem();
                                    item.desc = Descriptor.Let;
                                    item.var = ProcessVarName((VarName)recs2[0].Arg0);
                                    item.assignExpr = ProcessExprSingle(notation, recs2[0].Arg2);
                                    item.convert = true;                                    
                                    if (recs2[0].Arg1 == null)
                                    {
                                        XQuerySequenceType type = EvalExprType(item.assignExpr);
                                        if (type.Cardinality == XmlTypeCardinality.One)
                                        {
                                            item.varType = type;
                                            item.convert = false;
                                        }
                                        else
                                            item.varType = XQuerySequenceType.Item;
                                    }
                                    else
                                        item.varType = ProcessTypeDecl(notation, recs2[0].Arg1);
                                    _varTable.PushVar(item.var, item.varType);
                                    flworItems.Add(item);
                                }
                            }
                        }
                        break;
                }
            }
            XQueryOrderSpec[] orderSpec = null;
            XQueryExprBase expr = XQueryExpr.Create(_context,
                new object[] { ProcessExprSingle(notation, rec.Arg3) });
            if (rec.Arg2 != null)
            {
                Notation.Record[] recs = notation.Select(rec.Arg2, new Descriptor[] { 
                    Descriptor.OrderBy, Descriptor.StableOrderBy });
                if (recs.Length > 0)
                {
                    if (!(expr is XQueryExpr))
                        expr = new XQueryExpr(_context, new object[] { expr.ToLispFunction() });
                    stable = (recs[0].descriptor == Descriptor.StableOrderBy);
                    Symbol[] arr3 = Lisp.ToArray<Symbol>(recs[0].args[0]);
                    orderSpec = new XQueryOrderSpec[arr3.Length];
                    object[] sortKey = new object[arr3.Length];
                    for (int k = 0; k < arr3.Length; k++)
                    {
                        sortKey[k] = ProcessExprSingle(notation, arr3[k]);
                        orderSpec[k].emptySpec = _context.EmptyOrderSpec;
                        orderSpec[k].collation = _context.DefaultCollation;
                        Notation.Record[] recs1 = notation.Select(arr3[k], Descriptor.Modifier, 1);
                        if (recs1.Length > 0)
                        {
                            Symbol[] modifier = Lisp.ToArray<Symbol>(recs1[0].args[0]);
                            if (modifier[0] != null)
                                switch (((TokenWrapper)modifier[0]).Data)
                                {
                                    case Token.ASCENDING:
                                        orderSpec[k].direction = XQueryOrderDirection.Ascending;
                                        break;
                                    case Token.DESCENDING:
                                        orderSpec[k].direction = XQueryOrderDirection.Descending;
                                        break;
                                }
                            if (modifier[1] != null)
                                switch (((TokenWrapper)modifier[1]).Data)
                                {
                                    case Token.EMPTY_GREATEST:
                                        orderSpec[k].emptySpec = XQueryEmptyOrderSpec.Greatest;
                                        break;
                                    case Token.EMPTY_LEAST:
                                        orderSpec[k].emptySpec = XQueryEmptyOrderSpec.Least;
                                        break;
                                }
                            if (modifier[2] != null)
                                orderSpec[k].collation = ((Literal)modifier[2]).Data;
                        }
                    }
                    ((XQueryExpr)expr).Annotation = sortKey;
                }
            }
            object whereExpr = null;
            if (rec.Arg1 != null)
            {
                Notation.Record[] recs3 = notation.Select(rec.Arg1, Descriptor.Where, 1);
                if (recs3.Length > 0)
                    whereExpr = ProcessExprSingle(notation, recs3[0].Arg0);
            }
            for (int k = flworItems.Count - 1; k >= 0; k--)
            {
                FLWORItem item = flworItems[k];                
                switch (item.desc)
                {
                    case Descriptor.For:
                        expr = new XQueryFLWOR(_context, item.var, item.varType, item.pos, item.assignExpr, expr, item.convert);
                        if (item.parallel && _context.EnableHPC)
                            ((XQueryFLWOR)expr).Parallel = true;
                        break;

                    case Descriptor.Let:
                        expr = new XQueryLET(_context, item.var, item.varType, item.assignExpr, expr, item.convert);
                        break;
                }
                if (k == flworItems.Count - 1)
                {
                    XQueryFLWORBase flworExpr = (XQueryFLWORBase)expr;
                    flworExpr.ConditionExpr = whereExpr;
                }
            }
            _varTable.EndFrame(stack_pos);
            if (orderSpec != null)
                expr = new XQuerySorter(_context, orderSpec, stable, expr);
            return Lisp.List(ID.DynExecuteExpr, expr, ID.Context, Lisp.ARGV, Lisp.MPOOL);
        }
Example #3
0
 private object ProcessTypeswitchExpr(Notation notation, Notation.Record rec)
 {
     Symbol[] arr = Lisp.ToArray<Symbol>(rec.args[1]);
     object[] branch = new object[arr.Length + 1];
     object x = ATOM.Create("_expr");
     for (int k = 0; k < arr.Length; k++)
     {
         Notation.Record[] recs = notation.Select(arr[k], new Descriptor[] { Descriptor.Case });
         if (recs.Length > 0)
         {
             if (recs[0].args.Length > 2)
             {
                 object var = ProcessVarName((VarName)recs[0].Arg0);
                 XQuerySequenceType seqtype = ProcessTypeDecl(notation, recs[0].Arg1);
                 int stack_pos = _varTable.BeginFrame();
                 _varTable.PushVar(var, seqtype);
                 XQueryExprBase expr = XQueryExpr.Create(_context, 
                     new object[] { ProcessExprSingle(notation, recs[0].Arg2) });
                 _varTable.EndFrame(stack_pos);
                 branch[k] = Lisp.List(Lisp.List(ID.InstanceOf, x, seqtype),
                     Lisp.List(ID.DynExecuteExpr, new XQueryLET(_context, var, seqtype, x, expr, true), ID.Context, Lisp.ARGV, Lisp.MPOOL));
             }
             else
                 branch[k] = Lisp.List(Lisp.List(ID.InstanceOf, x, ProcessTypeDecl(notation, recs[0].Arg0)),
                     ProcessExprSingle(notation, recs[0].Arg1));
         }
     }
     if (rec.args.Length > 3)
     {
         object var = ProcessVarName((VarName)rec.Arg2);
         int stack_pos = _varTable.BeginFrame();
         _varTable.PushVar(var, XQuerySequenceType.Item);
         XQueryExprBase expr = XQueryExpr.Create(_context, 
             new object[] { ProcessExprSingle(notation, rec.Arg3) });
         _varTable.EndFrame(stack_pos);
         branch[arr.Length] = Lisp.List(Lisp.T, Lisp.List(ID.DynExecuteExpr, 
             new XQueryLET(_context, var, XQuerySequenceType.Item, x, expr, true), ID.Context, Lisp.ARGV, Lisp.MPOOL));
     }
     else
         branch[arr.Length] = Lisp.List(Lisp.T, ProcessExprSingle(notation, rec.Arg2));
     object res = new XQueryLET(_context, x, XQuerySequenceType.Item, 
         ProcessExpr(notation, rec.args[0]).ToLispFunction(), 
             new XQueryExpr(_context, new object[] { Lisp.Append(Lisp.Cons(Funcs.Cond), Lisp.List(branch)) }), true).ToLispFunction();
     return res;
 }