jackfiled
cf19f8197e
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>
19 lines
706 B
C#
19 lines
706 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 IParseResult<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);
|
|
}
|
|
}
|