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