buildBlockHierarchy function

void buildBlockHierarchy(
  1. Config config
)

Build block hierarchy by establishing parent-child relationships.

This function traverses the configuration and sets the parent field on all nested AST nodes and the parentBlock field on nested blocks. By default, blocks parsed from configuration do not have their parent references set for performance reasons.

Example:

final config = Config.parse(configContent);
buildBlockHierarchy(config);

// Now elements have parent references
final nestedBlock = someBlock;
final parent = nestedBlock.parent; // Not null if nested

Implementation

void buildBlockHierarchy(Config config) {
  void linkElements(List<ConfigElement> elements, ConfigElement? parent) {
    for (final element in elements) {
      element.parent = parent;
      if (element is Block) {
        element.parentBlock = parent is Block ? parent : null;
        linkElements(element.body, element);
      } else if (element is Command && element.block != null) {
        final block = element.block!;
        block.parent = element;
        block.parentBlock = null;
        linkElements(block.body, block);
      }
    }
  }

  linkElements(config.statements, null);
}