2024-08-13 14:46:11 +08:00
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator.Parsers.Bases;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 恢复解析器
|
|
|
|
/// 在上游解析器失败的情况下调用指定恢复函数返回的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="parser">上游解析器</param>
|
|
|
|
/// <param name="failedHandler">返回新解析器的恢复函数</param>
|
|
|
|
/// <typeparam name="TToken">输入令牌类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析结果类型</typeparam>
|
|
|
|
internal sealed class ResumeParser<TToken, T>(
|
2024-08-18 12:01:27 +08:00
|
|
|
IParser<TToken, T> parser,
|
|
|
|
Func<IFailedResult<TToken, T>, IParser<TToken, T>> failedHandler) : IParser<TToken, T>
|
2024-08-13 14:46:11 +08:00
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
public IParseResult<TToken, TResult> Run<TState, TResult>(TState state,
|
|
|
|
Func<IParseResult<TToken, T>, IParseResult<TToken, TResult>> continuation)
|
|
|
|
where TState : IReadState<TToken, TState>
|
2024-08-13 14:46:11 +08:00
|
|
|
{
|
|
|
|
return parser.Run(state,
|
|
|
|
result => result.CaseOf(continuation,
|
|
|
|
failedResult => failedHandler(failedResult).Run(state, continuation)));
|
|
|
|
}
|
|
|
|
}
|