UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

87 lines (86 loc) • 5.47 kB
/** * The matcher of the filesystem glob dialect: a token list is compiled to a small program, and a path is matched by walking * it once, left to right, holding the SET of instructions that are live at the current position. An instruction is in the set * at most once, so the cost of one test is at most the program's length times the path's length plus one, however many stars * the pattern holds and however they are arranged — no expression is built from the pattern's text, so nothing backtracks and * no pattern needs a cap. That product is a limit a pattern can reach on an ordinary path, when it keeps every instruction live at * every place (a ring of empty alternatives does). The scratch it needs is two sets of the program's length, kept between tests for a * program of up to 65 536 instructions (a longer program's is made for each test that gets past the two checks below and dropped: a * cost of the program's length, whatever the path); a test whose threads all die at the first character costs what it touched, not * the product of the two lengths. Two cheap checks come before the walk and only ever say "cannot match": a value that lacks a * literal every match holds, or does not end with the literal the pattern ends on, is refused at once; inside the walk, a program * that opens on a literal starts a thread only where that literal stands. * * It answers a different question from {@link wildcardMatch} (the permission-rule name glob), which is why the two are two * engines and not one with a switch. The grammars differ: * * | construct | rule glob (`wildcardMatch`) | filesystem glob (this module) | * | ------------------ | ----------------------------- | ----------------------------------------------------- | * | `*` | any run, `/` included | any run of characters but `/` (`star`) | * | `**` | the same as `*` | any run, `/` included (`globstar`) | * | `?` | a literal | one character but `/` (`one`) | * | `[abc]`, `[!a-c]` | literals | one character of the class (`class`) | * | `{a,b}` | literals | one alternative of several (`alt`) | * | where it may start | the whole value | the whole path, or any path segment (`anchor`) | * * Each site that reads such a glob (the search tools' `pattern` / `glob` arguments and `.gitignore` lines, the shell read * station's expansion of a command word) parses its own spelling into tokens — what a spelling MEANS stays with the site — and * this module is the one place that matches them. * * Lengths are counted in UTF-16 code units, the unit a RegExp without the `u` flag counts in: `?` and a class stand for one unit. */ /** One bracket-expression's test of a single code unit, already validated: build it with {@link fsGlobClass}. */ export interface FsGlobClass { /** Does this one code unit belong to the class? */ matches(unit: string): boolean; } /** * A class from its character-class source (`[a-z]`, `[^/ab]`), or `undefined` when the source is not a class JavaScript accepts * (a reversed range, say). A class is one character wide: its expression holds no repetition, so it cannot backtrack and the * repetition structure of a glob stays in the program. */ export declare function fsGlobClass(source: string): FsGlobClass | undefined; /** A token that stands for a fixed or bounded part of the path. */ export type FsGlobUnit = { readonly kind: "text"; readonly text: string; } | { readonly kind: "one"; } | { readonly kind: "class"; readonly class: FsGlobClass; } | { readonly kind: "star"; }; export type FsGlobToken = FsGlobUnit | { readonly kind: "globstar"; } /** One of several alternatives, each a run of units (no nesting). */ | { readonly kind: "alt"; readonly alternatives: readonly (readonly FsGlobUnit[])[]; }; /** Where a match may begin: `start` only at the first character of the value; `segment` at the first character or right after any `/`. */ export type FsGlobAnchor = "start" | "segment"; /** A compiled glob: the whole of a value must match. */ export interface FsGlob { /** Does the whole of `value` match? */ test(value: string): boolean; } /** * What the matcher has done in this process so far, for a pin to state a bound as a count instead of timing a call. `visits`: the * (instruction, position) pairs entered — a test of a program `size` long against a path of `length` enters at most * `size * (length + 1)`. `scratchBytes`: the bytes of the sets allocated — it moves when a program larger than any before needs * scratch, and per test only for a program past the retained size. */ export declare function fsGlobWorkTotals(): { readonly visits: number; readonly scratchBytes: number; }; /** A compiled program (see the module header). */ export interface FsGlobProgram extends FsGlob { /** The program's length in instructions: the factor of the visit bound that belongs to the pattern. */ readonly size: number; } /** Compile a token list. The match is always over the whole value; `anchor` says where it may begin. */ export declare function compileFsGlob(tokens: readonly FsGlobToken[], anchor: FsGlobAnchor): FsGlobProgram;