-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathScriptCompiler.cs
More file actions
432 lines (346 loc) · 15.6 KB
/
ScriptCompiler.cs
File metadata and controls
432 lines (346 loc) · 15.6 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// ScriptCompiler.cs
// Script#/Core/Compiler
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using ScriptSharp.CodeModel;
using ScriptSharp.Compiler;
using ScriptSharp.Generator;
using ScriptSharp.Importer;
using ScriptSharp.ResourceModel;
using ScriptSharp.ScriptModel;
using ScriptSharp.Validator;
using ScriptSharp.SymbolFile;
namespace ScriptSharp {
/// <summary>
/// The Script# compiler.
/// </summary>
public sealed class ScriptCompiler : IErrorHandler {
private CompilerOptions _options;
private IErrorHandler _errorHandler;
private ParseNodeList _compilationUnitList;
private SymbolSet _symbols;
private ICollection<TypeSymbol> _importedSymbols;
private ICollection<TypeSymbol> _appSymbols;
private bool _hasErrors;
#if DEBUG
private string _testOutput;
#endif
public ScriptCompiler()
: this(null) {
}
public ScriptCompiler(IErrorHandler errorHandler) {
_errorHandler = errorHandler;
}
private void BuildCodeModel() {
_compilationUnitList = new ParseNodeList();
CodeModelBuilder codeModelBuilder = new CodeModelBuilder(_options, this);
CodeModelValidator codeModelValidator = new CodeModelValidator(this);
CodeModelProcessor validationProcessor = new CodeModelProcessor(codeModelValidator, _options);
foreach (IStreamSource source in _options.Sources) {
CompilationUnitNode compilationUnit = codeModelBuilder.BuildCodeModel(source);
if (compilationUnit != null) {
validationProcessor.Process(compilationUnit);
_compilationUnitList.Append(compilationUnit);
}
}
}
private void BuildImplementation() {
CodeBuilder codeBuilder = new CodeBuilder(_options, this);
ICollection<SymbolImplementation> implementations = codeBuilder.BuildCode(_symbols);
if (_options.Minimize) {
foreach (SymbolImplementation impl in implementations) {
if (impl.Scope == null) {
continue;
}
SymbolObfuscator obfuscator = new SymbolObfuscator();
SymbolImplementationTransformer transformer = new SymbolImplementationTransformer(obfuscator);
transformer.TransformSymbolImplementation(impl);
}
}
}
private void BuildMetadata() {
if ((_options.Resources != null) && (_options.Resources.Count != 0)) {
ResourcesBuilder resourcesBuilder = new ResourcesBuilder(_symbols);
resourcesBuilder.BuildResources(_options.Resources);
}
MetadataBuilder mdBuilder = new MetadataBuilder(this);
_appSymbols = mdBuilder.BuildMetadata(_compilationUnitList, _symbols, _options);
// Check if any of the types defined in this assembly conflict.
Dictionary<string, TypeSymbol> types = new Dictionary<string, TypeSymbol>();
foreach (TypeSymbol appType in _appSymbols) {
if ((appType.IsApplicationType == false) || (appType.Type == SymbolType.Delegate)) {
// Skip the check for types that are marked as imported, as they
// aren't going to be generated into the script.
// Delegates are implicitly imported types, as they're never generated into
// the script.
continue;
}
if ((appType.Type == SymbolType.Class) &&
(((ClassSymbol)appType).PrimaryPartialClass != appType)) {
// Skip the check for partial types, since they should only be
// checked once.
continue;
}
// TODO: We could allow conflicting types as long as both aren't public
// since they won't be on the exported types list. Internal types that
// conflict could be generated using full name.
string name = appType.GeneratedName;
if (types.ContainsKey(name)) {
string error = "The type '" + appType.FullName + "' conflicts with with '" + types[name].FullName + "' as they have the same name.";
((IErrorHandler)this).ReportError(error, null);
}
else {
types[name] = appType;
}
}
// Capture whether there are any test types in the project
// when not compiling the test flavor script. This is used to determine
// if the test flavor script should be compiled in the build task.
if (_options.IncludeTests == false) {
foreach (TypeSymbol appType in _appSymbols) {
if (appType.IsApplicationType && appType.IsTestType) {
_options.HasTestTypes = true;
}
}
}
#if DEBUG
if (_options.InternalTestType == "metadata") {
StringWriter testWriter = new StringWriter();
testWriter.WriteLine("Metadata");
testWriter.WriteLine("================================================================");
SymbolSetDumper symbolDumper = new SymbolSetDumper(testWriter);
symbolDumper.DumpSymbols(_symbols);
testWriter.WriteLine();
testWriter.WriteLine();
_testOutput = testWriter.ToString();
}
#endif // DEBUG
ISymbolTransformer transformer = null;
if (_options.Minimize) {
transformer = new SymbolObfuscator();
}
else {
transformer = new SymbolInternalizer();
}
if (transformer != null) {
SymbolSetTransformer symbolSetTransformer = new SymbolSetTransformer(transformer);
ICollection<Symbol> transformedSymbols = symbolSetTransformer.TransformSymbolSet(_symbols, /* useInheritanceOrder */ true);
#if DEBUG
if (_options.InternalTestType == "minimizationMap") {
StringWriter testWriter = new StringWriter();
testWriter.WriteLine("Minimization Map");
testWriter.WriteLine("================================================================");
List<Symbol> sortedTransformedSymbols = new List<Symbol>(transformedSymbols);
sortedTransformedSymbols.Sort(delegate(Symbol s1, Symbol s2) {
return String.Compare(s1.Name, s2.Name);
});
foreach (Symbol transformedSymbol in sortedTransformedSymbols) {
Debug.Assert(transformedSymbol is MemberSymbol);
testWriter.WriteLine(" Member '" + transformedSymbol.Name + "' renamed to '" + transformedSymbol.GeneratedName + "'");
}
testWriter.WriteLine();
testWriter.WriteLine();
_testOutput = testWriter.ToString();
}
#endif // DEBUG
}
}
public bool Compile(CompilerOptions options) {
if (options == null) {
throw new ArgumentNullException("options");
}
_options = options;
_hasErrors = false;
_symbols = new SymbolSet();
ImportMetadata();
if (_hasErrors) {
return false;
}
BuildCodeModel();
if (_hasErrors) {
return false;
}
BuildMetadata();
if (_hasErrors) {
return false;
}
BuildImplementation();
if (_hasErrors) {
return false;
}
GenerateScript();
if (_hasErrors) {
return false;
}
GenerateSymbol();
if (_hasErrors) {
return false;
}
return true;
}
private void GenerateSymbol() {
Stream outputStream = null;
TextWriter outputWriter = null;
try {
outputStream = _options.SymbolFile.GetStream();
if (outputStream == null) {
((IErrorHandler)this).ReportError("Unable to write to file " + _options.SymbolFile.FullName,
_options.SymbolFile.FullName);
return;
}
outputWriter = new StreamWriter(outputStream);
var sfg = new SymbolFileGenerator(outputWriter);
sfg.Generate(_symbols);
} catch (Exception e) {
Debug.Fail(e.ToString());
} finally {
if (outputWriter != null) {
outputWriter.Flush();
}
if (outputStream != null) {
_options.SymbolFile.CloseStream(outputStream);
}
}
}
private void GenerateScript() {
Stream outputStream = null;
TextWriter outputWriter = null;
try {
outputStream = _options.ScriptFile.GetStream();
if (outputStream == null) {
((IErrorHandler)this).ReportError("Unable to write to file " + _options.ScriptFile.FullName,
_options.ScriptFile.FullName);
return;
}
outputWriter = new StreamWriter(outputStream);
#if DEBUG
if (_options.InternalTestMode) {
if (_testOutput != null) {
outputWriter.WriteLine("/**");
outputWriter.Write(_testOutput);
outputWriter.WriteLine("Script");
outputWriter.WriteLine("================================================================");
outputWriter.Write("**/");
outputWriter.WriteLine();
outputWriter.WriteLine();
}
}
#endif // DEBUG
string script = GenerateScriptWithTemplate();
outputWriter.Write(script);
}
catch (Exception e) {
Debug.Fail(e.ToString());
}
finally {
if (outputWriter != null) {
outputWriter.Flush();
}
if (outputStream != null) {
_options.ScriptFile.CloseStream(outputStream);
}
}
}
private string GenerateScriptCore() {
StringWriter scriptWriter = new StringWriter();
try {
ScriptGenerator scriptGenerator = new ScriptGenerator(scriptWriter, _options, _symbols);
scriptGenerator.GenerateScript(_symbols);
}
catch (Exception e) {
Debug.Fail(e.ToString());
}
finally {
scriptWriter.Flush();
}
return scriptWriter.ToString();
}
private string GenerateScriptWithTemplate() {
string script = GenerateScriptCore();
string template = _options.ScriptInfo.Template;
if (String.IsNullOrEmpty(template)) {
return script;
}
template = PreprocessTemplate(template);
StringBuilder requiresBuilder = new StringBuilder();
StringBuilder dependenciesBuilder = new StringBuilder();
StringBuilder depLookupBuilder = new StringBuilder();
bool firstDependency = true;
foreach (ScriptReference dependency in _symbols.Dependencies) {
if (dependency.DelayLoaded) {
continue;
}
if (firstDependency) {
depLookupBuilder.Append("var ");
}
else {
requiresBuilder.Append(", ");
dependenciesBuilder.Append(", ");
depLookupBuilder.Append(",\r\n ");
}
requiresBuilder.Append("'" + dependency.Path + "'");
dependenciesBuilder.Append(dependency.Identifier);
depLookupBuilder.Append(dependency.Identifier);
depLookupBuilder.Append(" = require('" + dependency.Name + "')");
firstDependency = false;
}
depLookupBuilder.Append(";");
return template.TrimStart()
.Replace("{name}", _symbols.ScriptName)
.Replace("{description}", _options.ScriptInfo.Description ?? String.Empty)
.Replace("{copyright}", _options.ScriptInfo.Copyright ?? String.Empty)
.Replace("{version}", _options.ScriptInfo.Version ?? String.Empty)
.Replace("{compiler}", typeof(ScriptCompiler).Assembly.GetName().Version.ToString())
.Replace("{description}", _options.ScriptInfo.Description)
.Replace("{requires}", requiresBuilder.ToString())
.Replace("{dependencies}", dependenciesBuilder.ToString())
.Replace("{dependenciesLookup}", depLookupBuilder.ToString())
.Replace("{script}", script);
}
private void ImportMetadata() {
MetadataImporter mdImporter = new MetadataImporter(_options, this);
_importedSymbols = mdImporter.ImportMetadata(_options.References, _symbols);
}
private string PreprocessTemplate(string template) {
if (_options.IncludeResolver == null) {
return template;
}
Regex includePattern = new Regex("\\{include:([^\\}]+)\\}", RegexOptions.Multiline | RegexOptions.CultureInvariant);
return includePattern.Replace(template, delegate(Match include) {
string includedScript = String.Empty;
if (include.Groups.Count == 2) {
string includePath = include.Groups[1].Value;
IStreamSource includeSource = _options.IncludeResolver.Resolve(includePath);
if (includeSource != null) {
Stream includeStream = includeSource.GetStream();
StreamReader reader = new StreamReader(includeStream);
includedScript = reader.ReadToEnd();
includeSource.CloseStream(includeStream);
}
}
return includedScript;
});
}
#region Implementation of IErrorHandler
void IErrorHandler.ReportError(string errorMessage, string location) {
_hasErrors = true;
if (_errorHandler != null) {
_errorHandler.ReportError(errorMessage, location);
return;
}
if (String.IsNullOrEmpty(location) == false) {
Console.Error.Write(location);
Console.Error.Write(": ");
}
Console.Error.WriteLine(errorMessage);
}
#endregion
}
}