CanonSharp/CanonSharp.Combinator/Results/InternalSuccessfulResult.cs
jackfiled cf19f8197e feat: Grammar Parser (#3)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/3
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-18 12:01:27 +08:00

28 lines
1.1 KiB
C#

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;
}