Пример #1
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)));
        }
Пример #2
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)));
        }