Example #1
0
        /// <summary>
        /// Applies the parser zero or more times.
        /// </summary>
        public static IParser <string> Many(this IParser <char> parser)
        {
            Func <StringBuilder> seed = () => new StringBuilder();
            Func <StringBuilder, char, StringBuilder> func = (builder, c) => builder.Append(c);
            Func <StringBuilder, string> sel = builder => builder.ToString();

            return(parser.Aggregate(seed, func, sel));
        }
Example #2
0
        public static IParser <TextSpan> Many(this IParser <TextSpan> parser)
        {
            TextSpan Seed() => TextSpan.Empty;

            TextSpan AccFunc(TextSpan builder, TextSpan c)
            {
                return(builder + c);
            }

            TextSpan ResultSelector(TextSpan builder) => builder;

            return(parser.Aggregate(Seed, AccFunc, ResultSelector));
        }
Example #3
0
        /// <summary>
        /// Appends the results of each result sequence from the specified <paramref name="parser"/> into an <see cref="IList{TResult}"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the source elements.</typeparam>
        /// <typeparam name="TResult">The type of the elements that are generated by the parser.</typeparam>
        /// <param name="parser">The parser that produces a sequence of result sequences to be aggregated.</param>
        /// <returns>A parser that returns the results aggregated into an <see cref="IList{TResult}"/>.</returns>
        public static IParser <TSource, IList <TResult> > ToList <TSource, TResult>(
            this IParser <TSource, IEnumerable <TResult> > parser)
        {
            Contract.Requires(parser != null);
            Contract.Ensures(Contract.Result <IParser <TSource, IList <TResult> > >() != null);

            return(parser.Aggregate(
                       () => new List <TResult>(),
                       (list, result) =>
            {
                list.Add(result);
                return list;
            },
                       list => (IList <TResult>)list.AsReadOnly()));
        }
Example #4
0
 /// <summary>
 /// Applies the parser zero or more times.
 /// </summary>
 public static IParser <IEnumerable <T> > Many <T>(this IParser <T> parser)
 {
     return(parser.Aggregate(() => Enumerable.Empty <T>(), Concat, x => x));
 }