1
0

fix: finish enums migration

This commit is contained in:
2026-01-27 16:38:29 +08:00
parent 9cb4d50f22
commit 0419dc3939
15 changed files with 165 additions and 919 deletions

View File

@@ -1,7 +1,6 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -48,11 +47,35 @@ public class CommonHelper {
switch (comment.getType()) {
case CKGeneralLexer.CKGENERAL_LINE_COMMENT: {
return removeStars(comment.getText().substring(2));
// For line comment, we start to remove "//" prefix first
String slashRemoved = comment.getText().substring(2);
// Then remove successive starts
String starsRemoved = removeSeperator(slashRemoved);
// Do a strip for it.
String stripped = starsRemoved.strip();
// Then remove EOL to avoid unexpected line break.
String eolRemoved = removeEol(stripped);
// Okey
return eolRemoved;
}
case CKGeneralLexer.CKGENERAL_BLOCK_COMMENT: {
String cache = comment.getText();
return removeStars(cache.substring(2, cache.length() - 4));
// For block comment, we first cut "/*" head and "*/" tail.
String blockComment = comment.getText();
String slashRemoved = blockComment.substring(2, blockComment.length() - 4);
// Then we break it at line breaker and process each line one by one.
String beautyComment = slashRemoved.lines().map((String line) -> {
// Remove successive starts
String starsRemoved = removeSeperator(line);
// Do a strip for it.
String stripped = starsRemoved.strip();
// Then remove EOL to avoid unexpected line break.
String eolRemoved = removeEol(stripped);
// Line process is okey now
return eolRemoved;
}).collect(Collectors.joining("\n")); // Then re-join with fresh line breaker
// Then return
return beautyComment;
}
default:
return comment.getText();
@@ -73,9 +96,8 @@ public class CommonHelper {
return null;
return tokens.stream().map(value -> {
String text = cutComment(value).strip();
return text + " ";
}).collect(Collectors.joining(""));
return cutComment(value);
}).collect(Collectors.joining("\n"));
}
// =========== Number Operations ===========
@@ -92,46 +114,13 @@ 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.
*/
public static boolean isHexNumber(String numstr) {
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]+$", "");
}
// =========== Parts ===========
enum CKParts {
CK2, VxMath
}
enum LangType {
Cpp, Python, CSharp
}
public static String getCKPartsNamespace(CKParts parts) {
switch (parts) {
case CK2:
return "CK2";
case VxMath:
return "VxMath";
default:
throw new IllegalArgumentException("Unexpected value: " + parts);
}
}
// =========== File Operations ===========
@@ -182,80 +171,25 @@ public class CommonHelper {
// =========== String Process ===========
/**
* Escape String
*
* Escape all characters which are invalid in string quote.
*
* @param strl The string need to be escaped.
* @return The escaped string.
* @see removeEol
*/
public static String escapeString(String strl) {
return strl.replace("\\", "\\\\").replace("\t", "\\t").replace("\b", "\\b").replace("\n", "\\n")
.replace("\r", "\\r").replace("\f", "\\f").replace("\"", "\\\"");
}
/**
* Remove all EOL (End of Line) characters.
*
* @param strl The string need to be processed.
* @return The string eliminated all EOL.
* @see escapeString
*/
public static String removeEol(String strl) {
return strl.replace("\n", "").replace("\r", "");
}
/**
* Remove redundent star '*' (at least 5 continuous star chars)
* Remove seperator bar consisted by '*' or '-' (at least 5 successive chars)
*
* @param cmt The string provided.
* @return The string processed.
*/
public static String removeStars(String cmt) {
public static String removeSeperator(String cmt) {
// 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.
return cmt.replaceAll("[\\*\\-]{5,}", "");
}
}