Skip to main content

MAX_NESTING

Constant MAX_NESTING 

Source
pub const MAX_NESTING: usize = 40;
Expand description

The deepest nesting parse accepts; deeper patterns are rejected with the error “pattern nests too deeply”.

Nesting is counted along each path from the root of the pattern to a leaf as the number of parenthesized groups (at the observation level and inside [...] alike) plus the number of qualifiers (WITHIN, REPEATS, START..STOP), since each qualifier wraps its expression in another node. Chains such as a OR b OR c add no nesting however long they are: they parse into a single n-ary node.

The parser itself does not recurse per group. Lowering, the matcher, and the AST’s derived Clone, PartialEq, Debug, Serialize and drop glue do recurse over nesting depth, so this bound is what keeps them within the stack.

§How the value was chosen

By measurement, in a debug build on a thread with a 256 KiB stack (well below wasm’s 1 MiB default). The pipeline is parse, lower, validate, render, serde_json::to_string of the AST, matching, without_spans and drop. It was run on the worst shapes: observation-level groups with a FOLLOWEDBY, an OR and an AND node between each pair of groups; comparison-level groups; the two mixed; stacked qualifiers; and qualified groups. The deepest shape survived about 56 levels, limited by serde_json serialization of the AST. Every other stage and shape went deeper. 40 keeps roughly 40% headroom for other platforms and toolchains. stix-matcher’s tests/deep_nesting.rs reruns these shapes at this limit.

Real patterns nest a handful of levels at most.