extractPortAccessSliceComponents static method

({List<int>? dimensionAccess, String portName, int? sliceLowerIndex, int? sliceUpperIndex}) extractPortAccessSliceComponents(
  1. String portAccessString
)

Extracts port access components from a port access string.

Parses complex port access expressions to extract the base port name, array dimension indices, and slice bounds. This is a utility method used by factory constructors to decode access strings.

Returns a record containing:

  • portName: The base port identifier
  • dimensionAccess: List of array indices (null if none)
  • sliceUpperIndex/sliceLowerIndex: Slice bounds (null if no slicing)

Implementation

static ({
  String portName,
  int? sliceLowerIndex,
  int? sliceUpperIndex,
  List<int>? dimensionAccess
}) extractPortAccessSliceComponents(String portAccessString) {
  if (StandardPortReference._isStandardAccess(portAccessString)) {
    // if it's a standard port reference, just return the name and empty
    // dimension access
    return (
      portName: portAccessString,
      sliceLowerIndex: null,
      sliceUpperIndex: null,
      dimensionAccess: null
    );
  }

  final match = _sliceAccessRegex.firstMatch(portAccessString);

  if (match == null) {
    throw RohdBridgeException('Invalid port slice access string');
  }

  final portName = match.group(1)!;

  final bracketMatches =
      _bracketedAreas.allMatches(portAccessString).toList();

  final dimensionAccess = <int>[];
  int? sliceUpperIndex;
  int? sliceLowerIndex;

  // loop through all the groups in the match, and for each one:
  //  - if it has a :, split it and set the slice indices
  //  - if not, then put it into dimensionAccess
  // we don't know how many dimensions there may be, so keep looking til end
  for (final bracketMatch in bracketMatches) {
    final group = bracketMatch.group(1)!;

    assert(sliceLowerIndex == null,
        'should only be one slice, and the last one');
    assert(sliceUpperIndex == null,
        'should only be one slice, and the last one');

    if (group.contains(':')) {
      final sliceParts = group.split(':');
      sliceLowerIndex = int.parse(sliceParts[1]);
      sliceUpperIndex = int.parse(sliceParts[0]);
    } else {
      dimensionAccess.add(int.parse(group));
    }
  }

  return (
    portName: portName,
    sliceLowerIndex: sliceLowerIndex,
    sliceUpperIndex: sliceUpperIndex,
    dimensionAccess: dimensionAccess.isEmpty ? null : dimensionAccess
  );
}