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:
30
CanonSharp.Combinator/Parsers/Modifiers/DoParser.cs
Normal file
30
CanonSharp.Combinator/Parsers/Modifiers/DoParser.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using CanonSharp.Combinator.Abstractions;
|
||||
|
||||
namespace CanonSharp.Combinator.Parsers.Modifiers;
|
||||
|
||||
/// <summary>
|
||||
/// 对结果运行指定操作,但是不做修改操作的解析器
|
||||
/// </summary>
|
||||
/// <param name="parser">上游解析器</param>
|
||||
/// <param name="succeed">对成功结果的操作</param>
|
||||
/// <param name="fail">对失败结果的操作</param>
|
||||
/// <typeparam name="TToken">输入流类型</typeparam>
|
||||
/// <typeparam name="T">解析结果类型</typeparam>
|
||||
internal sealed class DoParser<TToken, T>(
|
||||
Parser<TToken, T> parser,
|
||||
Action<T> succeed,
|
||||
Action<FailedResult<TToken, T>> fail) : ModifiedParser<TToken, T, T>(parser)
|
||||
{
|
||||
protected override ParseResult<TToken, T> Succeed<TState>(TState state,
|
||||
SuccessfulResult<TToken, T> successfulResult)
|
||||
{
|
||||
succeed(successfulResult.Value);
|
||||
return successfulResult;
|
||||
}
|
||||
|
||||
protected override ParseResult<TToken, T> Fail<TState>(TState state, FailedResult<TToken, T> failedResult)
|
||||
{
|
||||
fail(failedResult);
|
||||
return failedResult;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user