2024-08-13 14:46:11 +08:00
|
|
|
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)
|
2024-08-18 12:01:27 +08:00
|
|
|
: ISuccessfulResult<TToken, T>
|
2024-08-13 14:46:11 +08:00
|
|
|
where TState : IReadState<TToken, TState>
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
public T Value => result;
|
|
|
|
|
|
|
|
public IParseResult<TToken, TResult> RunNext<TNext, TResult>(IParser<TToken, TNext> parser,
|
|
|
|
Func<IParseResult<TToken, TNext>, IParseResult<TToken, TResult>> continuation)
|
2024-08-13 14:46:11 +08:00
|
|
|
=> parser.Run(state, continuation);
|
|
|
|
|
2024-08-18 12:01:27 +08:00
|
|
|
public IParseResult<TToken, TResult> Map<TResult>(Func<T, TResult> map)
|
2024-08-13 14:46:11 +08:00
|
|
|
=> new InternalSuccessfulResult<TToken, TState, TResult>(map(Value), state);
|
2024-08-18 12:01:27 +08:00
|
|
|
|
|
|
|
public override string ToString() => Value?.ToString() ?? string.Empty;
|
2024-08-13 14:46:11 +08:00
|
|
|
}
|