/// <summary>
        /// Parses and returns the items within an enum element.
        /// </summary>
        /// <param name="parent">The parent enum element.</param>
        /// <param name="parentReference">Reference to the parent of the items we're creating.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <returns>Returns the element.</returns>
        private ICollection<EnumItem> ParseEnumItems(Enum parent, Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(unsafeCode);
            Param.AssertNotNull(parentReference, "parentReference");

            List<EnumItem> enumItems = new List<EnumItem>();

            SkipSymbols skip = SkipSymbols.All;
            skip &= ~SkipSymbols.XmlHeader;
            Symbol symbol = this.GetNextSymbol(skip, parentReference);

            while (symbol.SymbolType != SymbolType.CloseCurlyBracket)
            {
                // Get the enum header.
                XmlHeader xmlHeader = null;
                ICollection<Attribute> attributes;

                var enumItemReference = new Reference<ICodePart>();

                this.MoveToElementDeclaration(parentReference, enumItemReference, unsafeCode, out xmlHeader, out attributes);

                // If the next symbol is a close curly bracket, quit.
                symbol = this.GetNextSymbol(enumItemReference);
                if (symbol.SymbolType == SymbolType.CloseCurlyBracket)
                {
                    break;
                }

                // Get the enum item name.
                Node<CsToken> firstEnumItemToken = this.tokens.InsertLast(this.GetToken(CsTokenType.Other, SymbolType.Other, enumItemReference));

                Expression initializationExpression = null;

                // See if there is an equals sign.
                symbol = this.GetNextSymbol(enumItemReference);
                if (symbol.SymbolType == SymbolType.Equals)
                {
                    this.tokens.Add(this.GetOperatorToken(OperatorType.Equals, enumItemReference));

                    // Get the constant expression being assigned.
                    initializationExpression = this.GetNextExpression(ExpressionPrecedence.None, enumItemReference, unsafeCode);
                }

                CsTokenList enumItemTokens = new CsTokenList(this.tokens, firstEnumItemToken, this.tokens.Last);

                Declaration enumItemDeclaration = new Declaration(
                    enumItemTokens, firstEnumItemToken.Value.Text, ElementType.EnumItem, AccessModifierType.Public);

                EnumItem enumItem = new EnumItem(
                    this.document,
                    parent,
                    xmlHeader,
                    attributes,
                    enumItemDeclaration,
                    initializationExpression,
                    unsafeCode,
                    this.symbols.Generated);

                enumItemReference.Target = enumItem;
                enumItem.Tokens = new CsTokenList(this.tokens, firstEnumItemToken, this.tokens.Last);

                enumItems.Add(enumItem);
                parent.AddElement(enumItem);

                symbol = this.GetNextSymbol(parentReference);

                // If the symbol is not a comma, quit.
                if (symbol.SymbolType == SymbolType.Comma)
                {
                    this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, parentReference));
                }
                else
                {
                    break;
                }

                symbol = this.GetNextSymbol(skip, parentReference);
            }

            // Return the enum items as a read-only collection.
            return enumItems.ToArray();
        }
Example #2
0
 private ICollection<EnumItem> ParseEnumItems(Microsoft.StyleCop.CSharp.Enum parent, bool unsafeCode)
 {
     List<EnumItem> list = new List<EnumItem>();
     SkipSymbols all = SkipSymbols.All;
     all &= ~SkipSymbols.XmlHeader;
     for (Symbol symbol = this.GetNextSymbol(all); symbol.SymbolType != SymbolType.CloseCurlyBracket; symbol = this.GetNextSymbol(all))
     {
         ICollection<Microsoft.StyleCop.CSharp.Attribute> is2;
         XmlHeader xmlHeader = null;
         this.MoveToElementDeclaration(unsafeCode, out xmlHeader, out is2);
         if (this.GetNextSymbol().SymbolType == SymbolType.CloseCurlyBracket)
         {
             break;
         }
         Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Other, SymbolType.Other));
         Expression initialization = null;
         if (this.GetNextSymbol().SymbolType == SymbolType.Equals)
         {
             this.tokens.Add(this.GetOperatorToken(OperatorType.Equals));
             initialization = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
         }
         CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
         Declaration declaration = new Declaration(tokens, firstItemNode.Value.Text, ElementType.EnumItem, AccessModifierType.Public);
         EnumItem item = new EnumItem(this.document, parent, xmlHeader, is2, declaration, initialization, unsafeCode, this.symbols.Generated);
         item.Tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
         list.Add(item);
         parent.AddElement(item);
         if (this.GetNextSymbol().SymbolType != SymbolType.Comma)
         {
             break;
         }
         this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma));
     }
     return list.ToArray();
 }