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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
#include "CodegenConfig.hpp"
#include "CodegenDecl.hpp"
#include "CodegenMacros.hpp"
#include "CodegenInput.inl"
#include "CodegenOutput.inl"
#include "CodegenUtils.inl"
#include <Enum.hpp>
#include <LookupTable.hpp>
#include <Macros.hpp>
#include <ScopeGuard.hpp>
#include <Utils.hpp>
#include <frozen/string.h>
#include <frozen/unordered_map.h>
#include <robin_hood.h>
#include <stb_c_lexer.h>
#include <cinttypes>
#include <cstdlib>
#include <filesystem>
#include <memory>
#include <span>
#include <string>
#include <string_view>
using namespace std::literals;
namespace fs = std::filesystem;
// TODO handle namespace
// TODO support codegen target in .cpp files
struct AppState {
std::string_view outputDir;
CodegenOutput mainHeaderOutput;
CodegenOutput mainSourceOutput;
};
enum {
CLEX_ext_single_char = CLEX_first_unused_token,
CLEX_ext_COUNT,
};
STR_LUT_DECL(ClexNames, CLEX_eof, CLEX_ext_COUNT) {
STR_LUT_MAP_FOR(ClexNames);
STR_LUT_MAP_ENUM(CLEX_intlit);
STR_LUT_MAP_ENUM(CLEX_floatlit);
STR_LUT_MAP_ENUM(CLEX_id);
STR_LUT_MAP_ENUM(CLEX_dqstring);
STR_LUT_MAP_ENUM(CLEX_sqstring);
STR_LUT_MAP_ENUM(CLEX_charlit);
STR_LUT_MAP_ENUM(CLEX_eq);
STR_LUT_MAP_ENUM(CLEX_noteq);
STR_LUT_MAP_ENUM(CLEX_lesseq);
STR_LUT_MAP_ENUM(CLEX_greatereq);
STR_LUT_MAP_ENUM(CLEX_andand);
STR_LUT_MAP_ENUM(CLEX_oror);
STR_LUT_MAP_ENUM(CLEX_shl);
STR_LUT_MAP_ENUM(CLEX_shr);
STR_LUT_MAP_ENUM(CLEX_plusplus);
STR_LUT_MAP_ENUM(CLEX_minusminus);
STR_LUT_MAP_ENUM(CLEX_pluseq);
STR_LUT_MAP_ENUM(CLEX_minuseq);
STR_LUT_MAP_ENUM(CLEX_muleq);
STR_LUT_MAP_ENUM(CLEX_diveq);
STR_LUT_MAP_ENUM(CLEX_modeq);
STR_LUT_MAP_ENUM(CLEX_andeq);
STR_LUT_MAP_ENUM(CLEX_oreq);
STR_LUT_MAP_ENUM(CLEX_xoreq);
STR_LUT_MAP_ENUM(CLEX_arrow);
STR_LUT_MAP_ENUM(CLEX_eqarrow);
STR_LUT_MAP_ENUM(CLEX_shleq);
STR_LUT_MAP_ENUM(CLEX_shreq);
STR_LUT_MAP_ENUM(CLEX_ext_single_char);
}
enum CppKeyword {
CKw_Namespace,
CKw_Struct,
CKw_Class,
CKw_Enum,
CKw_COUNT,
};
BSTR_LUT_DECL(CppKeyword, 0, CKw_COUNT) {
BSTR_LUT_MAP_FOR(CppKeyword);
BSTR_LUT_MAP(CKw_Namespace, "namespace");
BSTR_LUT_MAP(CKw_Struct, "struct");
BSTR_LUT_MAP(CKw_Class, "class");
BSTR_LUT_MAP(CKw_Enum, "enum");
}
enum CodegenDirective {
CD_ClassInfo,
CD_EnumInfo,
CD_COUNT,
};
BSTR_LUT_DECL(CodegenDirective, 0, CD_COUNT) {
BSTR_LUT_MAP_FOR(CodegenDirective);
BSTR_LUT_MAP(CD_ClassInfo, "BRUSSEL_CLASS");
BSTR_LUT_MAP(CD_EnumInfo, "BRUSSEL_ENUM");
}
struct StbLexerToken {
std::string text;
// Can either be CLEX_* or CLEX_ext_* values
int type;
};
bool StbTokenIsSingleChar(int lexerToken) {
return lexerToken >= 0 && lexerToken < 256;
}
bool StbTokenIsMultiChar(int lexerToken) {
return !StbTokenIsMultiChar(lexerToken);
}
void CheckBraceDepth(int braceDpeth) {
if (braceDpeth < 0) {
printf("[WARNING] unbalanced brace\n");
}
}
const StbLexerToken*
PeekTokenOfTypeAt(const std::vector<StbLexerToken>& tokens, size_t idx, int type) {
auto& token = tokens[idx];
if (token.type != type) {
return nullptr;
}
return &token;
}
std::pair<const StbLexerToken*, size_t>
PeekTokenOfType(const std::vector<StbLexerToken>& tokens, size_t current, int type) {
for (size_t i = current; i < tokens.size(); ++i) {
if (auto token = PeekTokenOfTypeAt(tokens, i, type)) {
return { token, i };
}
}
return { nullptr, current };
}
std::pair<std::vector<std::vector<const StbLexerToken*>>, size_t>
PeekDirectiveArgumentList(const std::vector<StbLexerToken>& tokens, size_t current) {
std::vector<std::vector<const StbLexerToken*>> result;
decltype(result)::value_type currentArg;
size_t i = current;
int parenDepth = 0;
for (; i < tokens.size(); ++i) {
auto& token = tokens[i];
if (token.text[0] == '(') {
if (parenDepth > 0) {
currentArg.push_back(&token);
}
++parenDepth;
} else if (token.text[0] == ')') {
--parenDepth;
if (parenDepth == 0) {
// End of argument list
break;
}
} else if (parenDepth > 0) {
// Parse these only if we are inside the argument list
if (token.text[0] == ',') {
result.push_back(std::move(currentArg));
currentArg = {};
} else {
currentArg.push_back(&token);
}
}
}
if (!currentArg.empty()) {
result.push_back(std::move(currentArg));
}
return { result, i };
}
std::vector<StbLexerToken> RecordTokens(std::string_view source) {
stb_lexer lexer;
char stringStorage[65536];
const char* srcBegin = source.data();
const char* srcEnd = srcBegin + source.length();
stb_c_lexer_init(&lexer, srcBegin, srcEnd, stringStorage, sizeof(stringStorage));
std::vector<StbLexerToken> tokens;
while (true) {
// See stb_c_lexer.h's comments, here are a few additinos that aren't made clear in the file:
// - `lexer->token` (noted as "token" below) after calling stb_c_lexer_get_token() contains either:
// 1. 0 <= token < 256: an ASCII character (more precisely a single char that the lexer ate; technically can be an incomplete code unit)
// 2. token < 0: an unknown token
// 3. One of the `CLEX_*` enums: a special, recognized token such as an operator
int stbToken = stb_c_lexer_get_token(&lexer);
if (stbToken == 0) {
// EOF
break;
}
if (lexer.token == CLEX_parse_error) {
printf("[ERROR] stb_c_lexer countered a parse error.\n");
// TODO how to handle?
continue;
}
StbLexerToken token;
if (StbTokenIsSingleChar(lexer.token)) {
token.type = CLEX_ext_single_char;
token.text = std::string(1, lexer.token);
} else {
token.type = lexer.token;
// WORKAROUND: use null terminated string, stb_c_lexer doens't set string_len properly when parsing identifiers
token.text = std::string(lexer.string);
}
tokens.push_back(std::move(token));
token = {};
}
return tokens;
}
enum StructMetaGenOptions {
SMGO_InheritanceHiearchy,
SMGO_PublicFields,
SMGO_ProtectedFields,
SMGO_PrivateFields,
SMGO_COUNT,
};
BSTR_LUT_DECL(StructMetaGenOptions, 0, SMGO_COUNT) {
BSTR_LUT_MAP_FOR(StructMetaGenOptions);
BSTR_LUT_MAP(SMGO_InheritanceHiearchy, "GenInheritanceHiearchy");
BSTR_LUT_MAP(SMGO_PublicFields, "GenPublicFields");
BSTR_LUT_MAP(SMGO_ProtectedFields, "GenProtectedFields");
BSTR_LUT_MAP(SMGO_PrivateFields, "GenPrivateFields");
}
enum EnumMetaGenOptions {
EMGO_ToString,
EMGO_FromString,
EMGO_ExcludeUseHeuristics,
EMGO_COUNT,
};
BSTR_LUT_DECL(EnumMetaGenOptions, 0, EMGO_COUNT) {
BSTR_LUT_MAP_FOR(EnumMetaGenOptions);
BSTR_LUT_MAP(EMGO_ToString, "ToString");
BSTR_LUT_MAP(EMGO_FromString, "FromString");
BSTR_LUT_MAP(EMGO_ExcludeUseHeuristics, "ExcludeHeuristics");
}
std::string GenerateEnumStringArray(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) {
std::string arrayName;
APPEND_FMT(arrayName, "gCG_%s_Val2Str", decl.name.c_str());
CodegenOutputThing thing;
APPEND_FMT_LN(thing.text, "const char* %s[] = {", arrayName.c_str());
for (auto& elm : decl.elements) {
if (useHeruistics && elm.name.ends_with("COUNT")) {
continue;
}
APPEND_FMT_LN(thing.text, "\"%s\",", elm.name.c_str());
}
APPEND_LIT_LN(thing.text, "};");
out.AddOutputThing(std::move(thing));
return arrayName;
}
std::string GenerateEnumStringMap(CodegenOutput& out, const DeclEnum& decl, bool useHeruistics) {
std::string mapName;
// TODO
return mapName;
}
void GenerateForEnum(CodegenOutput& headerOut, CodegenOutput& sourceOut, const DeclEnum& decl, EnumFlags<EnumMetaGenOptions> options) {
char enumName[2048];
if (decl.container) {
snprintf(enumName, sizeof(enumName), "%.*s::%s", PRINTF_STRING_VIEW(decl.container->fullname), decl.name.c_str());
} else {
strncpy(enumName, decl.name.c_str(), sizeof(enumName));
}
auto useExcludeHeuristics = options.IsSet(EMGO_ExcludeUseHeuristics);
auto filteredElements = [&]() {
if (useExcludeHeuristics) {
decltype(decl.elements) result;
for (auto& elm : decl.elements) {
if (elm.name.ends_with("COUNT")) continue;
result.push_back(elm);
}
return result;
} else {
return decl.elements;
}
}();
if (options.IsSet(EMGO_ToString)) {
// Generate value -> string lookup table and function
switch (decl.GetPattern()) {
case EVP_Continuous: {
auto arrayName = GenerateEnumStringArray(sourceOut, decl, useExcludeHeuristics);
int minVal = filteredElements.empty() ? 0 : filteredElements.front().value;
int maxVal = filteredElements.empty() ? 0 : filteredElements.back().value;
CodegenOutputThing lookupFunctionDef;
{
auto& o = lookupFunctionDef.text;
APPEND_LIT_LN(o, "template <>");
APPEND_FMT_LN(o, "std::string_view Metadata::EnumToString<%s>(%s value) {", enumName, enumName);
APPEND_FMT_LN(o, " if (value < %d || value > %d) return {};", minVal, maxVal);
APPEND_FMT_LN(o, " return %s[value - %d];", arrayName.c_str(), minVal);
APPEND_LIT_LN(o, "}");
}
sourceOut.AddOutputThing(std::move(lookupFunctionDef));
} break;
case EVP_Bits: {
auto arrayName = GenerateEnumStringArray(sourceOut, decl, useExcludeHeuristics);
// TODO
} break;
case EVP_Random: {
auto mapName = GenerateEnumStringMap(sourceOut, decl, useExcludeHeuristics);
// TODO
} break;
case EVP_COUNT: break;
}
}
if (options.IsSet(EMGO_FromString)) {
// Generate string -> value lookup table
char mapName[1024];
// TODO mangle to prevent name conflicts of enum in different namespaces
snprintf(mapName, sizeof(mapName), "gCG_%s_Str2Val", decl.name.c_str());
CodegenOutputThing lookupTable;
{
auto& o = lookupTable.text;
// TODO use correct underlying type
APPEND_FMT_LN(o, "constinit frozen::unordered_map<frozen::string, uint64_t, %" PRId64 "> %s = {", filteredElements.size(), mapName);
for (auto& elm : filteredElements) {
APPEND_FMT_LN(o, "{\"%s\", %" PRId64 "},", elm.name.c_str(), elm.value);
}
APPEND_LIT_LN(o, "};");
}
// Generate lookup function
CodegenOutputThing lookupFunctionDef;
{
auto& o = lookupFunctionDef.text;
APPEND_LIT_LN(o, "template <>");
APPEND_FMT_LN(o, "std::optional<%s> Metadata::EnumFromString<%s>(std::string_view value) {", enumName, enumName);
APPEND_FMT_LN(o, " auto iter = %s.find(value);", mapName);
APPEND_FMT_LN(o, " if (iter != %s.end()) {", mapName);
APPEND_FMT_LN(o, " return (%s)iter->second;", enumName);
APPEND_LIT_LN(o, " } else {");
APPEND_LIT_LN(o, " return {};");
APPEND_LIT_LN(o, " }");
APPEND_LIT_LN(o, "}");
}
sourceOut.AddOutputThing(std::move(lookupTable));
sourceOut.AddOutputThing(std::move(lookupFunctionDef));
}
}
void HandleInputFile(AppState& state, std::string_view filenameStem, std::string_view source) {
auto tokens = RecordTokens(source);
size_t idx = 0;
#if CODEGEN_DEBUG_PRINT
printf("BEGIN tokens\n");
for (auto& token : tokens) {
printf(" token %-32s '%s'\n", STR_LUT_LOOKUP(ClexNames, token.type), token.text.c_str());
}
printf("END tokens\n");
#endif
CodegenInput cgInput;
CodegenOutput cgHeaderOutput;
Utils::ProduceGeneratedHeaderFileHeader(cgHeaderOutput);
CodegenOutput cgSourceOutput;
Utils::ProduceGeneratedSourceFileHeader(cgSourceOutput);
int currentBraceDepth = 0;
// The current effective namespace, see example
DeclNamespace* currentNamespace = nullptr;
struct NamespaceStackframe {
// The current namespace that owns the brace level, see example
DeclNamespace* ns = nullptr;
// Brace depth `ns` was created at (e.g. [std::details].depth == 0)
int depth = 0;
};
std::vector<NamespaceStackframe> nsStack;
// Example:
// namespace std::details {
// /* [stack top].ns = std::details */
// /* [stack top].depth = std */
// }
// namespace foo {
// /* [stack top].ns = foo */
// /* [stack top].depth = foo */
// namespace details {
// /* [stack top].ns = foo::details */
// /* [stack top].depth = foo::details */
// }
// }
while (idx < tokens.size()) {
auto& token = tokens[idx];
bool incrementTokenIdx = true;
switch (token.type) {
case CLEX_id: {
CppKeyword keyword;
{
auto& map = BSTR_LUT_S2V(CppKeyword);
auto iter = map.find(token.text);
if (iter != map.end()) {
keyword = iter->second;
} else {
keyword = CKw_COUNT; // Skip keyword section
}
}
switch (keyword) {
case CKw_Namespace: {
++idx;
incrementTokenIdx = false;
while (true) {
if (tokens[idx].type != CLEX_id) {
// TODO better error recovery
printf("[ERROR] invalid syntax for namespace\n");
break;
}
currentNamespace = cgInput.AddNamespace(DeclNamespace{
.container = currentNamespace,
.name = tokens[idx].text,
});
if (tokens[idx + 1].text[0] == ':' &&
tokens[idx + 2].text[0] == ':')
{
// Skip the two ':' tokens, try parse the next identifier
idx += 3;
} else {
break;
}
}
nsStack.push_back(NamespaceStackframe{
.ns = currentNamespace,
.depth = currentBraceDepth,
});
goto endIdenCase;
}
case CKw_Struct:
case CKw_Class: {
auto& idenTok = tokens[idx + 1]; // TODO handle end of list
DEBUG_PRINTF("[DEBUG] found struct named %s\n", idenTok.text.c_str());
goto endIdenCase;
}
case CKw_Enum: {
// Consume the "enum" keyword
++idx;
incrementTokenIdx = false;
DeclEnum enumDecl;
enumDecl.container = currentNamespace;
enumDecl.underlyingType = EUT_Int32; // TODO
if (tokens[idx].text == "class") {
// Consume the "class" keyword
++idx;
DEBUG_PRINTF("[DEBUG] found enum class named %s\n", tokens[idx].text.c_str());
} else {
DEBUG_PRINTF("[DEBUG] found enum named %s\n", tokens[idx].text.c_str());
}
// Consume the enum name identifier
enumDecl.name = tokens[idx].text;
++idx;
int enumClosingBraceCount = 0;
int enumBraceDepth = 0;
while (enumClosingBraceCount == 0 && idx < tokens.size()) {
auto& token = tokens[idx];
switch (token.type) {
case CLEX_id: {
auto& vec = enumDecl.elements;
// Set to the previous enum element's value + 1, or starting from 0 if this is the first
// Also overridden in the CLEX_intlit branch
auto value = vec.empty() ? 0 : vec.back().value + 1;
vec.push_back(DeclEnumElement{
.name = token.text,
.value = value,
});
} break;
case CLEX_intlit: {
} break;
case CLEX_ext_single_char: {
switch (token.text[0]) {
case '{': {
++enumBraceDepth;
} break;
case '}': {
--enumBraceDepth;
++enumClosingBraceCount;
} break;
}
} break;
}
++idx;
}
auto fullname = Utils::MakeFullName(enumDecl.name, currentNamespace);
cgInput.AddEnum(std::move(fullname), std::move(enumDecl));
goto endIdenCase;
}
case CKw_COUNT: break;
}
CodegenDirective directive;
{
auto& map = BSTR_LUT_S2V(CodegenDirective);
auto iter = map.find(token.text);
if (iter != map.end()) {
directive = iter->second;
} else {
directive = CD_COUNT; // Skip directive section
}
}
switch (directive) {
case CD_ClassInfo: {
// TODO
goto endIdenCase;
}
case CD_EnumInfo: {
// Consume the directive
++idx;
incrementTokenIdx = false;
auto& optionsStrMap = BSTR_LUT_S2V(EnumMetaGenOptions);
auto [argList, newIdx] = PeekDirectiveArgumentList(tokens, idx);
if (argList.size() < 1) {
printf("[ERROR] invalid syntax for BRUSSEL_ENUM\n");
break; // TODO handle this error case gracefully (advance to semicolon?)
}
auto& enumName = argList[0][0]->text;
auto enumDecl = cgInput.FindEnumByName(Utils::MakeFullName(enumName, currentNamespace));
if (!enumDecl) {
printf("[ERROR] BRUSSEL_ENUM: referring to non-existent enum '%s'\n", enumName.c_str());
break;
}
auto& directiveOptions = argList[1];
EnumFlags<EnumMetaGenOptions> options;
for (auto optionTok : directiveOptions) {
auto iter = optionsStrMap.find(optionTok->text);
if (iter != optionsStrMap.end()) {
options |= iter->second;
} else {
printf("[ERROR] BRUSSEL_ENUM: invalid option '%s'\n", optionTok->text.c_str());
}
}
GenerateForEnum(cgHeaderOutput, cgSourceOutput, *enumDecl, options);
idx = newIdx;
incrementTokenIdx = false;
goto endIdenCase;
}
case CD_COUNT: break;
}
endIdenCase:
break;
}
case CLEX_ext_single_char:
switch (token.text[0]) {
case '{': {
currentBraceDepth++;
CheckBraceDepth(currentBraceDepth);
} break;
case '}': {
currentBraceDepth--;
CheckBraceDepth(currentBraceDepth);
if (!nsStack.empty()) {
auto& ns = nsStack.back();
if (ns.depth == currentBraceDepth) {
nsStack.pop_back();
if (!nsStack.empty()) {
currentNamespace = nsStack.back().ns;
} else {
currentNamespace = nullptr;
}
}
}
} break;
}
break;
}
if (incrementTokenIdx) {
++idx;
}
}
if (currentBraceDepth != 0) {
printf("[WARNING] unbalanced brace at end of file.");
}
Utils::WriteOutputFile(cgHeaderOutput, state.outputDir, filenameStem, ".gh.inl"sv);
Utils::WriteOutputFile(cgSourceOutput, state.outputDir, filenameStem, ".gs.inl"sv);
// TODO see CMakeLists.txt for rationale, clean this up to be a proper citizen
Utils::WriteOutputFile(CodegenOutput{}, state.outputDir, filenameStem, ".gs.cpp"sv);
}
enum InputOpcode {
IOP_ProcessSingleFile,
IOP_ProcessRecursively,
IOP_COUNT,
};
void HandleArgument(AppState& state, InputOpcode opcode, std::string_view operand) {
switch (opcode) {
case IOP_ProcessSingleFile: {
DEBUG_PRINTF("Processing single file %.*s\n", PRINTF_STRING_VIEW(operand));
fs::path path(operand);
auto filenameStem = path.stem().string();
auto source = Utils::ReadFileAsString(path);
HandleInputFile(state, filenameStem, source);
} break;
case IOP_ProcessRecursively: {
DEBUG_PRINTF("Recursively processing folder %.*s\n", PRINTF_STRING_VIEW(operand));
fs::path startPath(operand);
for (auto& item : fs::recursive_directory_iterator(startPath)) {
if (!item.is_regular_file()) {
continue;
}
auto& path = item.path();
auto pathExt = path.extension();
auto pathStem = path.stem();
if (pathExt != ".h" &&
pathExt != ".hpp")
{
continue;
}
DEBUG_PRINTF("Processing subfile %s\n", path.string().c_str());
auto filenameStem = pathStem.string();
auto source = Utils::ReadFileAsString(path);
HandleInputFile(state, filenameStem, source);
}
} break;
case IOP_COUNT: break;
}
}
InputOpcode ParseInputOpcode(std::string_view text) {
if (text == "single"sv) {
return IOP_ProcessSingleFile;
} else if (text == "rec"sv) {
return IOP_ProcessRecursively;
} else {
DEBUG_PRINTF("Unknown input opcode %s\n", text.data());
throw std::runtime_error("Unknown input opcode");
}
}
int main(int argc, char* argv[]) {
STR_LUT_INIT(ClexNames);
BSTR_LUT_INIT(CppKeyword);
BSTR_LUT_INIT(CodegenDirective);
BSTR_LUT_INIT(StructMetaGenOptions);
BSTR_LUT_INIT(EnumMetaGenOptions);
// TODO better arg parser
// option 1: use cxxopts and positional arguments
// option 2: take one argument only, being a json objecet
AppState state;
Utils::ProduceGeneratedHeaderFileHeader(state.mainHeaderOutput);
Utils::ProduceGeneratedSourceFileHeader(state.mainSourceOutput);
// If no cli is provided (argv[0] conventionally but not mandatorily the cli), this will do thing
// Otherwise, start with the 2nd element in the array, which is the 1st actual argument
if (argc < 2) {
// NOTE: keep in sync with various enum options and parser code
printf(&R"""(
USAGE: codegen.exe <output path> [<opcode>:<input path>]...
where <output path>: the directory to write generated contents to. This will NOT automatically create the directory.
<opcode> is one of:
"single" process this <input path> file only
"rec" starting at the given directory <input path>, recursively process all .h .hpp files
)"""[1]);
return -1;
}
state.outputDir = std::string_view(argv[1]);
DEBUG_PRINTF("Outputting to directory %.*s.\n", PRINTF_STRING_VIEW(state.outputDir));
for (int i = 2; i < argc; ++i) {
const char* argRaw = argv[i];
std::string_view arg(argRaw);
DEBUG_PRINTF("Processing input command %s\n", argRaw);
auto separatorLoc = arg.find(':');
if (separatorLoc != std::string_view::npos) {
auto opcodeString = arg.substr(0, separatorLoc);
auto opcode = ParseInputOpcode(opcodeString);
auto operand = arg.substr(separatorLoc + 1);
HandleArgument(state, opcode, operand);
}
}
// TODO do we even need these?
// Utils::WriteOutputFile(state.mainHeaderOutput, state.outputDir, "GeneratedCode.hpp"sv);
// Utils::WriteOutputFile(state.mainSourceOutput, state.outputDir, "GeneratedCode.cpp"sv);
return 0;
}
|