Example #1
0
        /// <summary>
        /// Build an Array of a particular type from a list of tokens.
        /// The Type must be one that can be built with Convert.ChangeType.
        /// There are various ways to specify how many elements to parse.
        /// WARNING: This will throw an exception if any tokens cannot be
        /// converted.
        /// </summary>
        /// <param name="tokens">The ArrayList of tokens.</param>
        /// <param name="i">The starting (and ending) index.  This is
        /// modified, and left pointing at the last used token.</param>
        /// <param name="type">The Type of the array elements.</param>
        /// <param name="endToken">An optional end Token to look for.
        /// Parsing stops when a token equal to this is found.
        /// If this is null, then it is not used.</param>
        /// <param name="maxLength">The maximum number of array elements
        /// to parse.  If this is negative, then it is not used.</param>
        /// <param name="log">A Logger to use for messages.</param>
        /// <returns>The Array, or null for error.</returns>
        public static Array BuildArray(ArrayList tokens, ref int i, Type type,
                                       Token endToken, int maxLength, Logger log)
        {
            int len = tokens.Count;

            if (i >= len)
            {
                log.Error("BuildArray: Input index too large.");
                return(null);
            }

            // put the objects into an array list first, since we don't
            // know length
            ArrayList list = new ArrayList();

            // allow null endToken specified
            if (endToken == null)
            {
                endToken = new EofToken();
            }

            Token token = null;

            token = (Token)tokens[i++];
            int arrayLength = 0;

            while ((!(token is EofToken)) && (token != endToken) && (i < len) &&
                   ((maxLength < 0) || (arrayLength < maxLength)))
            {
                Object o = token.ConvertToType(type);
                list.Add(o);
                arrayLength++;
                token = (Token)tokens[i++];
            }
            i--;             // went one past

            return(list.ToArray(type));
        }