Beispiel #1
0
        private static RegExp MakeInterval(int min, int max, int digits)
        {
            var r = new RegExp();

            r.kind   = Kind.RegexpInterval;
            r.min    = min;
            r.max    = max;
            r.digits = digits;
            return(r);
        }
Beispiel #2
0
        private static RegExp MakeRepeat(RegExp exp, int min, int max)
        {
            var r = new RegExp();

            r.kind = Kind.RegexpRepeatMinMax;
            r.exp1 = exp;
            r.min  = min;
            r.max  = max;
            return(r);
        }
Beispiel #3
0
        private RegExp ParseConcatExp()
        {
            RegExp e = this.ParseRepeatExp();

            if (this.More() && !this.Peek(")|") && (!this.Check(RegExpSyntaxOptions.Intersection) || !this.Peek("&")))
            {
                e = RegExp.MakeConcatenation(e, this.ParseConcatExp());
            }

            return(e);
        }
Beispiel #4
0
        private RegExp ParseInterExp()
        {
            RegExp e = this.ParseConcatExp();

            if (this.Check(RegExpSyntaxOptions.Intersection) && this.Match('&'))
            {
                e = RegExp.MakeIntersection(e, this.ParseInterExp());
            }

            return(e);
        }
Beispiel #5
0
        private RegExp ParseUnionExp()
        {
            RegExp e = this.ParseInterExp();

            if (this.Match('|'))
            {
                e = RegExp.MakeUnion(e, this.ParseUnionExp());
            }

            return(e);
        }
Beispiel #6
0
        private RegExp ParseCharClasses()
        {
            RegExp e = this.ParseCharClass();

            while (this.More() && !this.Peek("]"))
            {
                e = RegExp.MakeUnion(e, this.ParseCharClass());
            }

            return(e);
        }
Beispiel #7
0
        private RegExp ParseCharClass()
        {
            char @char = this.ParseCharExp();

            if (this.Match('-'))
            {
                if (this.Peek("]"))
                {
                    return(RegExp.MakeUnion(RegExp.MakeChar(@char), RegExp.MakeChar('-')));
                }

                return(RegExp.MakeCharRange(@char, this.ParseCharExp()));
            }

            return(RegExp.MakeChar(@char));
        }
Beispiel #8
0
 private void FindLeaves(
     RegExp exp,
     Kind regExpKind,
     IList <Automaton> list,
     IDictionary <String, Automaton> automata,
     IAutomatonProvider automatonProvider,
     bool minimize)
 {
     if (exp.kind == regExpKind)
     {
         this.FindLeaves(exp.exp1, regExpKind, list, automata, automatonProvider, minimize);
         this.FindLeaves(exp.exp2, regExpKind, list, automata, automatonProvider, minimize);
     }
     else
     {
         list.Add(exp.ToAutomaton(automata, automatonProvider, minimize));
     }
 }
Beispiel #9
0
        private Automaton ToAutomatonAllowMutate(
            IDictionary <string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            bool @bool = false;

            if (allowMutation)
            {
                @bool = RegExp.SetAllowMutate(true); // This is not thead safe.
            }

            Automaton a = this.ToAutomaton(automata, automatonProvider, minimize);

            if (allowMutation)
            {
                RegExp.SetAllowMutate(@bool);
            }

            return(a);
        }
Beispiel #10
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()));
        }
Beispiel #11
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()));
        }
Beispiel #12
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), CultureInfo.CurrentCulture);
                    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), CultureInfo.CurrentCulture);
                        }
                    }
                    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);
        }