CanonSharp/CanonSharp.Combinator/Abstractions/IFailedResult.cs
2024-08-14 19:41:10 +08:00

48 lines
1.6 KiB
C#

namespace CanonSharp.Combinator.Abstractions;
/// <summary>
/// 失败解析结果基类
/// </summary>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="T">解析结果类型</typeparam>
public interface IFailedResult<TToken, out T> : IParseResult<TToken, T>
{
T IParseResult<TToken, T>.Value => throw Exception;
/// <summary>
/// 当前读取到的状态
/// </summary>
public IReadState<TToken> State { get; }
/// <summary>
/// 解析失败的消息
/// </summary>
public string Message { get; }
/// <summary>
/// 解析失败的异常
/// </summary>
public ParseException Exception => new(ToString());
/// <summary>
/// 转换该失败结果的类型
/// </summary>
/// <typeparam name="TNext">转换之后的结果类型</typeparam>
/// <returns>转换之后的失败解析类型</returns>
public IFailedResult<TToken, TNext> Convert<TNext>();
IParseResult<TToken, TResult> IParseResult<TToken, T>.Next<TNext, TResult>(
Func<T, IParser<TToken, TNext>> nextParser,
Func<IParseResult<TToken, TNext>, IParseResult<TToken, TResult>> continuation)
=> continuation(Convert<TNext>());
IParseResult<TToken, TResult> IParseResult<TToken, T>.Map<TResult>(Func<T, TResult> map)
=> Convert<TResult>();
TResult IParseResult<TToken, T>.CaseOf<TResult>(Func<ISuccessfulResult<TToken, T>, TResult> successfulHandler,
Func<IFailedResult<TToken, T>, TResult> failedHandler)
=> failedHandler(this);
string IParseResult<TToken, T>.ToString() => $"Parse Failed: {Message}";
}