From 218211bcf2d79bf8088aaa98e78221e256f24af7 Mon Sep 17 00:00:00 2001 From: Robert burner Schadek Date: Thu, 21 Dec 2023 15:39:13 +0100 Subject: [PATCH] generator started --- fakerjs2generator/dub.json | 2 +- fakerjs2generator/source/app.d | 2 + fakerjs2generator/source/defis.d | 2 - fakerjs2generator/source/generator.d | 100 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 fakerjs2generator/source/generator.d diff --git a/fakerjs2generator/dub.json b/fakerjs2generator/dub.json index 0aabd68..3636c23 100644 --- a/fakerjs2generator/dub.json +++ b/fakerjs2generator/dub.json @@ -6,4 +6,4 @@ "description": "faker.js to D converter", "license": "GPL3", "name": "fakerjs2generator" -} \ No newline at end of file +} diff --git a/fakerjs2generator/source/app.d b/fakerjs2generator/source/app.d index bf6a007..5835e96 100644 --- a/fakerjs2generator/source/app.d +++ b/fakerjs2generator/source/app.d @@ -2,8 +2,10 @@ import std.stdio; import parser; import defis; +import generator; void main() { writeln("Edit source/app.d to start your project."); Language en = parseLanguage("en"); + generate(en, "en"); } diff --git a/fakerjs2generator/source/defis.d b/fakerjs2generator/source/defis.d index 857c8c1..bf40565 100644 --- a/fakerjs2generator/source/defis.d +++ b/fakerjs2generator/source/defis.d @@ -1,7 +1,5 @@ module defis; -import std.typecons : Nullable; - struct Mustache { string str; } diff --git a/fakerjs2generator/source/generator.d b/fakerjs2generator/source/generator.d new file mode 100644 index 0000000..48bff02 --- /dev/null +++ b/fakerjs2generator/source/generator.d @@ -0,0 +1,100 @@ +module generator; + +import std.array : appender; +import std.algorithm.iteration : filter; +import std.algorithm.searching : endsWith; +import std.exception : enforce; +import std.conv : to; +import std.uni : toUpper; +import std.format : formattedWrite; +import std.range : empty; +import std.traits : FieldNameTuple; +import std.stdio; + +import defis; + +void generate(Language lang, string langName) { + auto app = appender!string(); + traverse(lang, app, []); + writeln(app.data); +} + +void traverse(T,Out)(T t, ref Out o, string[] path) { + static if(T.stringof.endsWith("Folder") || is(T == Language)) { + static foreach(string mem; [FieldNameTuple!(T)].filter!(m => !m.empty)) {{ + traverse(__traits(getMember, t, mem), o, path ~ mem); + }} + } else { + static if(is(T == string[])) { + genStringArray(t, o, path); + } + static if(is(T == Number[])) { + genNumberArray(t, o, path); + } + } +} + +void genNumberArray(Out)(Number[] strs, ref Out o, string[] path) { +} + +void genStringArray(Out)(string[] strs, ref Out o, string[] path) { + iformat(o, 1, "string %s() {\n", pathToFuncName(path)); + iformat(o, 2, "const string[] strs =\n\t\t[ "); + str80(o, strs, 2); + o.put(" ];\n\n"); + iformat(o, 2, "return choice(strs, this.rnd);\n\t}\n"); +} + +string pathToFuncName(string[] path) { + enforce(path.length > 1, to!string(path)); + string ret = path[0]; + foreach(it; path[1 .. $]) { + char up = to!char(it[0].toUpper()); + string ta = up ~ it[1 .. $]; + ret ~= ta; + } + return ret; +} + +void iformat(Out,Args...)(ref Out o, size_t indent, string f, Args args) { + foreach(_; 0 .. indent) { + o.put('\t'); + } + formattedWrite(o, f, args); +} + +void str80(Out)(ref Out o, string[] strs, size_t tabs) { + size_t curStrCount; + size_t curLength = tabs * 4; + size_t curLineCount = 0; + foreach(idx, s; strs) { + if(curStrCount == 0) { + if(idx > 0) { + o.put(", "); + curLength += 2; + } + o.put('"'); + o.put(s); + o.put('"'); + curLength += s.length + 2; + ++curStrCount; + } else { + if(curLength > 80) { + curLength = tabs * 4; + o.put('\n'); + foreach(_; 0 .. tabs) { + o.put('\t'); + } + curStrCount = 0; + } + if(idx > 0) { + o.put(", "); + curLength += 2; + } + o.put('"'); + o.put(s); + o.put('"'); + curLength += s.length + 2; + } + } +}