using CanonSharp.Combinator.Extensions; namespace CanonSharp.Combinator.Abstractions; /// /// 解析器抽象基类 /// /// 输入流类型 /// 解析结果类型 public interface IParser { /// /// 解析器运行函数 /// /// 解析的输入流状态 /// 运行之后的后继函数 /// 输入流状态类型 /// 后继函数运行之后的解析结果类型 /// internal IParseResult Run(TState state, Func, IParseResult> continuation) where TState : IReadState; public IParseResult Parse(TState state) where TState : IReadState { return Run(state); } private IParseResult Run(TState state) where TState : IReadState { try { return Run(state, result => result); } catch (Exception e) { return ParseResultBuilder.Fail(e, state); } } public static IParser operator |(IParser a, IParser b) => a.Alternative(b); }