TS parser improvements, compatibility (#3479)

* scalar layout

* wrong name in comments

* better compatibility for tooltips starting with + or not

* s

* sort of handle signature

* only strip nested

* new parse can mostly read signature

* oops

* jar

* jar
This commit is contained in:
Matthew Kennedy 2021-11-06 15:17:03 -07:00 committed by GitHub
parent 6a9c38f934
commit a3c569e5f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 21 deletions

Binary file not shown.

View File

@ -230,7 +230,9 @@ public class ConfigDefinition {
SystemOut.println(state.enumsReader.getEnums() + " total enumsReader");
}
long crc32 = signatureHash(state, tsPath, inputAllFiles);
ParseState parseState = new ParseState(state.enumsReader);
// Add the variable for the config signature
long crc32 = signatureHash(state, parseState, tsPath, inputAllFiles);
handleFiringOrder(firingEnumFileName, state.variableRegistry);
@ -247,10 +249,8 @@ public class ConfigDefinition {
// Parse the input files
{
ParseState parseState = new ParseState(state.enumsReader);
// First process yaml files
//processYamls(listener, yamlFiles);
//processYamls(parseState, yamlFiles);
// Process firing order enum
handleFiringOrder(firingEnumFileName, parseState);
@ -262,7 +262,7 @@ public class ConfigDefinition {
//for (String prependFile : prependFiles) {
// TODO: fix signature define file parsing
//parseFile(listener, prependFile);
//parseFile(parseState, prependFile);
//}
}
@ -275,12 +275,11 @@ public class ConfigDefinition {
// Write C structs
// CStructWriter cStructs = new CStructWriter();
// cStructs.writeCStructs(listener, destCHeaderFileName + ".test");
// cStructs.writeCStructs(parseState, destCHeaderFileName + ".test");
// Write tunerstudio layout
// TsWriter writer = new TsWriter();
// writer.writeTunerstudio(listener, "TODO", tsPath + "/test.ini");
// writer.writeTunerstudio(parseState, tsPath + "/rusefi.input", tsPath + "/" + TSProjectConsumer.TS_FILE_OUTPUT_NAME);
}
BufferedReader definitionReader = new BufferedReader(new InputStreamReader(new FileInputStream(definitionInputFile), IoUtils.CHARSET.name()));
@ -346,7 +345,7 @@ public class ConfigDefinition {
}
}
private static long signatureHash(ReaderState state, String tsPath, List<String> inputAllFiles) throws IOException {
private static long signatureHash(ReaderState state, ParseState parseState, String tsPath, List<String> inputAllFiles) throws IOException {
// get CRC32 of given input files
long crc32 = 0;
for (String iFile : inputAllFiles) {
@ -356,8 +355,13 @@ public class ConfigDefinition {
}
SystemOut.println("CRC32 from all input files = " + crc32);
// store the CRC32 as a built-in variable
if (tsPath != null) // nasty trick - do not insert signature into live data files
// nasty trick - do not insert signature into live data files
if (tsPath != null) {
state.variableRegistry.register(SIGNATURE_HASH, "" + crc32);
parseState.addDefinition(SIGNATURE_HASH, Long.toString(crc32), Definition.OverwritePolicy.NotAllowed);
}
return crc32;
}

View File

@ -212,7 +212,7 @@ public class ConfigField {
public String getCommentContent() {
if (comment == null || comment.isEmpty())
return comment;
return comment.charAt(0) == TS_COMMENT_TAG ? comment.substring(1) : comment;
return (comment.charAt(0) == TS_COMMENT_TAG ? comment.substring(1) : comment).trim();
}
public int[] getArraySizes() {

View File

@ -85,6 +85,28 @@ public class ScalarLayout extends Layout {
printAfterArrayLength(ps);
}
private String makeScaleString() {
float scale = this.options.scale;
int mul, div;
if (scale < 1) {
mul = Math.round(1 / scale);
div = 1;
} else {
mul = 1;
div = Math.round(scale);
}
float actualScale = (float)mul / div;
if (mul < 1 || div < 1 || (Math.abs(scale - actualScale) < 0.0001f)) {
throw new RuntimeException("assertion failure: scale string generation failure for " + this.name);
}
return mul + ", " + div;
}
@Override
public void writeCLayout(PrintStream ps) {
this.writeCOffsetHeader(ps, this.options.comment, this.options.units);
@ -92,7 +114,7 @@ public class ScalarLayout extends Layout {
String cTypeName = this.type.cType.replaceAll("^int32_t$", "int");
if (this.autoscale) {
cTypeName = "scaled_channel<" + cTypeName + ", " + Math.round(1 / this.options.scale) + ">";
cTypeName = "scaled_channel<" + cTypeName + ", " + makeScaleString() + ">";
}
ps.print("\t" + cTypeName + " " + this.name);
@ -120,7 +142,7 @@ public class ScalarLayout extends Layout {
String cTypeName = this.type.cType.replaceAll("^int32_t$", "int");
if (this.autoscale) {
cTypeName = "scaled_channel<" + cTypeName + ", " + Math.round(1 / this.options.scale) + ">";
cTypeName = "scaled_channel<" + cTypeName + ", " + makeScaleString() + ">";
}
ps.println("\t" + cTypeName + " " + this.name + "[" + al + "];");

View File

@ -12,12 +12,16 @@ public class TsMetadata {
}
comment = comment.trim();
if (!comment.startsWith("+")) {
return;
// LEGACY FEATURE: clips off the previously-required +
if (comment.startsWith("+")) {
// Clip off leading +, and any leading/trailing whitespace
comment = comment.substring(1).trim();
}
// Clip off leading +, and any leading/trailing whitespace
comment = comment.substring(1).trim();
if (comment.length() == 0) {
return;
}
comments.add("\t" + name + " = \"" + comment + "\"");
}

View File

@ -18,9 +18,6 @@ public class TsWriter {
BufferedReader is = new BufferedReader(new FileReader(inputFile));
PrintStream ps = new PrintStream(new FileOutputStream(outputFile));
// TODO: handle signature
parser.addDefinition("TS_SIGNATURE", "sig", Definition.OverwritePolicy.NotAllowed);
while (is.ready()) {
String line = is.readLine();
@ -45,6 +42,9 @@ public class TsWriter {
line = line.replace(match.group(0), "");
}
// Don't strip surrounding quotes of the FIRST replace of the line - only do it in nested replacements
boolean isNested = false;
// While there is a line to replace, do it
while (line.contains("@@")) {
match = VAR.matcher(line);
@ -55,7 +55,19 @@ public class TsWriter {
String varName = match.group(1);
Definition def = parser.findDefinition(varName);
line = line.replaceAll(match.group(0), def != null ? def.toString() : "MISSING DEFINITION");
String replacement = def != null ? def.toString() : "MISSING DEFINITION";
// Strip off any quotes from the resolved string - we may be trying to concatenate inside a string literal where quotes aren't allowed
while (isNested && replacement.startsWith("\"") && replacement.endsWith("\"")) {
replacement = replacement.substring(1, replacement.length() - 1);
}
line = line.replaceAll(match.group(0), replacement);
if (!isNested) {
isNested = true;
}
}
// TODO: remove extra whitespace from the line