CanonSharp/CanonSharp.Combinator/Parsers/Primitives/PureParser.cs

28 lines
1.1 KiB
C#
Raw Normal View History

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>
{
protected override IParseResult<TToken, T> Run<TState>(TState state)
=> 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>
{
protected override IParseResult<TToken, T> Run<TState>(TState state)
=> ParseResultBuilder.Succeed<TToken, TState, T>(valueFunc(state), state);
}