From 09fd67fb5e4388bf7e0184632578bec0a1050e1b Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Thu, 12 Oct 2023 10:42:06 +0800 Subject: [PATCH] update CodeGen for suiting new requirement of Python --- CodeGen/EnumsMigration/CommonHelper.java | 66 ++++++++++++++++++++--- CodeGen/EnumsMigration/GeneralWriter.java | 27 +++++++--- CodeGen/EnumsMigration/IndentHelper.java | 44 ++++++++------- 3 files changed, 105 insertions(+), 32 deletions(-) diff --git a/CodeGen/EnumsMigration/CommonHelper.java b/CodeGen/EnumsMigration/CommonHelper.java index 34a2313..49b75e8 100644 --- a/CodeGen/EnumsMigration/CommonHelper.java +++ b/CodeGen/EnumsMigration/CommonHelper.java @@ -4,6 +4,7 @@ import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.antlr.v4.runtime.*; @@ -24,7 +25,7 @@ public class CommonHelper { return null; return tokens.get(0); } - + public static List getPreChannelTokens(BufferedTokenStream stream, Token token, int channel) { return stream.getHiddenTokensToLeft(token.getTokenIndex(), channel); } @@ -59,8 +60,7 @@ public class CommonHelper { /** * Cut multiple comments. *

- * Each comment will be cut the head and tail first. - * And strip all whitespace. + * Each comment will be cut the head and tail first. And strip all whitespace. * Then join together. * * @param tokens Multiple comments. @@ -75,7 +75,7 @@ public class CommonHelper { return text + " "; }).collect(Collectors.joining("")); } - + // =========== Number Operations =========== /** @@ -90,6 +90,7 @@ public class CommonHelper { /** * Check whether Altlr captured CKGENERAL_NUM is a hex number. + * * @param numstr The captured number. * @return true if it is hex number. */ @@ -97,6 +98,18 @@ public class CommonHelper { return numstr.startsWith("0x") || numstr.startsWith("0X"); } + /** + * Convert accepted string into Python cupported format. + * + * It actually just remove trail "UL". + * + * @param numstr The captured number. + * @return The Python style number string. + */ + public static String convertToPythonNumber(String numstr) { + return numstr.replaceFirst("[ulUL]+$", ""); + } + /** * Get underlying type of enum. * @@ -113,7 +126,7 @@ public class CommonHelper { enum CKParts { CK2, VxMath } - + enum LangType { CPP, Python } @@ -184,6 +197,7 @@ public class CommonHelper { /** * Remove redundent star '*' (at least 5 continuous star chars) + * * @param cmt The string provided. * @return The string processed. */ @@ -191,5 +205,45 @@ public class CommonHelper { // only remove at least 5 continuous star chars. return cmt.replaceAll("\\*{5,}", ""); } - + + /** + * Get indent char by style + * + * @param use_tab Whether indent use Tab, not Space. + * @return The indent chars + */ + public static String getIndentString(boolean use_tab) { + if (use_tab) + return "\t"; + else + return " "; + } + + /** + * Re-indent a block of string + * + * This function will first split block string by line. Then remove all indent + * (strip Tab and Space). At last, re-indent and join each line + * + * @param block_str The string provided. + * @param use_tab Use Tab, not Space as indent chars. + * @param indent_level The level of indent, started by 0. + * @return The re-indent string + */ + public static String reindentBlockString(String block_str, boolean use_tab, int indent_level) { + // pre-create indent string + String indentChars = getIndentString(use_tab); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < indent_level; ++i) { + sb.append(indentChars); + } + String indents = sb.toString(); + + // split line + return block_str.lines().map((String line) -> { + // strip space and tab, then re-indent it. + return indents + line.trim(); + }).collect(Collectors.joining(System.lineSeparator())); // then join with new line breaker and return. + } + } diff --git a/CodeGen/EnumsMigration/GeneralWriter.java b/CodeGen/EnumsMigration/GeneralWriter.java index df7106f..e95e85a 100644 --- a/CodeGen/EnumsMigration/GeneralWriter.java +++ b/CodeGen/EnumsMigration/GeneralWriter.java @@ -1,4 +1,5 @@ import java.io.OutputStreamWriter; +import java.util.Locale; /** * Generic Enum Writer. Including Data Type Defination and Nameof Values. @@ -85,8 +86,8 @@ public class GeneralWriter { } } - public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog, - CommonHelper.CKParts parts) throws Exception { + public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog, CommonHelper.CKParts parts) + throws Exception { OutputStreamWriter fs = CommonHelper.openOutputFile(filename); writeAccVals(fs, prog, parts); fs.close(); @@ -122,7 +123,8 @@ public class GeneralWriter { if (enumEntry_t.mEntryValue == null) { indent.printf("%s = auto()", enumEntry_t.mEntryName); } else { - indent.printf("%s = %s", enumEntry_t.mEntryName, enumEntry_t.mEntryValue); + indent.printf("%s = %s", enumEntry_t.mEntryName, + CommonHelper.convertToPythonNumber(enumEntry_t.mEntryValue)); } // write entry comment after member @@ -152,13 +154,24 @@ public class GeneralWriter { fs.close(); } + 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); + } + 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 - indent.printf("g_Annotation_%s: dict[int, str] = {", enum_t.mEnumName); + indent.printf("g_Annotation_%s: dict[int, tuple[str, str]] = {", enum_t.mEnumName); indent.inc(); // write enum desc entries @@ -167,9 +180,9 @@ public class GeneralWriter { if (enumEntry_t.mEntryComment != null) { comment = CommonHelper.escapeString(enumEntry_t.mEntryComment); } - - indent.printf("%s.%s.value: \"%s\",", enum_t.mEnumName, enumEntry_t.mEntryName, - comment); + + indent.printf("%s.%s.value: (\"%s\", \"%s\", ),", enum_t.mEnumName, enumEntry_t.mEntryName, + extractHumanReadableEntryName(enumEntry_t.mEntryName), comment); } // write enum tail diff --git a/CodeGen/EnumsMigration/IndentHelper.java b/CodeGen/EnumsMigration/IndentHelper.java index c3aa7fc..ec098bd 100644 --- a/CodeGen/EnumsMigration/IndentHelper.java +++ b/CodeGen/EnumsMigration/IndentHelper.java @@ -5,22 +5,29 @@ public class IndentHelper { mIndent = 0; mLangType = lang_type; mWriter = writer; + + // set indent chars + switch (mLangType) { + case CPP: + mIndentChars = CommonHelper.getIndentString(true); + break; + case Python: + mIndentChars = CommonHelper.getIndentString(false); + break; + default: + mIndentChars = ""; + break; + } } private int mIndent; private CommonHelper.LangType mLangType; + private String mIndentChars; private OutputStreamWriter mWriter; private void rawIndent() throws Exception { for (int i = 0; i < mIndent; ++i) { - switch (mLangType) { - case CPP: - mWriter.write("\t"); - break; - case Python: - mWriter.write(" "); - break; - } + mWriter.write(mIndentChars); } } @@ -33,7 +40,7 @@ public class IndentHelper { } public void indent() throws Exception { - mWriter.write("\n"); + mWriter.write(System.lineSeparator()); rawIndent(); } @@ -61,21 +68,19 @@ public class IndentHelper { switch (mLangType) { case CPP: - rawIndent(); puts("/**"); + + mWriter.write(System.lineSeparator()); + mWriter.write(CommonHelper.reindentBlockString(comment, true, mIndent)); - puts(comment); - - rawIndent(); puts(" */"); break; case Python: - rawIndent(); puts("\"\"\"!"); - - puts(comment); - rawIndent(); + mWriter.write(System.lineSeparator()); + mWriter.write(CommonHelper.reindentBlockString(comment, false, mIndent)); + puts("\"\"\""); break; } @@ -93,12 +98,13 @@ public class IndentHelper { if (comment == null) return; + mWriter.write(mIndentChars); switch (mLangType) { case CPP: - mWriter.write(String.format("\t/**< %s */", CommonHelper.removeEol(comment))); + mWriter.write(String.format("/**< %s */", CommonHelper.removeEol(comment))); break; case Python: - mWriter.write(String.format(" ##< %s", CommonHelper.removeEol(comment))); + mWriter.write(String.format("##< %s", CommonHelper.removeEol(comment))); break; }