visitLabeledStatement method

  1. @override
Object? visitLabeledStatement(
  1. SLabeledStatement node
)
override

Implementation

@override
Object? visitLabeledStatement(SLabeledStatement node) {
  final labelNames = node.labels.map((l) => l.label!.name).toSet();
  final oldLabels = _currentStatementLabels;
  _currentStatementLabels = labelNames;
  Logger.debug("[SLabeledStatement] Entering with labels: $labelNames");

  try {
    return node.statement!.accept<Object?>(this);
  } on BreakException catch (e) {
    Logger.debug(
      "[SLabeledStatement] Caught BreakException (label: ${e.label}) with current labels: $_currentStatementLabels",
    );
    if (e.label != null && _currentStatementLabels.contains(e.label)) {
      // This break was targeting this labeled statement.
      Logger.debug(
        "[SLabeledStatement] Consuming labeled break: '${e.label}'.",
      );
      return null; // Effectively breaks out of the labeled statement.
    } else {
      // Unlabeled break or break for an outer label, rethrow.
      Logger.debug("[SLabeledStatement] Rethrowing break...");
      rethrow;
    }
  }
  // ContinueException with a label matching this statement is an error
  // (you can only continue loops/switch members), so we don't catch it here.
  // It should be caught by the loop/switch or propagate further up.
  finally {
    Logger.debug("[SLabeledStatement] Exiting labels: $labelNames");
    _currentStatementLabels = oldLabels;
  }
}