// Searches the given source file for inline IL snippets. static InlineILSnippet [] FindInlineILSnippets(ILanguage lang, string pathSourceFile) { ArrayList snippets = new ArrayList(); // An IL snippet begins at the first line prefixed with the startMarker // and goes until the first endMarker after that. string stStartMarker = lang.StartMarker.ToLower(); string stEndMarker = lang.EndMarker.ToLower(); TextReader reader = new StreamReader(new FileStream(pathSourceFile, FileMode.Open)); StringCollection list = null; int idxStartLine = 0; // 0 means we're not tracking a snippet. int idxLine = 0; // current line into source file. string line; while ((line = reader.ReadLine()) != null) { string lineLowercase = line.ToLower(); idxLine++; if (idxStartLine != 0) { if (lineLowercase == stEndMarker) { // We found the end of the IL snippet. int idxEndLine = idxLine - 1; // end line was the previous line InlineILSnippet snippet = new InlineILSnippet(pathSourceFile, idxStartLine, idxEndLine, list); snippets.Add(snippet); idxStartLine = 0; // reset tracking the IL snippet. list = null; } else { list.Add(line); } } if (lineLowercase.StartsWith(stStartMarker)) { // We found the start of an IL snippet. The actual snippet will start at the next line. list = new StringCollection(); idxStartLine = idxLine + 1; } } // If we got to the end of the file and are still tracking, then we have an unterminated inline IL segment. if (idxStartLine != 0) { throw new ArgumentException("Unterminated Inline IL segment in file '" + pathSourceFile + "' starting with '" + stStartMarker + "' at line '" + idxStartLine + "'. Expecting to find closing '" + stEndMarker + "'"); } return((InlineILSnippet[])snippets.ToArray(typeof(InlineILSnippet))); }
// Insert a snippet of IL into the document. public void InsertSnippet(InlineILSnippet snippet) { int idxStartLine = snippet.StartLine; int idxEndLine = snippet.EndLine; // We need to find where to place the IL snippet in our ildasm output. // If the IL snippet is at source line (f, g) (inclusive), the ildasm should contain // consecutive .line directives such '.line x' and '.line y' such that (x > f) && (g > y). // Once we find such a pair, we can inject the ilasm snippet into the ilasm document at // the line before the one containing '.line y'. // intentionally pick MaxValue so that (idxLast < idxStartLine) is false until we initialize idxLast int idxLast = int.MaxValue; int idxInsertAt = -1; int idxIlasmLine = 1; // source files are 1-based. foreach (string line in m_lines) { int idxCurrent = GetLineMarker(line); if (idxCurrent != 0) { if ((idxLast < idxStartLine) && (idxEndLine < idxCurrent)) { // What if there are multiple such values of (x,y)? // Probably should inject at each spot. - which means we may need a while-loop here instead of foreach if (idxInsertAt != -1) { throw new NotImplementedException("ILAsm snippet needs to be inserted at multiple spots."); } // Found snippets location! Insert before ilasm source line idxIlasmLine // (which is index idxIlasmLine-1, since the array is 0-based) idxInsertAt = idxIlasmLine - 1; } idxLast = idxCurrent; } idxIlasmLine++; } if (idxInsertAt == -1) { throw new ArgumentException("Can't find where to place " + snippet); } m_lines = Util.InsertArrayIntoArray(m_lines, snippet.Lines, idxInsertAt); }
// Searches the given source file for inline IL snippets. static List<InlineILSnippet> FindInlineILSnippets(ILDocument doc) { var snippets = new List<InlineILSnippet>(); var sourceFiles = doc.GetSourceFiles(); foreach (var f in sourceFiles) { var lang = GetLanguageForFile(f); // An IL snippet begins at the first line prefixed with the startMarker // and goes until the first endMarker after that. var stStartMarker = lang.StartMarker.ToLower(); var stEndMarker = lang.EndMarker.ToLower(); TextReader reader = new StreamReader(new FileStream(f, FileMode.Open)); StringCollection list = null; var idxStartLine = 0; // 0 means we're not tracking a snippet. var idxLine = 0; // current line into source file. string line; while ((line = reader.ReadLine()) != null) { var lineLowercase = line.ToLower(); idxLine++; if (idxStartLine != 0) { if (lineLowercase == stEndMarker) { // We found the end of the IL snippet. var idxEndLine = idxLine - 1; // end line was the previous line var snippet = new InlineILSnippet(f, idxStartLine, idxEndLine, list); snippets.Add(snippet); idxStartLine = 0; // reset tracking the IL snippet. list = null; } else { list.Add(line); } } if (!lineLowercase.StartsWith(stStartMarker)) continue; // We found the start of an IL snippet. The actual snippet will start at the next line. list = new StringCollection(); idxStartLine = idxLine + 1; } // If we got to the end of the file and are still tracking, then we have an unterminated inline IL segment. if (idxStartLine != 0) { throw new ArgumentException(string.Format("Unterminated Inline IL segment in file '{0}' starting with '{1}' at line '{2}'. Expecting to find closing '{3}'", f, stStartMarker, idxStartLine, stEndMarker)); } } return snippets; }
// Insert a snippet of IL into the document. public int FindSnippetLocation(InlineILSnippet snippet) { var snippetStart = snippet.StartLine; var snippetEnd = snippet.EndLine; // We need to find where to place the IL snippet in our ildasm output. // If the IL snippet is at source line (f, g) (inclusive), the ildasm should contain // consecutive .line directives such '.line x' and '.line y' such that (x > f) && (g > y). // Once we find such a pair, we can inject the ilasm snippet into the ilasm document at // the line before the one containing '.line y'. // intentionally pick MaxValue so that (idxLast < idxStartLine) is false until we initialize idxLast var lastKnownLine = int.MaxValue; var idxInsertAt = -1; var idxIlasmLine = 1; // source files are 1-based. var fileName = String.Empty; foreach (var line in Lines) { //Console.WriteLine(line); Match m; if ((m = _lineFileRegex.Match(line)).Success) { fileName = m.Groups["filename"].Value; //Console.WriteLine("Switching to {0} @ line {1}", fileName, idxIlasmLine); } if (fileName != snippet.Sourcefile) { idxIlasmLine++; continue; } var idxCurrent = GetLineMarker(line); if (idxCurrent != 0) { if ((lastKnownLine < snippetStart) && (snippetEnd < idxCurrent)) { // What if there are multiple such values of (x,y)? // Probably should inject at each spot. - which means we may need a while-loop here instead of foreach if (idxInsertAt != -1) { throw new Exception("ILAsm snippet needs to be inserted at multiple spots."); } // Found snippets location! Insert before ilasm source line idxIlasmLine // (which is index idxIlasmLine-1, since the array is 0-based) //Console.WriteLine("Found possible snippet {0}:{1}-{2} location @ line {3}", // snippet.Sourcefile, snippet.StartLine, snippet.EndLine, idxIlasmLine); idxInsertAt = idxIlasmLine - 1; } lastKnownLine = idxCurrent; } idxIlasmLine++; } if (idxInsertAt == -1) { throw new ArgumentException("Can't find where to place " + snippet); } //Console.WriteLine("Inserting to .il file @ {0}", idxInsertAt); //Lines = Util.InsertArrayIntoArray(Lines, snippet.Lines, idxInsertAt); return(idxInsertAt); }
// Searches the given source file for inline IL snippets. static List <InlineILSnippet> FindInlineILSnippets(ILDocument doc) { var snippets = new List <InlineILSnippet>(); var sourceFiles = doc.GetSourceFiles(); foreach (var f in sourceFiles) { var lang = GetLanguageForFile(f); // An IL snippet begins at the first line prefixed with the startMarker // and goes until the first endMarker after that. var stStartMarker = lang.StartMarker.ToLower(); var stEndMarker = lang.EndMarker.ToLower(); TextReader reader = new StreamReader(new FileStream(f, FileMode.Open)); StringCollection list = null; var idxStartLine = 0; // 0 means we're not tracking a snippet. var idxLine = 0; // current line into source file. string line; while ((line = reader.ReadLine()) != null) { var lineLowercase = line.ToLower(); idxLine++; if (idxStartLine != 0) { if (lineLowercase == stEndMarker) { // We found the end of the IL snippet. var idxEndLine = idxLine - 1; // end line was the previous line var snippet = new InlineILSnippet(f, idxStartLine, idxEndLine, list); snippets.Add(snippet); idxStartLine = 0; // reset tracking the IL snippet. list = null; } else { list.Add(line); } } if (!lineLowercase.StartsWith(stStartMarker)) { continue; } // We found the start of an IL snippet. The actual snippet will start at the next line. list = new StringCollection(); idxStartLine = idxLine + 1; } // If we got to the end of the file and are still tracking, then we have an unterminated inline IL segment. if (idxStartLine != 0) { throw new ArgumentException(string.Format("Unterminated Inline IL segment in file '{0}' starting with '{1}' at line '{2}'. Expecting to find closing '{3}'", f, stStartMarker, idxStartLine, stEndMarker)); } } return(snippets); }
// Insert a snippet of IL into the document. public int FindSnippetLocation(InlineILSnippet snippet) { var snippetStart = snippet.StartLine; var snippetEnd = snippet.EndLine; // We need to find where to place the IL snippet in our ildasm output. // If the IL snippet is at source line (f, g) (inclusive), the ildasm should contain // consecutive .line directives such '.line x' and '.line y' such that (x > f) && (g > y). // Once we find such a pair, we can inject the ilasm snippet into the ilasm document at // the line before the one containing '.line y'. // intentionally pick MaxValue so that (idxLast < idxStartLine) is false until we initialize idxLast var lastKnownLine = int.MaxValue; var idxInsertAt = -1; var idxIlasmLine = 1; // source files are 1-based. var fileName = String.Empty; foreach (var line in Lines) { //Console.WriteLine(line); Match m; if ((m = _lineFileRegex.Match(line)).Success) { fileName = m.Groups["filename"].Value; //Console.WriteLine("Switching to {0} @ line {1}", fileName, idxIlasmLine); } if (fileName != snippet.Sourcefile) { idxIlasmLine++; continue; } var idxCurrent = GetLineMarker(line); if (idxCurrent != 0) { if ((lastKnownLine < snippetStart) && (snippetEnd < idxCurrent)) { // What if there are multiple such values of (x,y)? // Probably should inject at each spot. - which means we may need a while-loop here instead of foreach if (idxInsertAt != -1) throw new Exception("ILAsm snippet needs to be inserted at multiple spots."); // Found snippets location! Insert before ilasm source line idxIlasmLine // (which is index idxIlasmLine-1, since the array is 0-based) //Console.WriteLine("Found possible snippet {0}:{1}-{2} location @ line {3}", // snippet.Sourcefile, snippet.StartLine, snippet.EndLine, idxIlasmLine); idxInsertAt = idxIlasmLine - 1; } lastKnownLine = idxCurrent; } idxIlasmLine++; } if (idxInsertAt == -1) { throw new ArgumentException("Can't find where to place " + snippet); } //Console.WriteLine("Inserting to .il file @ {0}", idxInsertAt); //Lines = Util.InsertArrayIntoArray(Lines, snippet.Lines, idxInsertAt); return idxInsertAt; }