enum_to_string: support for enums with values and defines

This commit is contained in:
andreika-git 2023-09-27 18:26:59 +03:00 committed by rusefillc
parent 06b524157b
commit 3e3dcb7f97
4 changed files with 39 additions and 13 deletions

View File

@ -85,7 +85,7 @@ public class EnumToString {
commonFilesHeader.insert(0, "// " + LazyFile.LAZY_FILE_TAG + " from " + f.getName() + " ");
includesSection.append("#include \"" + f.getName() + "\"\n");
enumsReader.read(new FileReader(f));
enumsReader.read(new FileReader(f), new VariableRegistry(), false);
}
public void outputData(EnumsReader enumsReader) {

View File

@ -54,11 +54,19 @@ public class EnumsReader {
}
public EnumsReader read(Reader in) throws IOException {
enums.putAll(readStatic(in));
return read(in, new VariableRegistry(), false);
}
public EnumsReader read(Reader in, VariableRegistry registry, boolean enumWithValues) throws IOException {
enums.putAll(readStatic(in, registry, enumWithValues));
return this;
}
public static Map<String, EnumState> readStatic(Reader in) throws IOException {
return readStatic(in, new VariableRegistry(), false);
}
public static Map<String, EnumState> readStatic(Reader in, VariableRegistry registry, boolean enumWithValues) throws IOException {
boolean isInsideEnum = false;
BufferedReader reader = new BufferedReader(in);
String line;
@ -66,6 +74,7 @@ public class EnumsReader {
boolean isEnumClass = false;
Map<String, Value> currentValues = new TreeMap<>();
Map<String, EnumState> enums = new TreeMap<>();
int lastNumericValue = -1;
boolean withAutoValue = false;
@ -99,7 +108,7 @@ public class EnumsReader {
if (log.debugEnabled())
log.debug(" EnumsReader: Ending enum " + enumName + " found " + currentValues.size() + " values");
if (withAutoValue)
validateValues(currentValues);
validateValues(currentValues, registry, enumWithValues);
enums.put(enumName, new EnumState(currentValues, enumName, isEnumClass));
} else {
@ -112,12 +121,14 @@ public class EnumsReader {
value = line.substring(index + 1);
line = line.substring(0, index);
} else {
value = Integer.toString(currentValues.size());
value = Integer.toString(lastNumericValue + 1);
withAutoValue = true;
}
if (log.debugEnabled())
log.debug(" EnumsReader: Line " + line);
currentValues.put(line, new Value(line, value));
Value newValue = new Value(line, value);
lastNumericValue = newValue.getIntValueMaybeResolve(registry);
currentValues.put(line, newValue);
} else {
if (log.debugEnabled())
log.debug(" EnumsReader: Skipping Line " + line);
@ -128,10 +139,10 @@ public class EnumsReader {
return enums;
}
private static void validateValues(Map<String, Value> currentValues) {
private static void validateValues(Map<String, Value> currentValues, VariableRegistry registry, boolean enumWithValues) {
for (Map.Entry<String, Value> entry : currentValues.entrySet()) {
int v = entry.getValue().getIntValue();
if (v < 0 || v >= currentValues.size())
int v = entry.getValue().getIntValueMaybeResolve(registry);
if (v < 0 || (v >= currentValues.size() && !enumWithValues))
throw new IllegalStateException("Unexpected " + entry);
}
}

View File

@ -15,12 +15,17 @@ import java.util.Map;
* produces java enum class from an enum defined in a C/C++ header
*/
public class ToJavaEnum {
static private boolean enumWithValues = false;
public static void main(String[] args) throws IOException {
InvokeReader invokeReader = new InvokeReader(args).invoke();
String outputPath = invokeReader.getOutputPath();
EnumsReader enumsReader = new EnumsReader();
enumWithValues = "true".equalsIgnoreCase(System.getProperty("enum_with_values"));
System.out.println("enumWithValues=" + enumWithValues);
VariableRegistry registry = new VariableRegistry();
for (String fileName : invokeReader.getDefinitionInputFiles())
registry.readPrependValues(fileName);
@ -32,7 +37,7 @@ public class ToJavaEnum {
SystemOut.println("Reading enums from " + f);
sb.append("// based on ").append(f).append("\n");
enumsReader.read(new FileReader(f));
enumsReader.read(new FileReader(f), registry, enumWithValues);
}
for (Map.Entry<String /*enum name*/, EnumsReader.EnumState> e : enumsReader.getEnums().entrySet()) {
@ -60,12 +65,22 @@ public class ToJavaEnum {
int index = 0;
for (Value value : sorted) {
int numericValue = value.getIntValueMaybeResolve(registry);
if (index != numericValue)
throw new IllegalStateException("Got explicit ordinal " + numericValue + " instead of ordinal " + index + " in " + value);
sb.append("\t" + value.getName() + ",\n");
if (enumWithValues) {
sb.append("\t" + value.getName() + "(" + numericValue + "),\n");
} else {
if (index != numericValue)
throw new IllegalStateException("Got explicit ordinal " + numericValue + " instead of ordinal " + index + " in " + value);
sb.append("\t" + value.getName() + ",\n");
}
index++;
}
if (enumWithValues) {
sb.append("\t;\n\n\n\tprivate int value;\n\n");
sb.append("\t" + key + "(int v) {\n\t\tvalue = v;\n\t}\n\n");
sb.append("\tpublic int get() {\n\t\treturn value;\n\t}\n");
}
sb.append("}\n");
return sb.toString();
}

View File

@ -56,6 +56,6 @@ public class Value implements Comparable<Value> {
private int handleNotInteger(VariableRegistry registry) {
String resolvedValue = registry.get(value);
Objects.requireNonNull(resolvedValue, value);
return Integer.parseInt(resolvedValue);
return Value.parseInt(resolvedValue);
}
}