tryFromPathname static method

OccurrenceAddress? tryFromPathname(
  1. String pathname,
  2. HierarchyOccurrence root
)

Resolve a pathname string (e.g. "Top/counter/clk" or "Top.counter.clk") to a OccurrenceAddress by walking root.

Supports both / hierarchy paths and dot-separated signal identifiers commonly produced by VCD/FST waveform files. If the first segment matches root's name, it is skipped — the root occurrence is always at the empty address.

The last segment is first tried as a signal name within the current occurrence; if that fails it is tried as a child occurrence name. This mirrors the pathname convention where a signal path has one more segment than its parent module path.

Returns null if any segment cannot be resolved.

final addr = OccurrenceAddress.tryFromPathname('Top/cpu/clk', root);
if (addr != null) {
  final signal = service.signalByAddress(addr);
}

Implementation

static OccurrenceAddress? tryFromPathname(
  String pathname,
  HierarchyOccurrence root,
) {
  final rootAddr = root.address ?? OccurrenceAddress.root;
  final parts = pathname
      .replaceAll('.', hierarchyPathSeparator)
      .split(hierarchyPathSeparator)
      .where((s) => s.isNotEmpty)
      .toList();

  // Skip leading segment that matches the root name.
  final segments =
      parts.isNotEmpty && parts.first == root.name ? parts.skip(1) : parts;

  ({HierarchyOccurrence node, OccurrenceAddress addr})? step(
    ({HierarchyOccurrence node, OccurrenceAddress addr})? cur,
    String segment,
  ) {
    if (cur == null) {
      return null;
    }
    final si = cur.node.signalIndexByName(segment);
    if (identical(segment, segments.last) && si >= 0) {
      return (node: cur.node, addr: cur.addr.signal(si));
    }
    final ci = cur.node.childIndexByName(segment);
    return ci >= 0
        ? (node: cur.node.children[ci], addr: cur.addr.child(ci))
        : null;
  }

  return segments.fold<({HierarchyOccurrence node, OccurrenceAddress addr})?>(
      (node: root, addr: rootAddr), step)?.addr;
}