示例#1
0
        public override Expression ToRegExp(Entity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            int  number = Number;
            Sign sign   = Sign;

            if (number > 0 && sign == Sign.MoreThanOrEqual)
            {
                number--;
                sign = Sign.MoreThan;
            }

            if (sign == Sign.MoreThanOrEqual)
            {
                return(new RegExpIteration(entity.ToRegExp(), false));
            }

            if (number < 1)
            {
                return(new RegExpIteration(entity.ToRegExp(), true));
            }

            Expression expression = entity.ToRegExp();

            Expression left  = new RegExpConstIteration(expression, number);
            Expression right = new RegExpIteration(expression, true);

            return(new RegExpConcat(left, right));
        }
示例#2
0
文件: Concat.cs 项目: apodavalov/FLaG
        public override Expression Optimize()
        {
            bool somethingChanged;

            Expression previous;

            IList <Expression> oldList = ConcatHelper.Iterate(Expressions.Select(e => e.Optimize())).ToList();
            IList <Expression> newList;

            do
            {
                somethingChanged = false;
                newList          = new List <Expression>();

                previous = null;

                foreach (Expression expression in oldList)
                {
                    if (expression == Empty.Instance)
                    {
                        somethingChanged = true;
                        continue;
                    }

                    if (previous == null)
                    {
                        previous = expression;
                        continue;
                    }

                    Iteration iteration1 = previous.As <Iteration>();
                    Iteration iteration2 = expression.As <Iteration>();

                    if (iteration1 != null || iteration2 != null)
                    {
                        if (iteration1 != null && iteration2 != null && iteration1.Expression == iteration2.Expression)
                        {
                            if (iteration1.IsPositive && iteration2.IsPositive)
                            {
                                newList.Add(iteration1.Expression);
                            }

                            previous         = new Iteration(iteration1.Expression, iteration1.IsPositive || iteration2.IsPositive);
                            somethingChanged = true;
                            continue;
                        }

                        Iteration  iteration;
                        Expression newExpression;

                        if (iteration1 != null)
                        {
                            iteration     = iteration1;
                            newExpression = expression;
                        }
                        else
                        {
                            iteration     = iteration2;
                            newExpression = previous;
                        }

                        if (iteration.Expression == newExpression && !iteration.IsPositive)
                        {
                            previous         = new Iteration(newExpression, true);
                            somethingChanged = true;
                            continue;
                        }
                    }

                    ConstIteration constIteration1 = previous.As <ConstIteration>();
                    ConstIteration constIteration2 = expression.As <ConstIteration>();

                    if (constIteration1 != null || constIteration2 != null)
                    {
                        if (constIteration1 != null && constIteration2 != null && constIteration1.Expression == constIteration2.Expression)
                        {
                            previous         = new ConstIteration(constIteration1.Expression, constIteration1.IterationCount + constIteration2.IterationCount);
                            somethingChanged = true;
                            continue;
                        }

                        ConstIteration constIteration;
                        Expression     newExpression;

                        if (constIteration1 != null)
                        {
                            constIteration = constIteration1;
                            newExpression  = expression;
                        }
                        else
                        {
                            constIteration = constIteration2;
                            newExpression  = previous;
                        }

                        if (constIteration.Expression == newExpression)
                        {
                            previous         = new ConstIteration(newExpression, constIteration.IterationCount + 1);
                            somethingChanged = true;
                            continue;
                        }
                    }

                    if (constIteration1 != null && iteration2 != null && constIteration1.Expression == iteration2.Expression)
                    {
                        if (constIteration1.IterationCount > 2)
                        {
                            newList.Add(new ConstIteration(constIteration1.Expression, constIteration1.IterationCount - 1));
                        }
                        else
                        {
                            newList.Add(constIteration1.Expression);
                        }

                        previous         = new Iteration(iteration2.Expression, true);
                        somethingChanged = true;
                        continue;
                    }

                    if (iteration1 != null && constIteration2 != null && iteration1.Expression == constIteration2.Expression)
                    {
                        newList.Add(new Iteration(iteration1.Expression, true));

                        if (constIteration2.IterationCount > 2)
                        {
                            previous = new ConstIteration(constIteration2.Expression, constIteration2.IterationCount - 1);
                        }
                        else
                        {
                            previous = constIteration2.Expression;
                        }

                        somethingChanged = true;
                        continue;
                    }

                    newList.Add(previous);
                    previous = expression;
                }

                if (previous != null)
                {
                    newList.Add(previous);
                }

                oldList = newList;

                somethingChanged |= TryToCompress(oldList);
            } while (somethingChanged);

            return(ConcatHelper.MakeExpression(newList));
        }