transformParser function

Parser transformParser (
  1. Parser parser,
  2. TransformationHandler handler

Transforms all parsers reachable from parser with the given handler. The identity function returns a copy of the the incoming parser.

The implementation first creates a copy of each parser reachable in the input grammar; then the resulting grammar is traversed until all references to old parsers are replaced with the transformed ones.


Parser transformParser(Parser parser, TransformationHandler handler) {
  final mapping = Map<Parser, Parser>.identity();
  for (final each in allParser(parser)) {
    mapping[each] = handler(each.copy());
  final seen = Set.of(mapping.values);
  final todo = List.of(mapping.values);
  while (todo.isNotEmpty) {
    final parent = todo.removeLast();
    for (final child in parent.children) {
      if (mapping.containsKey(child)) {
        parent.replace(child, mapping[child]);
      } else if (!seen.contains(child)) {
  return mapping[parser];