Пример #1
0
        private static void ProcessBinaryOperator(FormulaToken token, Stack operandsStack)
        {
            string text1 = operandsStack.Pop() as string;
            string text2 = operandsStack.Pop() as string;

            operandsStack.Push(text2 + token.ToString() + text1);
        }
Пример #2
0
        ///<summary>
        ///Converts the name record range to RPN bytes.
        ///</summary>
        ///<param name="range">The range to be converted.</param>
        ///<param name="sheetName">Sheet' name.</param>
        ///<param name="worksheets">The worksheets collection.</param>
        public static byte[] ConvertNameRecordRangeToRpnBytes(CellRange range, string sheetName, ExcelWorksheetCollection worksheets)
        {
            FormulaToken token1 = null;
            string       text1  = string.Empty;

            if ((range.Width == 1) && (range.Height == 1))
            {
                Match  match1 = RefFormulaToken.IsCellRegex.Match(range.ToString());
                string text2  = NameRecord.ConvertToAbsolute(match1.Groups["Row"].Value);
                string text3  = NameRecord.ConvertToAbsolute(match1.Groups["Column"].Value);
                text1  = sheetName + "!" + text3 + text2;
                token1 = new Ref3dFormulaToken(FormulaTokenCode.Ref3d1);
            }
            else
            {
                Match    match2     = AreaFormulaToken.IsCellRangeRegex.Match(range.ToString());
                string   text4      = NameRecord.ConvertToAbsolute(match2.Groups["Row1"].Value);
                string   text5      = NameRecord.ConvertToAbsolute(match2.Groups["Column1"].Value);
                string   text6      = NameRecord.ConvertToAbsolute(match2.Groups["Row2"].Value);
                string   text7      = NameRecord.ConvertToAbsolute(match2.Groups["Column2"].Value);
                string[] textArray1 = new string[] { sheetName, "!", text5, text4, ":", text7, text6 };
                text1  = string.Concat(textArray1);
                token1 = new Area3dFormulaToken(FormulaTokenCode.Area3d1);
            }
            object[] objArray1 = new object[] { text1, worksheets };
            token1.DelayInitialize(objArray1);
            return(token1.ConvertToBytes());
        }
Пример #3
0
        private bool TokenBytesToString(Stack operandsStack)
        {
            byte[] buffer1 = new byte[this.RpnBytes.Length];
            Array.Copy(this.RpnBytes, 0, buffer1, 0, this.RpnBytes.Length);
            int  num1  = 0;
            bool flag1 = false;
            bool flag2 = false;

            while (num1 < this.RpnBytes.Length)
            {
                FormulaToken token1 = FormulaTokensFactory.CreateFrom(buffer1, num1);
                if (token1.Type.IsControl)
                {
                    flag2 = true;
                }
                else
                {
                    flag1 = true;
                }
                CellFormula.ProcessToken(token1, operandsStack);
                num1 += token1.Size;
            }
            if (!flag1)
            {
                return(flag2);
            }
            return(false);
        }
Пример #4
0
        private static void ProcessFunction(FormulaToken token, Stack operandsStack)
        {
            byte num1 = (token is FunctionFormulaToken) ? (token as FunctionFormulaToken).ArgumentsCount : (token as FunctionVarFormulaToken).ArgumentsCount;

            if (!(token is FunctionFormulaToken))
            {
                ushort num4 = (token as FunctionVarFormulaToken).Function.Code;
            }
            else
            {
                ushort num5 = (token as FunctionFormulaToken).Function.Code;
            }
            StringBuilder builder1 = new StringBuilder();

            builder1.Append(token.ToString());
            builder1.Append("(");
            string[] textArray1 = new string[num1];
            for (byte num2 = 0; num2 < num1; num2 = (byte)(num2 + 1))
            {
                textArray1[num2] = operandsStack.Pop() as string;
            }
            for (byte num3 = num1; num3 > 0; num3 = (byte)(num3 - 1))
            {
                string text1 = textArray1[num3 - 1];
                builder1.Append(text1);
                if (num3 != 1)
                {
                    builder1.Append(";");
                }
            }
            builder1.Append(")");
            operandsStack.Push(builder1.ToString());
        }
