2024-08-14 19:41:10 +08:00
|
|
|
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>
|
2024-08-14 22:47:45 +08:00
|
|
|
public ParseException Exception => new(Message);
|
2024-08-14 19:41:10 +08:00
|
|
|
|
|
|
|
/// <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);
|
|
|
|
}
|