refactoring

This commit is contained in:
rusefi 2018-12-19 23:35:45 -05:00
parent a9752e7236
commit bcc51ad68b
3 changed files with 37 additions and 23 deletions

Binary file not shown.

View File

@ -4,7 +4,6 @@ import java.io.*;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Stack;
/** /**
* (c) Andrey Belomutskiy * (c) Andrey Belomutskiy
@ -31,7 +30,6 @@ public class ConfigDefinition {
private static final String FIELDS_JAVA = "models/src/com/rusefi/config/Fields.java"; private static final String FIELDS_JAVA = "models/src/com/rusefi/config/Fields.java";
private static int totalTsSize; private static int totalTsSize;
private static Stack<ConfigStructure> stack = new Stack<>();
public static Map<String, ConfigStructure> structures = new HashMap<>(); public static Map<String, ConfigStructure> structures = new HashMap<>();
public static Map<String, String> tsCustomLine = new HashMap<>(); public static Map<String, String> tsCustomLine = new HashMap<>();
public static Map<String, Integer> tsCustomSize = new HashMap<>(); public static Map<String, Integer> tsCustomSize = new HashMap<>();
@ -44,11 +42,11 @@ public class ConfigDefinition {
return; return;
} }
String inputPath = args[0]; String definitionInputPath = args[0];
String tsPath = args[1]; String tsPath = args[1];
String headerDestinationFolder = args[2]; String headerDestinationFolder = args[2];
String javaConsolePath = args[3]; String javaConsolePath = args[3];
String fullFileName = inputPath + File.separator + INPUT_FILE_NAME; String fullFileName = definitionInputPath + File.separator + INPUT_FILE_NAME;
System.out.println("Reading from " + fullFileName); System.out.println("Reading from " + fullFileName);
String destCHeader = headerDestinationFolder + File.separator + ENGINE_CONFIGURATION_GENERATED_STRUCTURES_H; String destCHeader = headerDestinationFolder + File.separator + ENGINE_CONFIGURATION_GENERATED_STRUCTURES_H;
System.out.println("Writing C header to " + destCHeader); System.out.println("Writing C header to " + destCHeader);
@ -61,7 +59,8 @@ public class ConfigDefinition {
CharArrayWriter javaFieldsWriter = new CharArrayWriter(); CharArrayWriter javaFieldsWriter = new CharArrayWriter();
processFile(br, cHeader, tsWriter, javaFieldsWriter); ReaderState state = new ReaderState();
processFile(state, br, cHeader, tsWriter, javaFieldsWriter);
BufferedWriter javaFields = new BufferedWriter(new FileWriter(javaConsolePath + File.separator + FIELDS_JAVA)); BufferedWriter javaFields = new BufferedWriter(new FileWriter(javaConsolePath + File.separator + FIELDS_JAVA));
javaFields.write("package com.rusefi.config;" + EOL + EOL); javaFields.write("package com.rusefi.config;" + EOL + EOL);
@ -75,15 +74,14 @@ public class ConfigDefinition {
BufferedWriter tsHeader = writeTunerStudioFile(tsPath, tsWriter.toString()); BufferedWriter tsHeader = writeTunerStudioFile(tsPath, tsWriter.toString());
if (!stack.isEmpty()) state.ensureEmptyAfterProcessing();
throw new IllegalStateException("Unclosed structure: " + stack.peek().getName());
cHeader.close(); cHeader.close();
tsHeader.close(); tsHeader.close();
VariableRegistry.INSTANCE.writeNumericsToFile(headerDestinationFolder); VariableRegistry.INSTANCE.writeNumericsToFile(headerDestinationFolder);
String inputFileName = inputPath + File.separator + ROM_RAIDER_XML_TEMPLATE; String inputFileName = definitionInputPath + File.separator + ROM_RAIDER_XML_TEMPLATE;
String outputFileName = javaConsolePath + File.separator + ROM_RAIDER_XML_OUTPUT; String outputFileName = javaConsolePath + File.separator + ROM_RAIDER_XML_OUTPUT;
processTextTemplate(inputFileName, outputFileName); processTextTemplate(inputFileName, outputFileName);
} }
@ -158,7 +156,7 @@ public class ConfigDefinition {
return new TsFileContent(prefix.toString(), postfix.toString()); return new TsFileContent(prefix.toString(), postfix.toString());
} }
private static void processFile(BufferedReader br, BufferedWriter cHeader, Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException { private static void processFile(ReaderState state, BufferedReader br, BufferedWriter cHeader, Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException {
String line; String line;
String message = "// this section " + MESSAGE + EOL; String message = "// this section " + MESSAGE + EOL;
@ -178,11 +176,11 @@ public class ConfigDefinition {
continue; continue;
if (line.startsWith(STRUCT)) { if (line.startsWith(STRUCT)) {
handleStartStructure(line.substring(STRUCT.length()), true); handleStartStructure(state, line.substring(STRUCT.length()), true);
} else if (line.startsWith(STRUCT_NO_PREFIX)) { } else if (line.startsWith(STRUCT_NO_PREFIX)) {
handleStartStructure(line.substring(STRUCT_NO_PREFIX.length()), false); handleStartStructure(state, line.substring(STRUCT_NO_PREFIX.length()), false);
} else if (line.startsWith(END_STRUCT)) { } else if (line.startsWith(END_STRUCT)) {
handleEndStruct(cHeader, tsHeader, javaFieldsWriter); handleEndStruct(state, cHeader, tsHeader, javaFieldsWriter);
} else if (line.startsWith(BIT)) { } else if (line.startsWith(BIT)) {
line = line.substring(BIT.length() + 1).trim(); line = line.substring(BIT.length() + 1).trim();
@ -198,7 +196,7 @@ public class ConfigDefinition {
} }
ConfigField bitField = new ConfigField(bitName, comment, true, null, null, 0, null, false); ConfigField bitField = new ConfigField(bitName, comment, true, null, null, 0, null, false);
stack.peek().addBoth(bitField); state.stack.peek().addBoth(bitField);
} else if (startsWithToken(line, CUSTOM)) { } else if (startsWithToken(line, CUSTOM)) {
line = line.substring(CUSTOM.length() + 1).trim(); line = line.substring(CUSTOM.length() + 1).trim();
@ -220,7 +218,7 @@ public class ConfigDefinition {
tsCustomLine.put(name, tunerStudioLine); tsCustomLine.put(name, tunerStudioLine);
} else { } else {
processLine(line); processLine(state, line);
} }
} }
cHeader.write("#endif" + EOL); cHeader.write("#endif" + EOL);
@ -232,7 +230,7 @@ public class ConfigDefinition {
return line.startsWith(token + " ") || line.startsWith(token + "\t"); return line.startsWith(token + " ") || line.startsWith(token + "\t");
} }
private static void handleStartStructure(String line, boolean withPrefix) { private static void handleStartStructure(ReaderState state, String line, boolean withPrefix) {
String name; String name;
String comment; String comment;
if (line.contains(" ")) { if (line.contains(" ")) {
@ -244,14 +242,14 @@ public class ConfigDefinition {
comment = null; comment = null;
} }
ConfigStructure structure = new ConfigStructure(name, comment, withPrefix); ConfigStructure structure = new ConfigStructure(name, comment, withPrefix);
stack.push(structure); state.stack.push(structure);
System.out.println("Starting structure " + structure.getName()); System.out.println("Starting structure " + structure.getName());
} }
private static void handleEndStruct(Writer cHeader, Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException { private static void handleEndStruct(ReaderState state, Writer cHeader, Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException {
if (stack.isEmpty()) if (state.stack.isEmpty())
throw new IllegalStateException("Unexpected end_struct"); throw new IllegalStateException("Unexpected end_struct");
ConfigStructure structure = stack.pop(); ConfigStructure structure = state.stack.pop();
System.out.println("Ending structure " + structure.getName()); System.out.println("Ending structure " + structure.getName());
structure.addAlignmentFill(); structure.addAlignmentFill();
@ -259,7 +257,7 @@ public class ConfigDefinition {
structure.headerWrite(cHeader); structure.headerWrite(cHeader);
if (stack.isEmpty()) { if (state.stack.isEmpty()) {
totalTsSize = structure.writeTunerStudio("", tsHeader, 0); totalTsSize = structure.writeTunerStudio("", tsHeader, 0);
tsHeader.write("; total TS size = " + totalTsSize + EOL); tsHeader.write("; total TS size = " + totalTsSize + EOL);
VariableRegistry.INSTANCE.register("TOTAL_CONFIG_SIZE", totalTsSize); VariableRegistry.INSTANCE.register("TOTAL_CONFIG_SIZE", totalTsSize);
@ -268,7 +266,7 @@ public class ConfigDefinition {
} }
} }
private static void processLine(String line) throws IOException { private static void processLine(ReaderState state, String line) {
/** /**
* for example * for example
* #define CLT_CURVE_SIZE 16 * #define CLT_CURVE_SIZE 16
@ -282,9 +280,9 @@ public class ConfigDefinition {
if (cf == null) if (cf == null)
throw new IllegalStateException("Cannot parse line [" + line + "]"); throw new IllegalStateException("Cannot parse line [" + line + "]");
if (stack.isEmpty()) if (state.stack.isEmpty())
throw new IllegalStateException(cf.name + ": Not enclosed in a struct"); throw new IllegalStateException(cf.name + ": Not enclosed in a struct");
ConfigStructure structure = stack.peek(); ConfigStructure structure = state.stack.peek();
if (cf.isIterate) { if (cf.isIterate) {
structure.addC(cf); structure.addC(cf);

View File

@ -0,0 +1,16 @@
package com.rusefi;
import java.util.Stack;
/**
* (c) Andrey Belomutskiy
* 12/19/18
*/
class ReaderState {
Stack<ConfigStructure> stack = new Stack<>();
public void ensureEmptyAfterProcessing() {
if (!this.stack.isEmpty())
throw new IllegalStateException("Unclosed structure: " + this.stack.peek().getName());
}
}