reni/lookbehind_tune

How Oniguruma reshapes a look-behind body before it compiles it (tune_look_behind). tuneLookBehinds gives every look-behind the shape Oniguruma leaves it in, innermost first: the body's leading repeats pinned to their lower bound (reduceBody), then the positive look-behind dropped, divided into one look-behind per branch (lookDivided, which the matcher retries), or kept whole, on the lengths of that reduced body (tuneLookBehind). A negative one is divided only where a window could tell (tuneNegLookBehind). Either is rejected where that body may take more characters than Oniguruma steps back over, or where a piece it divides into may (checkLength, checkLengths).

The decisions read a model of Oniguruma's tree -- lengths in characters, min_is_sure, the possessive rewrite and the case-fold unravelling that run before its reduction -- which nothing else in the compiler reads. Its own module for that reason.

Types

GroupSpans = object
What charSpan reads beyond the node it measures: the group bodies a backreference or a call is measured through, each group's span once measured, and the groups the pattern refers to anywhere. Collected when the first group is measured; most patterns never get there. The compiler's empty-check pass measures through one too, on the numbered final tree (see initGroupSpans).

Procs

proc initGroupSpans(root: Node; names: seq[(string, int)]; nodeCount = 0): GroupSpans {.
    ...raises: [], tags: [], forbids: [].}
Measure through the groups of root; names maps each capture name to its group. With nodeCount, the size of the table root was numbered into, repeatBodySpan keeps what it measures. Leave it 0 on a tree a pass is still reshaping, as tuneLookBehinds does.
proc repeatBodyMayBeEmpty(q: Node; g: var GroupSpans): bool {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}

Whether repeat q's body may match empty: what Oniguruma's node_min_byte_len(body) == 0 decides for the repeat's emptiness. That counts bytes and charSpan characters, but the two minimums are zero together outside three corners: (*FAIL), infinitely long there and the empty (?!) it parses to here; a reference inside the group it names, empty there (ND_IS_RECURSION) and measured through the group here; and a reference with a level other than 0, empty here wherever it stands. An iteration through the first, or through a plain reference into its own open group (Regex.opensUnset), always fails, so no empty check reads those.

Measured under no flags: they decide whether a class is fixed (charSpan), never lo.

proc tuneLookBehinds(ast: Node; names: seq[(string, int)]; flags: RegexFlags;
                     reads: bool) {....raises: [RegexLimitError, Exception,
    RegexError], tags: [RootEffect], forbids: [].}
Give every look-behind in ast the shape Oniguruma leaves it in (see the module documentation). names maps each capture name to its group, for the references and calls a body is measured through. reads says the pattern may hold a backreference or a call, without which no look-behind is measured for fixedChars. Must run where reduceBody says: before every pass that reads a look-behind's bounds, since a reduced body may become fixed-length.