CanonSharp/CanonSharp.Combinator/Parsers/Bases/FixParser.cs
jackfiled 3ed8bf5d36
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
feat: Parser Combinator库和词法分析器 (#2)
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>
2024-08-13 14:46:11 +08:00

34 lines
1.1 KiB
C#

using CanonSharp.Combinator.Abstractions;
namespace CanonSharp.Combinator.Parsers.Bases;
/// <summary>
/// 修正?解析器
/// 感觉是一种递归的高级实现?
///
/// </summary>
/// <typeparam name="TToken"></typeparam>
/// <typeparam name="T"></typeparam>
internal sealed class FixParser<TToken, T> : Parser<TToken, T>
{
private readonly Parser<TToken, T> _parser;
public FixParser(Func<Parser<TToken, T>, Parser<TToken, T>> func)
{
_parser = func(this);
}
internal override ParseResult<TToken, TResult> Run<TState, TResult>(TState state,
Func<ParseResult<TToken, T>, ParseResult<TToken, TResult>> continuation)
=> _parser.Run(state, continuation);
}
internal sealed class FixParser<TToken, TParameter, T>(
Func<Func<TParameter, Parser<TToken, T>>, TParameter, Parser<TToken, T>> func,
TParameter parameter) : Parser<TToken, T>
{
internal override ParseResult<TToken, TResult> Run<TState, TResult>(TState state,
Func<ParseResult<TToken, T>, ParseResult<TToken, TResult>> continuation)
=> func(p => new FixParser<TToken, TParameter, T>(func, p), parameter).Run(state, continuation);
}