Beispiel #1
0
        private static IParser <IReadOnlyList <T> > DoRepeat <T>(this IParser <T> parser, int atLeast, int?atMost)
        {
            return(Create(position =>
            {
                var values = new List <T>(capacity: atLeast);
                var remainder = position;
                int repeated = 0;

                while (true)
                {
                    if (repeated >= atMost)
                    {
                        break;
                    }
                    var result = parser.TryParse(remainder);
                    if (!result.Success || result.NextPosition == remainder)
                    {
                        break;
                    }
                    values.Add(result.Value);
                    remainder = result.NextPosition;
                    repeated++;
                }

                return repeated >= atLeast ? ParseResult.Success <IReadOnlyList <T> >(values, remainder) : ParseResult.Failure <IReadOnlyList <T> >(position);
            }));
        }