示例#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 IObservableParser <TSource, IObservable <TResult> > None <TSource, TResult>(
            this IObservableParser <TSource, TResult> parser)
        {
            Contract.Requires(parser != null);
            Contract.Ensures(Contract.Result <IObservableParser <TSource, IObservable <TResult> > >() != null);

            if (parser is IObservableParserCursor <TSource> )
            {
                return(parser.AtEndOfSequence());
            }
            else
            {
                return(parser.Yield <TSource, TResult, IObservable <TResult> >(
                           "None",
                           (source, observer) =>
                {
                    return parser.Parse(source).Any().SubscribeSafe(
                        any =>
                    {
                        if (!any)
                        {
                            observer.OnNext(ObservableParseResult.SuccessMany <TResult>(length: 0));
                        }
                    },
                        observer.OnError,
                        observer.OnCompleted);
                }));
            }
        }