Пример #1
0
        internal override GrammarBuilderBase Clone()
        {
            RuleElement ruleElement = new RuleElement(_name);

            ruleElement.CloneItems(this);
            return(ruleElement);
        }
Пример #2
0
        internal SemanticKeyElement(string semanticKey)
        {
            _semanticKey = semanticKey;
            RuleElement ruleElement = new RuleElement(semanticKey);

            _ruleRef = new RuleRefElement(ruleElement, _semanticKey);
            base.Items.Add(ruleElement);
            base.Items.Add(_ruleRef);
        }
Пример #3
0
        protected void Optimize(Collection <RuleElement> newRules)
        {
            SortedDictionary <int, Collection <BuilderElements> > sortedDictionary = new SortedDictionary <int, Collection <BuilderElements> >();

            GetDictionaryElements(sortedDictionary);
            int[] array = new int[sortedDictionary.Keys.Count];
            int   num   = array.Length - 1;

            foreach (int key in sortedDictionary.Keys)
            {
                array[num--] = key;
            }
            for (int i = 0; i < array.Length && array[i] >= 3; i++)
            {
                Collection <BuilderElements> collection = sortedDictionary[array[i]];
                for (int j = 0; j < collection.Count; j++)
                {
                    RuleElement    ruleElement    = null;
                    RuleRefElement ruleRefElement = null;
                    for (int k = j + 1; k < collection.Count; k++)
                    {
                        if (collection[j] == null || !collection[j].Equals(collection[k]))
                        {
                            continue;
                        }
                        BuilderElements builderElements = collection[k];
                        BuilderElements parent          = builderElements.Parent;
                        if (builderElements is SemanticKeyElement)
                        {
                            parent.Items[parent.Items.IndexOf(builderElements)] = collection[j];
                        }
                        else
                        {
                            if (ruleElement == null)
                            {
                                ruleElement = new RuleElement(builderElements, "_");
                                newRules.Add(ruleElement);
                            }
                            if (ruleRefElement == null)
                            {
                                ruleRefElement = new RuleRefElement(ruleElement);
                                collection[j].Parent.Items[collection[j].Parent.Items.IndexOf(collection[j])] = ruleRefElement;
                            }
                            parent.Items[builderElements.Parent.Items.IndexOf(builderElements)] = ruleRefElement;
                        }
                        builderElements.RemoveDictionaryElements(sortedDictionary);
                        collection[k] = null;
                    }
                }
            }
        }
Пример #4
0
        public override bool Equals(object obj)
        {
            RuleElement ruleElement = obj as RuleElement;

            if (ruleElement == null)
            {
                return(false);
            }
            if (!base.Equals(obj))
            {
                return(false);
            }
            return(_name == ruleElement._name);
        }
Пример #5
0
 internal RuleRefElement(RuleElement rule, string semanticKey)
 {
     _rule        = rule;
     _semanticKey = semanticKey;
 }
Пример #6
0
 internal RuleRefElement(RuleElement rule)
 {
     _rule = rule;
 }
Пример #7
0
        /// <summary>
        /// Optimization for a element tree
        /// </summary>
        protected void Optimize(Collection <RuleElement> newRules)
        {
            // Create an dictionary of [Count of elements, list of elements]
            SortedDictionary <int, Collection <BuilderElements> > dict = new();

            GetDictionaryElements(dict);

            // The dictionary is sorted from the smallest buckets to the largest.
            // Revert the order in the keys arrays
            int[] keys = new int[dict.Keys.Count];

            int index = keys.Length - 1;

            foreach (int key in dict.Keys)
            {
                keys[index--] = key;
            }

            // Look for each bucket from the largest to the smallest
            for (int i = 0; i < keys.Length && keys[i] >= 3; i++)
            {
                Collection <BuilderElements> gb = dict[keys[i]];
                for (int j = 0; j < gb.Count; j++)
                {
                    RuleElement    newRule = null;
                    RuleRefElement ruleRef = null;
                    for (int k = j + 1; k < gb.Count; k++)
                    {
                        if (gb[j] != null && gb[j].Equals(gb[k]))
                        {
                            BuilderElements current = gb[k];
                            BuilderElements parent  = current.Parent;
                            if (current is SemanticKeyElement)
                            // if current is already a ruleref. There is no need to create a new one
                            {
                                // Simply set the ruleref of the current element to the ruleref of the org element.
                                parent.Items[parent.Items.IndexOf(current)] = gb[j];
                            }
                            else
                            {
                                // Create a rule to store the common elements
                                if (newRule == null)
                                {
                                    newRule = new RuleElement(current, "_");
                                    newRules.Add(newRule);
                                }

                                // Create a ruleref and attach the
                                if (ruleRef == null)
                                {
                                    ruleRef = new RuleRefElement(newRule);
                                    gb[j].Parent.Items[gb[j].Parent.Items.IndexOf(gb[j])] = ruleRef;
                                }
                                parent.Items[current.Parent.Items.IndexOf(current)] = ruleRef;
                            }
                            //
                            current.RemoveDictionaryElements(dict);
                            gb[k] = null;
                        }
                    }
                }
            }
        }