25 lines
980 B
C#
25 lines
980 B
C#
|
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>(
|
||
|
Parser<TToken, T> parser,
|
||
|
Func<FailedResult<TToken, T>, Parser<TToken, T>> failedHandler) : Parser<TToken, T>
|
||
|
{
|
||
|
internal override ParseResult<TToken, TResult> Run<TState, TResult>(TState state,
|
||
|
Func<ParseResult<TToken, T>, ParseResult<TToken, TResult>> continuation)
|
||
|
{
|
||
|
return parser.Run(state,
|
||
|
result => result.CaseOf(continuation,
|
||
|
failedResult => failedHandler(failedResult).Run(state, continuation)));
|
||
|
}
|
||
|
}
|