22 lines
841 B
C#
22 lines
841 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>(
|
||
|
Parser<TToken, TIntermediate> parser,
|
||
|
Func<TIntermediate, T> func) : Parser<TToken, T>
|
||
|
{
|
||
|
internal override ParseResult<TToken, TResult> Run<TState, TResult>(TState state,
|
||
|
Func<ParseResult<TToken, T>, ParseResult<TToken, TResult>> continuation)
|
||
|
=> parser.Run(state, result => continuation(result.Map(func)));
|
||
|
}
|