示例#1
0
 internal override void Initialize()
 {
     base.Initialize();
     foreach (CsElement element in base.ChildElements)
     {
         Accessor accessor = element as Accessor;
         if (accessor == null)
         {
             throw new SyntaxException(base.Document.SourceCode, accessor.LineNumber);
         }
         if (accessor.AccessorType == AccessorType.Get)
         {
             if (this.get != null)
             {
                 throw new SyntaxException(base.Document.SourceCode, accessor.LineNumber);
             }
             this.get = accessor;
         }
         else
         {
             if (accessor.AccessorType != AccessorType.Set)
             {
                 throw new SyntaxException(base.Document.SourceCode, accessor.LineNumber);
             }
             if (this.set != null)
             {
                 throw new SyntaxException(base.Document.SourceCode, accessor.LineNumber);
             }
             this.set = accessor;
         }
     }
 }
示例#2
0
 private static bool DoesAccessorHaveBody(Accessor accessor)
 {
     for (Microsoft.StyleCop.Node<CsToken> node = accessor.Tokens.First; node != accessor.Tokens.Last; node = node.Next)
     {
         if (node.Value.CsTokenType == CsTokenType.OpenCurlyBracket)
         {
             return true;
         }
     }
     return false;
 }
示例#3
0
        /// <summary>
        /// Initializes the contents of the property.
        /// </summary>
        internal override void Initialize()
        {
            base.Initialize();

            // Find the get and set accessors for this property, if they exist.
            foreach (CsElement child in this.ChildElements)
            {
                Accessor accessor = child as Accessor;
                if (accessor == null)
                {
                    throw new SyntaxException(this.Document.SourceCode, accessor.LineNumber);
                }

                if (accessor.AccessorType == AccessorType.Get)
                {
                    if (this.get != null)
                    {
                        throw new SyntaxException(this.Document.SourceCode, accessor.LineNumber);
                    }

                    this.get = accessor;
                }
                else if (accessor.AccessorType == AccessorType.Set)
                {
                    if (this.set != null)
                    {
                        throw new SyntaxException(this.Document.SourceCode, accessor.LineNumber);
                    }

                    this.set = accessor;
                }
                else
                {
                    throw new SyntaxException(this.Document.SourceCode, accessor.LineNumber);
                }
            }
        }
        /// <summary>
        /// Determines whether to reference the set accessor within the property's summary documentation.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="setAccessor">The set accessor.</param>
        /// <returns>Returns true to reference the set accessor in the summary documentation, or false to omit it.</returns>
        private static bool IncludeSetAccessorInDocumentation(Property property, Accessor setAccessor)
        {
            Param.AssertNotNull(property, "property");
            Param.AssertNotNull(setAccessor, "setAccessor");

            // If the set accessor has the same access modifier as the property, always include it in the documentation.
            // Accessors get 'private' access modifiers by default if no access modifier is defined, in which case they
            // default to having the access of their parent property. Also include documentation for the set accessor
            // if it appears to be private but it does not actually define the 'private' keyword.
            if (setAccessor.AccessModifier == property.AccessModifier ||
                (setAccessor.AccessModifier == AccessModifierType.Private && !setAccessor.Declaration.ContainsModifier(CsTokenType.Private)))
            {
                return true;
            }

            // If the set accessor has internal access, and the property also has internal or protected internal access,
            // then include the set accessor in the docs since it effectively has the same access as the overall property.
            if (setAccessor.AccessModifier == AccessModifierType.Internal &&
                (property.ActualAccess == AccessModifierType.Internal || property.ActualAccess == AccessModifierType.ProtectedAndInternal))
            {
                return true;
            }

            // If the property is effectively private (contained within a private class), and the set accessor has any access modifier other than private, then
            // include the set accessor in the documentation. Within a private class, other access modifiers on the set accessor are meaningless.
            if (property.ActualAccess == AccessModifierType.Private && !setAccessor.Declaration.ContainsModifier(CsTokenType.Private))
            {
                return true;
            }

            // If the set accessor has protected access, then always include it in the docs since it will be visible to any
            // class that inherits from this class.
            if (setAccessor.AccessModifier == AccessModifierType.Protected || setAccessor.AccessModifier == AccessModifierType.ProtectedInternal)
            {
                return true;
            }

            // Otherwise, omit the set accessor from the documentation since its access is more restricted
            // than the access of the property.
            return false;
        }
        /// <summary>
        /// Parses and returns a property, indexer, or event accessor.
        /// </summary>
        /// <param name="parent">The parent of the element.</param>
        /// <param name="elementReference">A reference to the element being created.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <param name="generated">Indicates whether the code is marked as generated code.</param>
        /// <param name="xmlHeader">The element's documentation header.</param>
        /// <param name="attributes">The attributes on the element.</param>
        /// <returns>Returns the element.</returns>
        private Accessor ParseAccessor(
            CsElement parent,
            Reference<ICodePart> elementReference,
            bool unsafeCode,
            bool generated,
            XmlHeader xmlHeader,
            ICollection<Attribute> attributes)
        {
            Param.AssertNotNull(parent, "parent");
            Param.AssertNotNull(elementReference, "elementReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);
            Param.Ignore(xmlHeader);
            Param.Ignore(attributes);

            Node<CsToken> previousTokenNode = this.tokens.Last;

            // Get the modifiers and access.
            AccessModifierType accessModifier = AccessModifierType.Private;
            Dictionary<CsTokenType, CsToken> modifiers = this.GetElementModifiers(elementReference, ref accessModifier, null);

            // Get the accessor type token.
            AccessorType accessorType = AccessorType.Get;
            CsToken accessorName = null;

            Symbol symbol = this.GetNextSymbol(elementReference);
            if (symbol.Text == "get")
            {
                accessorName = this.GetToken(CsTokenType.Get, SymbolType.Other, elementReference);

                if (parent.ElementType != ElementType.Property && parent.ElementType != ElementType.Indexer)
                {
                    throw this.CreateSyntaxException();
                }
            }
            else if (symbol.Text == "set")
            {
                accessorType = AccessorType.Set;
                accessorName = this.GetToken(CsTokenType.Set, SymbolType.Other, elementReference);

                if (parent.ElementType != ElementType.Property && parent.ElementType != ElementType.Indexer)
                {
                    throw this.CreateSyntaxException();
                }
            }
            else if (symbol.Text == "add")
            {
                accessorType = AccessorType.Add;
                accessorName = this.GetToken(CsTokenType.Add, SymbolType.Other, elementReference);

                if (parent.ElementType != ElementType.Event)
                {
                    throw this.CreateSyntaxException();
                }
            }
            else if (symbol.Text == "remove")
            {
                accessorType = AccessorType.Remove;
                accessorName = this.GetToken(CsTokenType.Remove, SymbolType.Other, elementReference);

                if (parent.ElementType != ElementType.Event)
                {
                    throw this.CreateSyntaxException();
                }
            }
            else
            {
                throw this.CreateSyntaxException();
            }

            this.tokens.Add(accessorName);

            // Create the declaration.
            Node<CsToken> firstTokenNode = previousTokenNode == null ? this.tokens.First : previousTokenNode.Next;
            CsTokenList declarationTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);
            Declaration declaration = new Declaration(
                declarationTokens, accessorName.Text, ElementType.Accessor, accessModifier, modifiers);

            Accessor accessor = new Accessor(
                this.document, parent, accessorType, xmlHeader, attributes, declaration, unsafeCode, generated);
            elementReference.Target = accessor;

            // Get the method body.
            this.ParseStatementContainer(accessor, true, unsafeCode);

            return accessor;
        }
