예제 #1
0
파일: Method.cs 프로젝트: sarvex/StyleCop
        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <returns>The name of the element.</returns>
        protected override string GetElementName()
        {
            CodeUnit start = this.Children.First;

            if (!this.ContainsModifier(TokenType.Implicit, TokenType.Explicit))
            {
                // Get the return type.
                start = this.FindFirstChild <TypeToken>();
            }

            if (start != null)
            {
                Token next = start.FindNextSiblingToken();
                if (next != null)
                {
                    if (next.Is(TokenType.Operator))
                    {
                        // The next token is the operator name.
                        Token operatorName = next.FindNextSiblingToken();
                        if (operatorName != null)
                        {
                            return("operator " + operatorName.Text);
                        }
                    }
                    else
                    {
                        // This is a regular method.
                        return(next.Text);
                    }
                }
            }

            throw new SyntaxException(this.Document, this.LineNumber);
        }
예제 #2
0
        /// <summary>
        /// Extracts a variable from the clause.
        /// </summary>
        /// <param name="firstToken">The first token of the variable.</param>
        /// <param name="allowTypelessVariable">Indicates whether to allow a variable with no type defined.</param>
        /// <param name="onlyTypelessVariable">Indicates whether to only get a typeless variable.</param>
        /// <returns>Returns the variable.</returns>
        protected static QueryClauseVariable ExtractQueryVariable(Token firstToken, bool allowTypelessVariable, bool onlyTypelessVariable)
        {
            Param.RequireNotNull(firstToken, "firstToken");
            Param.Ignore(allowTypelessVariable);
            Param.Ignore(onlyTypelessVariable);

            if (onlyTypelessVariable || !firstToken.Is(TokenType.Type))
            {
                // In this case there is no type, only an identifier.
                return(new QueryClauseVariable(null, firstToken.Text, firstToken.Location, firstToken.Generated));
            }
            else
            {
                TypeToken type = (TypeToken)firstToken;

                // Attempt to get the identifier token coming after the type token.
                Token identifier = firstToken.FindNextSiblingToken();
                if (identifier == null || identifier.TokenType != TokenType.Literal)
                {
                    CsLanguageService.Debug.Assert(allowTypelessVariable, "The clause does not allow typeless variables. The parser should have thrown syntax exception already.");
                    return(new QueryClauseVariable(null, type.Text, type.Location, type.Generated));
                }
                else
                {
                    // There is a type and an identifier.
                    return(new QueryClauseVariable(type, identifier.Text, CodeUnit.JoinLocations(type, identifier), type.Generated || identifier.Generated));
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Sets the inherited items of the class.
        /// </summary>
        private void SetInheritedItems()
        {
            // Pull out the name of the base class and any implemented interfaces
            // from the declaration of this class.
            bool colon      = false;
            bool comma      = false;
            var  interfaces = new List <string>();

            this.baseClass.Value = string.Empty;

            for (Token token = this.FindFirstChildToken(); token != null; token = token.FindNextSiblingToken())
            {
                if (colon)
                {
                    if (token.Text.Length >= 2 &&
                        token.Text[0] == 'I' &&
                        char.IsUpper(token.Text[1]))
                    {
                        interfaces.Add(CodeParser.TrimType(token.Text));
                    }
                    else
                    {
                        this.baseClass.Value = CodeParser.TrimType(token.Text);
                    }

                    colon = false;
                }
                else if (comma)
                {
                    interfaces.Add(CodeParser.TrimType(token.Text));
                    comma = false;
                }
                else
                {
                    if (token.TokenType == TokenType.Where)
                    {
                        break;
                    }
                    else if (token.Text == ":")
                    {
                        if (this.baseClass.Value.Length > 0)
                        {
                            break;
                        }
                        else
                        {
                            colon = true;
                        }
                    }
                    else if (token.TokenType == TokenType.Comma)
                    {
                        comma = true;
                    }
                }
            }

            this.implementedInterfaces.Value = interfaces.AsReadOnly();
        }
예제 #4
0
파일: EnumItem.cs 프로젝트: sarvex/StyleCop
        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <returns>The name of the element.</returns>
        protected override string GetElementName()
        {
            for (Token token = this.FindFirstChildToken(); token != null; token = token.FindNextSiblingToken())
            {
                if (token.Is(TokenType.Literal) || token.Is(TokenType.Type))
                {
                    return(token.Text);
                }
            }

            throw new SyntaxException(this.Document, this.LineNumber);
        }
예제 #5
0
        /// <summary>
        /// Initializes the element.
        /// </summary>
        private void Init()
        {
            this.namespaceType.Value = this.alias.Value = string.Empty;

            // Find the 'using' keyword.
            Token usingKeyword = this.FindFirstChild <UsingDirectiveToken>();

            if (usingKeyword != null)
            {
                // Move past it.
                Token index = usingKeyword.FindNextSiblingToken();
                if (index != null)
                {
                    // This word is usually the namespace type, unless an alias is defined.
                    this.namespaceType.Value = CodeParser.TrimType(CodeParser.GetFullName(this.Document, this, index, out index));

                    // Now see if the next word is an equals sign.
                    index = index.FindNextSiblingToken();

                    if (index != null)
                    {
                        if (index.Text == "=")
                        {
                            // Get the word after the equals sign, which will be the namespace.
                            index = index.FindNextSiblingToken();

                            if (index != null)
                            {
                                // Set the alias and the namespace.
                                this.alias.Value         = this.namespaceType.Value;
                                this.namespaceType.Value = CodeParser.TrimType(index.Text);
                            }
                        }
                    }
                }
            }
        }
예제 #6
0
파일: Enum.cs 프로젝트: sarvex/StyleCop
        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <returns>The name of the element.</returns>
        protected override string GetElementName()
        {
            // Get the enum keyword.
            Token enumToken = this.FindFirstChild <EnumToken>();

            if (enumToken != null)
            {
                // The next Token is the name.
                Token nameToken = enumToken.FindNextSiblingToken();
                if (nameToken != null)
                {
                    return(nameToken.Text);
                }
            }

            throw new SyntaxException(this.Document, this.LineNumber);
        }
예제 #7
0
파일: Enum.cs 프로젝트: sarvex/StyleCop
        /// <summary>
        /// Gets the base type of the item.
        /// </summary>
        /// <returns>Returns the name of the base type or null if none.</returns>
        private string GetBaseType()
        {
            // Pull out the base type.
            bool foundColon = false;

            for (Token token = this.FindFirstChildToken(); token != null; token = token.FindNextSiblingToken())
            {
                if (foundColon)
                {
                    return(token.Text);
                }
                else if (token.Text == ":")
                {
                    foundColon = true;
                }
            }

            return(null);
        }
예제 #8
0
        /// <summary>
        /// Walks the element declaration and gathers the element modifiers.
        /// </summary>
        /// <param name="allowedModifiers">The modifiers which are allowed for the current element type.</param>
        private void GatherDeclarationModifiers(IEnumerable <string> allowedModifiers)
        {
            Param.Ignore(allowedModifiers);

            this.modifiers.Value = new Dictionary <TokenType, Token>();
            Token accessModifierSeen = null;

            this.accessModifier.Value = this.DefaultAccessModifierType;

            for (Token token = this.FirstDeclarationToken; token != null; token = token.FindNextSiblingToken())
            {
                if (token.TokenType == TokenType.Public)
                {
                    // A public access modifier can only be specified if there have been no other access modifiers.
                    if (accessModifierSeen != null)
                    {
                        throw new SyntaxException(this.Document, token.LineNumber);
                    }

                    this.accessModifier.Value = AccessModifierType.Public;
                    accessModifierSeen        = token;
                    this.modifiers.Value.Add(TokenType.Public, token);
                }
                else if (token.TokenType == TokenType.Private)
                {
                    // A private access modifier can only be specified if there have been no other access modifiers.
                    if (accessModifierSeen != null)
                    {
                        throw new SyntaxException(this.Document, token.LineNumber);
                    }

                    this.accessModifier.Value = AccessModifierType.Private;
                    accessModifierSeen        = token;
                    this.modifiers.Value.Add(TokenType.Private, token);
                }
                else if (token.TokenType == TokenType.Internal)
                {
                    // The access is internal unless we have already seen a protected access
                    // modifier, in which case it is protected internal.
                    if (accessModifierSeen == null)
                    {
                        this.accessModifier.Value = AccessModifierType.Internal;
                    }
                    else if (accessModifierSeen.TokenType == TokenType.Protected)
                    {
                        this.accessModifier.Value = AccessModifierType.ProtectedInternal;
                    }
                    else
                    {
                        throw new SyntaxException(this.Document, token.LineNumber);
                    }

                    accessModifierSeen = token;
                    this.modifiers.Value.Add(TokenType.Internal, token);
                }
                else if (token.TokenType == TokenType.Protected)
                {
                    // The access is protected unless we have already seen an internal access
                    // modifier, in which case it is protected internal.
                    if (accessModifierSeen == null)
                    {
                        this.accessModifier.Value = AccessModifierType.Protected;
                    }
                    else if (accessModifierSeen.TokenType == TokenType.Internal)
                    {
                        this.accessModifier.Value = AccessModifierType.ProtectedInternal;
                    }
                    else
                    {
                        throw new SyntaxException(this.Document, token.LineNumber);
                    }

                    accessModifierSeen = token;
                    this.modifiers.Value.Add(TokenType.Protected, token);
                }
                else
                {
                    if (!GetOtherElementModifier(allowedModifiers, this.modifiers.Value, token))
                    {
                        break;
                    }
                }
            }
        }
예제 #9
0
        /// <summary>
        /// Extracts a variable from the clause.
        /// </summary>
        /// <param name="firstToken">The first token of the variable.</param>
        /// <param name="allowTypelessVariable">Indicates whether to allow a variable with no type defined.</param>
        /// <param name="onlyTypelessVariable">Indicates whether to only get a typeless variable.</param>
        /// <returns>Returns the variable.</returns>
        protected static QueryClauseVariable ExtractQueryVariable(Token firstToken, bool allowTypelessVariable, bool onlyTypelessVariable)
        {
            Param.RequireNotNull(firstToken, "firstToken");
            Param.Ignore(allowTypelessVariable);
            Param.Ignore(onlyTypelessVariable);

            if (onlyTypelessVariable || !firstToken.Is(TokenType.Type))
            {
                // In this case there is no type, only an identifier.
                return new QueryClauseVariable(null, firstToken.Text, firstToken.Location, firstToken.Generated);
            }
            else
            {
                TypeToken type = (TypeToken)firstToken;

                // Attempt to get the identifier token coming after the type token.
                Token identifier = firstToken.FindNextSiblingToken();
                if (identifier == null || identifier.TokenType != TokenType.Literal)
                {
                    CsLanguageService.Debug.Assert(allowTypelessVariable, "The clause does not allow typeless variables. The parser should have thrown syntax exception already.");
                    return new QueryClauseVariable(null, type.Text, type.Location, type.Generated);
                }
                else
                {
                    // There is a type and an identifier.
                    return new QueryClauseVariable(type, identifier.Text, CodeUnit.JoinLocations(type, identifier), type.Generated || identifier.Generated);
                }
            }
        }