2023-08-20 12:13:40 +08:00
|
|
|
import java.io.OutputStreamWriter;
|
2023-10-12 10:42:06 +08:00
|
|
|
import java.util.Locale;
|
2023-08-20 12:13:40 +08:00
|
|
|
|
|
|
|
/**
|
2023-08-20 21:38:16 +08:00
|
|
|
* Generic Enum Writer. Including Data Type Defination and Nameof Values.
|
2023-08-20 12:13:40 +08:00
|
|
|
*/
|
2023-08-20 21:38:16 +08:00
|
|
|
public class GeneralWriter {
|
2023-08-26 20:34:51 +08:00
|
|
|
|
2023-08-20 21:38:16 +08:00
|
|
|
public static void writeEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
|
2023-10-10 10:39:27 +08:00
|
|
|
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
|
2023-08-20 12:13:40 +08:00
|
|
|
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
|
|
|
// write enum comment
|
|
|
|
indent.briefComment(enum_t.mEnumComment);
|
|
|
|
|
|
|
|
// write enum start
|
|
|
|
indent.printf("enum class %s : %s {", enum_t.mEnumName,
|
|
|
|
CommonHelper.getEnumUnderlyingType(enum_t.mCanUnsigned));
|
|
|
|
indent.inc();
|
|
|
|
|
|
|
|
// write enum entries
|
|
|
|
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
|
|
|
|
// write entry self
|
|
|
|
if (enumEntry_t.mEntryValue == null) {
|
|
|
|
indent.printf("%s,", enumEntry_t.mEntryName);
|
|
|
|
} else {
|
|
|
|
indent.printf("%s = %s,", enumEntry_t.mEntryName, enumEntry_t.mEntryValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write entry comment after member
|
|
|
|
indent.afterMemberComment(enumEntry_t.mEntryComment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write enum tail
|
|
|
|
indent.dec();
|
|
|
|
indent.puts("};");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:39:27 +08:00
|
|
|
public static void writeEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writeEnums(fs, prog);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
2023-08-26 20:34:51 +08:00
|
|
|
public static void writeEnum(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception {
|
|
|
|
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
|
|
|
|
col.mEnums.add(_enum);
|
|
|
|
writeEnums(writer, col);
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:39:27 +08:00
|
|
|
public static void writeEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writeEnum(fs, _enum);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
2023-08-26 20:34:51 +08:00
|
|
|
public static void writeAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog,
|
2023-08-20 21:38:16 +08:00
|
|
|
CommonHelper.CKParts parts) throws Exception {
|
2023-10-10 10:39:27 +08:00
|
|
|
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CPP);
|
2023-08-26 20:34:51 +08:00
|
|
|
// write decls
|
|
|
|
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
2023-10-10 10:39:27 +08:00
|
|
|
indent.printf("extern const GeneralReflectionArray<LibCmo::%s::%s> %s;",
|
|
|
|
CommonHelper.getCKPartsNamespace(parts), enum_t.mEnumName, enum_t.mEnumName);
|
2023-08-26 20:34:51 +08:00
|
|
|
}
|
2023-10-10 10:39:27 +08:00
|
|
|
|
2023-08-26 20:34:51 +08:00
|
|
|
indent.puts("");
|
|
|
|
indent.puts("");
|
|
|
|
indent.puts("");
|
2023-10-10 10:39:27 +08:00
|
|
|
|
2023-08-26 20:34:51 +08:00
|
|
|
// write implements
|
2023-08-20 12:13:40 +08:00
|
|
|
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
|
|
|
// write enum desc header
|
2023-08-20 16:10:22 +08:00
|
|
|
indent.printf("const GeneralReflectionArray<LibCmo::%s::%s> %s {", CommonHelper.getCKPartsNamespace(parts),
|
2023-08-20 12:13:40 +08:00
|
|
|
enum_t.mEnumName, enum_t.mEnumName);
|
|
|
|
indent.inc();
|
|
|
|
|
|
|
|
// write enum desc entries
|
|
|
|
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
|
2023-08-20 16:10:22 +08:00
|
|
|
indent.printf("{ LibCmo::%s::%s::%s, {\"%s\"} },", CommonHelper.getCKPartsNamespace(parts),
|
|
|
|
enum_t.mEnumName, enumEntry_t.mEntryName, enumEntry_t.mEntryName);
|
2023-08-20 12:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// write enum tail
|
|
|
|
indent.dec();
|
|
|
|
indent.puts("};");
|
|
|
|
}
|
|
|
|
}
|
2023-08-26 20:34:51 +08:00
|
|
|
|
2023-10-12 10:42:06 +08:00
|
|
|
public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog, CommonHelper.CKParts parts)
|
|
|
|
throws Exception {
|
2023-10-10 10:39:27 +08:00
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writeAccVals(fs, prog, parts);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
2023-08-26 20:34:51 +08:00
|
|
|
public static void writeAccVal(OutputStreamWriter writer, EnumsHelper.Enum_t _enum, CommonHelper.CKParts parts)
|
|
|
|
throws Exception {
|
|
|
|
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
|
|
|
|
col.mEnums.add(_enum);
|
|
|
|
writeAccVals(writer, col, parts);
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:39:27 +08:00
|
|
|
public static void writeAccVal(String filename, EnumsHelper.Enum_t _enum, CommonHelper.CKParts parts)
|
|
|
|
throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writeAccVal(fs, _enum, parts);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
|
|
|
|
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
|
|
|
|
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
|
|
|
// write enum start
|
|
|
|
indent.printf("class %s(enum.IntEnum):", enum_t.mEnumName);
|
|
|
|
indent.inc();
|
|
|
|
|
|
|
|
// write enum comment
|
|
|
|
indent.briefComment(enum_t.mEnumComment);
|
|
|
|
|
|
|
|
// write enum entries
|
|
|
|
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
|
|
|
|
// write entry self
|
|
|
|
if (enumEntry_t.mEntryValue == null) {
|
|
|
|
indent.printf("%s = auto()", enumEntry_t.mEntryName);
|
|
|
|
} else {
|
2023-10-12 10:42:06 +08:00
|
|
|
indent.printf("%s = %s", enumEntry_t.mEntryName,
|
|
|
|
CommonHelper.convertToPythonNumber(enumEntry_t.mEntryValue));
|
2023-10-10 10:39:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// write entry comment after member
|
|
|
|
indent.afterMemberComment(enumEntry_t.mEntryComment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// enum tail
|
|
|
|
indent.dec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writePyEnums(fs, prog);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyEnum(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception {
|
|
|
|
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
|
|
|
|
col.mEnums.add(_enum);
|
|
|
|
writePyEnums(writer, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writePyEnum(fs, _enum);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
2023-10-12 10:42:06 +08:00
|
|
|
private static String extractHumanReadableEntryName(String entry_name) {
|
|
|
|
// remove first part (any content before underline '_')
|
|
|
|
entry_name = entry_name.replaceFirst("^[a-zA-Z0-9]+_", "");
|
|
|
|
|
|
|
|
// lower all chars except first char
|
|
|
|
if (entry_name.length() < 1)
|
|
|
|
return entry_name;
|
|
|
|
else
|
|
|
|
return entry_name.substring(0, 1) + entry_name.substring(1).toLowerCase(Locale.ROOT);
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:39:27 +08:00
|
|
|
public static void writePyAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
|
|
|
|
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
|
|
|
|
|
|
|
|
// write implements
|
|
|
|
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
|
|
|
// write enum desc header
|
2023-11-26 20:36:25 +08:00
|
|
|
indent.printf("g_Annotation_%s: dict[int, EnumAnnotation] = {", enum_t.mEnumName);
|
2023-10-10 10:39:27 +08:00
|
|
|
indent.inc();
|
|
|
|
|
|
|
|
// write enum desc entries
|
|
|
|
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
|
|
|
|
String comment = "";
|
|
|
|
if (enumEntry_t.mEntryComment != null) {
|
|
|
|
comment = CommonHelper.escapeString(enumEntry_t.mEntryComment);
|
|
|
|
}
|
2023-10-12 10:42:06 +08:00
|
|
|
|
2023-11-26 20:36:25 +08:00
|
|
|
indent.printf("%s.%s.value: EnumAnnotation(\"%s\", \"%s\"),", enum_t.mEnumName, enumEntry_t.mEntryName,
|
2023-10-12 10:42:06 +08:00
|
|
|
extractHumanReadableEntryName(enumEntry_t.mEntryName), comment);
|
2023-10-10 10:39:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// write enum tail
|
|
|
|
indent.dec();
|
|
|
|
indent.puts("}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyAccVals(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writePyAccVals(fs, prog);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyAccVal(OutputStreamWriter writer, EnumsHelper.Enum_t _enum) throws Exception {
|
|
|
|
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
|
|
|
|
col.mEnums.add(_enum);
|
|
|
|
writePyAccVals(writer, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePyAccVal(String filename, EnumsHelper.Enum_t _enum) throws Exception {
|
|
|
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
|
|
|
writePyAccVal(fs, _enum);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
|
2023-08-20 12:13:40 +08:00
|
|
|
}
|