Example #1
0
 public override bool ParseFile(SourceCode sourceCode, int passNumber, ref CodeDocument document)
 {
     Param.RequireNotNull(sourceCode, "sourceCode");
     Param.RequireGreaterThanOrEqualToZero(passNumber, "passNumber");
     if (passNumber == 0)
     {
         try
         {
             using (TextReader reader = sourceCode.Read())
             {
                 if (reader == null)
                 {
                     base.AddViolation(sourceCode, 1, Microsoft.StyleCop.CSharp.Rules.FileMustBeReadable, new object[0]);
                 }
                 else
                 {
                     CodeLexer lexer = new CodeLexer(this, sourceCode, new CodeReader(reader));
                     CodeParser parser = new CodeParser(this, lexer);
                     parser.ParseDocument();
                     document = parser.Document;
                 }
             }
         }
         catch (SyntaxException exception)
         {
             base.AddViolation(exception.SourceCode, exception.LineNumber, Microsoft.StyleCop.CSharp.Rules.SyntaxException, new object[] { exception.Message });
             CsDocument document2 = new CsDocument(sourceCode, this);
             document2.FileHeader = new FileHeader(string.Empty);
             document = document2;
         }
     }
     return false;
 }
        /// <summary>
        /// Extracts the body of the given preprocessor directive symbol, parses it, and returns the parsed expression.
        /// </summary>
        /// <param name="parser">The C# parser.</param>
        /// <param name="sourceCode">The source code containing the preprocessor directive symbol.</param>
        /// <param name="preprocessorSymbol">The preprocessor directive symbol.</param>
        /// <param name="startIndex">The index of the start of the expression body within the text string.</param>
        /// <returns>Returns the expression.</returns>
        internal static Expression GetConditionalPreprocessorBodyExpression(
             CsParser parser, SourceCode sourceCode, Symbol preprocessorSymbol, int startIndex)
        {
            Param.AssertNotNull(parser, "parser");
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertNotNull(preprocessorSymbol, "preprocessorSymbol");
            Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex");
            Debug.Assert(preprocessorSymbol.SymbolType == SymbolType.PreprocessorDirective, "The symbol is not a preprocessor directive.");

            string text = preprocessorSymbol.Text.Substring(startIndex, preprocessorSymbol.Text.Length - startIndex).Trim();
            if (text.Length > 0)
            {
                using (StringReader reader = new StringReader(text))
                {
                    // Extract the symbols within this text.
                    CodeLexer lexer = new CodeLexer(parser, sourceCode, new CodeReader(reader));
                    List<Symbol> symbolList = lexer.GetSymbols(sourceCode, null);
                    SymbolManager directiveSymbols = new SymbolManager(symbolList);

                    CodeParser preprocessorBodyParser = new CodeParser(parser, directiveSymbols);

                    // Parse these symbols to create the body expression.
                    return preprocessorBodyParser.GetNextConditionalPreprocessorExpression(sourceCode);
                }
            }

            // The directive has no body.
            return null;
        }
Example #3
0
        /// <summary>
        /// Gets an attribute from the code.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the attribute lies within an unsafe code block.</param>
        /// <returns>Returns the attribute.</returns>
        private Attribute GetAttribute(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            CodeParser attributeParser = new CodeParser(this.parser, this.symbols);
            return attributeParser.ParseAttribute(parentReference, unsafeCode, this.document);
        }
Example #4
0
 private Microsoft.StyleCop.CSharp.Attribute GetAttribute(bool unsafeCode)
 {
     CodeParser parser = new CodeParser(this.parser, this.symbols);
     return parser.ParseAttribute(unsafeCode);
 }
Example #5
0
 internal static Expression GetConditionalPreprocessorBodyExpression(CsParser parser, SourceCode sourceCode, Symbol preprocessorSymbol, int startIndex)
 {
     string s = preprocessorSymbol.Text.Substring(startIndex, preprocessorSymbol.Text.Length - startIndex).Trim();
     if (s.Length > 0)
     {
         StringReader code = new StringReader(s);
         CodeLexer lexer = new CodeLexer(parser, sourceCode, new CodeReader(code));
         SymbolManager symbols = new SymbolManager(lexer.GetSymbols(sourceCode, null));
         CodeParser parser2 = new CodeParser(parser, symbols);
         return parser2.GetNextConditionalPreprocessorExpression(sourceCode);
     }
     return null;
 }
Example #6
0
        public override bool ParseFile(SourceCode sourceCode, int passNumber, ref CodeDocument document)
        {
            Param.RequireNotNull(sourceCode, "sourceCode");
            Param.RequireGreaterThanOrEqualToZero(passNumber, "passNumber");
            Param.Ignore(document);

            // The document is parsed on the first pass. On any subsequent passes, we do not do anything.
            if (passNumber == 0)
            {
                try
                {
                    using (TextReader reader = sourceCode.Read())
                    {
                        // Create the document.
                        if (reader == null)
                        {
                            this.AddViolation(sourceCode, 1, Rules.FileMustBeReadable);
                        }
                        else
                        {
                            // Create the lexer object for the code string.
                            CodeLexer lexer = new CodeLexer(this, sourceCode, new CodeReader(reader));

                            // Parse the document.
                            CodeParser parser = new CodeParser(this, lexer);
                            parser.ParseDocument();

                            document = parser.Document;
                        }
                    }
                }
                catch (SyntaxException syntaxex)
                {
                    this.AddViolation(syntaxex.SourceCode, syntaxex.LineNumber, Rules.SyntaxException, syntaxex.Message);
                    CsDocument csdocument = new CsDocument(sourceCode, this);
                    csdocument.FileHeader = new FileHeader(string.Empty, new CsTokenList(csdocument.Tokens), new Reference<ICodePart>(csdocument));
                    document = csdocument;
                }
            }

            return false;
        }