1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
25
namespace ICSharpCode.Decompiler.Tests
27
static class CodeSampleFileParser
29
public static IEnumerable<string> ListSections(string s)
31
var query = from line in ToLines(s)
32
let sectionName = ReadSectionName(line)
33
where sectionName != null
38
public static string GetSection(string sectionName, string s)
40
var lines = ToLines(s);
42
bool sectionFound = false;
43
var sectionText = new StringBuilder();
45
Action<string> parser = null;
47
Action<string> commonSectionReader = line =>
49
if (IsCommonSectionEnd(line))
52
sectionText.AppendLine(line);
55
Action<string> namedSectionReader = line =>
57
string name = ReadSectionName(line);
59
sectionText.AppendLine(line);
60
else if (name != sectionName)
64
Action<string> defaultReader = line =>
66
if (IsCommonSectionStart(line))
67
parser = commonSectionReader;
68
else if (ReadSectionName(line) == sectionName)
70
parser = namedSectionReader;
75
foreach(var line in lines)
77
(parser ?? defaultReader)(line);
81
return sectionText.ToString();
86
public static bool IsCommentOrBlank(string s)
88
if(String.IsNullOrWhiteSpace(s))
90
return s.Trim().StartsWith("//");
93
public static string ConcatLines(IEnumerable<string> lines)
95
var buffer = new StringBuilder();
96
foreach (var line in lines)
98
buffer.AppendLine(line);
100
return buffer.ToString();
103
static string ReadSectionName(string line)
105
line = line.TrimStart();
106
if (line.StartsWith("//$$"))
107
return line.Substring(4).Trim();
112
static bool IsCommonSectionStart(string line)
114
return line.Trim() == "//$CS";
117
static bool IsCommonSectionEnd(string line)
119
return line.Trim() == "//$CE";
122
static IEnumerable<string> ToLines(string s)
124
var reader = new StringReader(s);
126
while ((line = reader.ReadLine()) != null)