-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreSharpGenerator.RegionProcessing.cs
318 lines (270 loc) · 11.3 KB
/
PreSharpGenerator.RegionProcessing.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Parts of this file were based on Eric Parker's code available at
// http://web.archive.org/web/20060726075638/http://www.inmetrix.com/
/* Copyright (C) 2003 Inmetrix Corp. (www.inmetrix.com);
* All rights reserved.
*
* LICENSE
* You may use or modify the code in this file for your own projects, either
* personal or commerical, with the following restrictions:
*
* 1) You may not directly sell this code as part of a code generation library or product.
* 2) You must include the above copyright notice in any derived products.
* 3) NO WARRANTY. This code is provided on an "AS IS" basis.
* 4) Inmetrix Corp. shall not be liable to licensee or to any other party in any way as a result of using this code.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
partial class PreSharpGenerator {
private static readonly string PRESHARP_TEMPLATE_MARKER = "#if PRESHARP_TEMPLATE";
private static readonly string PRESHARP_TEMPLATE_LIBRARY_MARKER = "#if PRESHARP_TEMPLATE_LIBRARY";
private string processTemplateRegions(string[] lines,
string file) {
StringBuilder outputBuilder = new StringBuilder();
IEnumerator<string> lineIterator = lines.Cast<string>().GetEnumerator();
int lineNumberBeforeScript = -1;
int ifNesting = 0;
while (lineIterator.MoveNext()) {
string currentLine = lineIterator.Current;
string trimmedLine = currentLine.Trim();
lineNumberBeforeScript += 1;
outputBuilder.AppendLine(currentLine);
bool regionIsTemplate = trimmedLine == PRESHARP_TEMPLATE_MARKER;
bool regionIsTemplateLibrary = trimmedLine == PRESHARP_TEMPLATE_LIBRARY_MARKER || trimmedLine.StartsWith(PRESHARP_TEMPLATE_LIBRARY_MARKER + "_");
string writerName = "writer";
if (regionIsTemplateLibrary) {
if (trimmedLine.Length > PRESHARP_TEMPLATE_LIBRARY_MARKER.Length) {
writerName = trimmedLine.Substring(PRESHARP_TEMPLATE_LIBRARY_MARKER.Length + 1);
}
}
if (regionIsTemplate || regionIsTemplateLibrary) {
int linesOfThisScript = 0;
bool afterElse = false;
ifNesting += 1;
StringBuilder region = new StringBuilder();
while (ifNesting > 0 && lineIterator.MoveNext()) {
currentLine = lineIterator.Current;
trimmedLine = currentLine.TrimStart();
linesOfThisScript += 1;
if (trimmedLine.StartsWith("#if")) {
ifNesting += 1;
} else if (trimmedLine.StartsWith("#endif")) {
ifNesting -= 1;
} else if (trimmedLine.StartsWith("#else") && ifNesting == 1) {
afterElse = true;
}
if (ifNesting > 0 && !afterElse) {
region.AppendLine(currentLine);
outputBuilder.AppendLine(currentLine);
}
}
if (ifNesting == 0) {
string generatedCode;
if (regionIsTemplateLibrary) {
generatedCode = generateTemplateLibraryCode(new StringReader(region.ToString()), writerName);
} else {
generatedCode = generateTemplateRegionOutput(file, region.ToString(), lineNumberBeforeScript);
}
if (generatedCode != null) {
outputBuilder.AppendLine("#else");
outputBuilder.AppendLine("#region PreSharp Generated");
if (regionIsTemplateLibrary) {
outputBuilder.AppendLine(generatedCode);
} else {
outputBuilder.Append(generatedCode);
}
outputBuilder.AppendLine("#endregion");
}
outputBuilder.AppendLine("#endif");
lineNumberBeforeScript += linesOfThisScript;
}
}
}
return outputBuilder.ToString();
}
private static string generateTemplateLibraryCode(TextReader reader, string writerName) {
State state = State.TemplateMode;
StringBuilder temp = new StringBuilder();
StringBuilder code = new StringBuilder();
string indentation = string.Empty;
code.Append(indentation);
while (reader.Peek() > -1) {
state = processChar(ref indentation, (char)reader.Read(), state, temp, code, writerName);
}
dump(state, temp, code, writerName);
return code.ToString().TrimEnd();
}
private string generateTemplateRegionOutput(string file,
string templateRegionCode,
int lineNumberDelta) {
string prefix =
"\r\n" +
"using System;\r\n" +
"using System.IO;\r\n" +
"\r\n" +
"namespace PreSharp {\r\n" +
"\r\n" +
" internal static class Generator {\r\n" +
"\r\n" +
" public static StringWriter Generate() {\r\n" +
"\r\n" +
" StringWriter writer = new StringWriter();\r\n";
string suffix = "\r\n" +
" return writer;\r\n" +
" }\r\n" +
" }\r\n" +
"}\r\n";
string templateLibraryCode;
Assembly generatedAssembly = generateTemplateLibraryAssembly(file,
templateRegionCode,
prefix,
suffix,
defaultFileAndLineMatcher,
lineNumberDelta - 11,
"writer",
out templateLibraryCode);
if (generatedAssembly == null) {
return null;
}
try {
StringWriter writer = (StringWriter)generatedAssembly.GetType("PreSharp.Generator").GetMethod("Generate", BindingFlags.Static | BindingFlags.Public).Invoke(null, null);
return writer.ToString();
} catch (Exception exception) {
logger.LogException(file, exception);
return null;
}
}
private enum State {
TemplateMode,
ScriptMode,
ScriptEval,
LeftAngle,
LeftAnglePercent,
Percent,
EvalPercent,
};
private static State processChar(ref string indentation, char ch, State state, StringBuilder temp, StringBuilder code, string writerName) {
switch (state) {
case State.TemplateMode:
if (ch == '<') {
state = State.LeftAngle;
} else {
accumulateTemplateChar(ref indentation, ch, state, temp, code, writerName);
}
break;
case State.LeftAngle:
if (ch == '%') {
state = State.LeftAnglePercent;
} else {
accumulateTemplateChar(ref indentation, '<', state, temp, code, writerName);
state = State.TemplateMode;
state = processChar(ref indentation, ch, state, temp, code, writerName);
}
break;
case State.LeftAnglePercent:
if (ch == '=') {
dump(state, temp, code, writerName);
state = State.ScriptEval;
} else {
code.Append(" ");
if (code.ToString().EndsWith(indentation)) {
code.Remove(code.Length - indentation.Length, indentation.Length);
}
dump(state, temp, code, writerName);
state = State.ScriptMode;
goto case State.ScriptMode;
}
break;
case State.ScriptMode:
if (ch == '%') {
state = State.Percent;
} else {
if (ch == '{') {
indentation += " ";
} else if (ch == '}') {
if (indentation.Length >= 4) {
indentation = indentation.Substring(0, indentation.Length - 4);
}
}
temp.Append(ch);
}
break;
case State.ScriptEval:
if (ch == '%') {
state = State.EvalPercent;
} else {
temp.Append(ch);
}
break;
case State.EvalPercent:
case State.Percent:
if (ch == '>') {
dump(state, temp, code, writerName);
if (state == State.Percent) {
code.Append(indentation);
}
state = State.TemplateMode;
} else {
temp.Append(ch);
}
break;
}
return state;
}
private static void accumulateTemplateChar(ref string indentation, char ch, State state, StringBuilder temp, StringBuilder code, string writerName) {
switch (ch) {
case '\\':
temp.Append("\\\\");
break;
case '\r':
temp.Append("\\r");
break;
case '\n':
temp.Append("\\n");
break;
case '"':
temp.Append("\\\"");
break;
case '\t':
temp.Append("\\t");
break;
default:
temp.Append(ch);
break;
}
if (ch == '\n') {
dump(state, temp, code, writerName);
code.AppendLine();
code.Append(indentation);
}
}
private static void dump(State state, StringBuilder temp, StringBuilder code, string writerName) {
if (temp.Length != 0) {
switch (state) {
case State.TemplateMode:
case State.LeftAngle:
case State.LeftAnglePercent:
code.Append(string.Format("{0}.Write(\"{1}\");", writerName, temp));
break;
case State.ScriptEval:
case State.EvalPercent:
code.Append(string.Format("{0}.Write({1});", writerName, temp));
break;
default:
code.Append(temp);
break;
}
temp.Length = 0;
}
}
private static string TrimLastNewLine(string s) {
if (s.Length >= 2) {
return s.Substring(0, s.Length - 2);
} else {
return s;
}
}
}