Пример #1
0
        /// <summary>
        /// This routine will replace functions existing in a input string with thier respective values
        /// </summary>
        /// <param name="input">input string</param>
        /// <param name="handler">Additional function handler for custom functions</param>
        /// <returns>input string with all found functions replaced with returned values</returns>
        public static string Replace(string input, AdditionalFunctionEventHandler handler)
        {
            FunctionEval expr = new FunctionEval(input);

            if (handler != null)
            {
                expr.AdditionalFunctionEventHandler += handler;
            }
            return(expr.Replace());
        }
Пример #2
0
        /// <summary>
        /// Evaluates a string expression of a function
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="handler">attach a custom function handler</param>
        /// <returns>evauluated value</returns>
        public static object Evaluate(string expression, AdditionalFunctionEventHandler handler)
        {
            FunctionEval expr = new FunctionEval(expression);

            if (handler != null)
            {
                expr.AdditionalFunctionEventHandler += handler;
            }
            return(expr.Evaluate());
        }
Пример #3
0
        /// <summary>
        /// This routine will replace functions existing in the Expression property with thier respective values
        /// </summary>
        /// <returns>Expression string with all found functions replaced with returned values</returns>
        public string Replace()
        {
            StringBuilder strbRet = new StringBuilder(Expression);
            Match         m       = DefinedRegex.Function.Match(Expression);

            while (m.Success)
            {
                int nDepth = 1;
                int nIdx   = m.Index + m.Length;
                //Get the parameter string
                while (nDepth > 0)
                {
                    if (nIdx >= strbRet.Length)
                    {
                        throw new ArgumentException("Missing ')' in Expression");
                    }
                    if (strbRet[nIdx] == ')')
                    {
                        nDepth--;
                    }
                    if (strbRet[nIdx] == '(')
                    {
                        nDepth++;
                    }
                    nIdx++;
                }
                string       expression = strbRet.ToString(m.Index, nIdx - m.Index);
                FunctionEval eval       = new FunctionEval(expression);
                eval.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler;
                eval._variables = this._variables;
                strbRet.Replace(expression, "" + eval.Evaluate());
                m = DefinedRegex.Function.Match(strbRet.ToString());
            }

            //Replace Variable in the path!
            m = DefinedRegex.Variable.Match(strbRet.ToString());
            while (m.Success)
            {
                strbRet.Replace(m.Value, "" + _variables[m.Groups["Variable"].Value]);
                m = DefinedRegex.Variable.Match(strbRet.ToString());
            }

            return(strbRet.ToString());
        }
