readDefinitions function

({List<String> problems, Map<String, List<NotationDefinition>> words}) readDefinitions(
  1. Map<String, String> texts, {
  2. String? dictionary,
})

The definitions in texts (each by its file name: StickyNote.txt), by word, with what could not be read (StickyNote.txt:3: …). A word that uses itself, directly or through others, is left out.

Implementation

({Map<String, List<NotationDefinition>> words, List<String> problems}) readDefinitions(Map<String, String> texts, {String? dictionary}) {
  final problems = <String>[];
  final words = <String, List<NotationDefinition>>{};
  for (final e in texts.entries) {
    for (final d in _read(e.key, e.value, problems, dictionary: dictionary)) {
      (words[d.word] ??= []).add(d);
    }
  }
  // A word that uses itself is refused, with every word on the way.
  final refused = <String>{};
  for (final w in words.keys) {
    final path = _cycle(w, words, [w]);
    if (path != null) {
      refused.add(w);
      final d = words[w]!.first;
      problems.add('${d.at(d.line)}: $w uses itself (${path.join(' > ')}): it is not read.');
    }
  }
  for (final w in refused) {
    words.remove(w);
  }
  return (words: words, problems: problems);
}