CanonSharp/CanonSharp.Combinator/Parsers/Primitives/PureParser.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.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);
}