Пример #4
0
        /// <summary>
        /// Evaluates a string expression of a function
        /// </summary>
        /// <param name="expression"></param>
        /// <returns>evauluated value</returns>
        public static object Evaluate(string expression)
        {
            FunctionEval expr = new FunctionEval(expression);

            return(expr.Evaluate());
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string Replace(string input)
        {
            FunctionEval expr = new FunctionEval(input);

            return(expr.Replace());
        }
Пример #6
0
        /// <summary>
        /// This will search the expression for the next token (operand, operator, etc)
        /// </summary>
        /// <param name="nIdx">Start Position of Search</param>
        /// <returns>First character index after token.</returns>
        //[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        private int NextToken(int nIdx)
        {
            Match  mRet = null;
            int    nRet = nIdx;
            object val  = null;

            //Check for preceeding white space from last token index
            Match m = DefinedRegex.WhiteSpace.Match(Expression, nIdx);

            if (m.Success && m.Index == nIdx)
            {
                return(nIdx + m.Length);
            }

            //Check Parenthesis
            m = DefinedRegex.Parenthesis.Match(Expression, nIdx);
            if (m.Success)
            {
                mRet = m;
            }

            //Check Function
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.Function.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m;
                }
            }

            //Check Variable
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.Variable.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = new Variable(m.Groups["Variable"].Value, _variables);
                }
            }

            //Check Unary Operator
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.UnaryOp.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = new UnaryOp(m.Value);
                }
            }

            //Check Hexadecimal
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.Hexadecimal.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = Convert.ToInt32(m.Value, 16);
                }
            }

            //Check Boolean
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.Boolean.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = bool.Parse(m.Value);
                }
            }

            //Check DateTime
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.DateTime.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = Convert.ToDateTime(m.Groups["DateString"].Value, CultureInfo.CurrentCulture);
                }
            }

            //Check Timespan
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.TimeSpan.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m;
                    val  = new TimeSpan(
                        int.Parse("0" + m.Groups["Days"].Value),
                        int.Parse(m.Groups["Hours"].Value),
                        int.Parse(m.Groups["Minutes"].Value),
                        int.Parse("0" + m.Groups["Seconds"].Value),
                        int.Parse("0" + m.Groups["Milliseconds"].Value)
                        );
                }
            }

            //Check Numeric
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.Numeric.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    while (m.Success && ("" + m.Value == ""))
                    {
                        m = m.NextMatch();
                    }
                    if (m.Success)
                    {
                        mRet = m;
                        val  = double.Parse(m.Value, CultureInfo.CurrentCulture);
                    }
                }
            }

            if (mRet == null || mRet.Index > nIdx)
            {
                //Check String
                m = DefinedRegex.String.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = m.Groups["String"].Value.Replace("\\\"", "\"");
                }
            }

            //Check Binary Operator
            if (mRet == null || mRet.Index > nIdx)
            {
                m = DefinedRegex.BinaryOp.Match(Expression, nIdx);
                if (m.Success && (mRet == null || m.Index < mRet.Index))
                {
                    mRet = m; val = new BinaryOp(m.Value);
                }
            }

            if (mRet == null)
            {
                throw new ArgumentException("Invalid expression construction: \"" + Expression + "\".");
            }

            if (mRet.Index != nIdx)
            {
                throw new ArgumentException(
                          "Invalid token in expression: [" +
                          Expression.Substring(nIdx, mRet.Index - nIdx).Trim() + "]"
                          );
            }

            if (mRet.Value == "(" || mRet.Value.StartsWith("$"))
            {
                nRet = mRet.Index + mRet.Length;
                int  nDepth    = 1;
                bool bInQuotes = false;
                while (nDepth > 0)
                {
                    if (nRet >= Expression.Length)
                    {
                        throw new ArgumentException("Missing " + (bInQuotes ? "\"" : ")") + " in Expression");
                    }
                    if (!bInQuotes && Expression[nRet] == ')')
                    {
                        nDepth--;
                    }
                    if (!bInQuotes && Expression[nRet] == '(')
                    {
                        nDepth++;
                    }

                    if (Expression[nRet] == '"' && (nRet == 0 || Expression[nRet - 1] != '\\'))
                    {
                        bInQuotes = !bInQuotes;
                    }

                    nRet++;
                }
                if (mRet.Value == "(")
                {
                    ExpressionEval expr = new ExpressionEval(
                        Expression.Substring(mRet.Index + 1, nRet - mRet.Index - 2)
                        );
                    if (this.AdditionalFunctionEventHandler != null)
                    {
                        expr.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler;
                    }
                    expr._variables = this._variables;
                    _expressionlist.Add(expr);
                }
                else
                {
                    FunctionEval func = new FunctionEval(
                        Expression.Substring(mRet.Index, (nRet) - mRet.Index)
                        );
                    if (this.AdditionalFunctionEventHandler != null)
                    {
                        func.AdditionalFunctionEventHandler += this.AdditionalFunctionEventHandler;
                    }
                    func._variables = this._variables;
                    _expressionlist.Add(func);
                }
            }
            else
            {
                nRet = mRet.Index + mRet.Length;
                _expressionlist.Add(val);
            }

            return(nRet);
        }