using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Parsers.Modifiers;
///
/// 成功映射的解析器
///
/// 上游解析器
/// 当上游成功时的处理函数
/// 当上游失败时的处理函数
/// 输入流类型
/// 上游解析器解析结果类型
/// 最终的解析结果类型
internal sealed class SuccessfulMapParser(
Parser parser,
Func successfulHandler,
Func, T> failedHandler) : ModifiedParser(parser)
{
protected override ParseResult Succeed(TState state,
SuccessfulResult successfulResult)
=> successfulResult.Map(successfulHandler);
protected override ParseResult Fail(TState state,
FailedResult failedResult)
=> ParseResultBuilder.Succeed(failedHandler(failedResult), state);
}