CanonSharp/CanonSharp.Combinator/Abstractions/SuccessfulResult.cs

34 lines
1.5 KiB
C#
Raw Normal View History

namespace CanonSharp.Combinator.Abstractions;
/// <summary>
/// 成功解析结果基类
/// </summary>
/// <param name="value">实际的解析结果</param>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="T">实际的解析结果类型</typeparam>
public abstract class SuccessfulResult<TToken, T>(T value) : ParseResult<TToken, T>
{
public override T Value => value;
/// <summary>
/// 运行下一个解析器
/// </summary>
/// <param name="parser">下一个解析器</param>
/// <param name="continuation">处理解析结果的后继函数</param>
/// <typeparam name="TNext">下一个解析器返回的结果类型</typeparam>
/// <typeparam name="TResult">最终的结果类型</typeparam>
/// <returns>最终的结果</returns>
protected abstract ParseResult<TToken, TResult> RunNext<TNext, TResult>(Parser<TToken, TNext> parser,
Func<ParseResult<TToken, TNext>, ParseResult<TToken, TResult>> continuation);
internal override ParseResult<TToken, TResult> Next<TNext, TResult>(Func<T, Parser<TToken, TNext>> nextParser,
Func<ParseResult<TToken, TNext>, ParseResult<TToken, TResult>> continuation)
=> RunNext(nextParser(Value), continuation);
public override TResult CaseOf<TResult>(Func<SuccessfulResult<TToken, T>, TResult> successfulHandler,
Func<FailedResult<TToken, T>, TResult> failedHandler)
=> successfulHandler(this);
public override string ToString() => Value?.ToString() ?? string.Empty;
}