-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
368 lines (318 loc) · 17.9 KB
/
Copy pathProgram.cs
File metadata and controls
368 lines (318 loc) · 17.9 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Squared.FString;
namespace FStringCompiler {
class Program {
// HACK
public static HashSet<string> KnownFStringKeys = new HashSet<string>(StringComparer.Ordinal);
private static readonly Regex UsingRegex = new Regex(@"^using .+?;", RegexOptions.Compiled | RegexOptions.ExplicitCapture),
FunctionSignatureRegex = new Regex(@"^\(((?<type>(\w|\?)+)\s+(?<argName>\w+)\s*,?\s*)*\)\s*\{", RegexOptions.Compiled | RegexOptions.ExplicitCapture),
// HACK: Including " in the name regex for switch cases
StringRegex = new Regex("^(?<name>(\\w|[\"\\.\\-])+)\\s*=\\s*(null|(?<buck>\\$)?\"(?<text>.*)\");", RegexOptions.Compiled | RegexOptions.ExplicitCapture),
CasesRegex = new Regex(@"^\s*(?<name>\w+)\s*=\s*switch\s*\((?<selector>.*)\)\s*{", RegexOptions.Compiled | RegexOptions.ExplicitCapture),
StandaloneStringRegex = new Regex("^(?<name>\\w+)\\s*\\(((?<type>(\\w|\\?)+)\\s+(?<argName>\\w+)\\s*,?\\s*)*\\)\\s*=\\s*(null|(?<buck>\\$)?\"(?<text>.*)\");", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
public static void Main (string[] args) {
if (args.Length < 3) {
Console.Error.WriteLine("Usage: fstringcompiler [language name] [output directory] [input files...]");
Environment.Exit(1);
}
Directory.CreateDirectory(args[1]);
Console.WriteLine($"Writing output to {args[1]}");
var commentBuffer = new StringBuilder();
using (var xmlWriter = new FStringTableWriter(Path.Combine(args[1], $"FStringTable_{args[0]}.xml"), args[0])) {
foreach (var inputFile in args.Skip(2).Distinct().OrderBy(s => s)) {
Console.WriteLine(inputFile);
xmlWriter.StartFile(inputFile, File.GetLastWriteTimeUtc(inputFile));
int ln = 0;
var inClass = false;
var outPath = Path.Combine(args[1], Path.GetFileNameWithoutExtension(inputFile) + ".cs");
if (File.Exists(outPath) && File.GetLastWriteTimeUtc(outPath) > File.GetLastWriteTimeUtc(inputFile))
Console.WriteLine($"Skipping, output is newer: {outPath}");
StringGroup group = null;
StringSwitch swtch = null;
using (var input = new StreamReader(inputFile))
using (var output = new StreamWriter(outPath, false, Encoding.UTF8)) {
commentBuffer.Clear();
output.WriteLine("using System;");
output.WriteLine("using System.Text;");
output.WriteLine("using Squared.FString;");
output.WriteLine();
while (!input.EndOfStream) {
var line = input.ReadLine();
ln++;
if (string.IsNullOrWhiteSpace(line)) {
output.WriteLine();
continue;
}
line = line.Trim();
if (line.StartsWith("//")) {
commentBuffer.AppendLine(line);
output.WriteLine(line);
continue;
} else if (line.EndsWith("=")) {
if (input.EndOfStream) {
Console.Error.WriteLine($"error: {inputFile}({ln}): Expected string constant after = or on the line following it");
Environment.Exit(7);
}
var nextLine = input.ReadLine().Trim();
if (!nextLine.StartsWith("$\"") && !nextLine.StartsWith("\"")) {
Console.Error.WriteLine($"error: {inputFile}({ln}): Expected string constant after = or on the line following it");
Environment.Exit(7);
}
ln++;
line += nextLine;
}
while (line.EndsWith("\\")) {
if (input.EndOfStream) {
Console.Error.WriteLine($"error: {inputFile}({ln}): File ended after \\");
Environment.Exit(8);
}
var nextLine = input.ReadLine().Trim();
ln++;
line = line.Substring(0, line.Length - 1) + nextLine;
}
try {
if (group == null) {
if (UsingRegex.IsMatch(line)) {
if (inClass) {
Console.Error.WriteLine($"error: {inputFile}({ln}): Cannot add new using statements after defining a string");
Environment.Exit(5);
} else {
output.WriteLine(line);
}
} else {
var fsm = FunctionSignatureRegex.Match(line);
var ssm = StandaloneStringRegex.Match(line);
if (fsm.Success) {
group = new StringGroup(fsm);
} else if (ssm.Success) {
if (!inClass) {
output.WriteLine("public static partial class FStrings {");
inClass = true;
}
AutoWriteComments();
var tempGroup = new StringGroup(ssm);
var fs = new FString(tempGroup, ssm, swtch);
fs.Write(output, xmlWriter);
} else {
Console.Error.WriteLine($"error: {inputFile}({ln}): Unrecognized line: {line}");
Environment.Exit(2);
}
}
} else {
var sm = StringRegex.Match(line);
var cm = CasesRegex.Match(line);
if (sm.Success) {
if (!inClass) {
output.WriteLine("public static partial class FStrings {");
inClass = true;
}
var fs = new FString(group, sm, swtch);
if (swtch == null) {
AutoWriteComments();
fs.Write(output, xmlWriter);
}
} else if (cm.Success) {
if (swtch != null){
Console.Error.WriteLine($"error: {inputFile}({ln}): Nesting switches is disallowed");
Environment.Exit(6);
}
swtch = new StringSwitch(group, cm);
} else if (line == "}") {
// End of group or cases block
if (swtch != null) {
if (!inClass) {
output.WriteLine("public static partial class FStrings {");
inClass = true;
}
swtch.Write(output, xmlWriter);
swtch = null;
} else
group = null;
} else {
Console.Error.WriteLine($"error: {inputFile}({ln}): Unrecognized line: {line}");
Environment.Exit(3);
}
}
} catch (Exception exc) {
Console.Error.WriteLine($"error: {inputFile}({ln}): {exc}");
Environment.Exit(4);
}
}
if (inClass)
output.WriteLine("}");
}
}
void AutoWriteComments () {
if (commentBuffer.Length <= 0)
return;
xmlWriter.WriteComment(" " + commentBuffer.ToString().Replace("//", "").Trim() + " ");
commentBuffer.Clear();
}
}
Console.WriteLine("Done");
}
}
public struct Argument {
public string Type, Name;
}
public class StringGroup {
public readonly List<Argument> Arguments = new List<Argument>();
public StringGroup (Match m) {
var types = m.Groups["type"].Captures.Cast<Capture>().Select(c => c.Value).ToArray();
var names = m.Groups["argName"].Captures.Cast<Capture>().Select(c => c.Value).ToArray();
for (int i = 0; i < types.Length; i++)
Arguments.Add(new Argument { Type = types[i], Name = names[i] });
}
}
public class StringSwitch {
public StringGroup Group;
public string Name, Selector;
public Dictionary<string, FString> Cases = new Dictionary<string, FString>();
public StringSwitch (StringGroup group, Match m) {
Group = group;
Name = m.Groups["name"].Value;
Selector = m.Groups["selector"].Value;
}
internal void Write (StreamWriter output, FStringTableWriter writer) {
foreach (var value in Cases.Values) {
// Only generate all the code the first time.
value.Write(output, writer);
output = null;
}
}
internal void WriteNameSelector (StreamWriter output) {
output.WriteLine("\t\tpublic string StringTableKey { get {");
output.WriteLine($"\t\t\tswitch ({Selector}) {{");
foreach (var kvp in Cases) {
if (kvp.Key == "default")
output.WriteLine($"\t\t\t\tdefault: return \"{kvp.Value.StringTableKey}\";");
else
output.WriteLine($"\t\t\t\tcase {kvp.Key}: return \"{kvp.Value.StringTableKey}\";");
}
if (!Cases.ContainsKey("default")) {
var printableSelector = Selector.Replace("\"", "\\\")}");
output.WriteLine($"\t\t\t\tdefault: throw new ArgumentOutOfRangeException(\"{printableSelector}\");");
}
output.WriteLine("\t\t\t}");
output.WriteLine("\t\t} }");
}
}
public class FString {
public StringGroup Group;
public StringSwitch Switch;
public FStringDefinition Definition;
public string Name, StringTableKey, FormatString;
public FString (StringGroup group, Match m, StringSwitch swtch) {
Group = group;
Switch = swtch;
Name = m.Groups["name"].Value;
// HACK: Used for inference of whether an fstring argument is also an fstring, to avoid boxing
var actualKey = swtch?.Name ?? Name;
if ((swtch == null) && Program.KnownFStringKeys.Contains(actualKey))
throw new Exception($"Duplicate definition for string '{actualKey}'");
Program.KnownFStringKeys.Add(actualKey);
if (swtch != null)
swtch.Cases.Add(Name, this);
StringTableKey = (swtch != null) ? $"{swtch.Name}_{HashUtil.GetShortHash(Name)}" : Name;
FormatString = m.Groups["text"].Value;
Definition = FStringDefinition.Parse(null, Name, FormatString, !m.Groups["buck"].Success);
if (Definition.Opcodes.Any(o => o.emit && o.textOrId == "this"))
throw new Exception("{this} is invalid in FStrings");
}
public void Write (StreamWriter output, FStringTableWriter writer) {
// FIXME: Defer for sorting? Probably better to keep source file ordering
if (Switch != null)
writer.WriteComment($" ({Switch.Selector}) == {Name} ");
writer.WriteEntry(StringTableKey, FormatString, HashUtil.GetShortHash(FormatString), Definition.IsLiteral);
if (output == null)
return;
var structName = Switch?.Name ?? Name;
if (Group.Arguments.Count == 0) {
output.WriteLine($"\tpublic static FStringLiteral {Name} () => {Name}(FStringTable.Default);");
output.WriteLine($"\tpublic static FStringLiteral {Name} (FStringTable table) => new FStringLiteral(table.Get(\"{StringTableKey}\"));");
} else {
output.WriteLine($"\tpublic struct {structName} : IFString {{");
// Generate the name selector (for a switch) or name constant property
if (Switch != null)
Switch.WriteNameSelector(output);
else
output.WriteLine($"\t\tpublic string StringTableKey => \"{StringTableKey}\";");
// Generate the fields
foreach (var arg in Group.Arguments)
output.WriteLine($"\t\tpublic {arg.Type} {arg.Name};");
output.WriteLine();
// Generate the constructor
output.Write($"\t\tpublic {structName} (");
var isFirstArg = true;
foreach (var arg in Group.Arguments) {
if (!isFirstArg)
output.Write(", ");
output.Write($"{arg.Type} @{arg.Name}");
isFirstArg = false;
}
output.WriteLine(") {");
foreach (var arg in Group.Arguments)
output.WriteLine($"\t\t\tthis.{arg.Name} = @{arg.Name};");
output.WriteLine("\t\t}");
output.WriteLine();
// Generate the emitter that looks up a value by string key. This makes everything work
output.WriteLine("\t\tpublic void EmitValue (ref FStringBuilder __output__, string __id__) {");
output.WriteLine("\t\t\tswitch(__id__) {");
IEnumerable<string> keys;
if (Switch != null) {
keys = new string[0];
foreach (var value in Switch.Cases.Values)
keys = keys.Concat(GetIds(value.Definition));
} else {
keys = GetIds(Definition);
}
foreach (var key in keys.Distinct()) {
output.WriteLine($"\t\t\t\tcase \"{key.Replace("\"", "\\\"")}\":");
// TODO: Detect fstrings and try to do a fast-path, no-allocation append
// We need a way to store the fstring without boxing first though...
// if (Group.Arguments.Any(a => (a.Name == key) && (a.Type == "IFString"))
if (IsTypeKnownToBeFString(key))
output.WriteLine($"\t\t\t\t\t({key}).AppendTo(ref __output__);");
else
output.WriteLine($"\t\t\t\t\t__output__.Append({key});");
output.WriteLine($"\t\t\t\t\treturn;");
}
output.WriteLine("\t\t\t\tdefault:");
output.WriteLine("\t\t\t\t\tthrow new ArgumentOutOfRangeException(nameof(__id__));");
output.WriteLine("\t\t\t}");
output.WriteLine("\t\t}");
// Generate the append overloads that make things usable dynamically
output.WriteLine("\t\tpublic void AppendTo (ref FStringBuilder output) => output.GetDefinition(StringTableKey).AppendTo(ref this, ref output);");
// Generate ToString for simple uses
output.WriteLine("\t\tpublic override string ToString () {");
output.WriteLine("\t\t\tvar output = new FStringBuilder();");
output.WriteLine("\t\t\tAppendTo(ref output);");
output.WriteLine("\t\t\treturn output.ToString();");
output.WriteLine("\t\t}");
output.WriteLine("\t}");
}
output.WriteLine();
}
bool IsTypeKnownToBeFString (string key) {
foreach (var arg in Group.Arguments) {
if (arg.Name == key) {
if (Program.KnownFStringKeys.Contains(arg.Type))
return true;
}
}
return false;
}
private static IEnumerable<string> GetIds (FStringDefinition definition) =>
definition.Opcodes.Where(o => o.emit).Select(o => o.textOrId);
}
}