Пример #5
0
        public void AddToken(FormulaTokenCode code, object[] data)
        {
            FormulaToken token1 = FormulaTokensFactory.CreateFromCode(code);

            this.tokens.Add(token1);
            token1.DelayInitialize(data);
        }
Пример #6
0
 private void TokensToString(Stack operandsStack, FormulaToken[] tokens)
 {
     for (int num1 = 0; num1 < tokens.Length; num1++)
     {
         FormulaToken token1 = tokens[num1];
         CellFormula.ProcessToken(token1, operandsStack);
     }
 }
Пример #7
0
        ///<summary>
        ///Creates formula token from rpn bytes and the code read from that bytes.
        ///</summary>
        ///<param name="rpnBytes">The RPN bytes.</param>
        ///<param name="startIndex">The start index to read code from the RPN bytes.</param>
        ///<returns>created formula token</returns>
        public static FormulaToken CreateFrom(byte[] rpnBytes, int startIndex)
        {
            byte         num1   = rpnBytes[startIndex];
            FormulaToken token1 = FormulaTokensFactory.CreateFromCode(num1);

            token1.Read(rpnBytes, startIndex + 1);
            return(token1);
        }
Пример #8
0
        private static void ProcessUnaryOperator(FormulaToken token, Stack operandsStack)
        {
            string text1 = operandsStack.Pop() as string;

            if (token.Code == 20)
            {
                operandsStack.Push(text1 + token.ToString());
            }
            else if (token.Code == 0x15)
            {
                operandsStack.Push("(" + text1 + ")");
            }
            else
            {
                operandsStack.Push(token.ToString() + text1);
            }
        }
Пример #9
0
 private static void ProcessToken(FormulaToken token, Stack operandsStack)
 {
     if (token.Type.IsUnary)
     {
         CellFormula.ProcessUnaryOperator(token, operandsStack);
     }
     else if (token.Type.IsBinary)
     {
         CellFormula.ProcessBinaryOperator(token, operandsStack);
     }
     else if (token.Type.IsOperand)
     {
         CellFormula.ProcessOperand(token, operandsStack);
     }
     else if (token.Type.IsFunction)
     {
         CellFormula.ProcessFunction(token, operandsStack);
     }
     else if (!token.Type.IsControl)
     {
         throw new ArgumentException("Invalid RPN token code.");
     }
 }
Пример #10
0
 private static void ProcessOperand(FormulaToken token, Stack operandsStack)
 {
     operandsStack.Push(token.ToString());
 }
Пример #11
0
 public void AddToken(FormulaToken token)
 {
     this.tokens.Add(token);
 }
