support tokens with braces #6479

This commit is contained in:
kifir 2024-05-09 18:48:13 +03:00
parent 1bf463b233
commit 849297a38c
4 changed files with 47 additions and 6 deletions

View File

@ -70,7 +70,6 @@ public class ConfigFieldParserTest {
}
@Test
@Disabled
public void testArrayBitStringValue() {
String test = "struct pid_s\n" +
"int[3 x 1] afr_type;;{bitStringValue(fuelUnits, fuelAlgorithm) }, 1.0, 0, 0, 3000, 0, noMsqSave\n" +
@ -79,7 +78,7 @@ public class ConfigFieldParserTest {
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer("", state);
state.readBufferedReader(test, tsProjectConsumer);
assertEquals("afr_type = array, S32, 0, [1x3], \"ms\", 1, 0, 0, 3000, 0, noMsqSave\n" +
assertEquals("afr_type = array, S32, 0, [1x3], {bitStringValue(fuelUnits, fuelAlgorithm) }, 1, 0, 0, 3000, 0, noMsqSave\n" +
"; total TS size = 12\n", tsProjectConsumer.getContent());
}

View File

@ -12,6 +12,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.TokenUtils.tokenizeWithBraces;
import com.rusefi.parse.TypesHelper;
import org.jetbrains.annotations.Nullable;
@ -373,9 +374,7 @@ public class ConfigFieldImpl implements ConfigField {
}
private String[] getTokens() {
if (tsInfo == null)
return new String[0];
return tsInfo.split(",");
return tokenizeWithBraces(tsInfo);
}
@Override

View File

@ -0,0 +1,42 @@
package com.rusefi;
import java.util.ArrayList;
import java.util.List;
public class TokenUtils {
public static String[] tokenizeWithBraces(String stringToTokenize) {
final List<String> tokens = new ArrayList<>();
if (stringToTokenize != null) {
StringBuilder currentToken = new StringBuilder();
int openedBracesCount = 0;
for (char ch : stringToTokenize.toCharArray()) {
// we finish token parsing only if it contains no unclosed braces
if ((ch == ',') && (openedBracesCount == 0)) {
tokens.add(currentToken.toString());
currentToken.setLength(0);
} else {
currentToken.append(ch);
switch (ch) {
case '{': {
openedBracesCount++;
break;
}
case '}': {
if (0 < openedBracesCount) {
openedBracesCount--;
} else {
throw new IllegalStateException("Unexpected closing brace");
}
break;
}
}
}
}
if (openedBracesCount != 0) {
throw new IllegalStateException("Unclosed opening brace");
}
tokens.add(currentToken.toString());
}
return tokens.toArray(String[]::new);
}
}

View File

@ -13,6 +13,7 @@ import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
import static com.rusefi.TokenUtils.tokenizeWithBraces;
import static com.rusefi.ToolUtil.EOL;
import static com.rusefi.output.JavaSensorsConsumer.quote;
@ -164,7 +165,7 @@ public class TsOutput {
return quote("") + ", 1, 0";
}
try {
String[] fields = tsInfo.split(",");
String[] fields = tokenizeWithBraces(tsInfo);
if (fields.length > multiplierIndex) {
/**
* Evaluate static math on .ini layer to simplify rusEFI java and rusEFI PHP project consumers