示例#6
0
 private Accessor ParseAccessor(CsElement parent, bool unsafeCode, bool generated, XmlHeader xmlHeader, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes)
 {
     Microsoft.StyleCop.Node<CsToken> last = this.tokens.Last;
     AccessModifierType @private = AccessModifierType.Private;
     Dictionary<CsTokenType, CsToken> elementModifiers = this.GetElementModifiers(ref @private, null);
     AccessorType get = AccessorType.Get;
     CsToken item = null;
     Symbol nextSymbol = this.GetNextSymbol();
     if (nextSymbol.Text == "get")
     {
         item = this.GetToken(CsTokenType.Get, SymbolType.Other);
         if ((parent.ElementType != ElementType.Property) && (parent.ElementType != ElementType.Indexer))
         {
             throw this.CreateSyntaxException();
         }
     }
     else if (nextSymbol.Text == "set")
     {
         get = AccessorType.Set;
         item = this.GetToken(CsTokenType.Set, SymbolType.Other);
         if ((parent.ElementType != ElementType.Property) && (parent.ElementType != ElementType.Indexer))
         {
             throw this.CreateSyntaxException();
         }
     }
     else if (nextSymbol.Text == "add")
     {
         get = AccessorType.Add;
         item = this.GetToken(CsTokenType.Add, SymbolType.Other);
         if (parent.ElementType != ElementType.Event)
         {
             throw this.CreateSyntaxException();
         }
     }
     else
     {
         if (nextSymbol.Text != "remove")
         {
             throw this.CreateSyntaxException();
         }
         get = AccessorType.Remove;
         item = this.GetToken(CsTokenType.Remove, SymbolType.Other);
         if (parent.ElementType != ElementType.Event)
         {
             throw this.CreateSyntaxException();
         }
     }
     this.tokens.Add(item);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = (last == null) ? this.tokens.First : last.Next;
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     Declaration declaration = new Declaration(tokens, item.Text, ElementType.Accessor, @private, elementModifiers);
     Accessor element = new Accessor(this.document, parent, get, xmlHeader, attributes, declaration, unsafeCode, generated);
     this.ParseStatementContainer(element, true, unsafeCode);
     return element;
 }
示例#7
0
        /// <summary>4
        /// Determines whether the given accessor contains a body. 
        /// </summary>
        /// <param name="accessor">The accessor to check.</param>
        /// <returns>Returns true if the accessor contains a body.</returns>
        private static bool DoesAccessorHaveBody(Accessor accessor)
        {
            Param.AssertNotNull(accessor, "accessor");

            for (Node<CsToken> node = accessor.Tokens.First; node != accessor.Tokens.Last; node = node.Next)
            {
                if (node.Value.CsTokenType == CsTokenType.OpenCurlyBracket)
                {
                    return true;
                }
            }

            return false;
        }