parse static method

ConfigFile parse(
  1. String content
)

Implementation

static ConfigFile parse(String content) {
  var config = ConfigFile();
  Section? currentSection;

  for (var line in LineSplitter.split(content)) {
    RegExpMatch? match;
    match = _commentPattern.firstMatch(line);
    if (match != null) {
      continue;
    }

    match = _blankLinePattern.firstMatch(line);
    if (match != null) {
      continue;
    }

    match = _sectionPattern.firstMatch(line);
    if (match != null) {
      assert(match.groupCount == 1);

      currentSection = null;
      var name = match.group(1)!;
      var sectionNames = name.split(' ');

      assert(sectionNames.length <= 2);
      for (var i = 0; i < sectionNames.length; i++) {
        var sectionName = sectionNames[i];
        if (i != 0) {
          assert(sectionName.startsWith('"'), 'Section name: $sectionName');
          assert(sectionName.endsWith('"'), 'Section name: $sectionName');
          sectionName = sectionName.substring(1, sectionName.length - 1);
        }

        var section = config.sections.firstWhereOrNull(
          (s) => s.name == sectionName,
        );
        if (section == null) {
          section = Section(sectionName);
          if (currentSection == null) {
            config.sections.add(section);
          } else {
            currentSection.sections.add(section);
          }
        }

        currentSection = section;
      }
      continue;
    }

    match = _entryPattern.firstMatch(line);
    if (match != null) {
      assert(currentSection != null);
      assert(match.groupCount == 2);

      var key = match[1]!.trim();
      var value = match[2]!.trim();
      currentSection!.options[key] = value;
      continue;
    }
  }

  return config;
}