/// <summary> /// Creates a parser which parses and returns a single character, in a case insensitive manner. /// The parser returns the actual character parsed. /// </summary> /// <param name="character">The character to parse</param> /// <returns>A parser which parses and returns a single character</returns> public static Parser <char, char> CIChar(char character) { var theChar = char.ToLowerInvariant(character); var expected = ImmutableSortedSet.CreateRange( new[] { new Expected <char>(Rope.Create(char.ToUpperInvariant(character))), new Expected <char>(Rope.Create(char.ToLowerInvariant(character))) } ); return(Token(c => char.ToLowerInvariant(c) == theChar) .WithExpected(expected)); }
/// <summary> /// Creates a parser which parses and returns one of the specified characters, in a case insensitive manner. /// The parser returns the actual character parsed. /// </summary> /// <param name="chars">A sequence of characters to choose between</param> /// <returns>A parser which parses and returns one of the specified characters, in a case insensitive manner.</returns> public static Parser <char, char> CIOneOf(IEnumerable <char> chars) { if (chars == null) { throw new ArgumentNullException(nameof(chars)); } var cs = chars.Select(char.ToLowerInvariant).ToArray(); var builder = ImmutableSortedSet.CreateBuilder <Expected <char> >(); foreach (var c in cs) { builder.Add(new Expected <char>(Rope.Create(char.ToLowerInvariant(c)))); builder.Add(new Expected <char>(Rope.Create(char.ToUpperInvariant(c)))); } return(Parser <char> .Token(c => Array.IndexOf(cs, char.ToLowerInvariant(c)) != -1) .WithExpected(builder.ToImmutable())); }
public TokenParser(TToken token) : base(ImmutableSortedSet.Create(new Expected <TToken>(Rope.Create(token)))) { _token = token; }
public static Rope <T> CreateRange <T>(IEnumerable <T> items) => Rope <T> .Create(items.ToImmutableArray());
public static Rope <T> Create <T>(T item) => Rope <T> .Create(ImmutableArray.Create(item));
private protected override ImmutableSortedSet <Expected <TToken> > CalculateExpected() => ImmutableSortedSet.Create(new Expected <TToken>(Rope.Create(_token)));
/// <summary> /// Creates a parser which parses and returns one of the specified characters. /// </summary> /// <param name="chars">A sequence of characters to choose between</param> /// <returns>A parser which parses and returns one of the specified characters</returns> public static Parser <char, char> OneOf(IEnumerable <char> chars) { if (chars == null) { throw new ArgumentNullException(nameof(chars)); } var cs = chars.ToArray(); return(Parser <char> .Token(c => Array.IndexOf(cs, c) != -1) .WithExpected(ImmutableSortedSet.CreateRange(cs.Select(c => new Expected <char>(Rope.Create(c)))))); }