Example #1
0
        /// <summary>
        /// Removes empty Groupings and replaces single element groupings
        /// with their child elements.
        /// </summary>
        /// <returns>This, or if there is only 1 other element, that element
        /// with its link to this removed and made to this's parent</returns>
        public Rule Simplify(Grouping group)
        {
            Rule rule;
            Rule simplified = null;

            // simplify child groups.
            for (int i = group.Count - 1; i >= 0; i--)
            {
                rule = group[i];
                if (rule is Grouping)
                {
                    Simplify((Grouping)rule);
                }
            }

            if (group.Count == 1 && group.Quantifier == group[0].Quantifier)
            {
                simplified = group.RemoveAt(0);
            }
            else if (group.Count == 0)
            {
                simplified = new Empty();
            }

            if (simplified != null)
            {
                simplified.ProductionName = group.ProductionName;
                simplified.IsException    = group.IsException;

                if (group.Parent != null)
                {
                    group.Parent.Replace(group, simplified);
                }
                else
                {
                    simplified.Parent = null;
                }
                return(simplified);
            }

            return(group);
        }