2024-08-13 14:46:11 +08:00
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator.Parsers.Primitives;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 直接成功的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">解析成功返回的值</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析成功返回值的类型</typeparam>
|
|
|
|
internal sealed class PureParser<TToken, T>(T value) : PrimitiveParser<TToken, T>
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
protected override IParseResult<TToken, T> Run<TState>(TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
=> ParseResultBuilder.Succeed<TToken, TState, T>(value, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 按照输入状态返回结果的始终成功解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="valueFunc">按照输入状态返回解析结果的函数</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析成功返回值的类型</typeparam>
|
|
|
|
internal sealed class DelayedPureParser<TToken, T>(Func<IReadState<TToken>, T> valueFunc) : PrimitiveParser<TToken, T>
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
protected override IParseResult<TToken, T> Run<TState>(TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
=> ParseResultBuilder.Succeed<TToken, TState, T>(valueFunc(state), state);
|
|
|
|
}
|