19 lines
705 B
C#
19 lines
705 B
C#
|
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>
|
||
|
{
|
||
|
protected override ParseResult<TToken, TToken> Run<TState>(TState state)
|
||
|
{
|
||
|
return state.HasValue && predicate(state.Current)
|
||
|
? ParseResultBuilder.Succeed<TToken, TState, TToken>(state.Current, state.Next)
|
||
|
: ParseResultBuilder.Fail<TToken, TState, TToken>(state);
|
||
|
}
|
||
|
}
|