Exemplo n.º 1
0
        private RegExp ParseRepeatExp()
        {
            RegExp e = this.ParseComplExp();

            while (this.Peek("?*+{"))
            {
                if (this.Match('?'))
                {
                    e = RegExp.MakeOptional(e);
                }
                else if (this.Match('*'))
                {
                    e = RegExp.MakeRepeat(e);
                }
                else if (this.Match('+'))
                {
                    e = RegExp.MakeRepeat(e, 1);
                }
                else if (this.Match('{'))
                {
                    int start = pos;
                    while (this.Peek("0123456789"))
                    {
                        this.Next();
                    }

                    if (start == pos)
                    {
                        throw new ArgumentException("integer expected at position " + pos);
                    }

                    int n = int.Parse(b.Substring(start, pos - start));
                    int m = -1;
                    if (this.Match(','))
                    {
                        start = pos;
                        while (this.Peek("0123456789"))
                        {
                            this.Next();
                        }

                        if (start != pos)
                        {
                            m = int.Parse(b.Substring(start, pos - start));
                        }
                    }
                    else
                    {
                        m = n;
                    }

                    if (!this.Match('}'))
                    {
                        throw new ArgumentException("expected '}' at position " + pos);
                    }

                    e = m == -1 ? RegExp.MakeRepeat(e, n) : RegExp.MakeRepeat(e, n, m);
                }
            }

            return(e);
        }