Skip to content

Stress

The stress axis

The structural load at each token: how many brackets enclose it, how long the innermost has been open, and how long all of them together.

Source: src/stress.rs.

The readings

ReadingDefinition
depththe number of brackets enclosing the token
strainthe tokens the innermost open bracket has been held open
loadthe sum of every open bracket’s stretch, sum(now - opened)
EventDefinition
peaka local depth maximum at or above peak_min_depth
fracturethe start of a release cascade from a level of at least fracture_min_depth

A flat stream carries no stress; a deeply nested one builds load to a peak and releases it at a fracture.

Reading the axis

$ trex stress --text 'f(g(h(x)))'
trex stress: 10 bytes, 10 tokens, max depth 3, 2 peak(s), 1 fracture(s)
  peak load: 10 (depth 2, strain 4)

--field prints the per-token depth, strain and load, --peaks the stress peaks and --fractures the fracture points. In PowerShell each frame carries Offset, Text, Depth, Strain and Load, and -Detail adds a frame at every token.

In a pattern

@nested>k and @nested>=k are zero-width anchors on tokens at least that many brackets deep.

$ trex scan '@nested>=3 \W' --text 'f(g(h(x)))'
[6..7] "x"

Data model

StressFrame, one token’s reading:

FieldTypeMeaning
depthu16enclosing-bracket nesting depth
strainf32stretch of the innermost open bracket
loadf32total outstanding tension, the sum of the open-bracket stretches

StressField, keyed by byte offset:

FieldTypeMeaning
n_tokensusizetoken count
spansVec<(usize, usize)>byte span per token
framesVec<StressFrame>one per token
peaksVec<usize>stress-peak byte offsets
fracturesVec<usize>fracture byte offsets
max_depthu16deepest nesting reached

Query API: depth_at(byte), load_at(byte).

Algorithm

The open-bracket stack of token indices is kept from TokenKind::Open and Close. At each token a close pops first, so it reads the outer depth and is the release point; depth = stack.len(); strain = now - innermost_open; load = sum(now - open) over the stack. A fracture is the first decreasing-depth token of a cascade whose prior depth reached fracture_min_depth. Peaks are a second linear pass over the depth sequence, local maxima at or above peak_min_depth. One pass and one peak scan; O(depth) per token for load.