1
0

refactor: refactor EnumsMigration but not finished

This commit is contained in:
2026-01-26 11:11:58 +08:00
parent e0e5c9b090
commit c68bdce37b
36 changed files with 669 additions and 409 deletions

View File

@@ -1,43 +1,6 @@
# ===== Result =====
dest/*
!dest/*.gitkeep
# ===== ANTLR Output =====
*.interp
*.tokens
CKGeneralLexer*.java
CKEnumsParser*.java
CKDefinesParser*.java
# ===== Eclipse Java =====
# Eclipse projects
.classpath
.project
.settings/
.metadata
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
## ======== Personal ========
# Ignore intermediate stuff and output stuff.
Intermediate/*
!Intermediate/*.gitkeep
Output/*
!Output/*.gitkeep

View File

@@ -0,0 +1,126 @@
## ======== Personal ========
# Additional remove for JetBrains IDEA
.idea/
*.iml
## ======== ANTLR Output ========
*.interp
*.tokens
CKGeneralLexer*.java
CKEnumsParser*.java
CKDefinesParser*.java
## ======== Java ========
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
## ======== JetBrains ========
# Covers JetBrains IDEs: IntelliJ, GoLand, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
.idea/sonarlint.xml # see https://community.sonarsource.com/t/is-the-file-idea-idea-idea-sonarlint-xml-intended-to-be-under-source-control/121119
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based HTTP Client
.idea/httpRequests
http-client.private.env.json
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# Apifox Helper cache
.idea/.cache/.Apifox_Helper
.idea/ApifoxUploaderProjectSetting.xml
# Github Copilot persisted session migrations, see: https://github.com/microsoft/copilot-intellij-feedback/issues/712#issuecomment-3322062215
.idea/**/copilot.data.migration.*.xml

View File

