CanonSharp/CanonSharp.Combinator/Parsers/Bases/MapParser.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

23 lines
885 B
C#

using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Parsers.Bases;
/// <summary>
/// 映射解析器
/// 提供一个函数修改上游解析器返回的结果
/// </summary>
/// <param name="parser">上游解析器</param>
/// <param name="func">修改上游解析器返回结果的</param>
/// <typeparam name="TToken"></typeparam>
/// <typeparam name="TIntermediate"></typeparam>
/// <typeparam name="T"></typeparam>
internal sealed class MapParser<TToken, TIntermediate, T>(
IParser<TToken, TIntermediate> parser,
Func<TIntermediate, T> func) : 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 => continuation(result.Map(func)));
}