Пример #1
0
        /// <summary>
        /// Initializes a new instance of the CodeLexer class.
        /// </summary>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="source">The source to read.</param>
        /// <param name="codeReader">Used for reading the source code.</param>
        internal CodeLexer(CsLanguageService languageService, Code source, CodeReader codeReader)
        {
            Param.AssertNotNull(languageService, "languageService");
            Param.AssertNotNull(source, "source");
            Param.AssertNotNull(codeReader, "codeReader");

            this.languageService = languageService;
            this.source = source;
            this.codeReader = codeReader;
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the CodeParser class.
        /// </summary>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="lexer">The lexer to use for parsing the code.</param>
        /// <param name="preprocessorDefinitions">The optional preprocessor flags.</param>
        public CodeParser(CsLanguageService languageService, CodeLexer lexer, Dictionary<string, object> preprocessorDefinitions)
        {
            Param.AssertNotNull(languageService, "languageService");
            Param.AssertNotNull(lexer, "lexer");
            Param.Ignore(preprocessorDefinitions);

            this.languageService = languageService;
            this.lexer = lexer;
            this.preprocessorDefinitions = preprocessorDefinitions;
        }
Пример #3
0
        ////[TestMethod]
        public void TestNamespace()
        {
            string sourceCode =
@"namespace Namespace1
{
    public class Class1
    {
    }
}";
            CsLanguageService languageService = new CsLanguageService();
            CsDocument document1 = languageService.CreateCodeModel(sourceCode, "source1.cs", "test");
            CsDocument document2 = languageService.CreateCodeModel(sourceCode, "source2.cs", "test");

            Comparer comparer = new Comparer();
            comparer.AreEqual(document1, document2);
        }
Пример #4
0
        /// <summary>
        /// Parses the given file.
        /// </summary>
        /// <param name="sourceCode">The source code to parse.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <param name="document">The parsed representation of the file.</param>
        /// <returns>Returns false if no further analysis should be done on this file, or
        /// true if the file should be parsed again during the next pass.</returns>
        public override bool ParseFile(SourceCode sourceCode, int passNumber, ref ICodeDocument 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
                        {
                            CsLanguageService languageService = new CsLanguageService();
                            
                            document = new CsDocumentWrapper(
                                this, 
                                sourceCode, 
                                languageService.CreateCodeModel(reader, sourceCode.Name, sourceCode.Path));
                        }
                    }
                }
                catch (SyntaxException syntaxex)
                {
                    this.AddViolation(sourceCode, syntaxex.LineNumber, Rules.SyntaxException, syntaxex.Message);
                    document = null;
                }
            }

            return false;
        }
