resolve<T> function Null safety

Parser<T> resolve<T>(
  1. Parser<T> parser

Resolves all parser references reachable through parser. Returns an optimized parser graph that inlines all references directly.

This code in-lines parsers that purely reference another one (subclasses of ResolvableParser). This includes, but is not limited to, parsers created with ref0, ref1, ref2, ..., undefined, or SettableParserExtension,


Parser<T> resolve<T>(Parser<T> parser) {
  final mapping = <ResolvableParser, Parser>{};
  parser = _dereference(parser, mapping);
  final todo = <Parser>[parser];
  final seen = <Parser>{parser};
  while (todo.isNotEmpty) {
    final parent = todo.removeLast();
    for (var child in parent.children) {
      if (child is ResolvableParser) {
        final referenced = _dereference(child, mapping);
        parent.replace(child, referenced);
        child = referenced;
      if (seen.add(child)) {
  return parser;