namespace CanonSharp.Combinator.Abstractions;

/// <summary>
/// 成功解析结果基类
/// </summary>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="T">实际的解析结果类型</typeparam>
public interface ISuccessfulResult<TToken,out T> : IParseResult<TToken, T>
{
    /// <summary>
    /// 运行下一个解析器
    /// </summary>
    /// <param name="parser">下一个解析器</param>
    /// <param name="continuation">处理解析结果的后继函数</param>
    /// <typeparam name="TNext">下一个解析器返回的结果类型</typeparam>
    /// <typeparam name="TResult">最终的结果类型</typeparam>
    /// <returns>最终的结果</returns>
    protected IParseResult<TToken, TResult> RunNext<TNext, TResult>(IParser<TToken, TNext> parser,
        Func<IParseResult<TToken, TNext>, IParseResult<TToken, TResult>> continuation);

    IParseResult<TToken, TResult> IParseResult<TToken, T>.Next<TNext, TResult>(
        Func<T, IParser<TToken, TNext>> nextParser,
        Func<IParseResult<TToken, TNext>, IParseResult<TToken, TResult>> continuation)
        => RunNext(nextParser(Value), continuation);

    TResult IParseResult<TToken, T>.CaseOf<TResult>(Func<ISuccessfulResult<TToken, T>, TResult> successfulHandler,
        Func<IFailedResult<TToken, T>, TResult> failedHandler)
        => successfulHandler(this);
}