Example #1
0
        /// <summary>
        /// Creates a new character group with the given sub-expressions.
        /// </summary>
        /// <param name="isNegated">Indicates whether the character group should be negated.</param>
        /// <param name="expressions">The sub-expressions to add to the character group.</param>
        /// <returns>The character group.</returns>
        public static ICharacterGroup From(CharacterGroupOptions options, IEnumerable <ICharacterGroupMember> expressions)
        {
            if (expressions == null)
            {
                throw new ArgumentNullException(nameof(expressions));
            }
            var group = new CharacterGroup()
            {
                IsNegated  = options?.IsNegated ?? false,
                Exclusions = options?.Exclusions
            };

            foreach (var expression in expressions)
            {
                group.Add(expression);
            }
            return(group);
        }
Example #2
0
            private ICharacterGroup ParseCharacterGroupInternal()
            {
                ++index; // swallow '['
                var  members   = new List <ICharacterGroupMember>();
                bool isNegated = regex[index] == '^';

                if (isNegated)
                {
                    ++index; // swallow '^'
                }
                bool            isDone     = false;
                ICharacterGroup exclusions = null;

                while (!isDone)
                {
                    var nextChar = regex[index];
                    switch (nextChar)
                    {
                    case ']':
                        isDone = true;
                        ++index;     // swallow ']'
                        break;

                    case '-':
                        ++index;     // swallow '-'
                        if (index == regex.Length)
                        {
                            // The Regex constructor should prevent this
                            throw new InvalidOperationException();
                        }
                        else if (regex[index] == '[')
                        {
                            exclusions = ParseCharacterGroupInternal();
                        }
                        else if (members.Count == 0 || regex[index] == ']')
                        {
                            // If '-' is the first or last character, add it literally
                            members.Add(Literal.For('-'));
                        }
                        else
                        {
                            var lastMember = members[members.Count - 1];
                            var range      = ParseRange(lastMember);
                            members.RemoveAt(members.Count - 1);
                            members.Add(range);
                        }
                        break;

                    default:
                        var member = ParseCharacterGroupMember();
                        members.Add(member);
                        break;
                    }
                }
                var options = new CharacterGroupOptions()
                {
                    IsNegated = isNegated, Exclusions = exclusions
                };

                return(CharacterGroup.From(options, members));
            }