CanonSharp/CanonSharp.Combinator/Abstractions/ParseResult.cs

44 lines
1.9 KiB
C#

namespace CanonSharp.Combinator.Abstractions;
/// <summary>
/// 解析器结果
/// </summary>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="T">实际结果类型</typeparam>
public abstract class ParseResult<TToken, T>
{
/// <summary>
/// 实际结果对象
/// </summary>
public abstract T Value { get; }
/// <summary>
/// 在当前结果上应用下一个解析器
/// </summary>
/// <param name="nextParser">下一个解析器的函数</param>
/// <param name="continuation">处理解析结果的后继函数</param>
/// <typeparam name="TNext">下一个解析器函数返回的解析结果类型</typeparam>
/// <typeparam name="TResult">最终的解析结果类型</typeparam>
/// <returns></returns>
internal abstract ParseResult<TToken, TResult> Next<TNext, TResult>(Func<T, Parser<TToken, TNext>> nextParser,
Func<ParseResult<TToken, TNext>, ParseResult<TToken, TResult>> continuation);
/// <summary>
/// 映射结果
/// </summary>
/// <param name="map">映射结果的函数</param>
/// <typeparam name="TResult">映射结果函数返回解析结果的类型</typeparam>
/// <returns>最终的解析结果</returns>
public abstract ParseResult<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 abstract TResult CaseOf<TResult>(Func<SuccessfulResult<TToken, T>, TResult> successfulHandler,
Func<FailedResult<TToken, T>, TResult> failedHandler);
}