CanonSharp/CanonSharp.Combinator/Abstractions/IParseResult.cs

44 lines
1.8 KiB
C#
Raw Normal View History

namespace CanonSharp.Combinator.Abstractions;
/// <summary>
/// 解析器结果
/// </summary>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="T">实际结果类型</typeparam>
public interface IParseResult<TToken, out T>
{
/// <summary>
/// 实际结果对象
/// </summary>
public T Value { get; }
/// <summary>
/// 在当前结果上应用下一个解析器
/// </summary>
/// <param name="nextParser">下一个解析器的函数</param>
/// <param name="continuation">处理解析结果的后继函数</param>
/// <typeparam name="TNext">下一个解析器函数返回的解析结果类型</typeparam>
/// <typeparam name="TResult">最终的解析结果类型</typeparam>
/// <returns></returns>
internal IParseResult<TToken, TResult> Next<TNext, TResult>(Func<T, IParser<TToken, TNext>> nextParser,
Func<IParseResult<TToken, TNext>, IParseResult<TToken, TResult>> continuation);
/// <summary>
/// 映射结果
/// </summary>
/// <param name="map">映射结果的函数</param>
/// <typeparam name="TResult">映射结果函数返回解析结果的类型</typeparam>
/// <returns>最终的解析结果</returns>
public IParseResult<TToken, TResult> Map<TResult>(Func<T, TResult> map);
/// <summary>
/// 在成功或者失败解析结果上应用不同的后继函数
/// </summary>
/// <param name="successfulHandler">在成功解析结果上应用的函数</param>
/// <param name="failedHandler">在失败解析结构上应用的函数</param>
/// <typeparam name="TResult">最后返回解析结果的类型</typeparam>
/// <returns>最后的解析结果</returns>
public TResult CaseOf<TResult>(Func<ISuccessfulResult<TToken, T>, TResult> successfulHandler,
Func<IFailedResult<TToken, T>, TResult> failedHandler);
}