2024-08-13 14:46:11 +08:00
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
using CanonSharp.Combinator.Extensions;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator.Parsers.Primitives;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 跳过指定数量的输入令牌
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="count">需要跳过的令牌数量</param>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
internal sealed class SkipParser<TToken>(int count) : PrimitiveParser<TToken, Unit>
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
protected override IParseResult<TToken, Unit> Run<TState>(TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
{
|
|
|
|
List<TState> result = state.AsEnumerable<TToken, TState>().Take(count).ToList();
|
|
|
|
|
|
|
|
return result.Count == count
|
|
|
|
? ParseResultBuilder.Succeed<TToken, TState, Unit>(Unit.Instance,
|
|
|
|
result.Count == 0 ? state : result.Last().Next)
|
|
|
|
: ParseResultBuilder.Fail<TToken, TState, Unit>("An input does not have required length.", state);
|
|
|
|
}
|
|
|
|
}
|