2024-08-13 14:46:11 +08:00
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator.Parsers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 解析器原型基类
|
|
|
|
/// 实际上就是处理了一个后继调用
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TToken">输入流类型</typeparam>
|
|
|
|
/// <typeparam name="T">解析结果的类型</typeparam>
|
2024-08-14 19:41:10 +08:00
|
|
|
public abstract class PrimitiveParser<TToken, T> : IParser<TToken, T>
|
2024-08-13 14:46:11 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 运行解析器 返回解析结果
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">当前输入流的状态</param>
|
|
|
|
/// <typeparam name="TState">输入流状态的类型</typeparam>
|
|
|
|
/// <returns>解析结果</returns>
|
2024-08-14 19:41:10 +08:00
|
|
|
protected abstract IParseResult<TToken, T> Run<TState>(TState state)
|
2024-08-13 14:46:11 +08:00
|
|
|
where TState : IReadState<TToken, TState>;
|
|
|
|
|
2024-08-14 19:41:10 +08:00
|
|
|
public IParseResult<TToken, TResult> Run<TState, TResult>(TState state,
|
|
|
|
Func<IParseResult<TToken, T>, IParseResult<TToken, TResult>> continuation)
|
|
|
|
where TState : IReadState<TToken, TState>
|
2024-08-13 14:46:11 +08:00
|
|
|
=> continuation(Run(state));
|
|
|
|
}
|