Exemplo n.º 1
0
        /// <summary>
        ///   Initializes a new instance of the <see cref = "RegExp" /> class from a string.
        /// </summary>
        /// <param name = "s">A string with the regular expression.</param>
        /// <param name = "syntaxFlags">Boolean 'or' of optional syntax constructs to be enabled.</param>
        internal RegExp(string s, RegExpSyntaxOptions syntaxFlags)
        {
            this.b     = RegExp.ReplaceShorthandCharacterClasses(s);
            this.flags = syntaxFlags;
            RegExp e;

            if (s.Length == 0)
            {
                e = RegExp.MakeString(string.Empty);
            }
            else
            {
                e = this.ParseUnionExp();
                if (this.pos < b.Length)
                {
                    throw new ArgumentException("end-of-string expected at position " + this.pos);
                }
            }

            this.kind   = e.kind;
            this.exp1   = e.exp1;
            this.exp2   = e.exp2;
            this.s      = e.s;
            this.c      = e.c;
            this.min    = e.min;
            this.max    = e.max;
            this.digits = e.digits;
            this.from   = e.from;
            this.to     = e.to;
            this.b      = null;
        }
Exemplo n.º 2
0
        private static RegExp MakeConcatenation(RegExp exp1, RegExp exp2)
        {
            if ((exp1.kind == Kind.RegexpChar || exp1.kind == Kind.RegexpString) &&
                (exp2.kind == Kind.RegexpChar || exp2.kind == Kind.RegexpString))
            {
                return(RegExp.MakeString(exp1, exp2));
            }

            var r = new RegExp();

            r.kind = Kind.RegexpConcatenation;
            if (exp1.kind == Kind.RegexpConcatenation &&
                (exp1.exp2.kind == Kind.RegexpChar || exp1.exp2.kind == Kind.RegexpString) &&
                (exp2.kind == Kind.RegexpChar || exp2.kind == Kind.RegexpString))
            {
                r.exp1 = exp1.exp1;
                r.exp2 = RegExp.MakeString(exp1.exp2, exp2);
            }
            else if ((exp1.kind == Kind.RegexpChar || exp1.kind == Kind.RegexpString) &&
                     exp2.kind == Kind.RegexpConcatenation &&
                     (exp2.exp1.kind == Kind.RegexpChar || exp2.exp1.kind == Kind.RegexpString))
            {
                r.exp1 = RegExp.MakeString(exp1, exp2.exp1);
                r.exp2 = exp2.exp2;
            }
            else
            {
                r.exp1 = exp1;
                r.exp2 = exp2;
            }

            return(r);
        }
Exemplo n.º 3
0
        private static RegExp MakeString(RegExp exp1, RegExp exp2)
        {
            var sb = new StringBuilder();

            if (exp1.kind == Kind.RegexpString)
            {
                sb.Append(exp1.s);
            }
            else
            {
                sb.Append(exp1.c);
            }

            if (exp2.kind == Kind.RegexpString)
            {
                sb.Append(exp2.s);
            }
            else
            {
                sb.Append(exp2.c);
            }

            return(RegExp.MakeString(sb.ToString()));
        }
Exemplo n.º 4
0
        private RegExp ParseSimpleExp()
        {
            if (this.Match('.'))
            {
                return(RegExp.MakeAnyChar());
            }

            if (this.Check(RegExpSyntaxOptions.Empty) && this.Match('#'))
            {
                return(RegExp.MakeEmpty());
            }

            if (this.Check(RegExpSyntaxOptions.Anystring) && this.Match('@'))
            {
                return(RegExp.MakeAnyString());
            }

            if (this.Match('"'))
            {
                int start = pos;
                while (this.More() && !this.Peek("\""))
                {
                    this.Next();
                }

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

                return(RegExp.MakeString(b.Substring(start, ((pos - 1) - start))));
            }

            if (this.Match('('))
            {
                if (this.Match('?'))
                {
                    this.SkipNonCapturingSubpatternExp();
                }

                if (this.Match(')'))
                {
                    return(RegExp.MakeString(string.Empty));
                }

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

                return(e);
            }

            if ((this.Check(RegExpSyntaxOptions.Automaton) || this.Check(RegExpSyntaxOptions.Interval)) && this.Match('<'))
            {
                int start = pos;
                while (this.More() && !this.Peek(">"))
                {
                    this.Next();
                }

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

                string str = b.Substring(start, ((pos - 1) - start));
                int    i   = str.IndexOf('-');
                if (i == -1)
                {
                    if (!this.Check(RegExpSyntaxOptions.Automaton))
                    {
                        throw new ArgumentException("interval syntax error at position " + (pos - 1));
                    }

                    return(RegExp.MakeAutomaton(str));
                }

                if (!this.Check(RegExpSyntaxOptions.Interval))
                {
                    throw new ArgumentException("illegal identifier at position " + (pos - 1));
                }

                try
                {
                    if (i == 0 || i == str.Length - 1 || i != str.LastIndexOf('-'))
                    {
                        throw new FormatException();
                    }

                    string smin      = str.Substring(0, i - 0);
                    string smax      = str.Substring(i + 1, (str.Length - (i + 1)));
                    int    imin      = int.Parse(smin, CultureInfo.CurrentCulture);
                    int    imax      = int.Parse(smax, CultureInfo.CurrentCulture);
                    int    numdigits = smin.Length == smax.Length ? smin.Length : 0;
                    if (imin > imax)
                    {
                        int t = imin;
                        imin = imax;
                        imax = t;
                    }

                    return(RegExp.MakeInterval(imin, imax, numdigits));
                }
                catch (FormatException)
                {
                    throw new ArgumentException("interval syntax error at position " + (pos - 1));
                }
            }

            return(RegExp.MakeChar(this.ParseCharExp()));
        }