CanonSharp/CanonSharp.Combinator/Parsers/Primitives/SatisfyParser.cs
jackfiled 3ed8bf5d36
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
feat: Parser Combinator库和词法分析器 (#2)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/2
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-13 14:46:11 +08:00

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);
}
}