示例#1
0
        // TODO: next stuff taken from CommonTreeNodeStream

        /** <summary>
         *  Given a node, add this to the reverse index tokenTypeToStreamIndexesMap.
         *  You can override this method to alter how indexing occurs.  The
         *  default is to create a
         *
         *    Map&lt;Integer token type,ArrayList&lt;Integer stream index&gt;&gt;
         *  </summary>
         *
         *  <remarks>
         *  This data structure allows you to find all nodes with type INT in order.
         *
         *  If you really need to find a node of type, say, FUNC quickly then perhaps
         *
         *    Map&lt;Integertoken type,Map&lt;Object tree node,Integer stream index&gt;&gt;
         *
         *  would be better for you.  The interior maps map a tree node to
         *  the index so you don't have to search linearly for a specific node.
         *
         *  If you change this method, you will likely need to change
         *  getNodeIndex(), which extracts information.
         *  </remarks>
         */
        protected void fillReverseIndex(object node, int streamIndex)
        {
            //System.out.println("revIndex "+node+"@"+streamIndex);
            if (tokenTypesToReverseIndex == null)
            {
                return; // no indexing if this is empty (nothing of interest)
            }
            if (tokenTypeToStreamIndexesMap == null)
            {
                tokenTypeToStreamIndexesMap = new Dictionary <int, IList <int> >(); // first indexing op
            }
            int tokenType = adaptor.getType(node);

            if (!(tokenTypesToReverseIndex == INDEX_ALL ||
                  tokenTypesToReverseIndex.Contains(tokenType)))
            {
                return; // tokenType not of interest
            }
            IList <int> indexes;

            if (!tokenTypeToStreamIndexesMap.TryGetValue(tokenType, out indexes) || indexes == null)
            {
                indexes = new List <int>(); // no list yet for this token type
                indexes.Add(streamIndex);   // not there yet, add
                tokenTypeToStreamIndexesMap[tokenType] = indexes;
            }
            else
            {
                if (!indexes.Contains(streamIndex))
                {
                    indexes.Add(streamIndex);   // not there yet, add
                }
            }
        }
        public static TValue get <TKey, TValue>(this IDictionary <TKey, TValue> map, TKey key)
        {
            TValue value;

            if (map.TryGetValue(key, out value))
            {
                return(value);
            }

            if (typeof(TValue).IsValueType)
            {
                throw new KeyNotFoundException();
            }

            return(default(TValue));
        }
示例#3
0
        /** <summary>Using the map of token names to token types, return the type.</summary> */
        public virtual int GetTokenType(string tokenName)
        {
            if (tokenNameToTypeMap == null)
            {
                return(TokenTypes.Invalid);
            }

            int value;

            if (tokenNameToTypeMap.TryGetValue(tokenName, out value))
            {
                return(value);
            }

            return(TokenTypes.Invalid);
        }
示例#4
0
        /** <summary>Using the map of token names to token types, return the type.</summary> */
        public virtual int GetTokenType(string tokenName)
        {
            if (tokenNameToTypeMap == null)
            {
                return(TokenConstants.INVALID_TOKEN_TYPE);
            }

            int value;

            if (tokenNameToTypeMap.TryGetValue(tokenName, out value))
            {
                return(value);
            }

            return(TokenConstants.INVALID_TOKEN_TYPE);
        }
示例#5
0
        /// <inheritdoc/>
        /// <remarks>
        /// <para>The <see cref="Variables"/> are rendered in order. This method divides the rendering
        /// process into separate methods according to the type of parameter provided in
        /// <paramref name="parameters"/> for each of the variables in <see cref="Variables"/>.</para>
        ///
        /// <list type="table">
        /// <listheader>
        /// <term>Parameter Type</term>
        /// <term>Render Action</term>
        /// </listheader>
        /// <item>
        /// <description><see cref="IDictionary"/></description>
        /// <description>The variable is rendered by calling <see cref="RenderDictionary"/>.</description>
        /// </item>
        /// <item>
        /// <description><see cref="IEnumerable"/> (except <see cref="string"/>)</description>
        /// <description>The variable is rendered by calling <see cref="RenderEnumerable"/>.</description>
        /// </item>
        /// <item>
        /// <description><see cref="string"/>, or any other <see cref="object"/></description>
        /// <description>The variable is rendered by calling <see cref="RenderElement"/>.</description>
        /// </item>
        /// <item>
        /// <description><see langword="null"/></description>
        /// <description>The output is not modified.</description>
        /// </item>
        /// </list>
        /// </remarks>
        public override sealed void Render <T>(StringBuilder builder, IDictionary <string, T> parameters)
        {
            bool added = false;

            for (int i = 0; i < _variables.Length; i++)
            {
                T result;
                if (!parameters.TryGetValue(_variables[i].Name, out result) || result == null)
                {
                    continue;
                }

                IDictionary dictionary = result as IDictionary;
                if (dictionary != null)
                {
                    if (_variables[i].Prefix != null)
                    {
                        throw new InvalidOperationException(string.Format("Cannot apply prefix modifier to associative map value '{0}'", _variables[i].Name));
                    }

                    RenderDictionary(builder, _variables[i], dictionary, !added);
                }
                else
                {
                    IEnumerable enumerable = result as IEnumerable;
                    if (enumerable != null && !(result is string))
                    {
                        if (_variables[i].Prefix != null)
                        {
                            throw new InvalidOperationException(string.Format("Cannot apply prefix modifier to composite value '{0}'", _variables[i].Name));
                        }

                        RenderEnumerable(builder, _variables[i], enumerable, !added);
                    }
                    else
                    {
                        RenderElement(builder, _variables[i], result, !added);
                    }
                }

                added = true;
            }
        }
示例#6
0
        public virtual int GetUniqueID(object node)
        {
            if (treeToUniqueIDMap == null)
            {
                treeToUniqueIDMap = new Dictionary <object, int>();
            }
            int id;

            if (treeToUniqueIDMap.TryGetValue(node, out id))
            {
                return(id);
            }

            id = uniqueNodeID;
            treeToUniqueIDMap[node] = id;
            uniqueNodeID++;
            return(id);
            // GC makes these nonunique:
            // return System.identityHashCode(node);
        }
示例#7
0
        /** <summary>Do the work for index</summary> */
        protected virtual void IndexCore(object t, IDictionary <int, IList> m)
        {
            if (t == null)
            {
                return;
            }
            int   ttype = adaptor.GetType(t);
            IList elements;

            if (!m.TryGetValue(ttype, out elements) || elements == null)
            {
                elements = new List <object>();
                m[ttype] = elements;
            }
            elements.Add(t);
            int n = adaptor.GetChildCount(t);

            for (int i = 0; i < n; i++)
            {
                object child = adaptor.GetChild(t, i);
                IndexCore(child, m);
            }
        }