Пример #5
0
        private void ReadAndWriteFile(string filePath)
        {
            Console.WriteLine("Checking file " + filePath);

            if (File.Exists(filePath))
            {
                string fileContents = null;

                try
                {
                    fileContents = File.ReadAllText(filePath);
                }
                catch (IOException)
                {
                }

                CsLanguageService languageService = new CsLanguageService(preprocessorDefinitions);
                CsDocument doc = null;

                try
                {
                    doc = languageService.CreateCodeModel(fileContents, Path.GetFileName(filePath), filePath);
                }
                catch (SyntaxException)
                {
                }
                catch (Exception ex)
                {
                    Assert.Fail("Exception from CodeModel: " + ex.GetType() + ", " + ex.Message + ". FilePath=" + filePath);
                }

                if (doc != null)
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        doc.Write(writer);

                        this.CompareDocs(fileContents, writer.ToString(), filePath);
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the CodeLexer class.
        /// </summary>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="source">The source to read.</param>
        /// <param name="codeReader">Used for reading the source code.</param>
        /// <param name="index">The starting absolute index of the code being parsed.</param>
        /// <param name="indexOnLine">The starting index on line of the code being parsed.</param>
        /// <param name="lineNumber">The starting line number of the code being parsed.</param>
        internal CodeLexer(CsLanguageService languageService, Code source, CodeReader codeReader, int index, int indexOnLine, int lineNumber)
        {
            Param.AssertNotNull(languageService, "languageService");
            Param.AssertNotNull(source, "source");
            Param.AssertNotNull(codeReader, "codeReader");
            Param.AssertGreaterThanOrEqualToZero(index, "index");
            Param.AssertGreaterThanOrEqualToZero(indexOnLine, "indexOnLine");
            Param.AssertGreaterThanZero(lineNumber, "lineNumber");

            this.languageService = languageService;
            this.source = source;
            this.codeReader = codeReader;

            this.marker.Index = index;
            this.marker.IndexOnLine = indexOnLine;
            this.marker.LineNumber = lineNumber;
        }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the CodeParser class.
        /// </summary>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="document">The document being parsed.</param>
        /// <param name="symbols">The symbols in the document to parse.</param>
        /// <param name="preprocessorDefinitions">The optional preprocessor flags.</param>
        public CodeParser(CsLanguageService languageService, CsDocument document, SymbolManager symbols, IDictionary<string, object> preprocessorDefinitions)
        {
            Param.AssertNotNull(languageService, "languageService");
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(symbols, "symbols");
            Param.Ignore(preprocessorDefinitions);

            this.languageService = languageService;
            this.document = document;
            this.symbols = symbols;
            this.preprocessorDefinitions = preprocessorDefinitions;
        }
        /// <summary>
        /// Extracts the body of the given preprocessor directive symbol, parses it, and returns the parsed expression.
        /// </summary>
        /// <param name="document">The parent document.</param>
        /// <param name="code">The source code.</param>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="preprocessorDefinitions">Optional preprocessor definitions.</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(
            CsDocument document, 
            Code code, 
            CodeUnitProxy parentProxy, 
            CsLanguageService languageService, 
            IDictionary<string, object> preprocessorDefinitions,
            Symbol preprocessorSymbol, 
            int startIndex)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(code, "code");
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.AssertNotNull(languageService, "languageService");
            Param.Ignore(preprocessorDefinitions);
            Param.AssertNotNull(preprocessorSymbol, "preprocessorSymbol");
            Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex");
            CsLanguageService.Debug.Assert(preprocessorSymbol.SymbolType == SymbolType.PreprocessorDirective, "The symbol is not a preprocessor directive.");

            string text = preprocessorSymbol.Text.Substring(startIndex, preprocessorSymbol.Text.Length - startIndex).TrimEnd(null);
            if (text.Length > 0)
            {
                // Trim off the whitespace at the beginning and advance the start index.
                int trimIndex = 0;
                for (int i = 0; i < text.Length; ++i)
                {
                    if (char.IsWhiteSpace(text[i]))
                    {
                        ++trimIndex;
                    }
                    else
                    {
                        break;
                    }
                }

                if (trimIndex > 0)
                {
                    text = text.Substring(trimIndex, text.Length - trimIndex);
                    startIndex += trimIndex;
                }

                if (text.Length > 0)
                {
                    // Extract the symbols within this text.
                    Code preprocessorCode = new Code(text, "Preprocessor", "Preprocessor");
                    
                    var lexer = new CodeLexer(
                        languageService,
                        preprocessorCode,
                        new CodeReader(preprocessorCode),
                        preprocessorSymbol.Location.StartPoint.Index + startIndex,
                        preprocessorSymbol.Location.StartPoint.IndexOnLine + startIndex,
                        preprocessorSymbol.Location.StartPoint.LineNumber);

                    List<Symbol> symbolList = lexer.GetSymbols(document, null);
                    var directiveSymbols = new SymbolManager(symbolList);

                    var preprocessorBodyParser = new CodeParser(languageService, document, directiveSymbols, preprocessorDefinitions);

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

            // The directive has no body.
            return null;
        }
Пример #9
0
        /// <summary>
        /// Extracts the body of the given preprocessor directive symbol, parses it, and returns the parsed expression.
        /// </summary>
        /// <param name="document">The parent document.</param>
        /// <param name="code">The source code.</param>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="preprocessorDefinitions">Optional preprocessor definitions.</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(
            CsDocument document,
            Code code,
            CodeUnitProxy parentProxy,
            CsLanguageService languageService,
            IDictionary <string, object> preprocessorDefinitions,
            Symbol preprocessorSymbol,
            int startIndex)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(code, "code");
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.AssertNotNull(languageService, "languageService");
            Param.Ignore(preprocessorDefinitions);
            Param.AssertNotNull(preprocessorSymbol, "preprocessorSymbol");
            Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex");
            CsLanguageService.Debug.Assert(preprocessorSymbol.SymbolType == SymbolType.PreprocessorDirective, "The symbol is not a preprocessor directive.");

            string text = preprocessorSymbol.Text.Substring(startIndex, preprocessorSymbol.Text.Length - startIndex).TrimEnd(null);

            if (text.Length > 0)
            {
                // Trim off the whitespace at the beginning and advance the start index.
                int trimIndex = 0;
                for (int i = 0; i < text.Length; ++i)
                {
                    if (char.IsWhiteSpace(text[i]))
                    {
                        ++trimIndex;
                    }
                    else
                    {
                        break;
                    }
                }

                if (trimIndex > 0)
                {
                    text        = text.Substring(trimIndex, text.Length - trimIndex);
                    startIndex += trimIndex;
                }

                if (text.Length > 0)
                {
                    // Extract the symbols within this text.
                    Code preprocessorCode = new Code(text, "Preprocessor", "Preprocessor");

                    var lexer = new CodeLexer(
                        languageService,
                        preprocessorCode,
                        new CodeReader(preprocessorCode),
                        preprocessorSymbol.Location.StartPoint.Index + startIndex,
                        preprocessorSymbol.Location.StartPoint.IndexOnLine + startIndex,
                        preprocessorSymbol.Location.StartPoint.LineNumber);

                    List <Symbol> symbolList       = lexer.GetSymbols(document, null);
                    var           directiveSymbols = new SymbolManager(symbolList);

                    var preprocessorBodyParser = new CodeParser(languageService, document, directiveSymbols, preprocessorDefinitions);

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

            // The directive has no body.
            return(null);
        }