update CodeGen for suiting new requirement of Python
This commit is contained in:
parent
0071e001fd
commit
09fd67fb5e
|
@ -4,6 +4,7 @@ import java.io.OutputStreamWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
|
|
||||||
|
@ -59,8 +60,7 @@ public class CommonHelper {
|
||||||
/**
|
/**
|
||||||
* Cut multiple comments.
|
* Cut multiple comments.
|
||||||
* <p>
|
* <p>
|
||||||
* Each comment will be cut the head and tail first.
|
* Each comment will be cut the head and tail first. And strip all whitespace.
|
||||||
* And strip all whitespace.
|
|
||||||
* Then join together.
|
* Then join together.
|
||||||
*
|
*
|
||||||
* @param tokens Multiple comments.
|
* @param tokens Multiple comments.
|
||||||
|
@ -90,6 +90,7 @@ public class CommonHelper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether Altlr captured CKGENERAL_NUM is a hex number.
|
* Check whether Altlr captured CKGENERAL_NUM is a hex number.
|
||||||
|
*
|
||||||
* @param numstr The captured number.
|
* @param numstr The captured number.
|
||||||
* @return true if it is hex number.
|
* @return true if it is hex number.
|
||||||
*/
|
*/
|
||||||
|
@ -97,6 +98,18 @@ public class CommonHelper {
|
||||||
return numstr.startsWith("0x") || numstr.startsWith("0X");
|
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.
|
* Get underlying type of enum.
|
||||||
*
|
*
|
||||||
|
@ -184,6 +197,7 @@ public class CommonHelper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove redundent star '*' (at least 5 continuous star chars)
|
* Remove redundent star '*' (at least 5 continuous star chars)
|
||||||
|
*
|
||||||
* @param cmt The string provided.
|
* @param cmt The string provided.
|
||||||
* @return The string processed.
|
* @return The string processed.
|
||||||
*/
|
*/
|
||||||
|
@ -192,4 +206,44 @@ public class CommonHelper {
|
||||||
return cmt.replaceAll("\\*{5,}", "");
|
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.
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic Enum Writer. Including Data Type Defination and Nameof Values.
|
* 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,
|
public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog, CommonHelper.CKParts parts)
|
||||||
CommonHelper.CKParts parts) throws Exception {
|
throws Exception {
|
||||||
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
|
||||||
writeAccVals(fs, prog, parts);
|
writeAccVals(fs, prog, parts);
|
||||||
fs.close();
|
fs.close();
|
||||||
|
@ -122,7 +123,8 @@ public class GeneralWriter {
|
||||||
if (enumEntry_t.mEntryValue == null) {
|
if (enumEntry_t.mEntryValue == null) {
|
||||||
indent.printf("%s = auto()", enumEntry_t.mEntryName);
|
indent.printf("%s = auto()", enumEntry_t.mEntryName);
|
||||||
} else {
|
} 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
|
// write entry comment after member
|
||||||
|
@ -152,13 +154,24 @@ public class GeneralWriter {
|
||||||
fs.close();
|
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 {
|
public static void writePyAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog) throws Exception {
|
||||||
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
|
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
|
||||||
|
|
||||||
// write implements
|
// write implements
|
||||||
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
|
||||||
// write enum desc header
|
// 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();
|
indent.inc();
|
||||||
|
|
||||||
// write enum desc entries
|
// write enum desc entries
|
||||||
|
@ -168,8 +181,8 @@ public class GeneralWriter {
|
||||||
comment = CommonHelper.escapeString(enumEntry_t.mEntryComment);
|
comment = CommonHelper.escapeString(enumEntry_t.mEntryComment);
|
||||||
}
|
}
|
||||||
|
|
||||||
indent.printf("%s.%s.value: \"%s\",", enum_t.mEnumName, enumEntry_t.mEntryName,
|
indent.printf("%s.%s.value: (\"%s\", \"%s\", ),", enum_t.mEnumName, enumEntry_t.mEntryName,
|
||||||
comment);
|
extractHumanReadableEntryName(enumEntry_t.mEntryName), comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
// write enum tail
|
// write enum tail
|
||||||
|
|
|
@ -5,22 +5,29 @@ public class IndentHelper {
|
||||||
mIndent = 0;
|
mIndent = 0;
|
||||||
mLangType = lang_type;
|
mLangType = lang_type;
|
||||||
mWriter = writer;
|
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 int mIndent;
|
||||||
private CommonHelper.LangType mLangType;
|
private CommonHelper.LangType mLangType;
|
||||||
|
private String mIndentChars;
|
||||||
private OutputStreamWriter mWriter;
|
private OutputStreamWriter mWriter;
|
||||||
|
|
||||||
private void rawIndent() throws Exception {
|
private void rawIndent() throws Exception {
|
||||||
for (int i = 0; i < mIndent; ++i) {
|
for (int i = 0; i < mIndent; ++i) {
|
||||||
switch (mLangType) {
|
mWriter.write(mIndentChars);
|
||||||
case CPP:
|
|
||||||
mWriter.write("\t");
|
|
||||||
break;
|
|
||||||
case Python:
|
|
||||||
mWriter.write(" ");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +40,7 @@ public class IndentHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void indent() throws Exception {
|
public void indent() throws Exception {
|
||||||
mWriter.write("\n");
|
mWriter.write(System.lineSeparator());
|
||||||
rawIndent();
|
rawIndent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,21 +68,19 @@ public class IndentHelper {
|
||||||
|
|
||||||
switch (mLangType) {
|
switch (mLangType) {
|
||||||
case CPP:
|
case CPP:
|
||||||
rawIndent();
|
|
||||||
puts("/**");
|
puts("/**");
|
||||||
|
|
||||||
puts(comment);
|
mWriter.write(System.lineSeparator());
|
||||||
|
mWriter.write(CommonHelper.reindentBlockString(comment, true, mIndent));
|
||||||
|
|
||||||
rawIndent();
|
|
||||||
puts(" */");
|
puts(" */");
|
||||||
break;
|
break;
|
||||||
case Python:
|
case Python:
|
||||||
rawIndent();
|
|
||||||
puts("\"\"\"!");
|
puts("\"\"\"!");
|
||||||
|
|
||||||
puts(comment);
|
mWriter.write(System.lineSeparator());
|
||||||
|
mWriter.write(CommonHelper.reindentBlockString(comment, false, mIndent));
|
||||||
|
|
||||||
rawIndent();
|
|
||||||
puts("\"\"\"");
|
puts("\"\"\"");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -93,9 +98,10 @@ public class IndentHelper {
|
||||||
if (comment == null)
|
if (comment == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
mWriter.write(mIndentChars);
|
||||||
switch (mLangType) {
|
switch (mLangType) {
|
||||||
case CPP:
|
case CPP:
|
||||||
mWriter.write(String.format("\t/**< %s */", CommonHelper.removeEol(comment)));
|
mWriter.write(String.format("/**< %s */", CommonHelper.removeEol(comment)));
|
||||||
break;
|
break;
|
||||||
case Python:
|
case Python:
|
||||||
mWriter.write(String.format("##< %s", CommonHelper.removeEol(comment)));
|
mWriter.write(String.format("##< %s", CommonHelper.removeEol(comment)));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user