@@ -24,35 +24,35 @@ public class CSharpWriter {
* Internal real enum declaration writer.
*
* @param writer {@linkplain java.io.OutputStreamWriter} instance for writing.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for writing.
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing.
* @throws Exception
*/
private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog)
private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog)
throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CSharp);
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
// write enum comment
indent.briefComment(enum_t.mEnumComment);
indent.briefComment(benum.mEnumComment);
// write enum start
// write flasg attribute if it is
if (enum_t.mUseFlags) {
if (benum.mUseFlags) {
indent.puts("[Flags]");
}
indent.printf("public enum %s : %s {", enum_t.mEnumName, getEnumUnderlyingType(enum_t.mCanUnsigned));
indent.printf("public enum %s : %s {", benum.mEnumName, getEnumUnderlyingType(benum.mCanUnsigned));
indent.inc();
// write enum entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
// write entry self
if (enumEntry_t.mEntryValue == null) {
indent.printf("%s,", enumEntry_t.mEntryName);
if (enumEntry.mEntryValue == null) {
indent.printf("%s,", enumEntry.mEntryName);
} else {
indent.printf("%s = %s,", enumEntry_t.mEntryName, enumEntry_t.mEntryValue);
indent.printf("%s = %s,", enumEntry.mEntryName, enumEntry.mEntryValue);
}
// write entry comment after member
indent.afterMemberComment(enumEntry_t.mEntryComment);
indent.afterMemberComment(enumEntry.mEntryComment);
}
// write enum tail
@@ -67,11 +67,11 @@ public class CSharpWriter {
* Actually this is a wrapper of internal enum declaration collection writer.
*
* @param filename The name of written file.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for
* writing.
* @throws Exception
*/
public static void writeEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
public static void writeEnums(String filename, EnumsHelper.BEnumCollection prog) throws Exception {
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
internalWriteEnums(fs, prog);
@@ -84,12 +84,12 @@ public class CSharpWriter {
* Actually this is a wrapper of internal enum declaration collection writer.
*
* @param filename The name of written file.
* @param _enum {@linkplain EnumsHelper.Enum_t} instance for writing.
* @param _enum {@linkplain EnumsHelper.BEnum} instance for writing.
* @throws Exception
*/
public static void writeEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
public static void writeEnum(String filename, EnumsHelper.BEnum _enum) throws Exception {
// create collection from single enum
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection();
col.mEnums.add(_enum);
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
@@ -103,23 +103,23 @@ public class CSharpWriter {
* Internal real enum accessible value writer.
*
* @param writer {@linkplain java.io.OutputStreamWriter} instance for writing.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for writing.
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing.
* @throws Exception
*/
private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog)
private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog)
throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CSharp);
// write enum collections
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
// write enum desc header
indent.printf(
"public static readonly System.Collections.Generic.Dictionary<%s, string> %s = new System.Collections.Generic.Dictionary<%s, string>() {",
enum_t.mEnumName, enum_t.mEnumName, enum_t.mEnumName);
benum.mEnumName, benum.mEnumName, benum.mEnumName);
indent.inc();
// write enum desc entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
indent.printf("{ %s.%s, \"%s\" },", enum_t.mEnumName, enumEntry_t.mEntryName, enumEntry_t.mEntryName);
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
indent.printf("{ %s.%s, \"%s\" },", benum.mEnumName, enumEntry.mEntryName, enumEntry.mEntryName);
}
// write enum tail
@@ -135,11 +135,11 @@ public class CSharpWriter {
* writer.
*
* @param filename The name of written file.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for
* writing.
* @throws Exception
*/
public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
public static void writeAccVals(String filename, EnumsHelper.BEnumCollection prog) throws Exception {
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
internalWriteAccVals(fs, prog);
@@ -153,12 +153,12 @@ public class CSharpWriter {
* writer.
*
* @param filename The name of written file.
* @param _enum {@linkplain EnumsHelper.Enum_t} instance for writing.
* @param _enum {@linkplain EnumsHelper.BEnum} instance for writing.
* @throws Exception
*/
public static void writeAccVal(String filename, EnumsHelper.Enum_t _enum) throws Exception {
public static void writeAccVal(String filename, EnumsHelper.BEnum _enum) throws Exception {
// create a collection with single enum.
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection();
col.mEnums.add(_enum);
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);

View File

@@ -2,7 +2,6 @@
import java.util.Stack;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
/**
* The specialized walker for collecting CK_CLASSID and its inherit
@@ -19,7 +18,7 @@ public class ClassidWalker extends CKDefinesParserBaseListener {
mCurrentEntry = null;
}
public EnumsHelper.Enum_t getEnum() {
public EnumsHelper.BEnum getEnum() {
return mResult;
}
@@ -54,18 +53,18 @@ public class ClassidWalker extends CKDefinesParserBaseListener {
}
private BufferedTokenStream mTokenStream;
private EnumsHelper.Enum_t mResult;
private EnumsHelper.BEnum mResult;
private int mLevel;
private Stack<EnumsHelper.EnumEntryWithHierarchy_t> mLevelStack;
private EnumsHelper.Enum_t mCurrentEnum;
private EnumsHelper.EnumEntryWithHierarchy_t mCurrentEntry;
private Stack<EnumsHelper.BHierarchyEnumEntry> mLevelStack;
private EnumsHelper.BEnum mCurrentEnum;
private EnumsHelper.BHierarchyEnumEntry mCurrentEntry;
@Override
public void enterProg(CKDefinesParser.ProgContext ctx) {
mLevel = 0;
mLevelStack = new Stack<EnumsHelper.EnumEntryWithHierarchy_t>();
mCurrentEnum = new EnumsHelper.Enum_t();
mLevelStack = new Stack<EnumsHelper.BHierarchyEnumEntry>();
mCurrentEnum = new EnumsHelper.BEnum();
}
@Override
@@ -82,7 +81,7 @@ public class ClassidWalker extends CKDefinesParserBaseListener {
@Override
public void enterDefinePair(CKDefinesParser.DefinePairContext ctx) {
mCurrentEntry = new EnumsHelper.EnumEntryWithHierarchy_t();
mCurrentEntry = new EnumsHelper.BHierarchyEnumEntry();
}
@Override

View File

@@ -1,7 +1,10 @@
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;
import java.util.List;
import java.util.stream.Collectors;
@@ -132,6 +135,11 @@ public class CommonHelper {
// =========== File Operations ===========
private static Path getRootDirectoryPath() throws Exception {
String rootDir = System.getenv("ENUMS_MIGRATION_ROOT");
return Paths.get(rootDir);
}
public static class InputFilePair {
public CharStream mAntlrStream;
public FileInputStream mUnderlyingStream;
@@ -144,6 +152,12 @@ public class CommonHelper {
return pair;
}
public static String getInputFilePath(String filename) throws Exception {
Path rootDir = getRootDirectoryPath();
Path filePath = rootDir.resolve("Input").resolve(filename);
return filePath.toString();
}
/**
* Get output file for writing.
*
@@ -156,6 +170,12 @@ public class CommonHelper {
return new OutputStreamWriter(fs, StandardCharsets.UTF_8);
}
public static String getOutputFilePath(String filename) throws Exception {
Path rootDir = getRootDirectoryPath();
Path filePath = rootDir.resolve("Intermediate").resolve(filename);
return filePath.toString();
}
// =========== String Process ===========
/**

View File

@@ -25,31 +25,31 @@ public class CppWriter {
* Internal real enum declarations writer.
*
* @param writer {@linkplain java.io.OutputStreamWriter} instance for writing.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for writing.
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing.
* @throws Exception
*/
private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog)
private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog)
throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp);
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
// write enum comment
indent.briefComment(enum_t.mEnumComment);
indent.briefComment(benum.mEnumComment);
// write enum start
indent.printf("enum class %s : %s {", enum_t.mEnumName, getEnumUnderlyingType(enum_t.mCanUnsigned));
indent.printf("enum class %s : %s {", benum.mEnumName, getEnumUnderlyingType(benum.mCanUnsigned));
indent.inc();
// write enum entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
// write entry self
if (enumEntry_t.mEntryValue == null) {
indent.printf("%s,", enumEntry_t.mEntryName);
if (enumEntry.mEntryValue == null) {
indent.printf("%s,", enumEntry.mEntryName);
} else {
indent.printf("%s = %s,", enumEntry_t.mEntryName, enumEntry_t.mEntryValue);
indent.printf("%s = %s,", enumEntry.mEntryName, enumEntry.mEntryValue);
}
// write entry comment after member
indent.afterMemberComment(enumEntry_t.mEntryComment);
indent.afterMemberComment(enumEntry.mEntryComment);
}
// write enum tail
@@ -64,11 +64,11 @@ public class CppWriter {
* Actually this is a wrapper of internal enum collection writer.
*
* @param filename The name of written file.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for
* writing.
* @throws Exception
*/
public static void writeEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
public static void writeEnums(String filename, EnumsHelper.BEnumCollection prog) throws Exception {
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
internalWriteEnums(fs, prog);
@@ -81,13 +81,13 @@ public class CppWriter {
* Actually this is a wrapper of internal enum collection writer.
*
* @param filename The name of written file.
* @param _enum {@linkplain EnumsHelper.Enum_t} instance for writing.
* @param _enum {@linkplain EnumsHelper.BEnum} instance for writing.
* @throws Exception
*/
public static void writeEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
public static void writeEnum(String filename, EnumsHelper.BEnum _enum) throws Exception {
// create an collection from single enum declaration
// for suit the argument requirement of real writer.
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection();
col.mEnums.add(_enum);
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
@@ -101,12 +101,12 @@ public class CppWriter {
* Internal real enum collection accessible value writer.
*
* @param writer {@linkplain java.io.OutputStreamWriter} instance for writing.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for writing.
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing.
* @param parts The part of these enum declarations. It will indicate the
* namespace where find given enum collection.
* @throws Exception
*/
private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog,
private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog,
CommonHelper.CKParts parts) throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp);
@@ -120,9 +120,9 @@ public class CppWriter {
indent.puts("");
// write declarations
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
indent.printf("extern const GeneralReflectionArray<LibCmo::%s::%s> %s;",
CommonHelper.getCKPartsNamespace(parts), enum_t.mEnumName, enum_t.mEnumName);
CommonHelper.getCKPartsNamespace(parts), benum.mEnumName, benum.mEnumName);
}
indent.puts("");
@@ -130,16 +130,16 @@ public class CppWriter {
indent.puts("");
// write implements
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
// write enum desc header
indent.printf("const GeneralReflectionArray<LibCmo::%s::%s> %s {", CommonHelper.getCKPartsNamespace(parts),
enum_t.mEnumName, enum_t.mEnumName);
benum.mEnumName, benum.mEnumName);
indent.inc();
// write enum desc entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
indent.printf("{ LibCmo::%s::%s::%s, { u8\"%s\" } },", CommonHelper.getCKPartsNamespace(parts),
enum_t.mEnumName, enumEntry_t.mEntryName, enumEntry_t.mEntryName);
benum.mEnumName, enumEntry.mEntryName, enumEntry.mEntryName);
}
// write enum tail
@@ -155,12 +155,12 @@ public class CppWriter {
* writer.
*
* @param filename The name of written file.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for
* writing.
* @param parts The part of these enum declarations.
* @throws Exception
*/
public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog, CommonHelper.CKParts parts)
public static void writeAccVals(String filename, EnumsHelper.BEnumCollection prog, CommonHelper.CKParts parts)
throws Exception {
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
@@ -175,14 +175,14 @@ public class CppWriter {
* writer.
*
* @param filename The name of written file.
* @param _enum {@linkplain EnumsHelper.Enum_t} instance for writing.
* @param _enum {@linkplain EnumsHelper.BEnum} instance for writing.
* @param parts The part of these enum declarations.
* @throws Exception
*/
public static void writeAccVal(String filename, EnumsHelper.Enum_t _enum, CommonHelper.CKParts parts)
public static void writeAccVal(String filename, EnumsHelper.BEnum _enum, CommonHelper.CKParts parts)
throws Exception {
// create a enum collection to fulfill the requirement of internal writer.
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection();
col.mEnums.add(_enum);
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
@@ -201,10 +201,10 @@ public class CppWriter {
* common writer.
*
* @param filename The name of written file.
* @param errors The {@linkplain EnumsHelper.Enum_t} instance storing CKERROR.
* @param errors The {@linkplain EnumsHelper.BEnum} instance storing CKERROR.
* @throws Exception
*/
public static void writeCkErrorAccVal(String filename, EnumsHelper.Enum_t errors) throws Exception {
public static void writeCkErrorAccVal(String filename, EnumsHelper.BEnum errors) throws Exception {
OutputStreamWriter writer = CommonHelper.openOutputFile(filename);
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp);
@@ -219,7 +219,7 @@ public class CppWriter {
// write implementation
indent.puts("const CkErrorReflectionArray CKERROR {");
indent.inc();
for (EnumsHelper.EnumEntry_t entry : errors.mEntries) {
for (EnumsHelper.BEnumEntry entry : errors.mEntries) {
String comment = CommonHelper.escapeString(entry.mEntryComment);
if (comment == null)
comment = "";
@@ -240,11 +240,11 @@ public class CppWriter {
* common writer.
*
* @param filename The name of written file.
* @param classids The {@linkplain EnumsHelper.Enum_t} instance storing
* @param classids The {@linkplain EnumsHelper.BEnum} instance storing
* CK_CLASSID.
* @throws Exception
*/
public static void writeCkClassidAccVal(String filename, EnumsHelper.Enum_t classids) throws Exception {
public static void writeCkClassidAccVal(String filename, EnumsHelper.BEnum classids) throws Exception {
OutputStreamWriter writer = CommonHelper.openOutputFile(filename);
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp);
@@ -258,8 +258,8 @@ public class CppWriter {
indent.puts("const CkClassidReflectionArray CK_CLASSID {");
indent.inc();
for (EnumsHelper.EnumEntry_t entry : classids.mEntries) {
EnumsHelper.EnumEntryWithHierarchy_t specialized = (EnumsHelper.EnumEntryWithHierarchy_t) entry;
for (EnumsHelper.BEnumEntry entry : classids.mEntries) {
EnumsHelper.BHierarchyEnumEntry specialized = (EnumsHelper.BHierarchyEnumEntry) entry;
String hierarchy = specialized.mHierarchy.stream().map(value -> value.mEntryName)
.collect(Collectors.joining("\", u8\""));

View File

@@ -1,6 +1,5 @@
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
/**
* The generic walker for collecting defines as a enum.
@@ -14,19 +13,19 @@ public class DefinesWalker extends CKDefinesParserBaseListener {
mCurrentEntry = null;
}
public EnumsHelper.Enum_t getEnum() {
public EnumsHelper.BEnum getEnum() {
return mResult;
}
private CommentsFinder mCommentsFinder;
private EnumsHelper.Enum_t mResult;
private EnumsHelper.BEnum mResult;
private EnumsHelper.Enum_t mCurrentEnum;
private EnumsHelper.EnumEntry_t mCurrentEntry;
private EnumsHelper.BEnum mCurrentEnum;
private EnumsHelper.BEnumEntry mCurrentEntry;
@Override
public void enterProg(CKDefinesParser.ProgContext ctx) {
mCurrentEnum = new EnumsHelper.Enum_t();
mCurrentEnum = new EnumsHelper.BEnum();
}
@Override
@@ -37,7 +36,7 @@ public class DefinesWalker extends CKDefinesParserBaseListener {
@Override
public void enterDefinePair(CKDefinesParser.DefinePairContext ctx) {
mCurrentEntry = new EnumsHelper.EnumEntry_t();
mCurrentEntry = new EnumsHelper.BEnumEntry();
}
@Override

View File

@@ -0,0 +1,72 @@
import java.util.Vector;
public class EnumsHelper {
/**
* The struct to describe the entry of an enum.
*/
public static class BEnumEntry {
public BEnumEntry() {
mEntryName = null;
mEntryValue = null;
mEntryComment = null;
}
/** The name of this entry. Can not be null. */
public String mEntryName;
/** The value of this entry. null if this entry do not have explicit value. */
public String mEntryValue;
/** The comment of this entry. null if no comment. */
public String mEntryComment;
}
/**
* The specialized EnumEntry type which can store extra hierarchy info.
* Used in CK_CLASSID parsing.
*/
public static class BHierarchyEnumEntry extends BEnumEntry {
public BHierarchyEnumEntry() {
super();
mHierarchy = new Vector<BHierarchyEnumEntry>();
}
/** The list to store this CK_CLASSID inheritance relationship. */
public Vector<BHierarchyEnumEntry> mHierarchy;
}
/**
* The struct to describe an enum.
*/
public static class BEnum {
public BEnum() {
mEnumName = null;
mEnumComment = null;
mCanUnsigned = true;
mUseFlags = false;
mEntries = new Vector<BEnumEntry>();
}
/** The name of this enum. Can not be null. */
public String mEnumName;
/** The comment of this enum. null if no comment. */
public String mEnumComment;
/** True if this enum can use unsigned integer as its underlying type. */
public boolean mCanUnsigned;
/** True if this enum will use flags feature (supporting OR, AND, operators). */
public boolean mUseFlags;
/** The list to store entries of this enum. */
public Vector<BEnumEntry> mEntries;
}
/**
* The struct to describe a collection of enums.
*/
public static class BEnumCollection {
public BEnumCollection() {
mEnums = new Vector<BEnum>();
}
/** The list to store enums. */
public Vector<BEnum> mEnums;
}
}

View File

@@ -18,7 +18,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
mCurrentEntry = null;
}
public EnumsHelper.EnumCollection_t getEnums() {
public EnumsHelper.BEnumCollection getEnums() {
return mResult;
}
@@ -29,15 +29,15 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
private BufferedTokenStream mTokenStream;
private CommentsFinder mCommentsFinder;
private EnumsHelper.EnumCollection_t mResult;
private EnumsHelper.BEnumCollection mResult;
private EnumsHelper.EnumCollection_t mCurrentProg;
private EnumsHelper.Enum_t mCurrentEnum;
private EnumsHelper.EnumEntry_t mCurrentEntry;
private EnumsHelper.BEnumCollection mCurrentProg;
private EnumsHelper.BEnum mCurrentEnum;
private EnumsHelper.BEnumEntry mCurrentEntry;
@Override
public void enterProg(CKEnumsParser.ProgContext ctx) {
mCurrentProg = new EnumsHelper.EnumCollection_t();
mCurrentProg = new EnumsHelper.BEnumCollection();
}
@Override
@@ -48,7 +48,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
@Override
public void enterEnumBody(CKEnumsParser.EnumBodyContext ctx) {
mCurrentEnum = new EnumsHelper.Enum_t();
mCurrentEnum = new EnumsHelper.BEnum();
}
@Override
@@ -65,7 +65,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
@Override
public void enterEntryPair(CKEnumsParser.EntryPairContext ctx) {
mCurrentEntry = new EnumsHelper.EnumEntry_t();
mCurrentEntry = new EnumsHelper.BEnumEntry();
}
@Override

View File

@@ -0,0 +1,73 @@
import java.io.OutputStreamWriter;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonWriter {
private static JsonObject writeBEnumEntry(EnumsHelper.BEnumEntry enumEntry) {
JsonObject data = new JsonObject();
data.addProperty("name", enumEntry.mEntryName);
data.addProperty("value", enumEntry.mEntryValue);
data.addProperty("comment", enumEntry.mEntryComment);
// Export hierarchy if possible
if (enumEntry instanceof EnumsHelper.BHierarchyEnumEntry hierarchyEnumEntry) {
// We only export name in hierarchy.
// Otherwise we may cause recursive calling.
JsonArray entries = new JsonArray();
for (EnumsHelper.BHierarchyEnumEntry subEntry : hierarchyEnumEntry.mHierarchy) {
entries.add(subEntry.mEntryName);
}
data.add("hierarchy", entries);
}
return data;
}
private static JsonObject writeBEnum(EnumsHelper.BEnum benum) {
JsonObject data = new JsonObject();
data.addProperty("name", benum.mEnumName);
data.addProperty("comment", benum.mEnumComment);
data.addProperty("can_unsigned", benum.mCanUnsigned);
data.addProperty("use_flags", benum.mUseFlags);
data.addProperty("use_flags", benum.mUseFlags);
JsonArray entries = new JsonArray();
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
entries.add(writeBEnumEntry(enumEntry));
}
data.add("entries", entries);
return data;
}
private static JsonArray writeBEnumCollection(EnumsHelper.BEnumCollection enumCollection) {
JsonArray data = new JsonArray();
for (EnumsHelper.BEnum benum : enumCollection.mEnums) {
data.add(writeBEnum(benum));
}
return data;
}
private static void writeJson(String filename, EnumsHelper.BEnumCollection enumCollection) throws Exception {
OutputStreamWriter writer = CommonHelper.openOutputFile(filename);
//Gson gsonInstance = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
Gson gsonInstance = new GsonBuilder().disableHtmlEscaping().create();
writer.write(gsonInstance.toJson(writeBEnumCollection(enumCollection)));
writer.close();
}
public static void writeEnums(String filename, EnumsHelper.BEnumCollection enumCollection) throws Exception {
writeJson(filename, enumCollection);
}
public static void writeEnum(String filename, EnumsHelper.BEnum benum) throws Exception {
// Build collection manually.
EnumsHelper.BEnumCollection collection = new EnumsHelper.BEnumCollection();
collection.mEnums.add(benum);
// Call underlying writer
writeEnums(filename, collection);
}
}

View File

@@ -0,0 +1,196 @@
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class MainRunner {
/**
* Extract an enums collection from given file.
* <p>
* This function is the most commonly used function for extracting enums.
* <p>
* This function is used for a file which only contain enum declarations. This
* is not suit for extracting CKERROR and CK_CLASSID. For these declarations,
* please use their specialized extractor as described following.
*
* @param filename The file name relative to input directory for reading.
* @return An {@linkplain EnumsHelper.BEnumCollection} instance.
* @throws Exception
*/
private static EnumsHelper.BEnumCollection getEnumsCollection(String filename) throws Exception {
String infile = CommonHelper.getInputFilePath(filename);
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CKEnumsParser parser = new CKEnumsParser(tokens);
ParseTree tree = parser.prog();
ParseTreeWalker walker = new ParseTreeWalker();
EnumsWalker worker = new EnumsWalker(tokens);
walker.walk(worker, tree);
pair.mUnderlyingStream.close();
return worker.getEnums();
}
/**
* Extract a series of "#define" syntax as an enum.
* <p>
* This function will assume that given file only contain C++ "#define" syntax.
* After reading it, it will re-organize it as an enum and return. This only is
* used by CKERROR now. But it suit for more scenarios if there are something
* like CKERROR in future.
*
* @param filename The file name relative to input directory for reading.
* @param assignedEnumName The desired name of organized enum instance.
* Contemporary this field should always be "CKERROR"
* because no one else is using it.
* @return An {@linkplain EnumsHelper.BEnum} instance.
* @throws Exception
*/
private static EnumsHelper.BEnum organiseDefines(String filename, String assignedEnumName) throws Exception {
String infile = CommonHelper.getInputFilePath(filename);
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CKDefinesParser parser = new CKDefinesParser(tokens);
ParseTree tree = parser.prog();
ParseTreeWalker walker = new ParseTreeWalker();
DefinesWalker worker = new DefinesWalker(tokens);
walker.walk(worker, tree);
pair.mUnderlyingStream.close();
EnumsHelper.BEnum result = worker.getEnum();
result.mEnumName = assignedEnumName;
return result;
}
/**
* Extract a series of macro define as an enum, considering its indent to build
* hierarchy.
* <p>
* This is specialized enum extractor of CK_CLASSID. The given file should use a
* series "#define" syntax to describe enum, and use Tab as the indent before
* each "#define" syntax to indicate its hierarchy.
*
* @param filename The file name relative to input directory for reading.
* @return An {@linkplain EnumsHelper.BEnum} instance. Actually it is an
* instance to {@linkplain EnumsHelper.BEnum} whose entries is
* {@linkplain EnumsHelper.BHierarchyEnumEntry}, the child class of
* {@linkplain EnumsHelper.BEnumEntry} (the entry type of common
* {@linkplain EnumsHelper.BEnum}) with extra hierarchy infos.
* @throws Exception
*/
private static EnumsHelper.BEnum organiseClassid(String filename) throws Exception {
String infile = CommonHelper.getInputFilePath(filename);
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CKDefinesParser parser = new CKDefinesParser(tokens);
ParseTree tree = parser.prog();
ParseTreeWalker walker = new ParseTreeWalker();
ClassidWalker worker = new ClassidWalker(tokens);
walker.walk(worker, tree);
pair.mUnderlyingStream.close();
EnumsHelper.BEnum result = worker.getEnum();
result.mEnumName = "CK_CLASSID";
return result;
}
public static void main(String[] args) throws Exception {
// =========== CKERROR ===========
EnumsHelper.BEnum ckerror = organiseDefines("CKERROR.txt", "CKERROR");
JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CKERROR.json"), ckerror);
// CppWriter.writeEnum("dest/CKERROR.hpp", ckerror);
// PythonWriter.writeEnum("dest/CKERROR.py", ckerror);
// CSharpWriter.writeEnum("dest/CKERROR.cs", ckerror);
// CppWriter.writeCkErrorAccVal("dest/CKERROR.AccVal.hpp", ckerror);
// PythonWriter.writeAccVal("dest/CKERROR.AccVal.py", ckerror);
// CSharpWriter.writeAccVal("dest/CKERROR.AccVal.cs", ckerror);
// =========== CK_CLASSID ===========
EnumsHelper.BEnum classid = organiseClassid("CK_CLASSID.txt");
JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_CLASSID.json"), classid);
// CppWriter.writeEnum("dest/CK_CLASSID.hpp", classid);
// PythonWriter.writeEnum("dest/CK_CLASSID.py", classid);
// CSharpWriter.writeEnum("dest/CK_CLASSID.cs", classid);
// CppWriter.writeCkClassidAccVal("dest/CK_CLASSID.AccVal.hpp", classid);
// PythonWriter.writeAccVal("dest/CK_CLASSID.AccVal.py", classid);
// =========== Define2 ===========
// Define2 do not need annotation output.
// Because they are CKStateChunk used value which are not exposed to outside.
EnumsHelper.BEnumCollection def2 = getEnumsCollection("Defines2.txt");
JsonWriter.writeEnums(CommonHelper.getOutputFilePath("Defines2.json"), def2);
// CppWriter.writeEnums("dest/Defines2.hpp", def2);
// PythonWriter.writeEnums("dest/Defines2.py", def2);
// CSharpWriter.writeEnums("dest/Defines2.cs", def2);
// =========== Combined enums ===========
EnumsHelper.BEnumCollection ck2Enums = getEnumsCollection("CKEnums.txt"),
vxEnums = getEnumsCollection("VxEnums.txt");
JsonWriter.writeEnums(CommonHelper.getOutputFilePath("CKEnums.json"), ck2Enums);
JsonWriter.writeEnums(CommonHelper.getOutputFilePath("VxEnums.json"), vxEnums);
// CppWriter.writeEnums("dest/CKEnums.hpp", ck2Enums);
// PythonWriter.writeEnums("dest/CKEnums.py", ck2Enums);
// CSharpWriter.writeEnums("dest/CKEnums.cs", ck2Enums);
// CppWriter.writeAccVals("dest/CKEnums.AccVal.hpp", ck2Enums, CommonHelper.CKParts.CK2);
// PythonWriter.writeAccVals("dest/CKEnums.AccVal.py", ck2Enums);
// CSharpWriter.writeAccVals("dest/CKEnums.AccVal.cs", ck2Enums);
// CppWriter.writeEnums("dest/VxEnums.hpp", vxEnums);
// PythonWriter.writeEnums("dest/VxEnums.py", vxEnums);
// CSharpWriter.writeEnums("dest/VxEnums.cs", vxEnums);
// CppWriter.writeAccVals("dest/VxEnums.AccVal.hpp", vxEnums, CommonHelper.CKParts.VxMath);
// PythonWriter.writeAccVals("dest/VxEnums.AccVal.py", vxEnums);
// CSharpWriter.writeAccVals("dest/VxEnums.AccVal.cs", vxEnums);
// =========== Single enums ===========
EnumsHelper.BEnum single;
single = organiseDefines("CK_STATECHUNK_CHUNKVERSION.txt", "CK_STATECHUNK_CHUNKVERSION");
JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_STATECHUNK_CHUNKVERSION.json"), single);
// CppWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.hpp", single);
// PythonWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.py", single);
// CSharpWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.cs", single);
// CppWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
// PythonWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.py", single);
// CSharpWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.cs", single);
single = organiseDefines("CK_STATECHUNK_DATAVERSION.txt", "CK_STATECHUNK_DATAVERSION");
JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_STATECHUNK_DATAVERSION.json"), single);
// CppWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.hpp", single);
// PythonWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.py", single);
// CSharpWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.cs", single);
// CppWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
// PythonWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.py", single);
// CSharpWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.cs", single);
single = organiseDefines("CK_BITMAPDATA_FLAGS.txt", "CK_BITMAPDATA_FLAGS");
JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_BITMAPDATA_FLAGS.json"), single);
// CppWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.hpp", single);
// PythonWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.py", single);
// CSharpWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.cs", single);
// CppWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.hpp", single, CommonHelper.CKParts.CK2);
// PythonWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.py", single);
// CSharpWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.cs", single);
single = organiseDefines("CK_CAMERA_PROJECTION.txt", "CK_CAMERA_PROJECTION");
JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_CAMERA_PROJECTION.json"), single);
// CppWriter.writeEnum("dest/CK_CAMERA_PROJECTION.hpp", single);
// PythonWriter.writeEnum("dest/CK_CAMERA_PROJECTION.py", single);
// CSharpWriter.writeEnum("dest/CK_CAMERA_PROJECTION.cs", single);
// CppWriter.writeAccVal("dest/CK_CAMERA_PROJECTION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
// PythonWriter.writeAccVal("dest/CK_CAMERA_PROJECTION.AccVal.py", single);
// CSharpWriter.writeAccVal("dest/CK_CAMERA_PROJECTION.AccVal.cs", single);
// print message.
System.out.println("Done");
}
}

View File

@@ -12,32 +12,32 @@ public class PythonWriter {
* Internal real enum declaration writer.
*
* @param writer {@linkplain java.io.OutputStreamWriter} instance for writing.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for writing.
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing.
* @throws Exception
*/
private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog)
private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog)
throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
// write enum start
indent.printf("class %s(enum.IntEnum):", enum_t.mEnumName);
indent.printf("class %s(enum.IntEnum):", benum.mEnumName);
indent.inc();
// write enum comment
indent.briefComment(enum_t.mEnumComment);
indent.briefComment(benum.mEnumComment);
// write enum entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
// write entry self
if (enumEntry_t.mEntryValue == null) {
indent.printf("%s = auto()", enumEntry_t.mEntryName);
if (enumEntry.mEntryValue == null) {
indent.printf("%s = auto()", enumEntry.mEntryName);
} else {
indent.printf("%s = %s", enumEntry_t.mEntryName,
CommonHelper.convertToPythonNumber(enumEntry_t.mEntryValue));
indent.printf("%s = %s", enumEntry.mEntryName,
CommonHelper.convertToPythonNumber(enumEntry.mEntryValue));
}
// write entry comment after member
indent.afterMemberComment(enumEntry_t.mEntryComment);
indent.afterMemberComment(enumEntry.mEntryComment);
}
// enum tail
@@ -51,11 +51,11 @@ public class PythonWriter {
* Actually this is a wrapper of internal enum declaration collection writer.
*
* @param filename The name of written file.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for
* writing.
* @throws Exception
*/
public static void writeEnums(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
public static void writeEnums(String filename, EnumsHelper.BEnumCollection prog) throws Exception {
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
internalWriteEnums(fs, prog);
@@ -68,12 +68,12 @@ public class PythonWriter {
* Actually this is a wrapper of internal enum declaration collection writer.
*
* @param filename The name of written file.
* @param _enum {@linkplain EnumsHelper.Enum_t} instance for writing.
* @param _enum {@linkplain EnumsHelper.BEnum} instance for writing.
* @throws Exception
*/
public static void writeEnum(String filename, EnumsHelper.Enum_t _enum) throws Exception {
public static void writeEnum(String filename, EnumsHelper.BEnum _enum) throws Exception {
// create collection from single enum
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection();
col.mEnums.add(_enum);
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
@@ -114,28 +114,28 @@ public class PythonWriter {
* Internal real enum accessible value writer.
*
* @param writer {@linkplain java.io.OutputStreamWriter} instance for writing.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for writing.
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing.
* @throws Exception
*/
private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.EnumCollection_t prog)
private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog)
throws Exception {
IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python);
// write implements
for (EnumsHelper.Enum_t enum_t : prog.mEnums) {
for (EnumsHelper.BEnum benum : prog.mEnums) {
// write enum desc header
indent.printf("g_Annotation_%s: dict[int, EnumAnnotation] = {", enum_t.mEnumName);
indent.printf("g_Annotation_%s: dict[int, EnumAnnotation] = {", benum.mEnumName);
indent.inc();
// write enum desc entries
for (EnumsHelper.EnumEntry_t enumEntry_t : enum_t.mEntries) {
for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) {
String comment = "";
if (enumEntry_t.mEntryComment != null) {
comment = CommonHelper.escapeString(enumEntry_t.mEntryComment);
if (enumEntry.mEntryComment != null) {
comment = CommonHelper.escapeString(enumEntry.mEntryComment);
}
indent.printf("%s.%s.value: EnumAnnotation(\"%s\", \"%s\"),", enum_t.mEnumName, enumEntry_t.mEntryName,
extractHumanReadableEntryName(enumEntry_t.mEntryName), comment);
indent.printf("%s.%s.value: EnumAnnotation(\"%s\", \"%s\"),", benum.mEnumName, enumEntry.mEntryName,
extractHumanReadableEntryName(enumEntry.mEntryName), comment);
}
// write enum tail
@@ -151,11 +151,11 @@ public class PythonWriter {
* writer.
*
* @param filename The name of written file.
* @param prog {@linkplain EnumsHelper.EnumCollection_t} instance for
* @param prog {@linkplain EnumsHelper.BEnumCollection} instance for
* writing.
* @throws Exception
*/
public static void writeAccVals(String filename, EnumsHelper.EnumCollection_t prog) throws Exception {
public static void writeAccVals(String filename, EnumsHelper.BEnumCollection prog) throws Exception {
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);
internalWriteAccVals(fs, prog);
@@ -169,12 +169,12 @@ public class PythonWriter {
* writer.
*
* @param filename The name of written file.
* @param _enum {@linkplain EnumsHelper.Enum_t} instance for writing.
* @param _enum {@linkplain EnumsHelper.BEnum} instance for writing.
* @throws Exception
*/
public static void writeAccVal(String filename, EnumsHelper.Enum_t _enum) throws Exception {
public static void writeAccVal(String filename, EnumsHelper.BEnum _enum) throws Exception {
// create a collection with single enum.
EnumsHelper.EnumCollection_t col = new EnumsHelper.EnumCollection_t();
EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection();
col.mEnums.add(_enum);
// open file and write
OutputStreamWriter fs = CommonHelper.openOutputFile(filename);

View File

@@ -1,62 +0,0 @@
import java.util.Vector;
public class EnumsHelper {
/**
* The struct to describe the entry of an enum.
*/
public static class EnumEntry_t {
public EnumEntry_t() {
mEntryName = null;
mEntryValue = null;
mEntryComment = null;
}
public String mEntryName; ///< The name of this entry. Can not be null.
public String mEntryValue; ///< The value of this entry. null if this entry do not have explicit value.
public String mEntryComment; ///< The comment of this entry. null if no comment.
}
/**
* The specialized EnumEntry type which can store extra hierarchy info.
* Used in CK_CLASSID parsing.
*/
public static class EnumEntryWithHierarchy_t extends EnumEntry_t {
public EnumEntryWithHierarchy_t() {
super();
mHierarchy = new Vector<EnumEntryWithHierarchy_t>();
}
public Vector<EnumEntryWithHierarchy_t> mHierarchy; ///< The list to store this CK_CLASSID inheritance relationship.
}
/**
* The struct to describe an enum.
*/
public static class Enum_t {
public Enum_t() {
mEnumName = null;
mEnumComment = null;
mCanUnsigned = true;
mUseFlags = false;
mEntries = new Vector<EnumEntry_t>();
}
public String mEnumName; ///< The name of this enum. Can not be null.
public String mEnumComment; ///< The comment of this enum. null if no comment.
public boolean mCanUnsigned; ///< true if this enum can use unsigned integer as its underlaying type.
public boolean mUseFlags; ///< true if this enum will have flags feature (supporting OR, AND, operators).
public Vector<EnumEntry_t> mEntries; ///< The list to store entries of this enum.
}
/**
* The struct to describe a collection of enums.
*/
public static class EnumCollection_t {
public EnumCollection_t() {
mEnums = new Vector<Enum_t>();
}
public Vector<Enum_t> mEnums; ///< The list to store enums.
}
}

View File

@@ -0,0 +1 @@
3.11

View File

@@ -0,0 +1,6 @@
def main():
print("Hello from enums-render!")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,7 @@
[project]
name = "enums-render"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = []

View File

@@ -1,184 +0,0 @@
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class MainRunner {
/**
* Extract an enums collection from given file.
* <p>
* This function is the most commonly used function for extracting enums.
* <p>
* This function is used for a file which only contain enum declarations. This
* is not suit for extracting CKERROR and CK_CLASSID. For these declarations,
* please use their specialized extractor as described following.
*
* @param infile The file for reading.
* @return An {@linkplain EnumsHelper.EnumCollection_t} instance.
* @throws Exception
*/
private static EnumsHelper.EnumCollection_t getEnumsCollection(String infile) throws Exception {
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CKEnumsParser parser = new CKEnumsParser(tokens);
ParseTree tree = parser.prog();
ParseTreeWalker walker = new ParseTreeWalker();
EnumsWalker worker = new EnumsWalker(tokens);
walker.walk(worker, tree);
pair.mUnderlyingStream.close();
return worker.getEnums();
}
/**
* Extract a series of "#define" syntax as an enum.
* <p>
* This function will assume that given file only contain C++ "#define" syntax.
* After reading it, it will re-organize it as an enum and return. This only is
* used by CKERROR now. But it suit for more scenarios if there are something
* like CKERROR in future.
*
* @param infile The file for reading.
* @param assignedEnumName The desired name of organized enum instance.
* Contemporary this field should always be "CKERROR"
* because no one else is using it.
* @return An {@linkplain EnumsHelper.Enum_t} instance.
* @throws Exception
*/
private static EnumsHelper.Enum_t organiseDefines(String infile, String assignedEnumName) throws Exception {
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CKDefinesParser parser = new CKDefinesParser(tokens);
ParseTree tree = parser.prog();
ParseTreeWalker walker = new ParseTreeWalker();
DefinesWalker worker = new DefinesWalker(tokens);
walker.walk(worker, tree);
pair.mUnderlyingStream.close();
EnumsHelper.Enum_t result = worker.getEnum();
result.mEnumName = assignedEnumName;
return result;
}
/**
* Extract a series of macro define as an enum, considering its indent to build
* hierarchy.
* <p>
* This is specialized enum extractor of CK_CLASSID. The given file should use a
* series "#define" syntax to describe enum, and use Tab as the indent before
* each "#define" syntax to indicate its hierarchy.
*
* @param infile The file for reading.
* @return An {@linkplain EnumsHelper.Enum_t} instance. Actually it is an
* instance to {@linkplain EnumsHelper.Enum_t} whose entries is
* {@linkplain EnumsHelper.EnumEntryWithHierarchy_t}, the child class of
* {@linkplain EnumsHelper.EnumEntry_t} (the entry type of common
* {@linkplain EnumsHelper.Enum_t}) with extra hierarchy infos.
* @throws Exception
*/
private static EnumsHelper.Enum_t organiseClassid(String infile) throws Exception {
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CKDefinesParser parser = new CKDefinesParser(tokens);
ParseTree tree = parser.prog();
ParseTreeWalker walker = new ParseTreeWalker();
ClassidWalker worker = new ClassidWalker(tokens);
walker.walk(worker, tree);
pair.mUnderlyingStream.close();
EnumsHelper.Enum_t result = worker.getEnum();
result.mEnumName = "CK_CLASSID";
return result;
}
public static void main(String[] args) throws Exception {
// =========== CKERROR ===========
EnumsHelper.Enum_t ckerror = organiseDefines("src/CKERROR.txt", "CKERROR");
CppWriter.writeEnum("dest/CKERROR.hpp", ckerror);
PythonWriter.writeEnum("dest/CKERROR.py", ckerror);
CSharpWriter.writeEnum("dest/CKERROR.cs", ckerror);
CppWriter.writeCkErrorAccVal("dest/CKERROR.AccVal.hpp", ckerror);
PythonWriter.writeAccVal("dest/CKERROR.AccVal.py", ckerror);
CSharpWriter.writeAccVal("dest/CKERROR.AccVal.cs", ckerror);
// =========== CK_CLASSID ===========
EnumsHelper.Enum_t classid = organiseClassid("src/CK_CLASSID.txt");
CppWriter.writeEnum("dest/CK_CLASSID.hpp", classid);
PythonWriter.writeEnum("dest/CK_CLASSID.py", classid);
CSharpWriter.writeEnum("dest/CK_CLASSID.cs", classid);
CppWriter.writeCkClassidAccVal("dest/CK_CLASSID.AccVal.hpp", classid);
PythonWriter.writeAccVal("dest/CK_CLASSID.AccVal.py", classid);
// =========== Define2 ===========
// Define2 do not need annotation output.
// Because they are CKStateChunk used value which are not exposed to outside.
EnumsHelper.EnumCollection_t def2 = getEnumsCollection("src/Defines2.txt");
CppWriter.writeEnums("dest/Defines2.hpp", def2);
PythonWriter.writeEnums("dest/Defines2.py", def2);
CSharpWriter.writeEnums("dest/Defines2.cs", def2);
// =========== Combined enums ===========
EnumsHelper.EnumCollection_t ck2Enums = getEnumsCollection("src/CKEnums.txt"),
vxEnums = getEnumsCollection("src/VxEnums.txt");
CppWriter.writeEnums("dest/CKEnums.hpp", ck2Enums);
PythonWriter.writeEnums("dest/CKEnums.py", ck2Enums);
CSharpWriter.writeEnums("dest/CKEnums.cs", ck2Enums);
CppWriter.writeAccVals("dest/CKEnums.AccVal.hpp", ck2Enums, CommonHelper.CKParts.CK2);
PythonWriter.writeAccVals("dest/CKEnums.AccVal.py", ck2Enums);
CSharpWriter.writeAccVals("dest/CKEnums.AccVal.cs", ck2Enums);
CppWriter.writeEnums("dest/VxEnums.hpp", vxEnums);
PythonWriter.writeEnums("dest/VxEnums.py", vxEnums);
CSharpWriter.writeEnums("dest/VxEnums.cs", vxEnums);
CppWriter.writeAccVals("dest/VxEnums.AccVal.hpp", vxEnums, CommonHelper.CKParts.VxMath);
PythonWriter.writeAccVals("dest/VxEnums.AccVal.py", vxEnums);
CSharpWriter.writeAccVals("dest/VxEnums.AccVal.cs", vxEnums);
// =========== Single enums ===========
EnumsHelper.Enum_t single;
single = organiseDefines("src/CK_STATECHUNK_CHUNKVERSION.txt", "CK_STATECHUNK_CHUNKVERSION");
CppWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.hpp", single);
PythonWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.py", single);
CSharpWriter.writeEnum("dest/CK_STATECHUNK_CHUNKVERSION.cs", single);
CppWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
PythonWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.py", single);
CSharpWriter.writeAccVal("dest/CK_STATECHUNK_CHUNKVERSION.AccVal.cs", single);
single = organiseDefines("src/CK_STATECHUNK_DATAVERSION.txt", "CK_STATECHUNK_DATAVERSION");
CppWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.hpp", single);
PythonWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.py", single);
CSharpWriter.writeEnum("dest/CK_STATECHUNK_DATAVERSION.cs", single);
CppWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
PythonWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.py", single);
CSharpWriter.writeAccVal("dest/CK_STATECHUNK_DATAVERSION.AccVal.cs", single);
single = organiseDefines("src/CK_BITMAPDATA_FLAGS.txt", "CK_BITMAPDATA_FLAGS");
CppWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.hpp", single);
PythonWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.py", single);
CSharpWriter.writeEnum("dest/CK_BITMAPDATA_FLAGS.cs", single);
CppWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.hpp", single, CommonHelper.CKParts.CK2);
PythonWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.py", single);
CSharpWriter.writeAccVal("dest/CK_BITMAPDATA_FLAGS.AccVal.cs", single);
single = organiseDefines("src/CK_CAMERA_PROJECTION.txt", "CK_CAMERA_PROJECTION");
CppWriter.writeEnum("dest/CK_CAMERA_PROJECTION.hpp", single);
PythonWriter.writeEnum("dest/CK_CAMERA_PROJECTION.py", single);
CSharpWriter.writeEnum("dest/CK_CAMERA_PROJECTION.cs", single);
CppWriter.writeAccVal("dest/CK_CAMERA_PROJECTION.AccVal.hpp", single, CommonHelper.CKParts.CK2);
PythonWriter.writeAccVal("dest/CK_CAMERA_PROJECTION.AccVal.py", single);
CSharpWriter.writeAccVal("dest/CK_CAMERA_PROJECTION.AccVal.cs", single);
// print message.
System.out.println("DONE!");
}
}

View File

@@ -2,15 +2,53 @@
A helper program to migrate existing Virtools enum declarations into other formats.
Original Virtools SDK have various enum declarations. All of them are defined as C format and their formation are not uniform. This sub-project will use laxer and parser to recognize these diverse declarations, extract them as a series of uniform Java data struct and output them as C++ code (as C++ enum class syntax for LibCmo using), Python code (for PyBMap using), and C# code (for BMapSharp using).
Original Virtools SDK have various enum declarations. All of them are defined as C format and their formation are not uniform. This sub-project will use laxer and parser to recognize these diverse declarations, extract them as a series of uniform Java data struct and output them as C++ code (as C++ enum class syntax for LibCmo using), Python code (for PyBMap using), C# code (for BMapSharp using) and Rust code (for rusty-bmap using).
The steps processing existing enum declaration is called migration as this sub-project name told.
## Usage
This project is consisting of two parts: Enums Analyzer and Enums Render
### Setup Environment
First we stay at the root directory of this project (this README file located).
And execute `set ENUMS_MIGRATION_ROOT=$(pwd)` on POSIX-like OS, or `set ENUMS_MIGRATION_ROOT=%CD%` on Windows, to set environment variable.
This environment variable will be used by Enums Analyzer and Enums Render later.
### Enums Analyzer
We should first run Eunms Analyzer to recognize existing enum declarations.
And export them as JSON file which will be utilized by Enums Render.
#### Build
Enter `EnumsAnalyzer` directory, and execute following command to generate Antlr lexer and parser:
```
antlr4 CKGeneralLexer.g4
antlr4 CKEnumsParser.g4
antlr4 CKDefinesParser.g4
```
Keep staying that directory, and execute following command to build Java code.
```
javac *.java
```
#### Run
Keep staying this directory, and execute following command to run program.
```
java MainRunner
```
After running, program will process input file located in `Input`directory, and output JSON file to `Intermediate` directory.
### Enums Render
* Enter `EnumsRender` directory and setup it witl Astral UV.
* Execute `uv run main.py` to run program.
* Program will process JSON file located in `Intermediate` directory, and output final artifacts to `Output` directory.

View File

@@ -2,7 +2,7 @@
This article tells the details of this project for the developer of this project.
## Java and Antlr
## Java
### Java
@@ -20,7 +20,7 @@ The only restriction is that you should not break this premise: use primitive wa
After understanding this premise, you now can configure Java.
The JDK I used is OpenJDK 21.
It would be okey for you to use any JDK you like, but obviouslly JDK 1.8 is NOT recommended.
It would be okey for you to use any JDK you like, but obviouslly JDK 8 is NOT recommended.
### Antlr
@@ -33,6 +33,12 @@ So that you can correctly execute commands provided in each projects.
The Antlr I used is Antlr 4.13.0.
It would be okey for you to use any Antlr you like.
### Gson
Some Java code use Google Gson as its dependency.
The Gson versio I used is Gson 2.10.1.
It would be okey for you to use any Antlr you like.
## Python
For most Python code written in this project, we use Astral UV to manage them.