/// <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; }
/// <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); }
/// <summary> /// Initializes a new instance of the CsDocumentWrapper class. /// </summary> /// <param name="parser">The parser.</param> /// <param name="sourceCode">The original source code.</param> public CsDocumentWrapper(CsParser parser, SourceCode sourceCode) { Param.AssertNotNull(parser, "parser"); Param.AssertNotNull(sourceCode, "sourceCode"); this.parser = parser; this.sourceCode = sourceCode; }
/// <summary> /// Initializes a new instance of the CsDocument class. /// </summary> /// <param name="sourceCode"> /// The source code that this document represents. /// </param> /// <param name="parser"> /// The parser that is creating this object. /// </param> internal CsDocument(SourceCode sourceCode, CsParser parser) : this(sourceCode, parser, null) { Param.AssertNotNull(sourceCode, "sourceCode"); Param.AssertNotNull(parser, "parser"); this.tokens = new MasterList <CsToken>(); }
/// <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; }
/// <summary> /// Initializes a new instance of the CsDocument class. /// </summary> /// <param name="sourceCode"> /// The source code that this document represents. /// </param> /// <param name="parser"> /// The parser that is creating this object. /// </param> /// <param name="tokens"> /// The tokens in the document. /// </param> internal CsDocument(SourceCode sourceCode, CsParser parser, MasterList <CsToken> tokens) : base(sourceCode) { Param.AssertNotNull(sourceCode, "sourceCode"); Param.AssertNotNull(parser, "parser"); Param.Ignore(tokens); this.parser = parser; this.tokens = tokens; }
protected override void Dispose(bool disposing) { Param.Ignore(disposing); base.Dispose(disposing); if (disposing) { this.contents = null; this.tokens = null; this.fileHeader = null; this.parser = null; } }
/// <summary> /// Initializes a new instance of the CsDocumentWrapper class. /// </summary> /// <param name="parser">The parser.</param> /// <param name="sourceCode">The original source code.</param> /// <param name="document">The document to wrap.</param> public CsDocumentWrapper(CsParser parser, SourceCode sourceCode, CsDocument document) : base(document) { Param.AssertNotNull(parser, "parser"); Param.AssertNotNull(sourceCode, "sourceCode"); Param.AssertNotNull(document, "document"); this.parser = parser; this.sourceCode = sourceCode; this.document = document; Debug.Assert(this.document.Tag == null, "Wrapper has already been set!"); this.document.Tag = this; }
/// <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, sourceCode); CodeProject project = new CodeProject(0, string.Empty, new Configuration(null)); CsParser parser = new CsParser(); parser.FileTypes.Add("CS"); parser.PreParse(); CodeFile file = new CodeFile(tempFile, project, parser); CodeDocument doc = null; parser.ParseFile(file, 0, ref doc); File.Delete(tempFile); return (CsDocument)doc; }
/// <summary> /// Adds any suppressions for the given element by scanning the attributes on the element. /// </summary> private void AddRuleSuppressionsForElement() { if (this.element != null && this.element.Attributes != null && this.element.Attributes.Count > 0) { foreach (StyleCop.CSharp.CodeModel.Attribute attribute in this.element.Attributes) { if (attribute.AttributeExpressions != null) { foreach (AttributeExpression attributeExpression in attribute.AttributeExpressions) { if (attributeExpression.Initialization != null) { MethodInvocationExpression methodInvocation = attributeExpression.Initialization as MethodInvocationExpression; if (methodInvocation != null) { if (IsCodeAnalysisSuppression(methodInvocation.Name)) { // Crack open the expression and extract the rule checkID. string checkId; string ruleName; string ruleNamespace; if (TryCrackCodeAnalysisSuppression(methodInvocation, out checkId, out ruleName, out ruleNamespace)) { Debug.Assert(!string.IsNullOrEmpty(checkId), "Rule ID should not be null"); Debug.Assert(!string.IsNullOrEmpty(ruleName), "Rule Name should not be null"); Debug.Assert(!string.IsNullOrEmpty(ruleNamespace), "Rule Namespace should not be null"); CsParser parser = CsDocumentWrapper.Wrapper(this.element.Document).Parser; parser.AddRuleSuppression(this.element, checkId, ruleName, ruleNamespace); } } } } } } } } }
/// <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; }
public static CsDocument ParseCsFile(string filePath, CodeProject project) { var csharpParser = new CsParser(); var sourceCode = new CodeFile(Path.GetFullPath(filePath), project, csharpParser); return Parse(sourceCode, project); }
/// <summary> /// Initializes a new instance of the <see cref="SharpToCppInterpreter"/> class. /// Initializes a new instance of the <see cref="CsDocumentSaver"/> class. /// </summary> /// <param name="document"> /// The document. /// </param> public SharpToCppInterpreter(CsDocument document) { this.document = document; this.name = this.Document.SourceCode.Name.Substring(0, this.Document.SourceCode.Name.Length - 3); this.emptyCodeProject = new CodeProject( string.Empty.GetHashCode(), string.Empty, new Configuration(new string[] { })); this.defaultParser = new CsParser(); }
/// <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; }
public static CsDocument ParseCode(string code, CodeProject project) { var csharpParser = new CsParser(); var sourceCode = new CodeText(code, project, csharpParser); return Parse(sourceCode, project); }