Exemplo n.º 1
0
        /** for all alts, find which ref X or r needs List
         * Must see across alts.  If any alt needs X or r as list, then
         * define as list.
         */
        public virtual ISet <Decl.Decl> GetDeclsForAllElements(IList <AltAST> altASTs)
        {
            ISet <string>      needsList = new HashSet <string>();
            ISet <string>      optional  = new HashSet <string>();
            ISet <string>      suppress  = new HashSet <string>();
            IList <GrammarAST> allRefs   = new List <GrammarAST>();

            foreach (AltAST ast in altASTs)
            {
                IntervalSet        reftypes = new IntervalSet(ANTLRParser.RULE_REF, ANTLRParser.TOKEN_REF);
                IList <GrammarAST> refs     = ast.GetNodesWithType(reftypes);
                foreach (var @ref in refs)
                {
                    allRefs.Add(@ref);
                }

                System.Tuple <FrequencySet <string>, FrequencySet <string> > minAndAltFreq = GetElementFrequenciesForAlt(ast);
                FrequencySet <string> minFreq = minAndAltFreq.Item1;
                FrequencySet <string> altFreq = minAndAltFreq.Item2;
                foreach (GrammarAST t in refs)
                {
                    string refLabelName = GetLabelName(rule.g, t);
                    if (altFreq.GetCount(refLabelName) == 0)
                    {
                        suppress.Add(refLabelName);
                    }
                    if (minFreq.GetCount(refLabelName) == 0)
                    {
                        optional.Add(refLabelName);
                    }
                    if (altFreq.GetCount(refLabelName) > 1)
                    {
                        needsList.Add(refLabelName);
                    }
                }
            }

            ISet <Decl.Decl> decls = new LinkedHashSet <Decl.Decl>();

            foreach (GrammarAST t in allRefs)
            {
                string refLabelName = GetLabelName(rule.g, t);
                if (suppress.Contains(refLabelName))
                {
                    continue;
                }

                IList <Decl.Decl> d = GetDeclForAltElement(t,
                                                           refLabelName,
                                                           needsList.Contains(refLabelName),
                                                           optional.Contains(refLabelName));
                decls.UnionWith(d);
            }

            return(decls);
        }
        /**
         * Generate a frequency set as the union of two input sets. If an
         * element is contained in both sets, the value for the output will be
         * the minimum of the two input values.
         *
         * @param a The first set.
         * @param b The second set. If this set is {@link #SENTINEL}, it is treated
         * as though no second set were provided.
         * @return The union of the two sets, with the minimum value chosen
         * whenever both sets contain the same key.
         */
        protected static FrequencySet <string> combineMin(FrequencySet <string> a, FrequencySet <string> b)
        {
            if (b == SENTINEL)
            {
                return(a);
            }

            Debug.Assert(a != SENTINEL);
            FrequencySet <string> result = CombineAndClip(a, b, 1);

            foreach (KeyValuePair <string, StrongBox <int> > entry in result)
            {
                entry.Value.Value = Math.Min(a.GetCount(entry.Key), b.GetCount(entry.Key));
            }

            return(result);
        }
        /**
         * Generate a frequency set as the union of two input sets. If an
         * element is contained in both sets, the value for the output will be
         * the minimum of the two input values.
         *
         * @param a The first set.
         * @param b The second set. If this set is {@link #SENTINEL}, it is treated
         * as though no second set were provided.
         * @return The union of the two sets, with the minimum value chosen
         * whenever both sets contain the same key.
         */
        protected static FrequencySet<string> combineMin(FrequencySet<string> a, FrequencySet<string> b)
        {
            if (b == SENTINEL)
            {
                return a;
            }

            Debug.Assert(a != SENTINEL);
            FrequencySet<string> result = CombineAndClip(a, b, 1);
            foreach (KeyValuePair<string, StrongBox<int>> entry in result)
            {
                entry.Value.Value = Math.Min(a.GetCount(entry.Key), b.GetCount(entry.Key));
            }

            return result;
        }