Пример #1
0
        /// <summary>
        /// Calculator operator and get the result TwoFactorExpression.
        /// </summary>
        private void CalculateOprator()
        {
            var op = this.operatorStack.Pop();

            if (op.Text != "(")
            {
                try
                {
                    var right = this.dataStack.Pop();
                    var left  = this.dataStack.Pop();

                    TwoFactorExpression exp = new TwoFactorExpression()
                    {
                        RightExpression = right,
                        LeftExpression  = left,
                        OperatorToken   = op
                    };

                    this.dataStack.Push(exp);
                }
                catch
                {
                    throw new ArgumentException($"Wrong Expression Passed Near {op.Text}");
                }
            }
        }
Пример #2
0
        private FilterModelCollection BuildFilterFromExpression(TwoFactorExpression expression)
        {
            if (expression == null)
            {
                return(null);
            }

            Debug.WriteLine(expression.ToString());
            FilterModelCollection collection = new FilterModelCollection();

            if (expression.OperatorToken.Text == "and")
            {
                collection.Operator = LogicOperator.And;
                if (expression.LeftExpression != null)
                {
                    var child = this.BuildFilterFromExpression(expression.LeftExpression);
                    collection.Filters.Add(child);
                }

                if (expression.RightExpression != null)
                {
                    var child = this.BuildFilterFromExpression(expression.RightExpression);
                    collection.Filters.Add(child);
                }
            }
            else if (expression.OperatorToken.Text == "or")
            {
                collection.Operator = LogicOperator.Or;
                if (expression.LeftExpression != null)
                {
                    var child = this.BuildFilterFromExpression(expression.LeftExpression);
                    collection.Filters.Add(child);
                }

                if (expression.RightExpression != null)
                {
                    var child = this.BuildFilterFromExpression(expression.RightExpression);
                    collection.Filters.Add(child);
                }
            }
            else
            {
                if (expression.LeftExpression.LeftExpression != null ||
                    expression.LeftExpression.RightExpression != null ||
                    expression.RightExpression.LeftExpression != null ||
                    expression.RightExpression.RightExpression != null)
                {
                    throw new ArgumentException(
                              "Non logic expression should not have sub expression",
                              nameof(expression));
                }
                FilterModel model = new FilterModel();
                model.Name     = expression.LeftExpression.ValueToken.Text;
                model.Value    = expression.RightExpression.ValueToken.Text;
                model.Operator = expression.OperatorToken.Text;
                collection.Filters.Add(model);
            }

            return(collection);
        }
Пример #3
0
        /// <summary>
        /// Parse the filter string in to filter model collection.
        /// </summary>
        /// <param name="filterStr">The filter string to be parsed.</param>
        /// <returns>The filter Model collection.</returns>
        public FilterModelCollection ParseFilter(string filterStr)
        {
            this.reader = new TokenReader(filterStr);
            Token token;

            do
            {
                token = this.reader.ReadNextToken();
                if (token != null)
                {
                    if (token.Type == TokenType.Operator)
                    {
                        if (token.Text == ")")
                        {
                            this.expectedBracket--;
                        }

                        if (this.operatorStack.Count > 0)
                        {
                            Token lastToken = this.operatorStack.Peek();
                            if (this.HighPriorityToken(lastToken, token))
                            {
                                this.PushOperator(token);
                            }
                            else
                            {
                                while (!this.HighPriorityToken(lastToken, token) && this.operatorStack.Count != 0)
                                {
                                    this.CalculateOprator();
                                    if (this.operatorStack.Count > 0)
                                    {
                                        lastToken = this.operatorStack.Peek();
                                    }
                                }

                                if (token.Text != ")")
                                {
                                    this.PushOperator(token);
                                }
                                else
                                {
                                    var matchToken = this.operatorStack.Peek();
                                    if (matchToken.Text != "(")
                                    {
                                        throw new ArgumentException("Unmatched bracket");
                                    }
                                    else
                                    {
                                        this.operatorStack.Pop();
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (token.Text != ")")
                            {
                                this.PushOperator(token);
                            }
                            else
                            {
                                throw new ArgumentException("Unmatched bracket");
                            }
                        }
                    }
                    else
                    {
                        TwoFactorExpression exp = new TwoFactorExpression {
                            ValueToken = token
                        };
                        this.dataStack.Push(exp);
                    }
                }
            }while (token != null);


            while (this.operatorStack.Count > 0)
            {
                this.CalculateOprator();
            }

            if (this.expectedBracket != 0)
            {
                throw new ArgumentException("unmatched bracket");
            }

            if (this.dataStack.Count > 1)
            {
                throw new ArgumentException("The parse parse a wrong result based on given filter");
            }
            else
            {
                return(this.BuildFilterFromExpression(this.dataStack.Pop()));
            }
        }