28 lines
1.2 KiB
C#
28 lines
1.2 KiB
C#
|
using CanonSharp.Combinator.Abstractions;
|
||
|
|
||
|
namespace CanonSharp.Combinator.Parsers;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 修改解析器返回结果的解析器基类
|
||
|
/// </summary>
|
||
|
/// <param name="parser">需要修改结果的解析器</param>
|
||
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
||
|
/// <typeparam name="TIntermediate">需要修改结果的解析器</typeparam>
|
||
|
/// <typeparam name="T">最终返回的解析结果</typeparam>
|
||
|
public abstract class ModifiedParser<TToken, TIntermediate, T>(Parser<TToken, TIntermediate> parser) : Parser<TToken, T>
|
||
|
{
|
||
|
protected abstract ParseResult<TToken, T> Fail<TState>(TState state,
|
||
|
FailedResult<TToken, TIntermediate> failedResult)
|
||
|
where TState : IReadState<TToken, TState>;
|
||
|
|
||
|
protected abstract ParseResult<TToken, T> Succeed<TState>(TState state,
|
||
|
SuccessfulResult<TToken, TIntermediate> successfulResult)
|
||
|
where TState : IReadState<TToken, TState>;
|
||
|
|
||
|
internal override ParseResult<TToken, TResult> Run<TState, TResult>(TState state,
|
||
|
Func<ParseResult<TToken, T>, ParseResult<TToken, TResult>> continuation)
|
||
|
=> parser.Run(state, result => result.CaseOf(
|
||
|
success => continuation(Succeed(state, success)),
|
||
|
failure => continuation(Fail(state, failure))));
|
||
|
}
|