예제 #1
0
파일: Nodes.cs 프로젝트: hesam/SketchSharp
 public LiteralElement()
   : base((NodeType)SpecSharpNodeType.None){
   this.AttributeNames = new IdentifierList();
   this.AttributeValues = new ExpressionList();
   this.Contents = new ExpressionList();
   this.ContentsType = new Int32List();
 }
예제 #2
0
        /// <summary>
        /// FmlaWithoutArrayLen might be less than the total array len
        /// </summary>
        internal static byte[] GetTokenData(TNameRecordList Names, TParsedTokenList Tokens, TFormulaType FmlaType, out int FmlaLenWithoutArray)
        {
            // Remember to ungrow maxrow and maxcol from 2007 too biff8
            // Tokens that grow are ptgref, area, 3dref and 3darea. All of them (and nothing else) should ungrow.

            using (MemoryStream Data = new MemoryStream())
            {
                using (MemoryStream ArrayData = new MemoryStream())
                {
                    int[]        TokenOffset = new int[Tokens.Count + 1];
                    TTokenOffset StreamPos   = new TTokenOffset();
                    Int32List    FuturePos   = new Int32List();

                    Tokens.ResetPositionToStart();
                    while (!Tokens.Eof())
                    {
                        TBaseParsedToken Token = Tokens.ForwardPop();
                        TokenOffset[Tokens.SavePosition()] = (int)Data.Position;
                        ptg BaseToken = Token.GetBaseId;
                        Add(Data, ArrayData, Token, BaseToken, Tokens, StreamPos, FmlaType, FuturePos);
                    } //while

                    TokenOffset[Tokens.Count] = (int)Data.Position;
                    FixFutureFunctions(Names, FuturePos, Tokens, Data, TokenOffset, ref StreamPos);
                    FixGotoAndMemTokens(Tokens, Data, TokenOffset, StreamPos);
                    ArrayData.Position  = 0;
                    Data.Position       = Data.Length;               //FixGoto will change this.
                    FmlaLenWithoutArray = (int)Data.Length;
                    ArrayData.WriteTo(Data);
                    return(Data.ToArray());
                }
            }
        }
예제 #3
0
 public LiteralElement()
     : base((NodeType)SpecSharpNodeType.None)
 {
     this.AttributeNames  = new IdentifierList();
     this.AttributeValues = new ExpressionList();
     this.Contents        = new ExpressionList();
     this.ContentsType    = new Int32List();
 }
예제 #4
0
        private static void FixFutureFunctions(TNameRecordList Names, Int32List FuturePos, TParsedTokenList Tokens, MemoryStream Data, int[] TokenOffset, ref TTokenOffset StreamPos)
        {
            if (FuturePos.Count == 0)
            {
                return;
            }


            List <byte> NewData = new List <byte>(Data.ToArray()); //we need to insert in any order

            for (int i = 0; i < FuturePos.Count; i++)
            {
                Tokens.MoveTo(FuturePos[i]);
                TBaseFunctionToken FuncToken = (TBaseFunctionToken)Tokens.GetToken(FuturePos[i]);
                for (int k = 0; k < FuncToken.ArgumentCount; k++) //this doesn't include the name.
                {
                    Tokens.Flush();
                }

                int TokPos = Tokens.SavePosition();
                while (TokPos > 0)
                {
                    if (!(Tokens.GetToken(TokPos - 1) is TIgnoreInCalcToken) || Tokens.GetToken(TokPos - 1) is TAttrToken)
                    {
                        break;
                    }
                    TokPos--;
                }

                int ofs = TokenOffset[TokPos];
                WriteFutureName(NewData, ofs, FindName(Names, FuncToken.GetFunctionData().FutureName));

                for (int k = TokPos; k < TokenOffset.Length; k++)
                {
                    TokenOffset[k] += 5;
                }

                TTokenOffset NewStreamPos = new TTokenOffset();
                foreach (int streamofs in StreamPos.Keys)
                {
                    int sofs = streamofs;
                    if (sofs >= ofs)
                    {
                        sofs += 5;
                    }
                    NewStreamPos.Add(sofs, StreamPos[streamofs]);
                }

                StreamPos = NewStreamPos;
            }

            Data.SetLength(0);
            Data.Write(NewData.ToArray(), 0, NewData.Count);
        }
