2024-08-13 14:46:11 +08:00
|
|
|
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
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 识别单个字符
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="token">识别的单个字符</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> Char(char token) => Satisfy<char>(x => x == token);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 忽略大小写识别单个字符
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="token"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> CharIgnoreCase(char token) =>
|
2024-08-13 14:46:11 +08:00
|
|
|
Satisfy<char>(x => char.ToUpperInvariant(x) == char.ToUpperInvariant(token));
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别提供字符串中的一个字符
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="candidate"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> OneOf(string candidate) => Satisfy<char>(candidate.Contains);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 忽略大小写识别字符串中的一个字符
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="candidate"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> OneOfIgnoreCase(string candidate) =>
|
2024-08-13 14:46:11 +08:00
|
|
|
Satisfy<char>(x => candidate.Contains(x, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别一个字符串
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="except">识别的字符串</param>
|
|
|
|
/// <param name="comparison">字符串比较方法</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, string> String(string except, StringComparison comparison) =>
|
2024-08-13 14:46:11 +08:00
|
|
|
new StringParser(except, comparison);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别一个字符串
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="except"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, string> String(string except) => String(except, StringComparison.Ordinal);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 忽略大小写识别一个字符串
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="except"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, string> StringIgnoreCase(string except) =>
|
2024-08-13 14:46:11 +08:00
|
|
|
String(except, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别范围内的所有字符
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="start">包括的起始字符</param>
|
|
|
|
/// <param name="end">包括的终止字符</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> Range(char start, char end) => Satisfy<char>(x => x >= start && x <= end);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别Unicode字符类别的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> Letter() => Satisfy<char>(char.IsLetter);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别Unicode数字类别的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> Digit() => Satisfy<char>(char.IsDigit);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别ASCII字符类别的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> AsciiLetter() =>
|
2024-08-13 14:46:11 +08:00
|
|
|
Satisfy<char>(x => x is >= 'a' and <= 'z' or >= 'A' and <= 'Z');
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别ASCII数字类别的解析器
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> AsciiDigit() => Satisfy<char>(x => x is >= '0' and <= '9');
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别Unicode空白类型的字符
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, char> Space() => Satisfy<char>(char.IsWhiteSpace);
|
2024-08-13 14:46:11 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 识别所有的换行符
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-08-18 12:01:27 +08:00
|
|
|
public static IParser<char, string> LineBreak() =>
|
2024-08-13 14:46:11 +08:00
|
|
|
OneOf("\u000D\u000A\u0085\u2028\u2029\n").Map(x => x.ToString()) | String("\r\n");
|
|
|
|
}
|