private void ProcessDocumentRoot(DocumentRoot documentRoot)
 {
     foreach (var child in documentRoot.ChildElements)
     {
         if (child.ElementType == ElementType.Namespace)
         {
             ProcessNamespace((Namespace)child);
         }
     }
 }
示例#2
0
        /// <summary>
        /// Parses the contents of the document.
        /// </summary>
        internal void ParseDocument()
        {
            Debug.Assert(this.document == null, "A CodeParser instance may only be used once.");

            // Find the list of symbols in the document.
            List<Symbol> symbolList = this.lexer.GetSymbols(this.lexer.SourceCode, this.lexer.SourceCode.Project.Configuration);

            // Create the symbol manager class.
            this.symbols = new SymbolManager(symbolList);

            // Create the document object.
            this.document = new CsDocument(this.lexer.SourceCode, this.parser, this.tokens);

            Reference<ICodePart> documentRootReference = new Reference<ICodePart>();

            // Get the file header if it exists.
            FileHeader fileHeader = this.GetFileHeader(documentRootReference);

            // Let the symbol manager know if this document contains generated code.
            if (fileHeader.Generated)
            {
                this.symbols.IncrementGeneratedCodeBlocks();
            }

            this.document.FileHeader = fileHeader;

            // Create a declaration for the root element.
            Declaration declaration = new Declaration(
                new CsTokenList(this.document.Tokens), Strings.Root, ElementType.Root, AccessModifierType.Public, new Dictionary<CsTokenType, CsToken>());

            // Create the root element for the document.
            DocumentRoot root = new DocumentRoot(this.document, declaration, fileHeader.Generated);
            documentRootReference.Target = root;

            // Parse the contents of the document.
            this.ParseElementContainerBody(root, documentRootReference, this.parser.PartialElements, false);

            // Check if there are any tokens in the document.
            if (this.document.Tokens.Count > 0)
            {
                // Fill in the token list for the root element.
                root.Tokens = new CsTokenList(this.document.Tokens, this.document.Tokens.First, this.document.Tokens.Last);

                // Fill in the location for the element.
                root.Location = CsToken.JoinLocations(this.document.Tokens.First, this.document.Tokens.Last);
            }

            // Add the root element to the document.
            this.document.RootElement = root;

            // When in debug mode, ensure that all tokens are correctly mapped to a parent element.
            this.DebugValidateParentReferences();
        }
        private void CheckForEmptyComments(DocumentRoot element)
        {
            Param.AssertNotNull(element, "element");

            // Loop through all the tokens in the file looking for comments.
            for (Node<CsToken> tokenNode = element.Tokens.First; !element.Tokens.OutOfBounds(tokenNode); tokenNode = tokenNode.Next)
            {
                if (this.Cancel)
                {
                    break;
                }

                CsToken token = tokenNode.Value;

                // Skip generated code.
                if (!token.Generated)
                {
                    // Check for the two comment types.
                    if (token.CsTokenType == CsTokenType.SingleLineComment)
                    {
                        // This is a single line comment.
                        int slashCount = 0;
                        bool found = false;

                        // Loop through the characters in the comment text.
                        for (int character = 0; character < token.Text.Length; ++character)
                        {
                            // See if we've found the slashes at the beginning of the comment
                            if (slashCount == 2)
                            {
                                // Check whether there's anything here other than whitespace.
                                // If so, then this comment is ok.
                                if (token.Text[character] != ' ' && token.Text[character] != '\t' && token.Text[character] != '\r' && token.Text[character] != '\n')
                                {
                                    found = true;
                                    break;
                                }
                            }
                            else if (token.Text[character] == '/')
                            {
                                ++slashCount;
                            }
                        }

                        // Check whether the comment contained any text.
                        if (!found)
                        {
                            // This is only allowed if this comment is sandwiched in between two other comments.
                            bool comment = false;
                            int lines = 0;
                            foreach (CsToken item in element.Tokens.ReverseIterator(tokenNode.Previous))
                            {
                                if (item.Text == "\n")
                                {
                                    ++lines;
                                    if (lines > 1)
                                    {
                                        break;
                                    }
                                }
                                else if (item.CsTokenType == CsTokenType.SingleLineComment)
                                {
                                    comment = true;
                                    break;
                                }
                                else if (item.CsTokenType != CsTokenType.WhiteSpace)
                                {
                                    break;
                                }
                            }

                            if (!comment)
                            {
                                CsElement errorElement = token.Parent as CsElement ?? element;
                                this.AddViolation(errorElement, token.Location, Rules.CommentsMustContainText);
                            }
                            else
                            {
                                comment = false;
                                lines = 0;
                                foreach (CsToken item in element.Tokens.ForwardIterator(tokenNode.Next))
                                {
                                    if (item.Text == "\n")
                                    {
                                        ++lines;
                                        if (lines > 1)
                                        {
                                            break;
                                        }
                                    }
                                    else if (item.CsTokenType == CsTokenType.SingleLineComment)
                                    {
                                        comment = true;
                                        break;
                                    }
                                    else if (item.CsTokenType != CsTokenType.WhiteSpace)
                                    {
                                        break;
                                    }
                                }

                                if (!comment)
                                {
                                    CsElement errorElement = token.Parent as CsElement ?? element;
                                    this.AddViolation(errorElement, token.Location, Rules.CommentsMustContainText);
                                }
                            }
                        }
                    }
                    else if (token.CsTokenType == CsTokenType.MultiLineComment)
                    {
                        // The is a multi-line comment. Get the start of the comment.
                        int start = token.Text.IndexOf("/*", StringComparison.Ordinal);
                        if (start > -1)
                        {
                            // Get the end of the comment
                            int end = token.Text.IndexOf("*/", start + 2, StringComparison.Ordinal);
                            if (end > -1)
                            {
                                // Look at the characters between the start and the end and see if there
                                // is anything here besides whitespace.
                                bool found = false;
                                for (int character = start + 2; character < end; ++character)
                                {
                                    if (token.Text[character] != ' ' && token.Text[character] != '\t' && token.Text[character] != '\r' && token.Text[character] != '\n')
                                    {
                                        found = true;
                                        break;
                                    }
                                }

                                // Check whether the comment contained any text.
                                if (!found)
                                {
                                    CsElement errorElement = token.Parent as CsElement ?? element;
                                    this.AddViolation(errorElement, token.Location, Rules.CommentsMustContainText);
                                }
                            }
                        }
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Checks the document for single line comments that start with three slashes.
        /// </summary>
        /// <param name="root">
        /// The document root.
        /// </param>
        private void CheckSingleLineComments(DocumentRoot root)
        {
            Param.AssertNotNull(root, "root");

            if (root.Tokens != null)
            {
                foreach (CsToken token in root.Tokens)
                {
                    if (token.CsTokenType == CsTokenType.SingleLineComment)
                    {
                        if (token.Text.StartsWith("///", StringComparison.Ordinal) && (token.Text.Length == 3 || (token.Text.Length > 3 && token.Text[3] != '/')))
                        {
                            this.AddViolation(token.FindParentElement(), token.LineNumber, Rules.SingleLineCommentsMustNotUseDocumentationStyleSlashes);
                        }
                    }
                }
            }
        }
        private void CheckForEmptyComments(DocumentRoot element)
        {
            Param.AssertNotNull(element, "element");

            // Loop through all the tokens in the file looking for comments.
            for (Node <CsToken> tokenNode = element.Tokens.First; !element.Tokens.OutOfBounds(tokenNode); tokenNode = tokenNode.Next)
            {
                if (this.Cancel)
                {
                    break;
                }

                CsToken token = tokenNode.Value;

                // Skip generated code.
                if (!token.Generated)
                {
                    // Check for the two comment types.
                    if (token.CsTokenType == CsTokenType.SingleLineComment)
                    {
                        // This is a single line comment.
                        int  slashCount = 0;
                        bool found      = false;

                        // Loop through the characters in the comment text.
                        for (int character = 0; character < token.Text.Length; ++character)
                        {
                            // See if we've found the slashes at the beginning of the comment
                            if (slashCount == 2)
                            {
                                // Check whether there's anything here other than whitespace.
                                // If so, then this comment is ok.
                                if (token.Text[character] != ' ' && token.Text[character] != '\t' && token.Text[character] != '\r' && token.Text[character] != '\n')
                                {
                                    found = true;
                                    break;
                                }
                            }
                            else if (token.Text[character] == '/')
                            {
                                ++slashCount;
                            }
                        }

                        // Check whether the comment contained any text.
                        if (!found)
                        {
                            // This is only allowed if this comment is sandwiched in between two other comments.
                            bool comment = false;
                            int  lines   = 0;
                            foreach (CsToken item in element.Tokens.ReverseIterator(tokenNode.Previous))
                            {
                                if (item.Text == "\n")
                                {
                                    ++lines;
                                    if (lines > 1)
                                    {
                                        break;
                                    }
                                }
                                else if (item.CsTokenType == CsTokenType.SingleLineComment)
                                {
                                    comment = true;
                                    break;
                                }
                                else if (item.CsTokenType != CsTokenType.WhiteSpace)
                                {
                                    break;
                                }
                            }

                            if (!comment)
                            {
                                CsElement errorElement = token.Parent as CsElement ?? element;
                                this.AddViolation(errorElement, token.Location, Rules.CommentsMustContainText);
                            }
                            else
                            {
                                comment = false;
                                lines   = 0;
                                foreach (CsToken item in element.Tokens.ForwardIterator(tokenNode.Next))
                                {
                                    if (item.Text == "\n")
                                    {
                                        ++lines;
                                        if (lines > 1)
                                        {
                                            break;
                                        }
                                    }
                                    else if (item.CsTokenType == CsTokenType.SingleLineComment)
                                    {
                                        comment = true;
                                        break;
                                    }
                                    else if (item.CsTokenType != CsTokenType.WhiteSpace)
                                    {
                                        break;
                                    }
                                }

                                if (!comment)
                                {
                                    CsElement errorElement = token.Parent as CsElement ?? element;
                                    this.AddViolation(errorElement, token.Location, Rules.CommentsMustContainText);
                                }
                            }
                        }
                    }
                    else if (token.CsTokenType == CsTokenType.MultiLineComment)
                    {
                        // The is a multi-line comment. Get the start of the comment.
                        int start = token.Text.IndexOf("/*", StringComparison.Ordinal);
                        if (start > -1)
                        {
                            // Get the end of the comment
                            int end = token.Text.IndexOf("*/", start + 2, StringComparison.Ordinal);
                            if (end > -1)
                            {
                                // Look at the characters between the start and the end and see if there
                                // is anything here besides whitespace.
                                bool found = false;
                                for (int character = start + 2; character < end; ++character)
                                {
                                    if (token.Text[character] != ' ' && token.Text[character] != '\t' && token.Text[character] != '\r' && token.Text[character] != '\n')
                                    {
                                        found = true;
                                        break;
                                    }
                                }

                                // Check whether the comment contained any text.
                                if (!found)
                                {
                                    CsElement errorElement = token.Parent as CsElement ?? element;
                                    this.AddViolation(errorElement, token.Location, Rules.CommentsMustContainText);
                                }
                            }
                        }
                    }
                }
            }
        }