Skip to content

The stress axis

The stress axis reads the brackets around every token. Its depth is how many brackets enclose it, its strain how long the innermost of them has been held open, and its load how long all of them together have been held open. A flat stream carries no stress; a deeply nested one builds load as it goes in and releases it as the brackets close.

Source: src/stress.rs.

What it reads that nothing else does

retry(fetch(url), 3)
retry(fetch(url, timeout=30, headers=h, auth=a, proxy=p), 3)

Both calls nest two deep, and relation counts their enclosures over each whole call, 5 against 37. Stress reads at each token how long every bracket around it has stayed open: the second call’s load peaks at p, 36, with fetch open 17 tokens and retry 19, and drops at the ) closing fetch; the first call’s load never passes 6. The same reading finds a long argument list, or a block held open across a file.

The rest of this page reads one line, which nests four deep and holds two kinds of collection:

call(a, f(g(h(x))), [1, {k: v}]) ; done

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 token at least peak_min_depth deep, as deep as both neighbors and deeper than one of them
fracturethe first token of a release, where the depth falls from a level at least fracture_min_depth deep

Both thresholds are 2 by default and both comparisons are inclusive. An opening bracket reads the depth outside it and a closing bracket the depth it returns to, so a token’s depth is the count of brackets that hold it, not the brackets it opens or closes. Only brackets that pair count: an unmatched one is a character in the text.

Reading the axis

The summary gives the token count, the deepest nesting, the number of peaks and fractures, and the token of greatest load:

$ trex stress --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
trex stress: 39 bytes, 27 tokens, max depth 4, 6 peak(s), 2 fracture(s)
  peak load: 29 (depth 3, strain 3)

Every token’s reading

--field lists each token’s depth d, strain and load, and --limit N stops the list after N tokens and counts the rest. In PowerShell -Detail and in Python detail=True add the same frames:

$ trex stress --field --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
trex stress: 39 bytes, 27 tokens, max depth 4, 6 peak(s), 2 fracture(s)
  peak load: 29 (depth 3, strain 3)
  per-token (token -> depth | strain | load):
    call           d=  0 strain=    0 load=     0
    (              d=  0 strain=    0 load=     0
    a              d=  1 strain=    1 load=     1
    ,              d=  1 strain=    2 load=     2
    f              d=  1 strain=    3 load=     3
    (              d=  1 strain=    4 load=     4
    g              d=  2 strain=    1 load=     6
    (              d=  2 strain=    2 load=     8
    h              d=  3 strain=    1 load=    11
    (              d=  3 strain=    2 load=    14
    x              d=  4 strain=    1 load=    18
    )              d=  3 strain=    4 load=    20
    )              d=  2 strain=    7 load=    18
    )              d=  1 strain=   12 load=    12
    ,              d=  1 strain=   13 load=    13
    [              d=  1 strain=   14 load=    14
    1              d=  2 strain=    1 load=    16
    ,              d=  2 strain=    2 load=    18
    {              d=  2 strain=    3 load=    20
    k              d=  3 strain=    1 load=    23
    :              d=  3 strain=    2 load=    26
    v              d=  3 strain=    3 load=    29
    }              d=  2 strain=    7 load=    28
    ]              d=  1 strain=   22 load=    22
    )              d=  0 strain=    0 load=     0
    ;              d=  0 strain=    0 load=     0
    done           d=  0 strain=    0 load=     0

Strain and load differ where a collection holds several items. Inside [1, {k: v}] the innermost bracket changes from [ to {, so strain restarts at k, while load keeps counting every bracket still open: the closing ] reads strain 22, the length of time [ was held.

Peaks

A peak is the top of a rise: a token at least as deep as both its neighbors and deeper than one of them. Every step of a staircase of brackets rises to a new level, so g, h and x are each a peak as f(g(h(x))) goes in:

$ trex stress --peaks --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
trex stress: 39 bytes, 27 tokens, max depth 4, 6 peak(s), 2 fracture(s)
  peak load: 29 (depth 3, strain 3)
  stress peaks (6):
    @    10  g(h(x))), [1, {k: v}]) ;
    @    12  h(x))), [1, {k: v}]) ; d
    @    14  x))), [1, {k: v}]) ; don
    @    21  1, {k: v}]) ; done
    @    25  k: v}]) ; done
    @    28  v}]) ; done

