CanonSharp/CanonSharp.Combinator/Results/InternalSuccessfulResult.cs

28 lines
1.1 KiB
C#
Raw Normal View History

using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Results;
/// <summary>
/// 实际实现的解析成功结果
/// </summary>
/// <param name="result">解析结果</param>
/// <param name="state">解析成功之后的下一个输入流状态</param>
/// <typeparam name="TToken">输入流类型</typeparam>
/// <typeparam name="TState">输入流的状态类型</typeparam>
/// <typeparam name="T">解析结果的类型</typeparam>
internal sealed class InternalSuccessfulResult<TToken, TState, T>(T result, TState state)
: ISuccessfulResult<TToken, T>
where TState : IReadState<TToken, TState>
{
public T Value => result;
public IParseResult<TToken, TResult> RunNext<TNext, TResult>(IParser<TToken, TNext> parser,
Func<IParseResult<TToken, TNext>, IParseResult<TToken, TResult>> continuation)
=> parser.Run(state, continuation);
public IParseResult<TToken, TResult> Map<TResult>(Func<T, TResult> map)
=> new InternalSuccessfulResult<TToken, TState, TResult>(map(Value), state);
public override string ToString() => Value?.ToString() ?? string.Empty;
}