CanonSharp/CanonSharp.Combinator/Parsers/Bases/ResumeParser.cs
jackfiled 3ed8bf5d36
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
feat: Parser Combinator库和词法分析器 (#2)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/2
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-13 14:46:11 +08:00

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)));
}
}