CanonSharp/CanonSharp.Combinator/Parsers/Bases/NextParser.cs

28 lines
1.2 KiB
C#
Raw Permalink Normal View History

using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Parsers.Bases;
/// <summary>
/// 下一步解析器
/// </summary>
/// <param name="parser">上游解析器</param>
/// <param name="successfulParser">成功情况下的解析器函数</param>
/// <param name="failedParser">失败情况下的解析器函数</param>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="TIntermediate">上游解析器结果类型</typeparam>
/// <typeparam name="T">最终解析结果类型</typeparam>
internal sealed class NextParser<TToken, TIntermediate, T>(
IParser<TToken, TIntermediate> parser,
Func<TIntermediate, IParser<TToken, T>> successfulParser,
Func<IFailedResult<TToken, TIntermediate>, IParser<TToken, T>> failedParser) : IParser<TToken, T>
{
public IParseResult<TToken, TResult> Run<TState, TResult>(TState state,
Func<IParseResult<TToken, T>, IParseResult<TToken, TResult>> continuation)
where TState : IReadState<TToken, TState>
{
return parser.Run(state, result => result.CaseOf(
successfulResult => successfulResult.Next(successfulParser, continuation),
failedResult => failedParser(failedResult).Run(state, continuation)));
}
}