Пример #12
0
        ///<summary>
        ///Creates formula token from code.
        ///</summary>
        ///<param name="tokenCode">The token code.</param>
        ///<returns>created formula token</returns>
        public static FormulaToken CreateFromCode(FormulaTokenCode tokenCode)
        {
            FormulaToken token1 = null;

            switch (tokenCode)
            {
            case FormulaTokenCode.Exp:
            case FormulaTokenCode.Tbl:
            case FormulaTokenCode.Attr:
            {
                return(new ControlFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Add:
            case FormulaTokenCode.Sub:
            case FormulaTokenCode.Mul:
            case FormulaTokenCode.Div:
            case FormulaTokenCode.Power:
            case FormulaTokenCode.Concat:
            case FormulaTokenCode.Lt:
            case FormulaTokenCode.Le:
            case FormulaTokenCode.Eq:
            case FormulaTokenCode.Ge:
            case FormulaTokenCode.Gt:
            case FormulaTokenCode.Ne:
            case FormulaTokenCode.Isect:
            case FormulaTokenCode.List:
            case FormulaTokenCode.Range:
            {
                return(new BinaryOperatorFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Uplus:
            case FormulaTokenCode.Uminus:
            case FormulaTokenCode.Percent:
            case FormulaTokenCode.Parentheses:
            {
                return(new UnaryOperatorFormulaToken(tokenCode));
            }

            case FormulaTokenCode.MissArg:
            {
                return(new MissArgFormulaToken());
            }

            case FormulaTokenCode.Str:
            {
                return(new StrFormulaToken());
            }

            case FormulaTokenCode.Err:
            {
                return(new ErrFormulaToken());
            }

            case FormulaTokenCode.Bool:
            {
                return(new BoolFormulaToken());
            }

            case FormulaTokenCode.Int:
            {
                return(new IntFormulaToken());
            }

            case FormulaTokenCode.Num:
            {
                return(new NumFormulaToken());
            }

            case FormulaTokenCode.Array1:
            case FormulaTokenCode.Array2:
            case FormulaTokenCode.Array3:
            {
                return(new ArrayFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Func1:
            case FormulaTokenCode.Func2:
            case FormulaTokenCode.Func3:
            {
                return(new FunctionFormulaToken(tokenCode));
            }

            case FormulaTokenCode.FuncVar1:
            case FormulaTokenCode.FuncVar2:
            case FormulaTokenCode.FuncVar3:
            {
                return(new FunctionVarFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Name1:
            case FormulaTokenCode.Name2:
            case FormulaTokenCode.Name3:
            {
                return(new NameFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Ref1:
            case FormulaTokenCode.Ref2:
            case FormulaTokenCode.Ref3:
            {
                return(new RefFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Area1:
            case FormulaTokenCode.Area2:
            case FormulaTokenCode.Area3:
            {
                return(new AreaFormulaToken(tokenCode));
            }

            case FormulaTokenCode.RefErr1:
            case FormulaTokenCode.RefErr2:
            case FormulaTokenCode.RefErr3:
            {
                return(new RefErrFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Ref3d1:
            case FormulaTokenCode.Ref3d2:
            case FormulaTokenCode.Ref3d3:
            {
                return(new Ref3dFormulaToken(tokenCode));
            }

            case FormulaTokenCode.Area3d1:
            case FormulaTokenCode.Area3d2:
            case FormulaTokenCode.Area3d3:
            {
                return(new Area3dFormulaToken(tokenCode));
            }

            case FormulaTokenCode.RefErr3d1:
            case FormulaTokenCode.RefErr3d2:
            case FormulaTokenCode.RefErr3d3:
            {
                return(new RefErr3dFormulaToken(tokenCode));
            }

            case FormulaTokenCode.AreaErr3d1:
            case FormulaTokenCode.AreaErr3d2:
            case FormulaTokenCode.AreaErr3d3:
            {
                return(new AreaErr3dFormulaToken(tokenCode));
            }
            }
            throw new ArgumentException("We don't support specified formula token: " + tokenCode);
        }
Пример #13
0
        ///<summary>
        ///Creates formula token form the name of the function.
        ///</summary>
        ///<param name="name">The name of the function.</param>
        ///<param name="tokenClass">The token class.</param>
        ///<param name="argumentsCount">The arguments count for the function.</param>
        ///<returns>created formula token</returns>
        public static FormulaToken CreateFunctionFromName(string name, FormulaTokenClass tokenClass, byte argumentsCount)
        {
            FormulaToken      token1 = null;
            FormulaTokenClass class1;

            object[] objArray1;
            if (!FormulaFunctionsTable.Instance[name].IsFixedArgumentCount)
            {
                class1 = tokenClass;
                switch (class1)
                {
                case FormulaTokenClass.Reference:
                {
                    token1 = new FunctionVarFormulaToken(FormulaTokenCode.FuncVar1);
                    goto Label_008C;
                }

                case FormulaTokenClass.Variable:
                {
                    token1 = new FunctionVarFormulaToken(FormulaTokenCode.FuncVar2);
                    goto Label_008C;
                }

                case FormulaTokenClass.Array:
                {
                    token1 = new FunctionVarFormulaToken(FormulaTokenCode.FuncVar3);
                    goto Label_008C;
                }
                }
            }
            else
            {
                class1 = tokenClass;
                switch (class1)
                {
                case FormulaTokenClass.Reference:
                {
                    token1 = new FunctionFormulaToken(FormulaTokenCode.Func1);
                    break;
                }

                case FormulaTokenClass.Variable:
                {
                    token1 = new FunctionFormulaToken(FormulaTokenCode.Func2);
                    break;
                }

                case FormulaTokenClass.Array:
                {
                    token1 = new FunctionFormulaToken(FormulaTokenCode.Func3);
                    break;
                }
                }
                objArray1 = new object[] { name };
                token1.DelayInitialize(objArray1);
                return(token1);
            }
Label_008C:
            objArray1    = new object[2];
            objArray1[0] = name;
            objArray1[1] = argumentsCount;
            token1.DelayInitialize(objArray1);
            return(token1);
        }