Пример #1
0
        public override string ToGroupString(RegexGroupNode group)
        {
            if (!string.IsNullOrWhiteSpace(group.Name) && !string.IsNullOrWhiteSpace(group.Options))
            {
                throw new NotSupportedException($"Due to constraint from dotnet, property {nameof(group.Name)} and {nameof(group.Options)} of {nameof(RegexGroupNode)} cannot be set at the same time.");
            }

            return(base.ToGroupString(group));
        }
Пример #2
0
        /// <summary>
        /// Add an existing <see cref="RegexGroupNode"/> to the generator.
        /// </summary>
        /// <param name="group"><see cref="RegexGroupNode"/> to be added.</param>
        /// <returns><see cref="RegexGenerator"/></returns>
        /// <exception cref="NamedCapturingGroupNotSupportedException">Named capturing group is not supported by <see cref="RegexLanguage"/>.</exception>
        /// <exception cref="InlineGroupOptionsNotSupportedException">Inline group option is not supported by <see cref="RegexLanguage"/>.</exception>
        public RegexGenerator AddGroup(RegexGroupNode group)
        {
            if (!IsNamedCapturingGroupSupported && !string.IsNullOrWhiteSpace(group.Name))
            {
                throw new NamedCapturingGroupNotSupportedException(RegexLanguage);
            }
            if (!IsInlineGroupOptionsSupported && !string.IsNullOrWhiteSpace(group.Options))
            {
                throw new InlineGroupOptionsNotSupportedException(RegexLanguage);
            }

            return(Add(group));
        }
        public virtual string ToGroupString(RegexGroupNode group)
        {
            if (!string.IsNullOrWhiteSpace(group.Name) && !IsNamedCapturingGroupSupported)
            {
                throw new NamedCapturingGroupNotSupportedException(RegexLanguage);
            }
            if (group == null)
            {
                throw new ArgumentNullException(nameof(group));
            }

            var sb = new StringBuilder();

            sb.Append(ToTokenString(RegexToken.GroupOpen));
            if (!string.IsNullOrWhiteSpace(group.Name))
            {
                sb.Append(ToTokenString(RegexToken.GroupOptionStart))
                .Append(ToTokenString(RegexToken.GroupNameOpen))
                .Append(group.Name)
                .Append(ToTokenString(RegexToken.GroupNameClose));
            }
            else if (!string.IsNullOrWhiteSpace(group.Options))
            {
                sb.Append(ToTokenString(RegexToken.GroupOptionStart))
                .Append(group.Options)
                .Append(ToTokenString(RegexToken.GroupOptionEnd));
            }
            else if (!group.IsCapturingGroup)
            {
                sb.Append(ToTokenString(RegexToken.GroupNonCapturing));
            }
            sb.Append(group.IsInnerNodeIncluded ? ToString(group.InnerNode) : group.Pattern)
            .Append(ToTokenString(RegexToken.GroupClose));

            return(AddQuantifier(sb.ToString(), group.Minimum, group.Maximum, group.RegexQuantifierOption));
        }
Пример #4
0
        /// <summary>
        /// Create and add <see cref="RegexGroupNode"/> to the generator, and include another <see cref="RegexNode"/> inside.
        /// </summary>
        /// <param name="node">An existing <see cref="RegexNode"/> to be included.</param>
        /// <param name="capturing">Defines the group to be capturing or not.</param>
        /// <param name="name">Optional name of capture group.</param>
        /// <param name="options">Optional inline options for the group.</param>
        /// <param name="min">Optional minimum number of occurance.</param>
        /// <param name="max">Optional maximum number of occurance.</param>
        /// <param name="quantifierOption">Optional quantifier option.</param>
        /// <returns><see cref="RegexGenerator"/></returns>
        /// <exception cref="NamedCapturingGroupNotSupportedException">Named capturing group is not supported by <see cref="RegexLanguage"/>.</exception>
        /// <exception cref="InlineGroupOptionsNotSupportedException">Inline group option is not supported by <see cref="RegexLanguage"/>.</exception>
        public RegexGenerator AddGroup(RegexNode node, bool capturing = true, string name = null, string options = null, int?min = null, int?max = null, RegexQuantifierOption quantifierOption = RegexQuantifierOption.Greedy)
        {
            var groupNode = new RegexGroupNode(node.ToString(RegexLanguageStrategy.Stringifier), capturing, name, options, min, max, quantifierOption);

            return(AddGroup(groupNode));
        }
Пример #5
0
        /// <summary>
        /// Create and add <see cref="RegexGroupNode"/> to the generator.
        /// </summary>
        /// <param name="pattern">Regex pattern.</param>
        /// <param name="capturing">Defines the group to be capturing or not.</param>
        /// <param name="name">Optional name of capture group.</param>
        /// <param name="options">Optional inline options for the group.</param>
        /// <param name="min">Optional minimum number of occurance.</param>
        /// <param name="max">Optional maximum number of occurance.</param>
        /// <param name="quantifierOption">Optional quantifier option.</param>
        /// <returns><see cref="RegexGenerator"/></returns>
        /// <exception cref="NamedCapturingGroupNotSupportedException">Named capturing group is not supported by <see cref="RegexLanguage"/>.</exception>
        /// <exception cref="InlineGroupOptionsNotSupportedException">Inline group option is not supported by <see cref="RegexLanguage"/>.</exception>
        public RegexGenerator AddGroup(string pattern, bool capturing = true, string name = null, string options = null, int?min = null, int?max = null, RegexQuantifierOption quantifierOption = RegexQuantifierOption.Greedy)
        {
            var groupNode = new RegexGroupNode(pattern, capturing, name, options, min, max, quantifierOption);

            return(AddGroup(groupNode));
        }