CanonSharp/CanonSharp.Combinator/Text/TextParserBuilder.cs

124 lines
4.5 KiB
C#
Raw Permalink Normal View History

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