using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Parsers.Modifiers;
///
/// 尝试的解析器
/// 当成功时直接返回原结果
/// 当失败时调用resume函数处理失败结果并返回成功结果
///
/// 上游解析器
/// 处理失败结果的恢复函数
/// 输入流令牌
/// 解析器返回结果类型
internal sealed class TryParser(IParser parser, Func, T> resume)
: ModifiedParser(parser)
{
protected override IParseResult Succeed(TState state,
ISuccessfulResult successfulResult)
=> successfulResult;
protected override IParseResult Fail(TState state, IFailedResult failedResult)
=> ParseResultBuilder.Succeed(resume(failedResult), state);
}