-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSharpGenerator.cs
204 lines (170 loc) · 7.54 KB
/
CSharpGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace Maussoft.Mvc.ViewGen
{
public class CSharpGenerator : Generator
{
private string _baseDirecory;
private string _defaultNamespace;
private string _sessionClass;
public CSharpGenerator(string baseDirecory, string defaultNamespace, string sessionClass)
{
_baseDirecory = baseDirecory;
_defaultNamespace = defaultNamespace;
_sessionClass = sessionClass;
}
public override void ConvertFile(string filename)
{
string relative = filename.Substring(_baseDirecory.TrimEnd(Path.DirectorySeparatorChar).Length + 1);
//Console.WriteLine (filename2);
//Console.WriteLine (_baseDirecory);
//Console.WriteLine (relative);
List<Token> input = Tokenize(File.ReadAllText(filename));
/*for (int i = 0; i < input.Count; i++) {
Token token = input [i];
Console.WriteLine (token.Type + " '" + token.Value + "'");
}*/
Dictionary<string, string> properties = GetDefaultProperties();
string reldir = Path.GetDirectoryName(relative);
string spaceName = _defaultNamespace;
if (reldir.Length > 0)
{
spaceName = spaceName + '.' + reldir.Replace(Path.DirectorySeparatorChar, '.');
}
properties["Namespace"] = spaceName;
properties["Class"] = Path.GetFileNameWithoutExtension(relative);
List<Statement> statements = Parse(input, properties);
/*foreach (string key in properties.Keys) {
Console.WriteLine (key + " '" + properties[key] + "'");
}*/
Generate(properties, statements, filename);
}
private void Generate(Dictionary<string, string> properties, List<Statement> statements, string filename)
{
EscapeProperties(properties);
string output = GenerateClass(properties, statements);
string filename2 = Path.ChangeExtension(filename, ".cs");
/*Console.WriteLine (filename2);
Console.WriteLine (output);*/
File.WriteAllText(filename2, output);
}
private void EscapeProperties(Dictionary<string, string> properties)
{
List<string> keywords = new List<string>("abstract,as,base,bool,break,byte,case,catch,char,checked,class,const,continue,decimal,default,delegate,do,double,else,enum,event,explicit,extern,false,finally,fixed,float,for,foreach,goto,if,implicit,in,in (generic modifier),int,interface,internal,is,lock,long,namespace,new,null,object,operator,out,out (generic modifier),override,params,private,protected,public,readonly,ref,return,sbyte,sealed,short,sizeof,stackalloc,static,string,struct,switch,this,throw,true,try,typeof,uint,ulong,unchecked,unsafe,ushort,using,virtual,void,volatile,while".Split(','));
string[] keys = new string[] { "Class", "Inherits" };
foreach (string key in keys)
{
if (keywords.Contains(properties[key]))
{
properties[key] = '@' + properties[key];
}
}
}
private string GenerateClass(Dictionary<string, string> properties, List<Statement> statements)
{
StringBuilder output = new StringBuilder();
output.Append("/**\n * WARNING: Generated file, do not edit, changes will be lost!\n **/\n\n");
output.Append("using System;\n");
if (properties.ContainsKey("Using"))
{
string[] spaceNames = properties["Using"].Split(',');
foreach (string spaceName in spaceNames)
{
output.Append("using " + spaceName.Trim() + ";\n");
}
}
string functionName = "Content";
if (properties["Type"] == "Master")
{
functionName = "Header";
}
output.Append("\nnamespace " + properties["Namespace"] + "\n{\n\t");
output.Append("public class " + properties["Class"] + ": " + properties["Inherits"] + "\n\t{\n\t\t");
output.Append("public override void " + functionName + "()\n\t\t{\n\t\t\t");
if (properties["Type"] == "Master")
{
foreach (Statement statement in statements)
{
if (statement.Type == "Code" && statement.Tokens.Length == 1)
{
if (statement.Tokens[0].Value.Trim() == "RenderViewContent();")
{
statement.Tokens[0].Value = "\n\t\t}\n\n\t\tpublic override void Footer()\n\t\t{\n\t\t\t";
}
break;
}
}
}
output.Append(GenerateStatements(statements));
output.Append("\n\t\t}\n\t}\n}");
output.Replace("\n\t\t\t\n", "\n");
return output.ToString();
}
private string GenerateStatements(List<Statement> statements)
{
StringBuilder output = new StringBuilder();
for (int i = 0; i < statements.Count; i++)
{
Token[] tokens = statements[i].Tokens;
if (tokens[0].Type == "Code")
{
output.Append(tokens[0].Value);
}
else if (tokens[0].Type == "NewLine")
{
output.Append("\n\t\t\t");
}
else if (tokens[0].Type == "Text" || tokens[0].Type == "TextLine")
{
if (tokens.Length == 1 && tokens[0].Value == "")
{
if (tokens[0].Type == "TextLine")
{
output.Append("WriteLine();");
}
continue;
}
List<string> arguments = new List<string>();
if (tokens[0].Type == "TextLine")
{
output.Append("WriteLine(@\"");
}
else
{
output.Append("Write(@\"");
}
for (int j = 0; j < tokens.Length; j++)
{
Token token = tokens[j];
if (token.Type == "Expr")
{
output.Append("{" + arguments.Count + "}");
arguments.Add(token.Value);
}
else if (token.Type == "Text" || tokens[0].Type == "TextLine")
{
output.Append(token.Value.Replace("\"", "\"\""));
}
}
if (arguments.Count > 0)
{
output.Append("\", " + String.Join(", ", arguments.ToArray()) + ");");
}
else
{
output.Append("\");");
}
}
}
return output.ToString();
}
private Dictionary<string, string> GetDefaultProperties()
{
Dictionary<string, string> properties = new Dictionary<string, string>();
properties.Add("Inherits", "Maussoft.Mvc.View<" + _sessionClass + ">");
return properties;
}
}
}