Exemple #1
0
        public static IInput AdvanceAssert(this IInput input, Action <IInput, IInput> assertion)
        {
            var result = input.Advance();

            assertion(input, result);
            return(result);
        }
Exemple #2
0
        public static IInput AdvanceMany(this IInput input, int count)
        {
            for (int i = 0; i < count; i++)
            {
                input = input.Advance();
            }

            return(input);
        }
Exemple #3
0
        /// <summary>
        /// Skips <param name="count"></param> of bytes
        /// </summary>
        /// <param name="input">Input to skip bytes at</param>
        /// <param name="count">Number of bytes to skip</param>
        /// <returns>Input with current position right after skipped bytes</returns>
        public static IInput Skip(this IInput input, int count)
        {
            while (count-- > 0)
            {
                input = input.Advance();
            }

            return(input);
        }
Exemple #4
0
        /// <summary>
        /// Creates a parser from the specified regular expression, returning a parser of
        /// type <see cref="Match"/>.
        /// </summary>
        /// <param name="regex">The regular expression.</param>
        /// <param name="description">Description of characters that do not match.</param>
        /// <returns>
        /// A parser of regular expression <see cref="Match"/> objects.
        /// </returns>
        public static Parser <Match> RegexMatch(Regex regex, string description = null)
        {
            if (regex == null)
            {
                throw new ArgumentNullException(nameof(regex));
            }

            regex = OptimizeRegex(regex);

            string[] expectations = description == null
                ? new string[0]
                : new[] { description };

            return(i =>
            {
                if (!i.AtEnd)
                {
                    IInput remainder = i;
                    string input = i.Source.Substring(i.Position);
                    Match match = regex.Match(input);

                    if (match.Success)
                    {
                        for (int j = 0; j < match.Length; j++)
                        {
                            remainder = remainder.Advance();
                        }

                        return Result.Success(match, remainder);
                    }

                    string found = match.Index == input.Length
                        ? RS.EndOfSource
                        : string.Format("`{0}'", input[match.Index]);

                    return Result.Failure <Match>(
                        remainder,
                        string.Format(RS.RegexMismatch, regex, found),
                        expectations);
                }

                return Result.Failure <Match>(i, RS.UnexpectedEndOfInput, expectations);
            });
        }