CanonSharp/CanonSharp.Combinator/Parsers/Bases/BindParser.cs
jackfiled cf19f8197e feat: Grammar Parser (#3)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/3
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-18 12:01:27 +08:00

22 lines
905 B
C#

using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Parsers.Bases;
/// <summary>
/// 单子解析器
/// </summary>
/// <param name="parser">上游解析器</param>
/// <param name="next">下游解析器生成函数</param>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="TIntermediate">上游解析器结果类型</typeparam>
/// <typeparam name="T">下游解析器结果类型</typeparam>
internal sealed class BindParser<TToken, TIntermediate, T>(
IParser<TToken, TIntermediate> parser,
Func<TIntermediate, IParser<TToken, T>> next) : 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>
=> parser.Run(state, result => result.Next(next, continuation));
}