~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.Decompiler/Tests/CodeSampleFileParser.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
using System.Text;
 
23
using System.IO;
 
24
 
 
25
namespace ICSharpCode.Decompiler.Tests
 
26
{
 
27
        static class CodeSampleFileParser
 
28
        {
 
29
                public static IEnumerable<string> ListSections(string s)
 
30
                {
 
31
                        var query = from line in ToLines(s)
 
32
                                                let sectionName = ReadSectionName(line)
 
33
                                                where sectionName != null
 
34
                                                select sectionName;
 
35
                        return query;
 
36
                }
 
37
 
 
38
                public static string GetSection(string sectionName, string s)
 
39
                {
 
40
                        var lines = ToLines(s);
 
41
 
 
42
                        bool sectionFound = false;
 
43
                        var sectionText = new StringBuilder();
 
44
 
 
45
                        Action<string> parser = null;
 
46
 
 
47
                        Action<string> commonSectionReader = line =>
 
48
                                {
 
49
                                        if (IsCommonSectionEnd(line))
 
50
                                                parser = null;
 
51
                                        else
 
52
                                                sectionText.AppendLine(line);
 
53
                                };
 
54
 
 
55
                        Action<string> namedSectionReader = line =>
 
56
                                {
 
57
                                        string name = ReadSectionName(line);
 
58
                                        if (name == null)
 
59
                                                sectionText.AppendLine(line);
 
60
                                        else if (name != sectionName)
 
61
                                                parser = null;
 
62
                                };
 
63
 
 
64
                        Action<string> defaultReader = line =>
 
65
                                {
 
66
                                        if (IsCommonSectionStart(line))
 
67
                                                parser = commonSectionReader;
 
68
                                        else if (ReadSectionName(line) == sectionName)
 
69
                                        {
 
70
                                                parser = namedSectionReader;
 
71
                                                sectionFound = true;
 
72
                                        }
 
73
                                };
 
74
 
 
75
                        foreach(var line in lines)
 
76
                        {
 
77
                                (parser ?? defaultReader)(line);
 
78
                        }
 
79
 
 
80
                        if (sectionFound)
 
81
                                return sectionText.ToString();
 
82
                        else
 
83
                                return "";
 
84
                }
 
85
 
 
86
                public static bool IsCommentOrBlank(string s)
 
87
                {
 
88
                        if(String.IsNullOrWhiteSpace(s))
 
89
                                return true;
 
90
                        return s.Trim().StartsWith("//");
 
91
                }
 
92
 
 
93
                public static string ConcatLines(IEnumerable<string> lines)
 
94
                {
 
95
                        var buffer = new StringBuilder();
 
96
                        foreach (var line in lines)
 
97
                        {
 
98
                                buffer.AppendLine(line);
 
99
                        }
 
100
                        return buffer.ToString();
 
101
                }
 
102
 
 
103
                static string ReadSectionName(string line)
 
104
                {
 
105
                        line = line.TrimStart();
 
106
                        if (line.StartsWith("//$$"))
 
107
                                return line.Substring(4).Trim();
 
108
                        else
 
109
                                return null;
 
110
                }
 
111
 
 
112
                static bool IsCommonSectionStart(string line)
 
113
                {
 
114
                        return line.Trim() == "//$CS";
 
115
                }
 
116
 
 
117
                static bool IsCommonSectionEnd(string line)
 
118
                {
 
119
                        return line.Trim() == "//$CE";
 
120
                }
 
121
 
 
122
                static IEnumerable<string> ToLines(string s)
 
123
                {
 
124
                        var reader = new StringReader(s);
 
125
                        string line;
 
126
                        while ((line = reader.ReadLine()) != null)
 
127
                        {
 
128
                                yield return line;
 
129
                        }
 
130
                }
 
131
        }
 
132
}