/// <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;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the CodeParser class.
        /// </summary>
        /// <param name="parser">The C# parser.</param>
        /// <param name="lexer">The lexer to use for parsing the code.</param>
        public CodeParser(CsParser parser, CodeLexer lexer)
        {
            Param.AssertNotNull(parser, "parser");
            Param.AssertNotNull(lexer, "lexer");

            this.parser = parser;
            this.lexer = lexer;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the CodeLexer class.
        /// </summary>
        /// <param name="parser">The C# parser.</param>
        /// <param name="source">The source to read.</param>
        /// <param name="codeReader">Used for reading the source code.</param>
        internal CodeLexer(CsParser parser, SourceCode source, CodeReader codeReader)
        {
            Param.AssertNotNull(parser, "parser");
            Param.AssertNotNull(source, "source");
            Param.AssertNotNull(codeReader, "codeReader");

            this.parser = parser;
            this.source = source;
            this.codeReader = codeReader;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the CodeParser class.
        /// </summary>
        /// <param name="parser">The C# parser.</param>
        /// <param name="symbols">The symbols in the document to parse.</param>
        public CodeParser(CsParser parser, SymbolManager symbols)
        {
            Param.AssertNotNull(parser, "parser");
            Param.AssertNotNull(symbols, "symbols");

            this.parser = parser;
            this.symbols = symbols;
        }
Exemplo n.º 5
0
 public CodeParser(CsParser parser, SymbolManager symbols)
 {
     this.tokens = new MasterList<CsToken>();
     this.parser = parser;
     this.symbols = symbols;
 }
Exemplo n.º 6
0
 public CodeParser(CsParser parser, CodeLexer lexer)
 {
     this.tokens = new MasterList<CsToken>();
     this.parser = parser;
     this.lexer = lexer;
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
0
 internal CodeLexer(CsParser parser, Microsoft.StyleCop.SourceCode source, CodeReader codeReader)
 {
     this.parser = parser;
     this.source = source;
     this.codeReader = codeReader;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Builds code document from specified source code.
        /// </summary>
        private static CsDocument BuildCodeDocument(string sourceCode)
        {
            string tempFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CodeHelperTest.cs");
            File.WriteAllText(tempFile, StyleCop43Compatibility.ModifySourceForTest(sourceCode));

            CodeProject project = new CodeProject(0, string.Empty, new Configuration(null));
            CsParser parser = new CsParser();
            CodeFile file = new CodeFile(tempFile, project, parser);

            CodeDocument doc = null;
            parser.ParseFile(file, 0, ref doc);

            File.Delete(tempFile);

            return (CsDocument)doc;
        }