using System.Runtime.CompilerServices;
using CanonSharp.Combinator.Abstractions;
using CanonSharp.Combinator.Extensions;
using CanonSharp.Combinator.Parsers.Bases;
using CanonSharp.Combinator.Parsers.Primitives;
namespace CanonSharp.Combinator;
public static class ParserBuilder
{
#region PrimitiveParser
// 对应Parsers.Primitives命名空间下的Parser实现
///
/// 直接成功的解析器
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Pure(T value)
=> new PureParser(value);
///
/// 直接成功的解析器
///
/// 生成结果的函数
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Pure(Func, T> valueFunc)
=> new DelayedPureParser(valueFunc);
///
/// 生成空结果的解析器
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Null() => Pure(Unit.Instance);
///
/// 失败的解析器
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Fail() => new FailedParser();
///
/// 失败的解析器
///
/// 失败的原因
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Fail(string message) => new FailedParserWithMessage(message);
///
/// 失败的解析器
///
/// 产生失败原因的函数
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Fail(Func, string> messageFunc) =>
new FailedParserWithDelayedMessage(messageFunc);
///
/// 失败的解析器
///
/// 失败的异常
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Fail(Exception exception) =>
new FailedParserWithException(exception);
///
/// 满足指定条件的解析器
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Satisfy(Func predicate)
=> new SatisfyParser(predicate);
///
/// 识别任何输入的解析器
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Any() => Satisfy(_ => true);
///
/// 识别指定输入元素的解析器
///
/// 识别的指定元素
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Token(TToken token)
=> Satisfy(t => EqualityComparer.Default.Equals(t, token));
///
/// 跳过指定数量输入元素的解析器
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Skip(int count) => new SkipParser(count);
///
/// 识别指定数量输入元素的解析器
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser> Take(int count) => new TakeParser(count);
#endregion
#region Bases
///
/// 按照给定的函数修改解析器的解析器
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Fix(Func, IParser> parserFix)
=> new FixParser(parserFix);
#endregion
#region Combinators
///
/// 按照给定的解析器组依次尝试
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Choice(IEnumerable> parsers)
=> parsers.Aggregate((result, parser) => result.Alternative(parser));
///
/// 按照给定的解析器组依次尝试
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser Choice(params IParser[] parsers)
=> Choice(parsers.AsEnumerable());
///
/// 顺序应用所有输入的解析器
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser> Sequence(IEnumerable> parsers)
=> parsers.Reverse().Aggregate(Pure>([]),
(next, parser) => parser.Bind(
x => next.Map(result => result.Prepend(x))));
///
/// 顺序应用输入输入的解析器
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser> Sequence(params IParser[] parsers)
=> Sequence(parsers.AsEnumerable());
///
/// 识别输入令牌直到终止解析器运行成功
/// 在终止解析器之前可以存在零个或者多个输入令牌
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser> TakeTill(IParser terminator)
=> Any().ManyTill(terminator);
///
/// 识别输入令牌直到终止解析器运行成功
/// 在终止解析器之前至少存在一个输入令牌
///
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IParser> Take1Till(IParser termintor)
=> Any().Many1Till(termintor);
#endregion
}