using CanonSharp.Combinator.Abstractions;
using CanonSharp.Combinator.Extensions;
namespace CanonSharp.Combinator.Parsers.Primitives;
///
/// 解析指定数量的解析器
///
/// 需要解析的数量
/// 输入流类型
internal sealed class TakeParser(int count) : PrimitiveParser>
{
protected override IParseResult> Run(TState state)
{
List result = state.AsEnumerable().Take(count).ToList();
return result.Count == count
? ParseResultBuilder.Succeed>(result.Select(s => s.Current),
result.Count == 0 ? state : result.Last().Next)
: ParseResultBuilder.Fail>("An input does not have required length.",
state);
}
}