PageRenderTime 76ms CodeModel.GetById 70ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19using System;
 20using System.Collections.Generic;
 21using System.Linq;
 22using System.Text;
 23using System.IO;
 24
 25namespace 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			s = s.Trim();
 91			return s.StartsWith("//") || s.StartsWith("#");	// Also ignore #pragmas for warning suppression
 92		}
 93
 94		public static string ConcatLines(IEnumerable<string> lines)
 95		{
 96			var buffer = new StringBuilder();
 97			foreach (var line in lines)
 98			{
 99				buffer.AppendLine(line);
100			}
101			return buffer.ToString();
102		}
103
104		static string ReadSectionName(string line)
105		{
106			line = line.TrimStart();
107			if (line.StartsWith("//$$"))
108				return line.Substring(4).Trim();
109			else
110				return null;
111		}
112
113		static bool IsCommonSectionStart(string line)
114		{
115			return line.Trim() == "//$CS";
116		}
117
118		static bool IsCommonSectionEnd(string line)
119		{
120			return line.Trim() == "//$CE";
121		}
122
123		static IEnumerable<string> ToLines(string s)
124		{
125			var reader = new StringReader(s);
126			string line;
127			while ((line = reader.ReadLine()) != null)
128			{
129				yield return line;
130			}
131		}
132	}
133}