2024-08-13 14:46:11 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
using CanonSharp.Combinator.Results;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <see cref="T:CanonSharp.Combinator.Abstractions.ParseResult"/> 相关的扩展方法
|
|
|
|
/// </summary>
|
|
|
|
public static class ParseResultBuilder
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 生成解析成功的结果
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">解析成功的对象</param>
|
|
|
|
/// <param name="state">下一个输入流状态</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="TState">输入流状态类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析成功的对象类型</typeparam>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-14 19:41:10 +08:00
|
|
|
public static IParseResult<TToken, T> Succeed<TToken, TState, T>(T value, TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
where TState : IReadState<TToken, TState>
|
|
|
|
=> new InternalSuccessfulResult<TToken, TState, T>(value, state);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 生成错误类型的解析失败结果
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">解析的输入流状态</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="TState">输入流状态类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析成功的对象类型</typeparam>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-14 19:41:10 +08:00
|
|
|
public static IParseResult<TToken, T> Fail<TToken, TState, T>(TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
where TState : IReadState<TToken, TState>
|
|
|
|
=> new FailedResultWithError<TToken, TState, T>(state);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 生成消息类型的解析失败结果
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">错误消息</param>
|
|
|
|
/// <param name="state">输入流状态</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="TState">输入流状态类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析成功的对象类型</typeparam>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-14 19:41:10 +08:00
|
|
|
public static IParseResult<TToken, T> Fail<TToken, TState, T>(string message, TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
where TState : IReadState<TToken, TState>
|
|
|
|
=> new FailedResultWithMessage<TToken, TState, T>(message, state);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 生成异常类型的解析失败结果
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="exception">解析异常</param>
|
|
|
|
/// <param name="state">输入流状态</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="TState">输入流状态类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析成功的对象类型</typeparam>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-14 19:41:10 +08:00
|
|
|
public static IParseResult<TToken, T> Fail<TToken, TState, T>(Exception exception, TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
where TState : IReadState<TToken, TState>
|
|
|
|
=> new FailedResultWithException<TToken, TState, T>(exception, state);
|
|
|
|
}
|