예제 #1
0
 /// <summary>
 /// Retrieves the upcoming item without consuming it.
 /// </summary>
 /// <typeparam name="TItem">The item type of the stream.</typeparam>
 /// <param name="stream">The <see cref="IStream{TItem}"/> to peek in.</param>
 /// <returns>The next item in the stream.</returns>
 public static TItem Peek <TItem>(this IPeekableStream <TItem> stream) => stream.LookAhead(0);
예제 #2
0
    /// <summary>
    /// Applies a parser to some input, consuming the parsed tokens on success.
    /// </summary>
    /// <typeparam name="TItem">The item type of the stream.</typeparam>
    /// <typeparam name="TResult">The parsed value type.</typeparam>
    /// <param name="parser">The parser to apply.</param>
    /// <param name="stream">The stream to parse from.</param>
    /// <returns>The result of parsing with <paramref name="parser"/> on <paramref name="stream"/>.</returns>
    public static ParseResult <TResult> Parse <TItem, TResult>(this Parser <TItem, TResult> parser, IPeekableStream <TItem> stream)
    {
        var result = parser(stream, 0);

        if (result.IsOk)
        {
            stream.Consume(result.Ok.Offset);
        }
        return(result);
    }
예제 #3
0
 /// <summary>
 /// Peeks ahead a given amount of items without consuming them. With <paramref name="offset"/> set to 0
 /// this is equivalent to <see cref="Peek"/>.
 /// </summary>
 /// <typeparam name="TItem">The item type of the stream.</typeparam>
 /// <param name="stream">The <see cref="IPeekableStream{TItem}"/> to peek in.</param>
 /// <param name="offset">The offset to look ahead.</param>
 /// <returns>The peeked item.</returns>
 public static TItem LookAhead <TItem>(this IPeekableStream <TItem> stream, int offset) => stream.TryLookAhead(offset, out var item)
     ? item
     : throw new InvalidOperationException("The stream had no more items.");