Add support for namespaces

This commit is contained in:
Starbeamrainbowlabs 2016-08-04 12:15:15 +01:00
parent bab0024461
commit 9a8c0aa520
1 changed files with 49 additions and 25 deletions

View File

@ -4,6 +4,7 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.ConstrainedExecution;
namespace cscz namespace cscz
{ {
@ -42,6 +43,7 @@ namespace cscz
public List<string> Signatures = new List<string>(); public List<string> Signatures = new List<string>();
public string ClassName = "Carrot"; public string ClassName = "Carrot";
public string Namespace = string.Empty;
public GenerationMode GenerationMode = GenerationMode.PublicVariables; public GenerationMode GenerationMode = GenerationMode.PublicVariables;
@ -87,6 +89,12 @@ namespace cscz
{ {
// It's the class name // It's the class name
ClassName = UppercaseFirstLetter(nextLine.TrimStart(new char[] { '#' })).Trim(); ClassName = UppercaseFirstLetter(nextLine.TrimStart(new char[] { '#' })).Trim();
// Parse the namespace, if one is present
if(ClassName.Contains("."))
{
Namespace = ClassName.Substring(0, ClassName.LastIndexOf("."));
ClassName = ClassName.Substring(ClassName.LastIndexOf(".") + 1);
}
} }
else else
{ {
@ -97,22 +105,23 @@ namespace cscz
public override string ToString() public override string ToString()
{ {
StringBuilder result = new StringBuilder(); StringWriter usingStatements = new StringWriter();
// Using statements // Generate the using statements
foreach(string usingStatement in UsingStatements) foreach(string usingStatement in UsingStatements)
result.AppendLine(string.Format("using {0};", expandUsingShortcut(usingStatement))); usingStatements.WriteLine("using {0};", expandUsingShortcut(usingStatement));
result.AppendLine(); // Generate the class itself
StringWriter classCode = new StringWriter();
// Class name // Class name
result.AppendLine(string.Format("class {0} ", ClassName)); classCode.WriteLine("class {0} ", ClassName);
result.AppendLine("{"); classCode.WriteLine("{");
// Class body parts declaration // Class body parts declaration
StringBuilder properties = new StringBuilder(); StringBuilder constructorSignature = new StringBuilder(string.Format("\tpublic {0}(", ClassName)); // This has to be a StringBuilder because of the Remove() class later on
StringBuilder constructorSignature = new StringBuilder(string.Format("\tpublic {0}(", ClassName)); StringWriter properties = new StringWriter();
StringBuilder constructorBody = new StringBuilder(); StringWriter constructorBody = new StringWriter();
// Data member signature handling // Data member signature handling
foreach (string signature in Signatures) foreach (string signature in Signatures)
@ -127,39 +136,54 @@ namespace cscz
if (CreateProperties) if (CreateProperties)
{ {
// Private data member // Private data member
result.AppendLine(string.Format("\tprivate {0} {1};", datatypeName, privateDataMemberName)); classCode.WriteLine("\tprivate {0} {1};", datatypeName, privateDataMemberName);
// Public property associated with above private data member // Public property associated with above private data member
properties.AppendLine(string.Format("\tpublic {0} {1}", datatypeName, publicDataMemberName)); properties.WriteLine("\tpublic {0} {1}", datatypeName, publicDataMemberName);
properties.AppendLine(string.Format("\t{{")); properties.WriteLine("\t{{");
properties.AppendLine(string.Format("\t\tget {{ return {0}; }}", privateDataMemberName)); properties.WriteLine("\t\tget {{ return {0}; }}", privateDataMemberName);
properties.AppendLine(string.Format("\t\tset {{ {0} = value; }}", privateDataMemberName)); properties.WriteLine("\t\tset {{ {0} = value; }}", privateDataMemberName);
properties.AppendLine(string.Format("\t}}")); properties.WriteLine("\t}}");
// Constructor body // Constructor body
constructorBody.AppendLine(string.Format("\t\t{0} = in{0};", publicDataMemberName)); constructorBody.WriteLine("\t\t{0} = in{0};", publicDataMemberName);
} }
else else
{ {
// Constuctor body // Constuctor body
result.AppendLine(string.Format("\tpublic {0} {1};", datatypeName, publicDataMemberName)); classCode.WriteLine("\tpublic {0} {1};", datatypeName, publicDataMemberName);
constructorBody.AppendLine(string.Format("\t\t{0} = in{0};", publicDataMemberName)); constructorBody.WriteLine("\t\t{0} = in{0};", publicDataMemberName);
} }
} }
// Add the properties to the output // Add the properties to the output
result.AppendLine(properties.ToString()); classCode.WriteLine(properties.ToString());
// Add the constructor to the output // Add the constructor to the output
constructorSignature.Remove(constructorSignature.Length - 2, 2); // Remove the last ", " constructorSignature.Remove(constructorSignature.Length - 2, 2); // Remove the last ", "
constructorSignature.Append(")"); constructorSignature.Append(")");
result.AppendLine(constructorSignature.ToString()); classCode.WriteLine(constructorSignature.ToString());
result.AppendLine("\t{"); classCode.WriteLine("\t{");
result.Append(constructorBody.ToString()); classCode.Write(constructorBody.ToString());
result.AppendLine("\t}"); classCode.WriteLine("\t}");
// Close the class off and end the file // Close the class off and end the file
result.AppendLine("}"); classCode.WriteLine("}");
StringWriter result = new StringWriter();
result.WriteLine(usingStatements);
if(Namespace == string.Empty)
{
result.WriteLine(classCode);
}
else
{
result.WriteLine("namespace {0}", Namespace);
result.WriteLine("{");
result.WriteLine("\t" + classCode.ToString().Replace("\n", "\n\t"));
result.WriteLine("}");
}
return result.ToString(); return result.ToString();
} }