Exemple #1
0
 /// <summary>
 /// Constructs automaton that accept strings representing decimal numbers that can be
 /// written with at most the given number of digits in the fraction part. Surrounding
 /// whitespace is permitted.
 /// </summary>
 /// <param name="i">The i max number of necessary fraction digits.</param>
 /// <returns></returns>
 public static Automaton MakeFractionDigits(int i)
 {
     return
         (Automaton.Minimize(
              new RegExp("[ \t\n\r]*[-+]?[0-9]+(\\.[0-9]{0," + i + "}0*)?[ \t\n\r]*")
              .ToAutomaton()));
 }
Exemple #2
0
 /// <summary>
 /// Constructs automaton that accept strings representing decimal numbers that can be
 /// written with at most the given number of digits. Surrounding whitespace is permitted.
 /// </summary>
 /// <param name="i">The i max number of necessary digits.</param>
 /// <returns></returns>
 public static Automaton MakeTotalDigits(int i)
 {
     return
         (Automaton.Minimize(
              new RegExp("[ \t\n\r]*[-+]?0*([0-9]{0," + i + "}|((([0-9]\\.*){0," + i + "})&@\\.@)0*)[ \t\n\r]*")
              .ToAutomaton()));
 }
Exemple #3
0
        /// <summary>
        /// Constructs automaton that accept strings representing nonnegative integers that are not
        /// less that the given value.
        /// </summary>
        /// <param name="n">The n string representation of minimum value.</param>
        /// <returns></returns>
        public static Automaton MakeMinInteger(String n)
        {
            int i = 0;

            while (i + 1 < n.Length && n[i] == '0')
            {
                i++;
            }

            var b = new StringBuilder();

            b.Append("0*");
            MinInteger(n.Substring(i), 0, b);
            b.Append("[0-9]*");
            return(Automaton.Minimize((new RegExp(b.ToString())).ToAutomaton()));
        }
Exemple #4
0
        /// <summary>
        /// Constructs automaton that accept strings representing nonnegative integer that are not
        /// larger than the given value.
        /// </summary>
        /// <param name="n">The n string representation of maximum value.</param>
        /// <returns></returns>
        public static Automaton MakeMaxInteger(String n)
        {
            int i = 0;

            while (i < n.Length && n[i] == '0')
            {
                i++;
            }

            var b = new StringBuilder();

            b.Append("0*(0|");
            if (i < n.Length)
            {
                b.Append("[0-9]{1," + (n.Length - i - 1) + "}|");
            }

            MaxInteger(n.Substring(i), 0, b);
            b.Append(")");
            return(Automaton.Minimize((new RegExp(b.ToString())).ToAutomaton()));
        }
Exemple #5
0
        /// <summary>
        /// Constructs automaton that accept strings representing the given integer. Surrounding
        /// whitespace is permitted.
        /// </summary>
        /// <param name="value">The value string representation of integer.</param>
        /// <returns></returns>
        public static Automaton MakeIntegerValue(String value)
        {
            bool minus = false;
            int  i     = 0;

            while (i < value.Length)
            {
                char c = value[i];
                if (c == '-')
                {
                    minus = true;
                }

                if (c >= '1' && c <= '9')
                {
                    break;
                }

                i++;
            }

            var b = new StringBuilder();

            b.Append(value.Substring(i));
            if (b.Length == 0)
            {
                b.Append("0");
            }

            Automaton s  = minus ? Automaton.MakeChar('-') : Automaton.MakeChar('+').Optional();
            Automaton ws = Datatypes.WhitespaceAutomaton;

            return(Automaton.Minimize(
                       ws.Concatenate(
                           s.Concatenate(Automaton.MakeChar('0').Repeat())
                           .Concatenate(Automaton.MakeString(b.ToString())))
                       .Concatenate(ws)));
        }
