158 lines
4.5 KiB
C#
158 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace cscz
|
|
{
|
|
public class ClassGenerator
|
|
{
|
|
/// <summary>
|
|
/// A mapping of namespaces to using shortcuts.
|
|
/// </summary>
|
|
public Dictionary<string, string> UsingShortcuts = new Dictionary<string, string>()
|
|
{
|
|
{ "s", "System" },
|
|
{ "c", "System.Collections.Generic" },
|
|
{ "cc", "System.Collections.Concurrent" },
|
|
{ "r", "System.Text.RegularExpressions" }
|
|
};
|
|
|
|
/// <summary>
|
|
/// The using statements to add to the generated class.
|
|
/// </summary>
|
|
public List<string> UsingStatements = new List<string>()
|
|
{
|
|
"s"
|
|
};
|
|
public List<string> Signatures = new List<string>();
|
|
|
|
public string ClassName = "Carrot";
|
|
|
|
/// <summary>
|
|
/// Whether to make data members private and create public properties for them instead of making the
|
|
/// data members public.
|
|
/// </summary>
|
|
public bool CreateProperties = true;
|
|
|
|
public ClassGenerator ()
|
|
{
|
|
}
|
|
|
|
public void ParseString(string source)
|
|
{
|
|
StringReader lines = new StringReader(source);
|
|
|
|
string nextLine = string.Empty;
|
|
do {
|
|
nextLine = lines.ReadLine();
|
|
|
|
if(nextLine == null) break;
|
|
if(nextLine.Length == 0) continue;
|
|
|
|
// It's a using statement
|
|
if(nextLine.StartsWith("u "))
|
|
{
|
|
string[] parts = Regex.Split(nextLine.Trim(), @"\s+");
|
|
UsingStatements.Add(parts[1]);
|
|
}
|
|
else if(nextLine.StartsWith("#"))
|
|
{
|
|
// It's the class name
|
|
ClassName = UppercaseFirstLetter(nextLine.TrimStart(new char[] { '#' })).Trim();
|
|
}
|
|
else
|
|
{
|
|
Signatures.Add(nextLine.Trim());
|
|
}
|
|
} while(nextLine != null);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder result = new StringBuilder();
|
|
foreach(string usingStatement in UsingStatements)
|
|
result.AppendLine(string.Format("using {0};", expandUsingShortcut(usingStatement)));
|
|
|
|
result.AppendLine();
|
|
result.AppendLine(string.Format("class {0} ", ClassName));
|
|
result.AppendLine("{");
|
|
|
|
StringBuilder properties = new StringBuilder();
|
|
StringBuilder constructorSignature = new StringBuilder(string.Format("\tpublic {0}(", ClassName));
|
|
StringBuilder constructorBody = new StringBuilder();
|
|
|
|
foreach (string signature in Signatures)
|
|
{
|
|
string[] parts = Regex.Split(signature, @"\s+");
|
|
string datatypeName = parts[0];
|
|
string privateDataMemberName = LowercaseFirstLetter(parts[1]);
|
|
string publicDataMemberName = UppercaseFirstLetter(parts[1]);
|
|
|
|
constructorSignature.AppendFormat("{0} in{1}, ", datatypeName, publicDataMemberName);
|
|
|
|
if (CreateProperties)
|
|
{
|
|
result.AppendLine(string.Format("\tprivate {0} {1};", datatypeName, privateDataMemberName));
|
|
|
|
properties.AppendLine(string.Format("\tpublic {0} {1}", datatypeName, publicDataMemberName));
|
|
properties.AppendLine(string.Format("\t{{"));
|
|
properties.AppendLine(string.Format("\t\tget {{ return {0}; }}", privateDataMemberName));
|
|
properties.AppendLine(string.Format("\t\tset {{ {0} = value; }}", privateDataMemberName));
|
|
properties.AppendLine(string.Format("\t}}"));
|
|
|
|
constructorBody.AppendLine(string.Format("\t\t{0} = in{0};", publicDataMemberName));
|
|
}
|
|
else
|
|
{
|
|
result.AppendLine(string.Format("\tpublic {0} {1};"));
|
|
constructorBody.AppendLine(string.Format("\t\t{0} = in{1};", privateDataMemberName, publicDataMemberName));
|
|
}
|
|
}
|
|
|
|
// Add the properties to the output
|
|
result.AppendLine("\t");
|
|
result.AppendLine(properties.ToString());
|
|
|
|
// Add the constructor to the output
|
|
constructorSignature.Remove(constructorSignature.Length - 2, 2); // Remove the last ", "
|
|
constructorSignature.Append(")");
|
|
result.AppendLine(constructorSignature.ToString());
|
|
result.AppendLine("\t{");
|
|
result.Append(constructorBody.ToString());
|
|
result.AppendLine("\t}");
|
|
|
|
// Close the class off and end the file
|
|
result.AppendLine("}");
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
protected string expandUsingShortcut(string shortcut)
|
|
{
|
|
if (UsingShortcuts.ContainsKey(shortcut))
|
|
return UsingShortcuts[shortcut];
|
|
else
|
|
return shortcut;
|
|
}
|
|
|
|
private string UppercaseFirstLetter(string str)
|
|
{
|
|
if (str == null)
|
|
return null;
|
|
if (str.Length > 1)
|
|
return char.ToUpper(str[0]) + str.Substring(1);
|
|
return str.ToUpper();
|
|
}
|
|
private string LowercaseFirstLetter(string str)
|
|
{
|
|
if (str == null)
|
|
return null;
|
|
if (str.Length > 1)
|
|
return char.ToLower(str[0]) + str.Substring(1);
|
|
return str.ToLower();
|
|
}
|
|
}
|
|
} |