Exemple #1
0
        /// <summary>
        /// Yields success when the specified <paramref name="parser"/> does not match.
        /// </summary>
        /// <typeparam name="TSource">The type of the source elements.</typeparam>
        /// <typeparam name="TResult">The type of the elements that are generated from parsing the source elements.</typeparam>
        /// <param name="parser">The parser for which any match results in failure.</param>
        /// <returns>A parser that yields failure when the specified <paramref name="parser"/> matches or
        /// an empty sequence to indicate success when it does not match.</returns>
        public static IParser <TSource, IEnumerable <TResult> > None <TSource, TResult>(
            this IParser <TSource, TResult> parser)
        {
            Contract.Requires(parser != null);
            Contract.Ensures(Contract.Result <IParser <TSource, IEnumerable <TResult> > >() != null);

            if (parser is IParserCursor <TSource> )
            {
                return(parser.AtEndOfSequence());
            }
            else
            {
                return(parser.Yield(
                           "None",
                           source =>
                           parser.Parse(source).Any()
            ? ParseResult.ReturnFailureMany <TResult>()
            : ParseResult.ReturnSuccessMany <TResult>(length: 0)));
            }
        }
Exemple #2
0
        /// <summary>
        /// Yields success when the specified <paramref name="parser"/> does not match.
        /// </summary>
        /// <typeparam name="TSource">The type of the source elements.</typeparam>
        /// <typeparam name="TResult">The type of the elements that are generated from parsing the source elements.</typeparam>
        /// <typeparam name="TSuccess">The type of the success value.</typeparam>
        /// <param name="parser">The parser for which any match results in failure.</param>
        /// <param name="successResult">The value that is yielded if the specified <paramref name="parser"/> does not match.</param>
        /// <returns>A parser that yields failure when the specified <paramref name="parser"/> matches or success when
        /// it does not match.</returns>
        public static IParser <TSource, TSuccess> None <TSource, TResult, TSuccess>(
            this IParser <TSource, TResult> parser,
            TSuccess successResult)
        {
            Contract.Requires(parser != null);
            Contract.Ensures(Contract.Result <IParser <TSource, TSuccess> >() != null);

            if (parser is IParserCursor <TSource> )
            {
                return(parser.AtEndOfSequence(successResult));
            }
            else
            {
                return(parser.Yield(
                           "None",
                           source =>
                           parser.Parse(source).Any()
            ? ParseResult.ReturnFailure <TSuccess>()
            : ParseResult.Return(successResult, length: 0)));
            }
        }