using CanonSharp.Combinator.Abstractions;

namespace CanonSharp.Combinator.Parsers.Primitives;

/// <summary>
/// 满足指定条件即成功的解析器
/// </summary>
/// <param name="predicate">满足的条件谓词</param>
/// <typeparam name="TToken">输入流类型</typeparam>
internal sealed class SatisfyParser<TToken>(Func<TToken, bool> predicate) : PrimitiveParser<TToken, TToken>
{
    protected override IParseResult<TToken, TToken> Run<TState>(TState state)
    {
        return state.HasValue && predicate(state.Current)
            ? ParseResultBuilder.Succeed<TToken, TState, TToken>(state.Current, state.Next)
            : ParseResultBuilder.Fail<TToken, TState, TToken>(state);
    }
}