Resolve Defines

This commit is contained in:
Piotr Rogowski 2023-12-29 15:59:34 +01:00
parent 0197ab69ed
commit bea98d37fa
No known key found for this signature in database
GPG Key ID: 4A842D702D9C6F8F
8 changed files with 2285 additions and 122 deletions

View File

@ -22,7 +22,7 @@ class INIParser {
final String raw;
final List<String> lines = [];
final List<String> settings = [];
final Map<String, List<String>> defines = {};
final PreProcessorDefines defines = {};
// assume that top level definitions are in the TunerStudio section (see FOME)
String _currentSection = 'TunerStudio';
@ -103,13 +103,13 @@ class INIParser {
}
Future<void> _parsePcVariables() async {
_config.pcVariables =
await PcVariablesParser().parse(_sections['PcVariables'] ?? []);
_config.pcVariables = await PcVariablesParser(defines: defines)
.parse(_sections['PcVariables'] ?? []);
}
Future<void> _parseConstants() async {
_config.constants =
await ConstantsParser().parse(_sections['Constants'] ?? []);
_config.constants = await ConstantsParser(defines: defines)
.parse(_sections['Constants'] ?? []);
}
Future<void> _parseOutputChannels() async {

View File

@ -1,5 +1,7 @@
import 'package:ini_parser/extensions.dart';
typedef PreProcessorDefines = Map<String, List<String>>;
class PreProcessor {
PreProcessor({
required this.raw,
@ -8,7 +10,7 @@ class PreProcessor {
late final String raw;
late final List<String> settings;
final List<String> lines = [];
final Map<String, List<String>> defines = {};
final PreProcessorDefines defines = {};
/// Pre-process INI:
/// - remove comments
@ -88,7 +90,8 @@ class PreProcessor {
if (line.startsWith('#define')) {
final parts = line.substring(7).trim().split('=');
final key = parts[0].sanitize();
final values = parts[1].sanitize().split(',').map((e) => e.sanitize());
final values =
parts[1].sanitize().split(',').map((e) => e.clearString());
defines.addAll({
key: values.toList(),

View File

@ -2,11 +2,16 @@ import 'package:ini_parser/extensions.dart';
import 'package:ini_parser/models/ini_config.dart';
import 'package:ini_parser/parsing_exception.dart';
import 'package:ini_parser/patterns.dart';
import 'package:ini_parser/pre_processor.dart';
import 'package:ini_parser/section.dart';
import 'package:text_parser/text_parser.dart';
class ConstantsParser {
ConstantsParser({required this.defines});
late final PreProcessorDefines defines;
final _constants = Constants();
int _currentPage = 0;
final _parser = TextParser(
@ -96,6 +101,14 @@ class ConstantsParser {
options = optionsRaw.asMap();
}
// resolve defines (ex. $loadSourceNames)
if (options[0] != null && options[0]!.startsWith(r'$')) {
final foundDefines = defines[options[0]!.substring(1)];
if (foundDefines != null) {
options = foundDefines.asMap();
}
}
final bitsRaw = result[4]
.replaceAll(RegExp(r'[^\d:]'), '')
.clearString()

View File

@ -2,10 +2,14 @@ import 'package:ini_parser/extensions.dart';
import 'package:ini_parser/models/ini_config.dart';
import 'package:ini_parser/parsing_exception.dart';
import 'package:ini_parser/patterns.dart';
import 'package:ini_parser/pre_processor.dart';
import 'package:ini_parser/section.dart';
import 'package:text_parser/text_parser.dart';
class PcVariablesParser {
PcVariablesParser({required this.defines});
late final PreProcessorDefines defines;
final List<PcVariable> _pcVariables = [];
final _parser = TextParser(
@ -73,6 +77,14 @@ class PcVariablesParser {
options = optionsRaw.asMap();
}
// resolve defines (ex. $loadSourceNames)
if (options[0] != null && options[0]!.startsWith(r'$')) {
final foundDefines = defines[options[0]!.substring(1)];
if (foundDefines != null) {
options = foundDefines.asMap();
}
}
final bitsRaw = result[3]
.replaceAll(RegExp(r'[^\d:]'), '')
.clearString()

File diff suppressed because it is too large Load Diff

View File

@ -76,9 +76,9 @@ void main() {
expect(parser.defines).toEqual(
{
'invalid_x16': ['"TEST"', r'$invalid_x8'],
'invalid_x16': ['TEST', r'$invalid_x8'],
'fullStatus_def': [r'$fullStatus_def_1', r'$fullStatus_def_2'],
'loadSourceUnits': ['"kPa"', '"% TPS"', '"%"', '"INVALID"'],
'loadSourceUnits': ['kPa', '% TPS', '%', 'INVALID'],
'trigger_missingTooth': ['0'],
},
);

View File

@ -25,6 +25,10 @@ page = 2
unusedBits4_123 = bits, U08, 123, [3:7]
rtc_trim = scalar, S08, 123, "ppm", 1, 0, -127, +127, 0
inj4CylPairing = bits, U08, 123, [1:2], "1+3 & 2+4", "1+4 & 2+3", "INVALID", "INVALID"
#define loadSourceNames = "MAP", "TPS", "IMAP/EMAP", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID"
ignAlgorithm = bits, U08, 26, [4:6], $loadSourceNames
''';
test('config', () async {
@ -244,6 +248,28 @@ page = 2
3: 'INVALID',
});
});
test('defined bits options', () async {
final result = await INIParser(raw).parse();
final constant = result.constants.pages[1].constants[5] as ConstantBits;
expect(constant.name).toEqual('ignAlgorithm');
expect(constant.type).toEqual(ConstantType.bits);
expect(constant.size).toEqual(ConstantSize.u08);
expect(constant.offset).toEqual(26);
expect(constant.bits.low).toEqual(4);
expect(constant.bits.high).toEqual(6);
expect(constant.options).toEqual({
0: 'MAP',
1: 'TPS',
2: 'IMAP/EMAP',
3: 'INVALID',
4: 'INVALID',
5: 'INVALID',
6: 'INVALID',
7: 'INVALID',
});
});
});
group('failure', () {

View File

@ -5,7 +5,7 @@ import 'package:spec/spec.dart';
void main() {
group('PcVariables', () {
group('success', () {
const raw = '''
const raw = r'''
[PcVariables]
tuneCrcPcVariable = continuousChannelValue, tuneCrc16
@ -16,6 +16,10 @@ void main() {
wueAFR = array, S16, [10], "AFR", 0.1, 0.0, -4.0, 4.0, 1
AUXin00Alias = string, ASCII, 20
iat_adcChannel = bits, U08, [0:5], 0="NONE", 1="18 - AN temp 1",13="19 - AN volt 4",5="28 - AN volt 10, Aux Reuse"
#define loadSourceNames = "MAP", "TPS", "IMAP/EMAP", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID"
algorithmNames = bits, U08, [0:2], $loadSourceNames
''';
test('scalar long', () async {
@ -155,6 +159,29 @@ void main() {
5: '28 - AN volt 10, Aux Reuse',
});
});
test('bits - with defined options', () async {
final result = await INIParser(raw).parse();
final variable = result.pcVariables[7] as PcVariableBits;
expect(variable.name).toEqual('algorithmNames');
expect(variable.type).toEqual(ConstantType.bits);
expect(variable.size).toEqual(ConstantSize.u08);
expect(variable.bits.low).toEqual(0);
expect(variable.bits.high).toEqual(2);
expect(variable.options).toEqual(
{
0: 'MAP',
1: 'TPS',
2: 'IMAP/EMAP',
3: 'INVALID',
4: 'INVALID',
5: 'INVALID',
6: 'INVALID',
7: 'INVALID'
},
);
});
});
group('failure', () {