@ac-kit/format-regex 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -0
- package/README.md +17 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/limits.d.ts +43 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +31 -0
- package/dist/limits.js.map +1 -0
- package/dist/regex.d.ts +6 -1
- package/dist/regex.d.ts.map +1 -1
- package/dist/regex.js +12 -4
- package/dist/regex.js.map +1 -1
- package/dist/vm/compile.d.ts +5 -1
- package/dist/vm/compile.d.ts.map +1 -1
- package/dist/vm/compile.js +16 -2
- package/dist/vm/compile.js.map +1 -1
- package/dist/vm/pike-vm.d.ts +7 -1
- package/dist/vm/pike-vm.d.ts.map +1 -1
- package/dist/vm/pike-vm.js +29 -4
- package/dist/vm/pike-vm.js.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @ac-kit/format-regex
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a5c4d66: Bound what a pattern may cost. `compileRegex` takes the new `RegexLimits`, and
|
|
8
|
+
`compileProgram` and `execProgram` take the single bound each one enforces —
|
|
9
|
+
`maxProgramSize` and `maxSteps`, as plain numbers.
|
|
10
|
+
|
|
11
|
+
The linear-time matching guarantee was never a guarantee about space. A program
|
|
12
|
+
is sized by the pattern — `a{1,200000}` emits one instruction per repetition
|
|
13
|
+
before a character is read — and a run costs program size × input length, so a
|
|
14
|
+
caller holding a pattern it did not write had a time bound and nothing else.
|
|
15
|
+
|
|
16
|
+
`maxProgramSize` is enforced as the program is built, so it never grows past the
|
|
17
|
+
bound rather than being measured after the fact; `maxSteps` bounds one run.
|
|
18
|
+
Exceeding either throws `RegexLimitExceededError`, which names the bound it hit.
|
|
19
|
+
Both default to `DEFAULT_REGEX_LIMITS`, high enough that no hand-written pattern
|
|
20
|
+
over a realistic input reaches them, and both are options rather than constants
|
|
21
|
+
so a caller holding a trusted pattern can raise them.
|
|
22
|
+
|
|
23
|
+
`RegexLimits` is resolved exactly once, by `compileRegex`. The `test` and `exec`
|
|
24
|
+
closures it returns hold the two resolved bounds, so a match does no option
|
|
25
|
+
handling and allocates no options object before reading a character.
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- Updated dependencies [a5c4d66]
|
|
30
|
+
- Updated dependencies [a5c4d66]
|
|
31
|
+
- Updated dependencies [a5c4d66]
|
|
32
|
+
- @ac-kit/algo@0.2.0
|
|
33
|
+
- @ac-kit/core@0.3.0
|
|
34
|
+
|
|
35
|
+
## 0.1.1
|
|
36
|
+
|
|
37
|
+
### Patch Changes
|
|
38
|
+
|
|
39
|
+
- Updated dependencies [a493b4a]
|
|
40
|
+
- Updated dependencies [a493b4a]
|
|
41
|
+
- Updated dependencies [a493b4a]
|
|
42
|
+
- Updated dependencies [2ab5593]
|
|
43
|
+
- Updated dependencies [a493b4a]
|
|
44
|
+
- @ac-kit/core@0.2.0
|
|
45
|
+
- @ac-kit/algo@0.1.1
|
package/README.md
CHANGED
|
@@ -16,6 +16,23 @@ backtracking), it is immune to catastrophic ("ReDoS") blowup: `(a+)+b` against a
|
|
|
16
16
|
long non-matching input returns in linear time, where a backtracking engine
|
|
17
17
|
(including native `RegExp`) is exponential.
|
|
18
18
|
|
|
19
|
+
## Limits
|
|
20
|
+
|
|
21
|
+
Linear is not the same as bounded. The program is sized by the pattern —
|
|
22
|
+
`a{1,200000}` emits one instruction per repetition before a character is read —
|
|
23
|
+
and the work is program size × input length, so a caller holding neither factor
|
|
24
|
+
needs a ceiling on both. `RegexLimits` supplies it:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
compileRegex("a{1,100}", { maxProgramSize: 16 }); // RegexLimitExceededError
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`maxProgramSize` is checked as the program is emitted, so the array never grows
|
|
31
|
+
past it; `maxSteps` bounds thread activations per run. Both default high enough
|
|
32
|
+
that no hand-written pattern over a realistic input reaches them, and both are
|
|
33
|
+
options rather than constants so a caller holding a trusted pattern can raise
|
|
34
|
+
them.
|
|
35
|
+
|
|
19
36
|
Supported syntax: literals, `.`, `^`/`$` anchors, `[...]`/`[^...]` classes (with
|
|
20
37
|
ranges and `\d\w\s\D\W\S` shorthands, also usable outside classes), `(...)`
|
|
21
38
|
capturing / `(?:...)` non-capturing groups, `|` alternation, and
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC"}
|
package/dist/limits.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounds on what a pattern may cost to compile and to run.
|
|
3
|
+
*
|
|
4
|
+
* The Pike VM gives an unconditional linear _time_ guarantee, but linear in a
|
|
5
|
+
* program the pattern itself sizes: `a{1,200000}` expands to one instruction
|
|
6
|
+
* per repetition before a single character is read. These bounds are the
|
|
7
|
+
* matching _space_ guarantee, and they are what makes it safe to compile a
|
|
8
|
+
* pattern that arrived from outside the program.
|
|
9
|
+
*/
|
|
10
|
+
export type RegexLimits = {
|
|
11
|
+
/**
|
|
12
|
+
* Most instructions a compiled program may hold.
|
|
13
|
+
*
|
|
14
|
+
* Bounds compile-time memory. Counted bounded quantifiers (`{m,n}`) are the
|
|
15
|
+
* construct that reaches it, since each repetition emits its own copy.
|
|
16
|
+
*/
|
|
17
|
+
readonly maxProgramSize?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Most thread activations one {@link execProgram} call may perform.
|
|
20
|
+
*
|
|
21
|
+
* Bounds run-time work, which grows as program size × input length even
|
|
22
|
+
* though neither factor alone is unbounded.
|
|
23
|
+
*/
|
|
24
|
+
readonly maxSteps?: number;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Defaults, chosen so no hand-written pattern over a realistic input reaches
|
|
28
|
+
* them: 64 Ki instructions is about three orders of magnitude above any regex a
|
|
29
|
+
* person writes, and 16 Mi steps covers a 100-instruction program over a 100 Ki
|
|
30
|
+
* character input.
|
|
31
|
+
*/
|
|
32
|
+
export declare const DEFAULT_REGEX_LIMITS: Required<RegexLimits>;
|
|
33
|
+
/** Which bound a {@link RegexLimitExceededError} reports. */
|
|
34
|
+
export type RegexLimitKind = "programSize" | "steps";
|
|
35
|
+
/** Thrown when a pattern exceeds one of the {@link RegexLimits}. */
|
|
36
|
+
export declare class RegexLimitExceededError extends Error {
|
|
37
|
+
readonly kind: RegexLimitKind;
|
|
38
|
+
readonly limit: number;
|
|
39
|
+
constructor(kind: RegexLimitKind, limit: number);
|
|
40
|
+
}
|
|
41
|
+
/** Fills in the unset bounds from {@link DEFAULT_REGEX_LIMITS}. */
|
|
42
|
+
export declare function regexLimits(limits?: RegexLimits): Required<RegexLimits>;
|
|
43
|
+
//# sourceMappingURL=limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.d.ts","sourceRoot":"","sources":["../src/limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB;;;;;OAKG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IAEjC;;;;;OAKG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,WAAW,CAGtD,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,OAAO,CAAC;AAErD,oEAAoE;AACpE,qBAAa,uBAAwB,SAAQ,KAAK;IAEhD,QAAQ,CAAC,IAAI,EAAE,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM;IAFvB,YACU,IAAI,EAAE,cAAc,EACpB,KAAK,EAAE,MAAM,EAQtB;CACD;AAED,mEAAmE;AACnE,wBAAgB,WAAW,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,CAMvE"}
|
package/dist/limits.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defaults, chosen so no hand-written pattern over a realistic input reaches
|
|
3
|
+
* them: 64 Ki instructions is about three orders of magnitude above any regex a
|
|
4
|
+
* person writes, and 16 Mi steps covers a 100-instruction program over a 100 Ki
|
|
5
|
+
* character input.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_REGEX_LIMITS = {
|
|
8
|
+
maxProgramSize: 65536,
|
|
9
|
+
maxSteps: 16777216,
|
|
10
|
+
};
|
|
11
|
+
/** Thrown when a pattern exceeds one of the {@link RegexLimits}. */
|
|
12
|
+
export class RegexLimitExceededError extends Error {
|
|
13
|
+
kind;
|
|
14
|
+
limit;
|
|
15
|
+
constructor(kind, limit) {
|
|
16
|
+
super(kind === "programSize"
|
|
17
|
+
? `Compiled regex program exceeds ${limit} instructions`
|
|
18
|
+
: `Regex execution exceeded ${limit} steps`);
|
|
19
|
+
this.kind = kind;
|
|
20
|
+
this.limit = limit;
|
|
21
|
+
this.name = "RegexLimitExceededError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Fills in the unset bounds from {@link DEFAULT_REGEX_LIMITS}. */
|
|
25
|
+
export function regexLimits(limits) {
|
|
26
|
+
return {
|
|
27
|
+
maxProgramSize: limits?.maxProgramSize ?? DEFAULT_REGEX_LIMITS.maxProgramSize,
|
|
28
|
+
maxSteps: limits?.maxSteps ?? DEFAULT_REGEX_LIMITS.maxSteps,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.js","sourceRoot":"","sources":["../src/limits.ts"],"names":[],"mappings":"AA2BA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAA0B;IAC1D,cAAc,EAAE,KAAM;IACtB,QAAQ,EAAE,QAAU;CACpB,CAAC;AAKF,oEAAoE;AACpE,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAEvC,IAAI;IACJ,KAAK;IAFf,YACU,IAAoB,EACpB,KAAa;QAEtB,KAAK,CACJ,IAAI,KAAK,aAAa;YACrB,CAAC,CAAC,kCAAkC,KAAK,eAAe;YACxD,CAAC,CAAC,4BAA4B,KAAK,QAAQ,CAC5C,CAAC;oBAPO,IAAI;qBACJ,KAAK;QAOd,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACvC,CAAC;CACD;AAED,mEAAmE;AACnE,MAAM,UAAU,WAAW,CAAC,MAAoB;IAC/C,OAAO;QACN,cAAc,EACb,MAAM,EAAE,cAAc,IAAI,oBAAoB,CAAC,cAAc;QAC9D,QAAQ,EAAE,MAAM,EAAE,QAAQ,IAAI,oBAAoB,CAAC,QAAQ;KAC3D,CAAC;AACH,CAAC"}
|
package/dist/regex.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type RegexLimits } from "./limits.js";
|
|
1
2
|
import { type RegexMatch } from "./vm/pike-vm.js";
|
|
2
3
|
/** A parsed and compiled pattern, ready to match repeatedly without re-parsing. */
|
|
3
4
|
export interface CompiledRegex {
|
|
@@ -14,6 +15,10 @@ export interface CompiledRegex {
|
|
|
14
15
|
* const re = compileRegex("(\\d+)-(\\d+)");
|
|
15
16
|
* re.exec("pages 10-20"); // { index: 6, length: 5, text: "10-20", groups: [...] }
|
|
16
17
|
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @throws {RegexLimitExceededError} If the compiled program outgrows
|
|
20
|
+
* {@link RegexLimits.maxProgramSize}. The returned matcher throws the same
|
|
21
|
+
* error if a run outgrows {@link RegexLimits.maxSteps}.
|
|
17
22
|
*/
|
|
18
|
-
export declare function compileRegex(source: string): CompiledRegex;
|
|
23
|
+
export declare function compileRegex(source: string, limits?: RegexLimits): CompiledRegex;
|
|
19
24
|
//# sourceMappingURL=regex.d.ts.map
|
package/dist/regex.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regex.d.ts","sourceRoot":"","sources":["../src/regex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"regex.d.ts","sourceRoot":"","sources":["../src/regex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAe,MAAM,aAAa,CAAC;AAG5D,OAAO,EAAe,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE/D,mFAAmF;AACnF,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAC;CAC5C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,GAClB,aAAa,CAaf"}
|
package/dist/regex.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { regexLimits } from "./limits.js";
|
|
1
2
|
import { parseRegex } from "./parser.js";
|
|
2
3
|
import { compileProgram } from "./vm/compile.js";
|
|
3
4
|
import { execProgram } from "./vm/pike-vm.js";
|
|
@@ -9,15 +10,22 @@ import { execProgram } from "./vm/pike-vm.js";
|
|
|
9
10
|
* const re = compileRegex("(\\d+)-(\\d+)");
|
|
10
11
|
* re.exec("pages 10-20"); // { index: 6, length: 5, text: "10-20", groups: [...] }
|
|
11
12
|
* ```;
|
|
13
|
+
*
|
|
14
|
+
* @throws {RegexLimitExceededError} If the compiled program outgrows
|
|
15
|
+
* {@link RegexLimits.maxProgramSize}. The returned matcher throws the same
|
|
16
|
+
* error if a run outgrows {@link RegexLimits.maxSteps}.
|
|
12
17
|
*/
|
|
13
|
-
export function compileRegex(source) {
|
|
18
|
+
export function compileRegex(source, limits) {
|
|
14
19
|
const pattern = parseRegex(source);
|
|
15
|
-
|
|
20
|
+
// Resolved once, not per call: the matcher closes over the step ceiling
|
|
21
|
+
// rather than over the options object it came from.
|
|
22
|
+
const { maxProgramSize, maxSteps } = regexLimits(limits);
|
|
23
|
+
const program = compileProgram(pattern, maxProgramSize);
|
|
16
24
|
return {
|
|
17
25
|
source,
|
|
18
26
|
groupCount: pattern.groupCount,
|
|
19
|
-
test: (input) => execProgram(program, input) !== undefined,
|
|
20
|
-
exec: (input) => execProgram(program, input),
|
|
27
|
+
test: (input) => execProgram(program, input, maxSteps) !== undefined,
|
|
28
|
+
exec: (input) => execProgram(program, input, maxSteps),
|
|
21
29
|
};
|
|
22
30
|
}
|
|
23
31
|
//# sourceMappingURL=regex.js.map
|
package/dist/regex.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regex.js","sourceRoot":"","sources":["../src/regex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAmB,MAAM,iBAAiB,CAAC;AAU/D
|
|
1
|
+
{"version":3,"file":"regex.js","sourceRoot":"","sources":["../src/regex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,WAAW,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAmB,MAAM,iBAAiB,CAAC;AAU/D;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAC3B,MAAc,EACd,MAAoB;IAEpB,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IACnC,wEAAwE;IACxE,oDAAoD;IACpD,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;IACzD,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAExD,OAAO;QACN,MAAM;QACN,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,SAAS;QACpE,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC;KACtD,CAAC;AACH,CAAC"}
|
package/dist/vm/compile.d.ts
CHANGED
|
@@ -10,6 +10,10 @@ import type { RegexProgram } from "./program.js";
|
|
|
10
10
|
* into the bytecode instead — at the cost of not special-casing patterns that
|
|
11
11
|
* open with `^` (those simply fail `assertStart` at every offset but 0, which
|
|
12
12
|
* is correct, just not the fastest possible instruction count for that case).
|
|
13
|
+
*
|
|
14
|
+
* @param maxProgramSize - Most instructions the program may hold; see
|
|
15
|
+
* `RegexLimits.maxProgramSize`, whose default this takes.
|
|
16
|
+
* @throws {RegexLimitExceededError} If the program outgrows `maxProgramSize`.
|
|
13
17
|
*/
|
|
14
|
-
export declare function compileProgram(pattern: RegexPattern): RegexProgram;
|
|
18
|
+
export declare function compileProgram(pattern: RegexPattern, maxProgramSize?: number): RegexProgram;
|
|
15
19
|
//# sourceMappingURL=compile.d.ts.map
|
package/dist/vm/compile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAGX,YAAY,EAGZ,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAGX,YAAY,EAGZ,MAAM,WAAW,CAAC;AAEnB,OAAO,KAAK,EAAe,YAAY,EAAE,MAAM,cAAc,CAAC;AA8M9D;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,YAAY,EACrB,cAAc,GAAE,MAA4C,GAC1D,YAAY,CAkBd"}
|
package/dist/vm/compile.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isAsciiAlphaNumeric, isAsciiDigit, isAsciiWhitespace, } from "@ac-kit/core";
|
|
2
|
+
import { DEFAULT_REGEX_LIMITS, RegexLimitExceededError } from "../limits.js";
|
|
2
3
|
const isWordChar = (char) => isAsciiAlphaNumeric(char.charCodeAt(0)) || char === "_";
|
|
3
4
|
const SHORTHAND_PREDICATES = {
|
|
4
5
|
d: (char) => isAsciiDigit(char.charCodeAt(0)),
|
|
@@ -40,11 +41,20 @@ function compileCharClassPredicate(negated, items) {
|
|
|
40
41
|
* known.
|
|
41
42
|
*/
|
|
42
43
|
class ProgramBuilder {
|
|
44
|
+
maxProgramSize;
|
|
43
45
|
instructions = [];
|
|
46
|
+
constructor(maxProgramSize) {
|
|
47
|
+
this.maxProgramSize = maxProgramSize;
|
|
48
|
+
}
|
|
44
49
|
get length() {
|
|
45
50
|
return this.instructions.length;
|
|
46
51
|
}
|
|
47
52
|
emit(instruction) {
|
|
53
|
+
// Checked here rather than after building: a counted quantifier emits its
|
|
54
|
+
// copies one at a time, so the array never grows past the bound.
|
|
55
|
+
if (this.instructions.length >= this.maxProgramSize) {
|
|
56
|
+
throw new RegexLimitExceededError("programSize", this.maxProgramSize);
|
|
57
|
+
}
|
|
48
58
|
this.instructions.push(instruction);
|
|
49
59
|
return this.instructions.length - 1;
|
|
50
60
|
}
|
|
@@ -180,9 +190,13 @@ function compileQuantified(builder, node) {
|
|
|
180
190
|
* into the bytecode instead — at the cost of not special-casing patterns that
|
|
181
191
|
* open with `^` (those simply fail `assertStart` at every offset but 0, which
|
|
182
192
|
* is correct, just not the fastest possible instruction count for that case).
|
|
193
|
+
*
|
|
194
|
+
* @param maxProgramSize - Most instructions the program may hold; see
|
|
195
|
+
* `RegexLimits.maxProgramSize`, whose default this takes.
|
|
196
|
+
* @throws {RegexLimitExceededError} If the program outgrows `maxProgramSize`.
|
|
183
197
|
*/
|
|
184
|
-
export function compileProgram(pattern) {
|
|
185
|
-
const builder = new ProgramBuilder();
|
|
198
|
+
export function compileProgram(pattern, maxProgramSize = DEFAULT_REGEX_LIMITS.maxProgramSize) {
|
|
199
|
+
const builder = new ProgramBuilder(maxProgramSize);
|
|
186
200
|
const scanSplitIndex = builder.emit({ op: "split", first: -1, second: -1 });
|
|
187
201
|
const tryStart = builder.length;
|
|
188
202
|
builder.emit({ op: "save", slot: 0 });
|
package/dist/vm/compile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,GACjB,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/vm/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,YAAY,EACZ,iBAAiB,GACjB,MAAM,cAAc,CAAC;AAStB,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAG7E,MAAM,UAAU,GAA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;;;;GAcG;AACH,MAAM,UAAU,cAAc,CAC7B,OAAqB,EACrB,cAAc,GAAW,oBAAoB,CAAC,cAAc;IAE5D,MAAM,OAAO,GAAG,IAAI,cAAc,CAAC,cAAc,CAAC,CAAC;IAEnD,MAAM,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IAC5E,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC;IAChC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;IACtC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;IACtC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;IAC9B,MAAM,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IACpC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;IAChC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC,CAAC;IACpD,OAAO,CAAC,UAAU,CAAC,cAAc,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC;IAE3D,OAAO;QACN,YAAY,EAAE,OAAO,CAAC,cAAc,EAAE;QACtC,UAAU,EAAE,OAAO,CAAC,UAAU;KAC9B,CAAC;AACH,CAAC"}
|
package/dist/vm/pike-vm.d.ts
CHANGED
|
@@ -23,6 +23,12 @@ export interface RegexMatch {
|
|
|
23
23
|
* lockstep, so this is O(program size × input length) with no backtracking — a
|
|
24
24
|
* pathological pattern like `(a+)+b` cannot exhibit catastrophic (ReDoS)
|
|
25
25
|
* behavior here the way it can in a backtracking engine.
|
|
26
|
+
*
|
|
27
|
+
* @param maxSteps - Most thread activations this run may perform; see
|
|
28
|
+
* `RegexLimits.maxSteps`, whose default this takes.
|
|
29
|
+
* @throws {RegexLimitExceededError} If the run outgrows `maxSteps`. Linear is
|
|
30
|
+
* not the same as bounded: the work is program size × input length, and a
|
|
31
|
+
* caller holding neither factor needs a ceiling on the product.
|
|
26
32
|
*/
|
|
27
|
-
export declare function execProgram(program: RegexProgram, input: string): RegexMatch | undefined;
|
|
33
|
+
export declare function execProgram(program: RegexProgram, input: string, maxSteps?: number): RegexMatch | undefined;
|
|
28
34
|
//# sourceMappingURL=pike-vm.d.ts.map
|
package/dist/vm/pike-vm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pike-vm.d.ts","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pike-vm.d.ts","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAe,YAAY,EAAE,MAAM,cAAc,CAAC;AAE9D,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,eAAe,GAAG,SAAS,CAAC,CAAC;CAC5D;AAgJD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAC1B,OAAO,EAAE,YAAY,EACrB,KAAK,EAAE,MAAM,EACb,QAAQ,GAAE,MAAsC,GAC9C,UAAU,GAAG,SAAS,CAuCxB"}
|
package/dist/vm/pike-vm.js
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
|
+
import { DEFAULT_REGEX_LIMITS, RegexLimitExceededError } from "../limits.js";
|
|
2
|
+
/** A run's remaining thread activations, shared by both thread lists. */
|
|
3
|
+
class StepBudget {
|
|
4
|
+
limit;
|
|
5
|
+
remaining;
|
|
6
|
+
constructor(limit) {
|
|
7
|
+
this.limit = limit;
|
|
8
|
+
this.remaining = limit;
|
|
9
|
+
}
|
|
10
|
+
spend() {
|
|
11
|
+
if (this.remaining-- <= 0) {
|
|
12
|
+
throw new RegexLimitExceededError("steps", this.limit);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
1
16
|
/**
|
|
2
17
|
* One run of the Pike VM: tracks which `pc`s have already been added this step,
|
|
3
18
|
* to keep each step O(program size).
|
|
4
19
|
*/
|
|
5
20
|
class ThreadList {
|
|
21
|
+
budget;
|
|
6
22
|
threads = [];
|
|
7
23
|
visitedAt;
|
|
8
24
|
generation = 0;
|
|
9
|
-
constructor(programLength) {
|
|
25
|
+
constructor(programLength, budget) {
|
|
26
|
+
this.budget = budget;
|
|
10
27
|
this.visitedAt = new Int32Array(programLength).fill(-1);
|
|
11
28
|
}
|
|
12
29
|
get all() {
|
|
@@ -27,6 +44,7 @@ class ThreadList {
|
|
|
27
44
|
* terminal instruction is reached.
|
|
28
45
|
*/
|
|
29
46
|
add(program, pc, saved, pos, inputLength) {
|
|
47
|
+
this.budget.spend();
|
|
30
48
|
if (this.hasVisited(pc))
|
|
31
49
|
return;
|
|
32
50
|
const instruction = program.instructions[pc];
|
|
@@ -107,12 +125,19 @@ function extractMatch(saved, input, groupCount) {
|
|
|
107
125
|
* lockstep, so this is O(program size × input length) with no backtracking — a
|
|
108
126
|
* pathological pattern like `(a+)+b` cannot exhibit catastrophic (ReDoS)
|
|
109
127
|
* behavior here the way it can in a backtracking engine.
|
|
128
|
+
*
|
|
129
|
+
* @param maxSteps - Most thread activations this run may perform; see
|
|
130
|
+
* `RegexLimits.maxSteps`, whose default this takes.
|
|
131
|
+
* @throws {RegexLimitExceededError} If the run outgrows `maxSteps`. Linear is
|
|
132
|
+
* not the same as bounded: the work is program size × input length, and a
|
|
133
|
+
* caller holding neither factor needs a ceiling on the product.
|
|
110
134
|
*/
|
|
111
|
-
export function execProgram(program, input) {
|
|
135
|
+
export function execProgram(program, input, maxSteps = DEFAULT_REGEX_LIMITS.maxSteps) {
|
|
112
136
|
const slotCount = (program.groupCount + 1) * 2;
|
|
113
137
|
const inputLength = input.length;
|
|
114
|
-
|
|
115
|
-
let
|
|
138
|
+
const budget = new StepBudget(maxSteps);
|
|
139
|
+
let clist = new ThreadList(program.instructions.length, budget);
|
|
140
|
+
let nlist = new ThreadList(program.instructions.length, budget);
|
|
116
141
|
clist.add(program, 0, Array.from({ length: slotCount }, () => -1), 0, inputLength);
|
|
117
142
|
let matched;
|
|
118
143
|
for (let pos = 0; pos <= inputLength; pos++) {
|
package/dist/vm/pike-vm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pike-vm.js","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pike-vm.js","sourceRoot":"","sources":["../../src/vm/pike-vm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAyB7E,yEAAyE;AACzE,MAAM,UAAU;IAGc,KAAK;IAF1B,SAAS,CAAS;IAE1B,YAA6B,KAAa;qBAAb,KAAK;QACjC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,KAAK;QACJ,IAAI,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,uBAAuB,CAAC,OAAO,EAAE,IAAI,CAAC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C,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,CAAC,EAAE,WAAW,CAAC,CAAC;YACvE,CAAC;QACF,CAAC;QAED,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,OAAO,KAAK,SAAS;QAC3B,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;AACrD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/format-regex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A regex-dialect lexer, parser and printer, with a Thompson-construction and Pike-VM matcher.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"automaton",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/algo": "^0.
|
|
49
|
-
"@ac-kit/core": "^0.
|
|
48
|
+
"@ac-kit/algo": "^0.2.0",
|
|
49
|
+
"@ac-kit/core": "^0.3.0",
|
|
50
50
|
"@types/node": "^26.5.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@ac-kit/tsconfig": "0.1.0",
|
|
54
54
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
|
-
"@ac-kit/vitest-config": "0.1.
|
|
55
|
+
"@ac-kit/vitest-config": "0.1.1",
|
|
56
56
|
"@typescript/native": "typescript@7.0.2",
|
|
57
57
|
"rimraf": "6.1.3",
|
|
58
58
|
"typedoc": "0.28.20",
|