/ICSharpCode.Decompiler/Tests/CodeSampleFileParser.cs

http://github.com/icsharpcode/ILSpy · C# · 133 lines · 100 code · 16 blank · 17 comment · 19 complexity · 6c4d4402fb51f7acd7187ce8d7ed35b5 MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using System.IO;
  23. namespace ICSharpCode.Decompiler.Tests
  24. {
  25. static class CodeSampleFileParser
  26. {
  27. public static IEnumerable<string> ListSections(string s)
  28. {
  29. var query = from line in ToLines(s)
  30. let sectionName = ReadSectionName(line)
  31. where sectionName != null
  32. select sectionName;
  33. return query;
  34. }
  35. public static string GetSection(string sectionName, string s)
  36. {
  37. var lines = ToLines(s);
  38. bool sectionFound = false;
  39. var sectionText = new StringBuilder();
  40. Action<string> parser = null;
  41. Action<string> commonSectionReader = line =>
  42. {
  43. if (IsCommonSectionEnd(line))
  44. parser = null;
  45. else
  46. sectionText.AppendLine(line);
  47. };
  48. Action<string> namedSectionReader = line =>
  49. {
  50. string name = ReadSectionName(line);
  51. if (name == null)
  52. sectionText.AppendLine(line);
  53. else if (name != sectionName)
  54. parser = null;
  55. };
  56. Action<string> defaultReader = line =>
  57. {
  58. if (IsCommonSectionStart(line))
  59. parser = commonSectionReader;
  60. else if (ReadSectionName(line) == sectionName)
  61. {
  62. parser = namedSectionReader;
  63. sectionFound = true;
  64. }
  65. };
  66. foreach(var line in lines)
  67. {
  68. (parser ?? defaultReader)(line);
  69. }
  70. if (sectionFound)
  71. return sectionText.ToString();
  72. else
  73. return "";
  74. }
  75. public static bool IsCommentOrBlank(string s)
  76. {
  77. if(String.IsNullOrWhiteSpace(s))
  78. return true;
  79. s = s.Trim();
  80. return s.StartsWith("//") || s.StartsWith("#"); // Also ignore #pragmas for warning suppression
  81. }
  82. public static string ConcatLines(IEnumerable<string> lines)
  83. {
  84. var buffer = new StringBuilder();
  85. foreach (var line in lines)
  86. {
  87. buffer.AppendLine(line);
  88. }
  89. return buffer.ToString();
  90. }
  91. static string ReadSectionName(string line)
  92. {
  93. line = line.TrimStart();
  94. if (line.StartsWith("//$$"))
  95. return line.Substring(4).Trim();
  96. else
  97. return null;
  98. }
  99. static bool IsCommonSectionStart(string line)
  100. {
  101. return line.Trim() == "//$CS";
  102. }
  103. static bool IsCommonSectionEnd(string line)
  104. {
  105. return line.Trim() == "//$CE";
  106. }
  107. static IEnumerable<string> ToLines(string s)
  108. {
  109. var reader = new StringReader(s);
  110. string line;
  111. while ((line = reader.ReadLine()) != null)
  112. {
  113. yield return line;
  114. }
  115. }
  116. }
  117. }