Skip to content

Commit

Permalink
Resolve Defines
Browse files Browse the repository at this point in the history
  • Loading branch information
karniv00l committed Dec 29, 2023
1 parent 0197ab6 commit bea98d3
Show file tree
Hide file tree
Showing 8 changed files with 2,300 additions and 137 deletions.
10 changes: 5 additions & 5 deletions lib/ini_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions lib/pre_processor.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:ini_parser/extensions.dart';

typedef PreProcessorDefines = Map<String, List<String>>;

class PreProcessor {
PreProcessor({
required this.raw,
Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
13 changes: 13 additions & 0 deletions lib/sections/constants_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions lib/sections/pc_variables_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit bea98d3

Please sign in to comment.