Ejemplo n.º 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;
 }
Ejemplo n.º 2
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;
        }