A Scope represents the region in which a stream is interpreted and resources remain active. It is either open or closed. An open scope carries a finalizer and may own nested subscopes; closing it closes those children before running its own finalizer. This makes cleanup interpreter-managed rather than an ordinary stream action that could be skipped when a stream ends early or is discarded. section 15.3.3
A scope organizes resource safety through a small state machine:
- Open a subscope to register a finalizer; the new scope is recorded under its parent. section 15.3.3
- Close subscopes in reverse creation order, then run the enclosing scope’s finalizer. section 15.3.3
- Make closing idempotent, while redirecting attempts to open beneath a closed non-root scope to an open parent. section 15.3.3
- Preserve cleanup attempts even when a finalizer fails, then raise the first encountered error. section 15.3.3
A file-backed stream
`Stream.resource` acquires a `Source` and associates `Source.close()` with a scope. A `lines` stream can then read incrementally, while a surrounding fold closes the root scope whether interpretation succeeds or fails. The same pattern safely scopes a `BufferedWriter` while writing transformed lines. section 15.3.3
A stream may also create a scope without a finalizer. For example, `take(n)` scopes its truncated pull so resources opened within the retained region are closed when that region finishes, rather than remaining live for the discarded remainder. section 15.3.3
Parser combinators use a separate `scope(msg)` operation with a different purpose: on failure, it pushes contextual text onto a `ParseError` stack without discarding inner messages. Nested parser scopes therefore describe both the broader grammar construct and the detailed point of failure; `label` instead replaces that detail. section 9.5.2 section 9.6.3
Hierarchical parse context
Wrapping a JSON array parser in `.scope("array")` and literal and object parsers in corresponding scopes lets a failed parse report the enclosing construct alongside lower-level failures. section 9.8
