feat: Parser Combinator库和词法分析器 (#2)
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
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>
This commit is contained in:
23
CanonSharp.Combinator/Parsers/Primitives/TakeParser.cs
Normal file
23
CanonSharp.Combinator/Parsers/Primitives/TakeParser.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
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 TakeParser<TToken>(int count) : PrimitiveParser<TToken, IEnumerable<TToken>>
|
||||
{
|
||||
protected override ParseResult<TToken, IEnumerable<TToken>> Run<TState>(TState state)
|
||||
{
|
||||
List<TState> result = state.AsEnumerable<TToken, TState>().Take(count).ToList();
|
||||
|
||||
return result.Count == count
|
||||
? ParseResultBuilder.Succeed<TToken, TState, IEnumerable<TToken>>(result.Select(s => s.Current),
|
||||
result.Count == 0 ? state : result.Last().Next)
|
||||
: ParseResultBuilder.Fail<TToken, TState, IEnumerable<TToken>>("An input does not have required length.",
|
||||
state);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user