using System.Runtime.CompilerServices;
using CanonSharp.Combinator.Abstractions;
using CanonSharp.Combinator.Extensions;
using static CanonSharp.Combinator.ParserBuilder;
namespace CanonSharp.Combinator.Text;
public static class TextParserBuilder
{
///
/// 识别单个字符
///
/// 识别的单个字符
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser Char(char token) => Satisfy(x => x == token);
///
/// 忽略大小写识别单个字符
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser CharIgnoreCase(char token) =>
Satisfy(x => char.ToUpperInvariant(x) == char.ToUpperInvariant(token));
///
/// 识别提供字符串中的一个字符
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser OneOf(string candidate) => Satisfy(candidate.Contains);
///
/// 忽略大小写识别字符串中的一个字符
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser OneOfIgnoreCase(string candidate) =>
Satisfy(x => candidate.Contains(x, StringComparison.OrdinalIgnoreCase));
///
/// 识别一个字符串
///
/// 识别的字符串
/// 字符串比较方法
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser String(string except, StringComparison comparison) =>
new StringParser(except, comparison);
///
/// 识别一个字符串
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser String(string except) => String(except, StringComparison.Ordinal);
///
/// 忽略大小写识别一个字符串
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser StringIgnoreCase(string except) =>
String(except, StringComparison.OrdinalIgnoreCase);
///
/// 识别范围内的所有字符
///
/// 包括的起始字符
/// 包括的终止字符
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser Range(char start, char end) => Satisfy(x => x >= start && x <= end);
///
/// 识别Unicode字符类别的解析器
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser Letter() => Satisfy(char.IsLetter);
///
/// 识别Unicode数字类别的解析器
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser Digit() => Satisfy(char.IsDigit);
///
/// 识别ASCII字符类别的解析器
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser AsciiLetter() =>
Satisfy(x => x is >= 'a' and <= 'z' or >= 'A' and <= 'Z');
///
/// 识别ASCII数字类别的解析器
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser AsciiDigit() => Satisfy(x => x is >= '0' and <= '9');
///
/// 识别Unicode空白类型的字符
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser Space() => Satisfy(char.IsWhiteSpace);
///
/// 识别所有的换行符
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Parser LineBreak() =>
OneOf("\u000D\u000A\u0085\u2028\u2029\n").Map(x => x.ToString()) | String("\r\n");
}