예제 #5
0
        public object ToObject(DbValue item, IMember member)
        {
            // Gracefully migrate from number sets to a custom binary serializer that respects order
            switch (item.Kind)
            {
            case DbValueType.NS: return(new Int32List(item.ToArray <int>()));

            case DbValueType.B: return(Int32List.FromBytes(item.ToBinary()));

            default: return(new Int32List());
            }
        }
예제 #6
0
        protected ApiCampaignSend GetSampleCampaignSend()
        {
            var contactIds = new Int32List {
                sampleContactId
            };
            var addressBookIds = new Int32List {
                sampleAddressBookId
            };

            return(new ApiCampaignSend
            {
                CampaignId = sampleCampaignId,
                ContactIds = contactIds,
                AddressBookIds = addressBookIds
            });
        }
예제 #7
0
        private void ParseDatFile(Stream inStream)
        {
            Dat = new DatContainer(inStream, datName);

            try
            {
                var containerData = DataEntries.ToList();

                foreach (var keyValuePair in containerData)
                {
                    if (keyValuePair.Value is UnicodeString)
                    {
                        Strings.Add((UnicodeString)keyValuePair.Value);
                    }
                    else if (keyValuePair.Value is UInt64List)
                    {
                        UInt64List ul = (UInt64List)keyValuePair.Value;
                        Strings.Add((UnicodeString) new UnicodeString(ul.Offset, ul.dataTableOffset, ul.ToString()));
                    }
                    else if (keyValuePair.Value is UInt32List)
                    {
                        UInt32List ul = (UInt32List)keyValuePair.Value;
                        Strings.Add((UnicodeString) new UnicodeString(ul.Offset, ul.dataTableOffset, ul.ToString()));
                    }
                    else if (keyValuePair.Value is Int32List)
                    {
                        Int32List ul = (Int32List)keyValuePair.Value;
                        Strings.Add((UnicodeString) new UnicodeString(ul.Offset, ul.dataTableOffset, ul.ToString()));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(Settings.Strings["DatWrapper_ParseDatFile_Failed"], ex.Message), ex);
            }
        }
예제 #8
0
        private static bool BackFromFutureToUserDef(Stream Data, TParsedTokenList Tokens, Int32List FuturePos, TBaseFunctionToken ft, TCellFunctionData fd)
        {
            string FutureName = fd.FutureName;

            if (FutureName != null)
            {
                Data.WriteByte((byte)(ft.ArgumentCount + 1));
                WriteWord(Data, 0xFF); //User def...
                FuturePos.Add(Tokens.SavePosition());
                return(true);
            }

            return(false);
        }
예제 #9
0
        private static void Add(Stream Data, Stream ArrayData, TBaseParsedToken Token, ptg BaseToken, TParsedTokenList Tokens, TTokenOffset StreamPos, TFormulaType FmlaType, Int32List FuturePos)
        {
            if (IsError(BaseToken, FmlaType))
            {
                Data.WriteByte(0x1C); //PtgErr
                Data.WriteByte(0x2A);
                return;
            }


            Data.WriteByte((byte)Token.GetId);
            switch (BaseToken)
            {
            case ptg.Exp:
                TExp_Token exp = (TExp_Token)Token;
                Biff8Utils.CheckRow(exp.Row);
                Biff8Utils.CheckCol(exp.Col);
                WriteWord(Data, exp.Row);
                WriteWord(Data, exp.Col);
                break;

            case ptg.Tbl:
                TTableObjToken tblo = Token as TTableObjToken;
                if (tblo != null)
                {
                    //no biff8 checks here. This numbers might be anything.
                    WriteWord(Data, tblo.Row);
                    WriteWord(Data, tblo.Col);
                }
                else
                {
                    TTableToken tbl = (TTableToken)Token;
                    Biff8Utils.CheckRow(tbl.Row);
                    Biff8Utils.CheckCol(tbl.Col);
                    WriteWord(Data, tbl.Row);
                    WriteWord(Data, tbl.Col);
                }
                break;

            case ptg.Add:
            case ptg.Sub:
            case ptg.Mul:
            case ptg.Div:
            case ptg.Power:
            case ptg.Concat:
            case ptg.LT:
            case ptg.LE:
            case ptg.EQ:
            case ptg.GE:
            case ptg.GT:
            case ptg.NE:
            case ptg.Isect:
            case ptg.Union:
            case ptg.Range:
            case ptg.Uplus:
            case ptg.Uminus:
            case ptg.Percent:
            case ptg.Paren:
            case ptg.MissArg:
                break;


            case ptg.Str:
            {
                TStrDataToken tkd = Token as TStrDataToken;
                string        s   = tkd.GetData();
                if (s.Length > FlxConsts.Max_FormulaStringConstant)
                {
                    FlxMessages.ThrowException(FlxErr.ErrStringConstantInFormulaTooLong, s, String.Empty);
                }
                TExcelString Xs = new TExcelString(TStrLenLength.is8bits, s, null, false);
                byte[]       b  = new byte[Xs.TotalSize()];
                Xs.CopyToPtr(b, 0);
                Data.Write(b, 0, b.Length);
                break;
            }

            case ptg.Attr:
                WriteAttr(Data, Token, StreamPos, Tokens.SavePosition());

                break;

            /*
             * case ptg.Sheet: //Can't happen, ConvertBiff8ToInternal skips them.
             * case ptg.EndSheet: //Can't happen, ConvertBiff8ToInternal skips them.
             *  break;*/

            case ptg.Err:
            {
                Data.WriteByte((byte)((TErrDataToken)Token).GetData());
                break;
            }

            case ptg.Bool:
            {
                if (((TBoolDataToken)Token).GetData())
                {
                    Data.WriteByte(1);
                }
                else
                {
                    Data.WriteByte(0);
                }
                break;
            }

            case ptg.Int:
            {
                UInt16 a = (UInt16)((TIntDataToken)Token).GetData();
                WriteWord(Data, a);
                break;
            }

            case ptg.Num:
            {
                double d = ((TNumDataToken)Token).GetData();
                Data.Write(BitConverter.GetBytes(d), 0, 8);
                break;
            }


            case ptg.Array:
            {
                Data.Write(new byte[7], 0, 7);
                TArrayDataToken tk = (TArrayDataToken)Token;
                object[,] Arr = tk.GetData;

                int ColCount = Arr.GetLength(1) - 1;
                if (ColCount < 0 || ColCount > FlxConsts.Max_Columns97_2003)
                {
                    FlxMessages.ThrowException(FlxErr.ErrInvalidCols, ColCount, FlxConsts.Max_Columns97_2003 + 1);
                }
                ArrayData.WriteByte((byte)(ColCount));

                int RowCount = Arr.GetLength(0) - 1;
                if (RowCount < 0 || RowCount > FlxConsts.Max_Rows)
                {
                    FlxMessages.ThrowException(FlxErr.ErrInvalidRows, RowCount, FlxConsts.Max_Rows + 1);
                }
                WriteWord(ArrayData, RowCount);

                for (int r = 0; r <= RowCount; r++)
                {
                    for (int c = 0; c <= ColCount; c++)
                    {
                        WriteArrayObject(ArrayData, Arr[r, c]);
                    }
                }

                break;
            }

            case ptg.Func:
            {
                TBaseFunctionToken ft = (TBaseFunctionToken)Token;
                TCellFunctionData  fd = ft.GetFunctionData();
                WriteWord(Data, fd.Index);
                break;
            }

            case ptg.FuncVar:
            {
                TBaseFunctionToken ft = (TBaseFunctionToken)Token;
                TCellFunctionData  fd = ft.GetFunctionData();
                if (!BackFromFutureToUserDef(Data, Tokens, FuturePos, ft, fd))
                {
                    Data.WriteByte((byte)ft.ArgumentCount);
                    WriteWord(Data, fd.Index);
                }
                break;
            }

            case ptg.Name:
                WriteWord(Data, ((TNameToken)Token).NameIndex);
                WriteWord(Data, 0);
                break;

            case ptg.Ref:
            case ptg.RefN:
            case ptg.RefErr:
            {
                TRefToken reft = (TRefToken)Token;
                WriteRef(Data, reft.CanHaveRelativeOffsets, reft.Row, reft.RowAbs, reft.Col, reft.ColAbs);
                break;
            }

            case ptg.Area:
            case ptg.AreaN:
            case ptg.AreaErr:
            {
                TAreaToken areat = (TAreaToken)Token;
                WriteArea(Data, areat.CanHaveRelativeOffsets, areat.Row1, areat.RowAbs1, areat.Col1, areat.ColAbs1, areat.Row2, areat.RowAbs2, areat.Col2, areat.ColAbs2);
                break;
            }

            case ptg.MemArea:
            {
                WriteWord(Data, 0);
                WriteWord(Data, 0);
                StreamPos.Add((int)Data.Position, Tokens.SavePosition());
                WriteWord(Data, 0);

                TRefRange[] Range = ((TMemAreaToken)Token).Data;
                WriteWord(ArrayData, Range.Length);
                foreach (TRefRange OneRef in Range)
                {
                    int r1 = Biff8Utils.CheckAndContractBiff8Row(OneRef.FirstRow);
                    int r2 = Biff8Utils.CheckAndContractBiff8Row(OneRef.LastRow);
                    int c1 = Biff8Utils.CheckAndContractBiff8Col(OneRef.FirstCol);
                    int c2 = Biff8Utils.CheckAndContractBiff8Col(OneRef.LastCol);

                    WriteWord(ArrayData, r1);
                    WriteWord(ArrayData, r2);
                    WriteWord(ArrayData, c1);
                    WriteWord(ArrayData, c2);
                }

                break;
            }

            case ptg.MemErr:
            case ptg.MemNoMem:
                WriteWord(Data, 0);
                WriteWord(Data, 0);
                StreamPos.Add((int)Data.Position, Tokens.SavePosition());
                WriteWord(Data, 0);
                break;

            case ptg.MemFunc:
            case ptg.MemAreaN:
            case ptg.MemNoMemN:
                StreamPos.Add((int)Data.Position, Tokens.SavePosition());
                WriteWord(Data, 0);
                break;

            case ptg.NameX:
                TNameXToken NameX = (TNameXToken)Token;
                WriteWord(Data, NameX.FExternSheet);
                WriteWord(Data, NameX.NameIndex);
                WriteWord(Data, 0);
                break;

            case ptg.Ref3dErr:
            case ptg.Ref3d:
                TRef3dToken reft3d = (TRef3dToken)Token;
                WriteWord(Data, reft3d.FExternSheet);
                WriteRef(Data, reft3d.CanHaveRelativeOffsets, reft3d.Row, reft3d.RowAbs, reft3d.Col, reft3d.ColAbs);
                break;

            case ptg.Area3d:
            case ptg.Area3dErr:
                TArea3dToken areat3d = (TArea3dToken)Token;
                WriteWord(Data, areat3d.FExternSheet);
                WriteArea(Data, areat3d.CanHaveRelativeOffsets, areat3d.Row1, areat3d.RowAbs1, areat3d.Col1, areat3d.ColAbs1, areat3d.Row2, areat3d.RowAbs2, areat3d.Col2, areat3d.ColAbs2);
                break;

            default:
                XlsMessages.ThrowException(XlsErr.ErrInternal);                         //All tokens here should exist
                break;
            }
        }
예제 #10
0
 private TypeNode ParseArrayType(int rank, TypeNode elementType, TokenSet followers){
   SourceContext sctx = elementType.SourceContext;
   Int32List rankList = new Int32List();
   do{
     rankList.Add(rank);
     rank = this.ParseRankSpecifier(false, followers);
   }while (rank > 0);
   for (int i = rankList.Count; i > 0; i--)
     elementType = new ArrayTypeExpression(elementType, rankList[i-1]);
   elementType.SourceContext = sctx;
   return elementType;
 }
예제 #11
0
 public virtual void InitializeMultiDimArray(int rank, int offset, Int32List indices, 
   ExpressionList initializers, MemberBinding setter, StatementList statements){
   for (int i = 0, n = initializers == null ? 0 : initializers.Count; i < n; i++){
     Expression initRow = initializers[i];
     if (initRow == null) continue;
     indices[offset] = i;
     if (rank > 1){
       this.InitializeMultiDimArray(rank-1, offset+1, indices, ((ConstructArray)initializers[i]).Initializers, setter, statements);
     }else{
       ExpressionList arguments = new ExpressionList(offset);
       for (int j = 0; j <= offset; j++) arguments.Add(new Literal(indices[j], SystemTypes.Int32));
       arguments.Add(this.VisitExpression(initializers[i]));
       statements.Add(new ExpressionStatement(new MethodCall(setter, arguments)));
     }
   }
 }
예제 #12
0
 public override Expression VisitConstructArray(ConstructArray consArr){
   if (consArr == null) return null;
   ArrayType arrayType = this.typeSystem.Unwrap(consArr.Type) as ArrayType;
    
   if (arrayType == null){Debug.Assert(consArr.Type == null); return null;}
   consArr.Operands = this.VisitExpressionList(consArr.Operands);
   ExpressionList initializers = consArr.Initializers;
   consArr.Initializers = null;
   int rank = consArr.Rank;
   if (initializers == null){
     if (consArr.Operands == null) return null;
     rank = consArr.Operands.Count;
     if (rank == 1) return CreateOwnerIsMethodCall(consArr);
     if (consArr.Type == null) return null;
     TypeNode[] types = new TypeNode[rank];
     for (int i = 0; i < rank; i++) types[i] = SystemTypes.Int32;
     InstanceInitializer ctor = this.GetTypeView(arrayType).GetConstructor(types);
     if (ctor == null){Debug.Assert(false); return null;}
     return CreateOwnerIsMethodCall(new Construct(new MemberBinding(null, ctor), consArr.Operands));
   }
   if (consArr.Operands == null || consArr.Operands.Count == 0){
     consArr.Operands = new ExpressionList(rank);
     ExpressionList inits = initializers;
     for (int i = 0; i < rank; i++){
       int m = inits == null ? 0 : inits.Count;
       consArr.Operands.Add(new Literal(m, SystemTypes.Int32));
       if (i == rank-1 || m < 1) break;
       ConstructArray cArr = inits[0] as ConstructArray;
       if (cArr == null) return null;
       inits = cArr.Initializers;
     }
   }
   int n = initializers.Count;
   bool isNonNullArray =  this.typeSystem.IsPossibleNonNullType(consArr.ElementType);
   if (n == 0) {
     if (isNonNullArray) {
       StatementList stmts = new StatementList(2);
       stmts.Add(new ExpressionStatement(CreateOwnerIsMethodCall(consArr)));
       stmts.Add(new ExpressionStatement(CreateAssertNonNullArrayCall(arrayType)));
       return new BlockExpression(new Block(stmts), consArr.Type);
       /*
       return CreateOwnerIsMethodCall(consArr);
       */
     }
     else {
       return CreateOwnerIsMethodCall(consArr);
     }
   }
   TypeNode elemType = this.typeSystem.GetUnderlyingType(consArr.ElementType);
   // 
   // If this is a non-null array with initializers, we add a call to 
   // NonNullType.AssertInitialized so that the analysis in definiteassignment
   // will know the "commit point" of the array. 
   
   int numStatements = (isNonNullArray) ? n + 2 : n + 1;
   StatementList statements = new StatementList(numStatements);
   if (rank > 1){
     TypeNode[] types = new TypeNode[rank];
     for (int i = 0; i < rank; i++) types[i] = SystemTypes.Int32;
     InstanceInitializer ctor = this.GetTypeView(arrayType).GetConstructor(types);
     if (ctor == null){Debug.Assert(false); return null;}
     statements.Add(new ExpressionStatement(CreateOwnerIsMethodCall(new Construct(new MemberBinding(null, ctor), consArr.Operands))));
     Int32List indices = new Int32List(rank);
     for (int i = 0; i < rank; i++) indices.Add(0);
     MemberBinding setter = new MemberBinding(new Expression(NodeType.Dup), arrayType.Setter);
     for (int i = 0; i < n; i++){
       indices[0] = i;
       this.InitializeMultiDimArray(rank-1, 1, indices, ((ConstructArray)initializers[i]).Initializers, setter, statements);
     }
     if (isNonNullArray) {
       // insert a call to AssertNonNullInit
       statements.Add(new ExpressionStatement(CreateAssertNonNullArrayCall(arrayType)));
     }
     return new BlockExpression(new Block(statements), consArr.Type);
   }
   statements.Add(new ExpressionStatement(CreateOwnerIsMethodCall(consArr)));
   for (int i = 0; i < n; i++){
     ExpressionList arguments = new ExpressionList(1);
     arguments.Add(new Literal(i, SystemTypes.Int32));
     Expression indexer = new Indexer(new Expression(NodeType.Dup, arrayType), arguments, elemType);
     if (elemType.IsValueType && !elemType.IsPrimitive)
       indexer = new AddressDereference(new UnaryExpression(indexer, NodeType.AddressOf, indexer.Type.GetReferenceType()), elemType);
     statements.Add(new AssignmentStatement(indexer, this.VisitExpression(initializers[i])));
   }
   if (isNonNullArray) {
     // insert a call to AssertNonNullInit
     statements.Add(new ExpressionStatement(CreateAssertNonNullArrayCall(arrayType)));
   }
   return new BlockExpression(new Block(statements), consArr.Type);
 }
예제 #13
0
 public virtual Expression VisitQualifiedIdentifier(QualifiedIdentifier qualifiedIdentifier, bool outerQualifier){
   if (qualifiedIdentifier == null || qualifiedIdentifier.Identifier == null) return null;
   Identifier prefix = qualifiedIdentifier.Identifier.Prefix;
   if (prefix != null){
     AliasDefinition aliasDef = this.LookupAlias(prefix);
     if (aliasDef != null){
       prefix = aliasDef.AliasedUri;
       if (prefix == null) prefix = (Identifier)aliasDef.AliasedExpression;
     }
     if (prefix != null)
       qualifiedIdentifier.Identifier.Prefix = prefix;
   }
   Expression qualifier = qualifiedIdentifier.Qualifier;
   Identifier id = qualifier as Identifier;
   if (id != null)
     qualifier = this.VisitIdentifier(id); //TODO: alias binding
   else if (qualifier is QualifiedIdentifier)
     qualifier = this.VisitQualifiedIdentifier((QualifiedIdentifier)qualifier, false);
   else
     qualifier = this.VisitExpression(qualifier);
   AliasBinding aliasBinding = qualifier as AliasBinding;
   if (aliasBinding != null){
     this.AddNodePositionAndInfo(qualifiedIdentifier, aliasBinding.AliasDefinition, IdentifierContexts.AllContext);
     qualifier = id;
   }else
     this.AddNodePositionAndInfo(qualifiedIdentifier, qualifiedIdentifier, IdentifierContexts.AllContext);
   if (qualifier is Identifier || qualifier is QualifiedIdentifier || qualifier is NameBinding || (qualifier is Literal && ((Literal)qualifier).Value is TypeNode)) {
     //The qualifier got bound to a namespace or a type, so the qualified identifier itself may bind
     //to a namespace or type. Do the binding here rather than in Resolver.
     Int32List idPos = this.identifierPositions;
     this.identifierPositions = null; //Do not rebind a new info object to the identifier position
     TypeNode t = this.LookupType(qualifiedIdentifier, true, 0);
     this.identifierPositions = idPos;
     if (t != null){
       NameBinding nb = qualifier as NameBinding;
       if (this.BoundNameHides(nb, t.DeclaringType))
         t = null;
       else
         return new Literal(t, SystemTypes.Type, qualifiedIdentifier.SourceContext);
     }else if (outerQualifier){
       Identifier ns = this.GetNamespaceIfValidAndComplainIfNeeded(qualifier);
       if (ns != null)
         this.HandleError(qualifiedIdentifier, Error.NoSuchQualifiedType, ns.ToString(), qualifiedIdentifier.Identifier.ToString());
       else if (qualifier is Literal){
         t = (TypeNode)((Literal)qualifier).Value;
         MemberBinding mb = this.LookupLiteralBinding(qualifiedIdentifier, qualifiedIdentifier.Identifier, t);
         if (mb != null) return mb;
       }
     }
   }
   if (id != null && id.Prefix == null && qualifier is Literal && ((Literal)qualifier).Value is TypeNode){
     //Look for literal field
     TypeNode t = (TypeNode)((Literal)qualifier).Value;
     MemberBinding mb = this.LookupLiteralBinding(qualifiedIdentifier, id, t);
     if (mb != null) return mb;
   }
   if (qualifier != null)
     qualifiedIdentifier.Qualifier = qualifier;
   return qualifiedIdentifier;
 }