2024-08-13 14:46:11 +08:00
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator.Parsers.Primitives;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 满足指定条件即成功的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="predicate">满足的条件谓词</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
internal sealed class SatisfyParser<TToken>(Func<TToken, bool> predicate) : PrimitiveParser<TToken, TToken>
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
protected override IParseResult<TToken, TToken> Run<TState>(TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
{
|
|
|
|
return state.HasValue && predicate(state.Current)
|
|
|
|
? ParseResultBuilder.Succeed<TToken, TState, TToken>(state.Current, state.Next)
|
|
|
|
: ParseResultBuilder.Fail<TToken, TState, TToken>(state);
|
|
|
|
}
|
|
|
|
}
|