Fractures

A fracture is where a run of closing brackets starts: the first token whose depth falls, from a level at least the fracture depth. One fracture marks one release, however many brackets close in it, so ))) after x is a single fracture at its first ):

$ trex stress --fractures --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
trex stress: 39 bytes, 27 tokens, max depth 4, 6 peak(s), 2 fracture(s)
  peak load: 29 (depth 3, strain 3)
  fractures (2):
    @    15  ))), [1, {k: v}]) ; done
    @    29  }]) ; done

Choosing a threshold

Both cuts on this axis are depths, so read --field and choose the depth that separates the nesting you care about from the nesting every line has.

The peak depth. At the default of 2 every collection inside a call peaks, 1 in [1, ...] among them. To keep only the deep structure, raise it to the depth the ordinary nesting never reaches. Here 3 drops the peaks at depth 2 and keeps the rise into h(x) and into {k: v}:

$ trex stress --peaks --peak-min-depth 3 --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
trex stress: 39 bytes, 27 tokens, max depth 4, 4 peak(s), 2 fracture(s)
  peak load: 29 (depth 3, strain 3)
  stress peaks (4):
    @    12  h(x))), [1, {k: v}]) ; d
    @    14  x))), [1, {k: v}]) ; don
    @    25  k: v}]) ; done
    @    28  v}]) ; done

The fracture depth. A fracture’s depth is the level the release starts from, the depth before its first closing bracket. Raising it to 4 keeps only the release from inside h(x), and drops the closing of {k: v}, which starts from depth 3:

$ trex stress --fractures --fracture-min-depth 4 --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
trex stress: 39 bytes, 27 tokens, max depth 4, 6 peak(s), 1 fracture(s)
  peak load: 29 (depth 3, strain 3)
  fractures (1):
    @    15  ))), [1, {k: v}]) ; done

A pattern’s depth. @nested>k reads the same depth, so choose k from the field as you would the peak depth: the depth your ordinary lines reach is the one to stay above.

Declarations

Declarations decide which tokens a bracket holds, so a declared shape that takes in a bracket removes it from the depth count. An index such as [1] declared as one token is no longer a bracket around 1:

$ trex stress --text 'x = a[1] + b[2]'
trex stress: 15 bytes, 11 tokens, max depth 1, 0 peak(s), 0 fracture(s)
  peak load: 1 (depth 1, strain 1)

$ trex stress --shape 'index = `\[[0-9]\]`' --text 'x = a[1] + b[2]'
trex stress: 15 bytes, 7 tokens, max depth 0, 0 peak(s), 0 fracture(s)
  peak load: 0 (depth 0, strain 0)

Every axis that reads tokens takes the declarations a scan takes: --lib, --shape, --shape-after, --kind, --let and --declare on the command line, -Library in PowerShell and lib= in Python (pattern files).

In a pattern

@nested>k and @nested>=k are zero-width anchors on tokens more than, or at least, k brackets deep. With \W after it the anchor takes the deep names; with . after it, every deep token, brackets included:

$ trex scan '@nested>2 \W' --text 'call(a, f(g(h(x))), [1, {k: v}]) ; done'
[12..13] "h"
[14..15] "x"
[25..26] "k"
[28..29] "v"

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). StressConfig holds peak_min_depth and fracture_min_depth, both 2; analyze_with(tokens, bytes, &cfg) reads under it.

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: a token at least peak_min_depth deep, at least as deep as both neighbors and deeper than one. One pass and one peak scan; O(depth) per token for load.