Exemple #6
0
        private Automaton ToAutomaton(
            IDictionary <string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            IList <Automaton> list;
            Automaton         a = null;

            switch (kind)
            {
            case Kind.RegexpUnion:
                list = new List <Automaton>();
                this.FindLeaves(exp1, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                this.FindLeaves(exp2, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                a = BasicOperations.Union(list);
                a.Minimize();
                break;

            case Kind.RegexpConcatenation:
                list = new List <Automaton>();
                this.FindLeaves(exp1, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                this.FindLeaves(exp2, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                a = BasicOperations.Concatenate(list);
                a.Minimize();
                break;

            case Kind.RegexpIntersection:
                a = exp1.ToAutomaton(automata, automatonProvider, minimize)
                    .Intersection(exp2.ToAutomaton(automata, automatonProvider, minimize));
                a.Minimize();
                break;

            case Kind.RegexpOptional:
                a = exp1.ToAutomaton(automata, automatonProvider, minimize).Optional();
                a.Minimize();
                break;

            case Kind.RegexpRepeat:
                a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat();
                a.Minimize();
                break;

            case Kind.RegexpRepeatMin:
                a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(min);
                a.Minimize();
                break;

            case Kind.RegexpRepeatMinMax:
                a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(min, max);
                a.Minimize();
                break;

            case Kind.RegexpComplement:
                a = exp1.ToAutomaton(automata, automatonProvider, minimize).Complement();
                a.Minimize();
                break;

            case Kind.RegexpChar:
                a = BasicAutomata.MakeChar(c);
                break;

            case Kind.RegexpCharRange:
                a = BasicAutomata.MakeCharRange(from, to);
                break;

            case Kind.RegexpAnyChar:
            {
                a = this.anyCharAlphabet != null?BasicAutomata.MakeCharSet(this.anyCharAlphabet) : BasicAutomata.MakeAnyChar();

                break;
            }

            case Kind.RegexpEmpty:
                a = BasicAutomata.MakeEmpty();
                break;

            case Kind.RegexpString:
                a = BasicAutomata.MakeString(s);
                break;

            case Kind.RegexpAnyString:
                a = BasicAutomata.MakeAnyString();
                break;

            case Kind.RegexpAutomaton:
                Automaton aa = null;
                if (automata != null)
                {
                    automata.TryGetValue(s, out aa);
                }

                if (aa == null && automatonProvider != null)
                {
                    try
                    {
                        aa = automatonProvider.GetAutomaton(s);
                    }
                    catch (IOException e)
                    {
                        throw new ArgumentException(string.Empty, e);
                    }
                }

                if (aa == null)
                {
                    throw new ArgumentException("'" + s + "' not found");
                }

                a = aa.Clone();     // Always clone here (ignore allowMutate).
                break;

            case Kind.RegexpInterval:
                a = BasicAutomata.MakeInterval(min, max, digits);
                break;
            }

            return(a);
        }
Exemple #7
0
 public static Automaton Minimize(Automaton a)
 {
     a.Minimize();
     return a;
 }
Exemple #8
0
 public static Automaton Minimize(Automaton a)
 {
     a.Minimize();
     return(a);
 }
Exemple #9
0
        /// <summary>
        /// Constructs automaton that accept strings representing the given decimal number.
        /// Surrounding whitespace is permitted.
        /// </summary>
        /// <param name="value">The value string representation of decimal number.</param>
        /// <returns></returns>
        public static Automaton MakeDecimalValue(String value)
        {
            bool minus = false;
            int  i     = 0;

            while (i < value.Length)
            {
                char c = value[i];
                if (c == '-')
                {
                    minus = true;
                }

                if ((c >= '1' && c <= '9') || c == '.')
                {
                    break;
                }

                i++;
            }

            var b1 = new StringBuilder();
            var b2 = new StringBuilder();
            int p  = value.IndexOf('.', i);

            if (p == -1)
            {
                b1.Append(value.Substring(i));
            }
            else
            {
                b1.Append(value.Substring(i, p - i));
                i = value.Length - 1;
                while (i > p)
                {
                    char c = value[i];
                    if (c >= '1' && c <= '9')
                    {
                        break;
                    }

                    i--;
                }

                b2.Append(value.Substring(p + 1, i + 1 - (p + 1)));
            }

            if (b1.Length == 0)
            {
                b1.Append("0");
            }

            Automaton s = minus ? Automaton.MakeChar('-') : Automaton.MakeChar('+').Optional();
            Automaton d;

            if (b2.Length == 0)
            {
                d = Automaton.MakeChar('.').Concatenate(Automaton.MakeChar('0').Repeat(1)).Optional();
            }
            else
            {
                d = Automaton.MakeChar('.')
                    .Concatenate(Automaton.MakeString(b2.ToString()))
                    .Concatenate(Automaton.MakeChar('0')
                                 .Repeat());
            }

            Automaton ws = Datatypes.WhitespaceAutomaton;

            return(Automaton.Minimize(
                       ws.Concatenate(
                           s.Concatenate(Automaton.MakeChar('0').Repeat())
                           .Concatenate(Automaton.MakeString(b1.ToString()))
                           .Concatenate(d))
                       .Concatenate(ws)));
        }
Exemple #10
0
        private Automaton ToAutomaton(
            IDictionary <string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            IList <Automaton> list;
            Automaton         a = null;

            switch (this.Kind)
            {
            case Kind.RegexpUnion:
                list = new List <Automaton>();
                this.FindLeaves(Expr1, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                this.FindLeaves(Expr2, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                a = BasicOperations.Union(list);
                a.Minimize();
                break;

            case Kind.RegexpConcatenation:
                list = new List <Automaton>();
                this.FindLeaves(Expr1, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                this.FindLeaves(Expr2, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                a = BasicOperations.Concatenate(list);
                a.Minimize();
                break;

            case Kind.RegexpIntersection:
                a = Expr1.ToAutomaton(automata, automatonProvider, minimize)
                    .Intersection(Expr2.ToAutomaton(automata, automatonProvider, minimize));
                a.Minimize();
                break;

            case Kind.RegexpOptional:
                a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Optional();
                a.Minimize();
                break;

            case Kind.RegexpRepeat:
                a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Repeat();
                a.Minimize();
                break;

            case Kind.RegexpRepeatMin:
                a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Repeat(Min);
                a.Minimize();
                break;

            case Kind.RegexpRepeatMinMax:
                a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Repeat(Min, Max);
                a.Minimize();
                break;

            case Kind.RegexpComplement:
                a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Complement();
                a.Minimize();
                break;

            case Kind.RegexpChar:
                a = BasicAutomata.MakeChar(this.Char);
                break;

            case Kind.RegexpCharRange:
                a = BasicAutomata.MakeCharRange(FromChar, ToChar);
                break;

            case Kind.RegexpAnyChar:
                a = BasicAutomata.MakeAnyChar();
                break;

            case Kind.RegexpEmpty:
                a = BasicAutomata.MakeEmpty();
                break;

            case Kind.RegexpString:
                a = BasicAutomata.MakeString(SourceRegExpr);
                break;

            case Kind.RegexpAnyString:
                a = BasicAutomata.MakeAnyString();
                break;

            case Kind.RegexpAutomaton:
                Automaton aa = null;
                if (automata != null)
                {
                    automata.TryGetValue(SourceRegExpr, out aa);
                }

                if (aa == null && automatonProvider != null)
                {
                    try
                    {
                        aa = automatonProvider.GetAutomaton(SourceRegExpr);
                    }
                    catch (IOException e)
                    {
                        throw new ArgumentException(string.Empty, e);
                    }
                }

                if (aa == null)
                {
                    throw new ArgumentException("'" + SourceRegExpr + "' not found");
                }

                a = aa.Clone();     // Always clone here (ignore allowMutate).
                break;

            case Kind.RegexpInterval:
                a = BasicAutomata.MakeInterval(Min, Max, Digits);
                break;
            }

            return(a);
        }
Exemple #11
0
 internal static Automaton Minimize(Automaton a)
 {
     a.Minimize();
     return(a);
 }