@ac-kit/format-regex 0.3.2 → 0.4.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 +80 -0
- package/README.md +47 -0
- package/dist/_iregexp-parse.d.ts.map +1 -1
- package/dist/_iregexp-parse.js +12 -6
- package/dist/_iregexp-parse.js.map +1 -1
- package/dist/_unicode-category.d.ts +5 -0
- package/dist/_unicode-category.d.ts.map +1 -1
- package/dist/_unicode-category.js +5 -0
- package/dist/_unicode-category.js.map +1 -1
- package/dist/code-point-class.d.ts +46 -0
- package/dist/code-point-class.d.ts.map +1 -0
- package/dist/code-point-class.js +50 -0
- package/dist/code-point-class.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/iregexp.d.ts.map +1 -1
- package/dist/iregexp.js +2 -1
- package/dist/iregexp.js.map +1 -1
- package/dist/lexer.d.ts.map +1 -1
- package/dist/lexer.js +26 -25
- package/dist/lexer.js.map +1 -1
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +36 -34
- package/dist/parser.js.map +1 -1
- package/dist/printer.d.ts.map +1 -1
- package/dist/printer.js +12 -6
- package/dist/printer.js.map +1 -1
- package/dist/regex-tree.d.ts +167 -0
- package/dist/regex-tree.d.ts.map +1 -0
- package/dist/regex-tree.js +209 -0
- package/dist/regex-tree.js.map +1 -0
- package/dist/token.d.ts +20 -20
- package/dist/token.d.ts.map +1 -1
- package/dist/token.js +20 -20
- package/dist/token.js.map +1 -1
- package/dist/unicode-general-category.d.ts +39 -0
- package/dist/unicode-general-category.d.ts.map +1 -0
- package/dist/unicode-general-category.js +44 -0
- package/dist/unicode-general-category.js.map +1 -0
- package/dist/vm/_class-predicate.d.ts +23 -10
- package/dist/vm/_class-predicate.d.ts.map +1 -1
- package/dist/vm/_class-predicate.js +45 -28
- package/dist/vm/_class-predicate.js.map +1 -1
- package/dist/vm/_thompson.d.ts.map +1 -1
- package/dist/vm/_thompson.js +12 -6
- package/dist/vm/_thompson.js.map +1 -1
- package/dist/vm/compile.d.ts.map +1 -1
- package/dist/vm/compile.js +2 -1
- package/dist/vm/compile.js.map +1 -1
- package/dist/vm/pike-vm.d.ts +2 -1
- package/dist/vm/pike-vm.d.ts.map +1 -1
- package/dist/vm/pike-vm.js +61 -12
- package/dist/vm/pike-vm.js.map +1 -1
- package/dist/vm/program.d.ts +19 -0
- package/dist/vm/program.d.ts.map +1 -1
- package/dist/walk.d.ts +2 -1
- package/dist/walk.d.ts.map +1 -1
- package/dist/walk.js.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { type Predicate, type WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { type RegexLimits } from "./limits.js";
|
|
3
|
+
/**
|
|
4
|
+
* Matches `text` exactly, code point by code point; `""` matches the empty
|
|
5
|
+
* string.
|
|
6
|
+
*/
|
|
7
|
+
export interface RegexTreeLiteral {
|
|
8
|
+
readonly kind: "literal";
|
|
9
|
+
readonly text: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Matches one code point `test` accepts: a character class, a named set, a
|
|
13
|
+
* Unicode general category, anything a caller can decide one code point at a
|
|
14
|
+
* time.
|
|
15
|
+
*/
|
|
16
|
+
export interface RegexTreePredicate {
|
|
17
|
+
readonly kind: "predicate";
|
|
18
|
+
/**
|
|
19
|
+
* Receives a code point, or the code unit of a lone surrogate. Must not throw
|
|
20
|
+
* for a reason the caller wants to tell apart from the run's own: an error
|
|
21
|
+
* propagates out of the match unchanged.
|
|
22
|
+
*/
|
|
23
|
+
readonly test: Predicate<[codePoint: number]>;
|
|
24
|
+
/**
|
|
25
|
+
* Work units charged to the run each time `test` is called, beyond the one
|
|
26
|
+
* every thread step pays: for a test whose work grows with what it was built
|
|
27
|
+
* from (a large set), so the run's bound still bounds its time. A
|
|
28
|
+
* non-negative integer; `0` when absent.
|
|
29
|
+
*/
|
|
30
|
+
readonly cost?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Matches the empty string at a word boundary: where `isWord` answers
|
|
34
|
+
* differently for the code points on the two sides, the positions before the
|
|
35
|
+
* input and after it counting as not part of a word. With `negated`, matches
|
|
36
|
+
* where it answers the same.
|
|
37
|
+
*/
|
|
38
|
+
export interface RegexTreeWordBoundary {
|
|
39
|
+
readonly kind: "wordBoundary";
|
|
40
|
+
/** Whether a code point (or a lone surrogate's code unit) is part of a word. */
|
|
41
|
+
readonly isWord: Predicate<[codePoint: number]>;
|
|
42
|
+
/** `true` for a non-boundary (`\B`); `false` when absent. */
|
|
43
|
+
readonly negated?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Work units charged each time the assertion is evaluated, as
|
|
46
|
+
* {@link RegexTreePredicate.cost}. `0` when absent.
|
|
47
|
+
*/
|
|
48
|
+
readonly cost?: number;
|
|
49
|
+
}
|
|
50
|
+
/** Matches its items one after the other; no items matches the empty string. */
|
|
51
|
+
export interface RegexTreeSequence {
|
|
52
|
+
readonly kind: "sequence";
|
|
53
|
+
readonly items: readonly AnyRegexTreeNode[];
|
|
54
|
+
}
|
|
55
|
+
/** Matches any of its alternatives; no alternatives matches nothing at all. */
|
|
56
|
+
export interface RegexTreeChoice {
|
|
57
|
+
readonly kind: "choice";
|
|
58
|
+
readonly alternatives: readonly AnyRegexTreeNode[];
|
|
59
|
+
}
|
|
60
|
+
/** Matches `body` at least `min` and at most `max` times. */
|
|
61
|
+
export interface RegexTreeRepeat {
|
|
62
|
+
readonly kind: "repeat";
|
|
63
|
+
readonly body: AnyRegexTreeNode;
|
|
64
|
+
/** A non-negative safe integer. */
|
|
65
|
+
readonly min: number;
|
|
66
|
+
/**
|
|
67
|
+
* A safe integer no smaller than `min`; absent or `Infinity` for no upper
|
|
68
|
+
* bound.
|
|
69
|
+
*/
|
|
70
|
+
readonly max?: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A node of a caller-built expression tree for {@link compileRegexTree}: three
|
|
74
|
+
* atoms (a literal, a code-point predicate, a word-boundary assertion) and
|
|
75
|
+
* three operators (sequence, choice, repetition).
|
|
76
|
+
*
|
|
77
|
+
* Nodes may be shared: a subtree used in several places is lowered once per
|
|
78
|
+
* use.
|
|
79
|
+
*/
|
|
80
|
+
export type AnyRegexTreeNode = RegexTreeLiteral | RegexTreePredicate | RegexTreeWordBoundary | RegexTreeSequence | RegexTreeChoice | RegexTreeRepeat;
|
|
81
|
+
/** A compiled {@link AnyRegexTreeNode}, ready to test strings repeatedly. */
|
|
82
|
+
export interface CompiledRegexTree {
|
|
83
|
+
/**
|
|
84
|
+
* Whether the whole of `input` matches the tree. Unary and unbound, so
|
|
85
|
+
* `texts.filter(tree.match)` works.
|
|
86
|
+
*
|
|
87
|
+
* O(program size × input code points) time, O(program size) memory.
|
|
88
|
+
*
|
|
89
|
+
* @throws {RegexLimitExceededError} If the run outgrows
|
|
90
|
+
* {@link RegexLimits.maxSteps}.
|
|
91
|
+
* @throws Whatever a predicate of the tree throws.
|
|
92
|
+
*/
|
|
93
|
+
readonly match: (input: string) => boolean;
|
|
94
|
+
/**
|
|
95
|
+
* {@link CompiledRegexTree.match}, charged to `budget` instead of a fresh
|
|
96
|
+
* allowance of {@link RegexLimits.maxSteps}.
|
|
97
|
+
*
|
|
98
|
+
* @throws Whatever `budget.spend` or a predicate of the tree throws.
|
|
99
|
+
*/
|
|
100
|
+
readonly matchWithin: (input: string, budget: WorkBudget) => boolean;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Compiles a caller-built expression tree onto this package's Pike VM, to test
|
|
104
|
+
* whether a whole string matches it.
|
|
105
|
+
*
|
|
106
|
+
* The entry point for a front end of another regular-expression notation: the
|
|
107
|
+
* front end parses its own syntax into an {@link AnyRegexTreeNode} and supplies
|
|
108
|
+
* whatever it means by a character class, a named set or a Unicode general
|
|
109
|
+
* category (`isInUnicodeGeneralCategory`) as a code-point predicate, and its
|
|
110
|
+
* own idea of a word as the word boundary's `isWord`. The tree is lowered by
|
|
111
|
+
* the same Thompson construction as {@link compileRegex} and
|
|
112
|
+
* {@link compileIRegexp}.
|
|
113
|
+
*
|
|
114
|
+
* Contract:
|
|
115
|
+
*
|
|
116
|
+
* - Matching is by code point: an astral character is one step and reaches a
|
|
117
|
+
* predicate whole; a lone surrogate in the input is one step of its own.
|
|
118
|
+
* - The match is anchored at both ends: the whole input must match, as if the
|
|
119
|
+
* tree were wrapped in `^(?:…)$`. Wrap it in a repetition of an always-true
|
|
120
|
+
* predicate on either side to search instead.
|
|
121
|
+
* - Linear and never backtracking, whatever the tree: every run is O(program size
|
|
122
|
+
* × input code points) time and O(program size) memory, where the program
|
|
123
|
+
* holds about one instruction per atom and two per operator, a counted
|
|
124
|
+
* repetition copying its body once per mandatory or optional copy.
|
|
125
|
+
* - Iterative: no depth of tree, width of choice or repetition count grows the
|
|
126
|
+
* JavaScript stack. A tree that contains itself is lowered until a limit
|
|
127
|
+
* stops it.
|
|
128
|
+
* - No captures and no laziness: only whether the input matches is answered.
|
|
129
|
+
*
|
|
130
|
+
* Limits: the program is bounded by {@link RegexLimits.maxProgramSize} (its
|
|
131
|
+
* states, checked as each is emitted), the work of compiling by
|
|
132
|
+
* {@link RegexLimits.maxSteps} or `budget` (one unit per node lowered and per
|
|
133
|
+
* jump patched, a literal by its length), and each run by
|
|
134
|
+
* {@link RegexLimits.maxSteps} or the budget given to `matchWithin`.
|
|
135
|
+
* `maxNestingDepth` does not apply: nothing walks the tree recursively. A run
|
|
136
|
+
* is synchronous, so it takes no `AbortSignal`; a caller needing a deadline
|
|
137
|
+
* passes `matchWithin` a `WorkBudget` whose `spend` checks the clock.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const digit = { kind: "predicate", test: (c: number) => c >= 0x30 && c <= 0x39 } as const;
|
|
142
|
+
* const phone = compileRegexTree({
|
|
143
|
+
* kind: "sequence",
|
|
144
|
+
* items: [
|
|
145
|
+
* { kind: "repeat", body: digit, min: 3, max: 3 },
|
|
146
|
+
* { kind: "literal", text: "-" },
|
|
147
|
+
* { kind: "repeat", body: digit, min: 4, max: 4 },
|
|
148
|
+
* ],
|
|
149
|
+
* });
|
|
150
|
+
* phone.match("555-1212"); // true
|
|
151
|
+
* phone.match("call 555-1212"); // false: the whole string must match
|
|
152
|
+
* ```;
|
|
153
|
+
*
|
|
154
|
+
* @param limits - Bounds on the program and on each run; see
|
|
155
|
+
* {@link RegexLimits}.
|
|
156
|
+
* @param budget - Charged for the compilation instead of a fresh allowance of
|
|
157
|
+
* {@link RegexLimits.maxSteps}.
|
|
158
|
+
* @throws {RegexLimitExceededError} If the program outgrows
|
|
159
|
+
* {@link RegexLimits.maxProgramSize}, or compiling outgrows
|
|
160
|
+
* {@link RegexLimits.maxSteps}.
|
|
161
|
+
* @throws {TypeError} If a node is not one of the tree's kinds, or a field has
|
|
162
|
+
* the wrong type.
|
|
163
|
+
* @throws {RangeError} If a repetition's bounds or a cost are not valid.
|
|
164
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
165
|
+
*/
|
|
166
|
+
export declare function compileRegexTree(tree: AnyRegexTreeNode, limits?: RegexLimits, budget?: WorkBudget): CompiledRegexTree;
|
|
167
|
+
//# sourceMappingURL=regex-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regex-tree.d.ts","sourceRoot":"","sources":["../src/regex-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAkB,MAAM,cAAc,CAAC;AAE/E,OAAO,EAAE,KAAK,WAAW,EAA+B,MAAM,aAAa,CAAC;AAS5E;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAChC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED;;;;GAIG;AAC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AAe,CAAC;AAEnB,6EAA6E;AAC7E,MAAM,WAAW,iBAAiB;IACjC;;;;;;;;;OASG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAE3C;;;;;OAKG;IACH,QAAQ,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC;CACrE;AAwKD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,gBAAgB,EACtB,MAAM,CAAC,EAAE,WAAW,EACpB,MAAM,CAAC,EAAE,UAAU,GACjB,iBAAiB,CAWnB"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { withWorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { regexLimits, stepsExceeded_ } from "./limits.js";
|
|
3
|
+
import { emitThompson, ProgramBuilder, } from "./vm/_thompson.js";
|
|
4
|
+
import { testProgram } from "./vm/pike-vm.js";
|
|
5
|
+
/** Marks the nodes the lowering makes up, which no caller can build. */
|
|
6
|
+
const EMIT_ = Symbol("emit");
|
|
7
|
+
const emitNode_ = (instruction) => ({
|
|
8
|
+
kind: EMIT_,
|
|
9
|
+
shape: { kind: "emit", instruction },
|
|
10
|
+
});
|
|
11
|
+
/** What an empty choice lowers to: a step no unit passes. */
|
|
12
|
+
const NOTHING_ = {
|
|
13
|
+
kind: "emit",
|
|
14
|
+
instruction: { op: "predicate", test: () => false, label: "(?!)" },
|
|
15
|
+
};
|
|
16
|
+
function checkCost_(cost) {
|
|
17
|
+
if (cost !== undefined && !(Number.isSafeInteger(cost) && cost >= 0)) {
|
|
18
|
+
throw new RangeError("A cost must be a non-negative safe integer");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function checkNodes_(nodes, what) {
|
|
22
|
+
if (!Array.isArray(nodes)) {
|
|
23
|
+
throw new TypeError(`A regex tree ${what} must be an array`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function repeatShape_(node) {
|
|
27
|
+
const { min, max } = node;
|
|
28
|
+
if (!(Number.isSafeInteger(min) && min >= 0)) {
|
|
29
|
+
throw new RangeError("A repeat's min must be a non-negative safe integer");
|
|
30
|
+
}
|
|
31
|
+
if (max !== undefined &&
|
|
32
|
+
max !== Infinity &&
|
|
33
|
+
!(Number.isSafeInteger(max) && max >= min)) {
|
|
34
|
+
throw new RangeError("A repeat's max must be a safe integer no smaller than min, or Infinity");
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
kind: "repeat",
|
|
38
|
+
body: node.body,
|
|
39
|
+
min,
|
|
40
|
+
max: max === Infinity ? undefined : max,
|
|
41
|
+
lazy: false,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function atomShape_(node, budget) {
|
|
45
|
+
switch (node.kind) {
|
|
46
|
+
case "literal": {
|
|
47
|
+
if (typeof node.text !== "string") {
|
|
48
|
+
throw new TypeError("A regex tree literal's text must be a string");
|
|
49
|
+
}
|
|
50
|
+
// Charged before the code points are read.
|
|
51
|
+
budget.spend(node.text.length);
|
|
52
|
+
const items = [];
|
|
53
|
+
for (const character of node.text) {
|
|
54
|
+
items.push(emitNode_({ op: "char", char: character }));
|
|
55
|
+
}
|
|
56
|
+
return items.length === 1 ? items[0].shape : { kind: "sequence", items };
|
|
57
|
+
}
|
|
58
|
+
case "predicate": {
|
|
59
|
+
const { test, cost } = node;
|
|
60
|
+
if (typeof test !== "function") {
|
|
61
|
+
throw new TypeError("A regex tree predicate's test must be a function");
|
|
62
|
+
}
|
|
63
|
+
checkCost_(cost);
|
|
64
|
+
return {
|
|
65
|
+
kind: "emit",
|
|
66
|
+
instruction: {
|
|
67
|
+
op: "predicate",
|
|
68
|
+
test: (unit) => test(unit.codePointAt(0)),
|
|
69
|
+
label: "predicate",
|
|
70
|
+
cost: cost ?? 0,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
case "wordBoundary": {
|
|
75
|
+
if (typeof node.isWord !== "function") {
|
|
76
|
+
throw new TypeError("A regex tree word boundary's isWord must be a function");
|
|
77
|
+
}
|
|
78
|
+
checkCost_(node.cost);
|
|
79
|
+
return {
|
|
80
|
+
kind: "emit",
|
|
81
|
+
instruction: {
|
|
82
|
+
op: "assertWordBoundary",
|
|
83
|
+
isWord: node.isWord,
|
|
84
|
+
negated: node.negated === true,
|
|
85
|
+
cost: node.cost ?? 0,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The lowering of one compilation: an atom met again (a shared subtree, a
|
|
93
|
+
* counted repetition's copies) reuses its shape rather than rebuilding it.
|
|
94
|
+
*/
|
|
95
|
+
function shapeOfTreeNode_() {
|
|
96
|
+
const atoms = new Map();
|
|
97
|
+
return (node, budget) => {
|
|
98
|
+
switch (node.kind) {
|
|
99
|
+
case EMIT_:
|
|
100
|
+
return node.shape;
|
|
101
|
+
case "sequence":
|
|
102
|
+
checkNodes_(node.items, "sequence's items");
|
|
103
|
+
return { kind: "sequence", items: node.items };
|
|
104
|
+
case "choice":
|
|
105
|
+
checkNodes_(node.alternatives, "choice's alternatives");
|
|
106
|
+
return node.alternatives.length === 0
|
|
107
|
+
? NOTHING_
|
|
108
|
+
: { kind: "choice", alternatives: node.alternatives };
|
|
109
|
+
case "repeat":
|
|
110
|
+
return repeatShape_(node);
|
|
111
|
+
case "literal":
|
|
112
|
+
case "predicate":
|
|
113
|
+
case "wordBoundary": {
|
|
114
|
+
let shape = atoms.get(node);
|
|
115
|
+
if (shape === undefined) {
|
|
116
|
+
shape = atomShape_(node, budget);
|
|
117
|
+
atoms.set(node, shape);
|
|
118
|
+
}
|
|
119
|
+
return shape;
|
|
120
|
+
}
|
|
121
|
+
default:
|
|
122
|
+
throw new TypeError(`Unknown regex tree node kind ${String(node.kind)}`);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function compileTreeProgram_(tree, maxProgramSize, budget) {
|
|
127
|
+
const builder = new ProgramBuilder(maxProgramSize);
|
|
128
|
+
emitThompson(builder, tree, shapeOfTreeNode_(), budget);
|
|
129
|
+
builder.emit({ op: "assertEnd" });
|
|
130
|
+
builder.emit({ op: "match" });
|
|
131
|
+
return {
|
|
132
|
+
instructions: builder.toInstructions(),
|
|
133
|
+
groupCount: 0,
|
|
134
|
+
unit: "codePoint",
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Compiles a caller-built expression tree onto this package's Pike VM, to test
|
|
139
|
+
* whether a whole string matches it.
|
|
140
|
+
*
|
|
141
|
+
* The entry point for a front end of another regular-expression notation: the
|
|
142
|
+
* front end parses its own syntax into an {@link AnyRegexTreeNode} and supplies
|
|
143
|
+
* whatever it means by a character class, a named set or a Unicode general
|
|
144
|
+
* category (`isInUnicodeGeneralCategory`) as a code-point predicate, and its
|
|
145
|
+
* own idea of a word as the word boundary's `isWord`. The tree is lowered by
|
|
146
|
+
* the same Thompson construction as {@link compileRegex} and
|
|
147
|
+
* {@link compileIRegexp}.
|
|
148
|
+
*
|
|
149
|
+
* Contract:
|
|
150
|
+
*
|
|
151
|
+
* - Matching is by code point: an astral character is one step and reaches a
|
|
152
|
+
* predicate whole; a lone surrogate in the input is one step of its own.
|
|
153
|
+
* - The match is anchored at both ends: the whole input must match, as if the
|
|
154
|
+
* tree were wrapped in `^(?:…)$`. Wrap it in a repetition of an always-true
|
|
155
|
+
* predicate on either side to search instead.
|
|
156
|
+
* - Linear and never backtracking, whatever the tree: every run is O(program size
|
|
157
|
+
* × input code points) time and O(program size) memory, where the program
|
|
158
|
+
* holds about one instruction per atom and two per operator, a counted
|
|
159
|
+
* repetition copying its body once per mandatory or optional copy.
|
|
160
|
+
* - Iterative: no depth of tree, width of choice or repetition count grows the
|
|
161
|
+
* JavaScript stack. A tree that contains itself is lowered until a limit
|
|
162
|
+
* stops it.
|
|
163
|
+
* - No captures and no laziness: only whether the input matches is answered.
|
|
164
|
+
*
|
|
165
|
+
* Limits: the program is bounded by {@link RegexLimits.maxProgramSize} (its
|
|
166
|
+
* states, checked as each is emitted), the work of compiling by
|
|
167
|
+
* {@link RegexLimits.maxSteps} or `budget` (one unit per node lowered and per
|
|
168
|
+
* jump patched, a literal by its length), and each run by
|
|
169
|
+
* {@link RegexLimits.maxSteps} or the budget given to `matchWithin`.
|
|
170
|
+
* `maxNestingDepth` does not apply: nothing walks the tree recursively. A run
|
|
171
|
+
* is synchronous, so it takes no `AbortSignal`; a caller needing a deadline
|
|
172
|
+
* passes `matchWithin` a `WorkBudget` whose `spend` checks the clock.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* const digit = { kind: "predicate", test: (c: number) => c >= 0x30 && c <= 0x39 } as const;
|
|
177
|
+
* const phone = compileRegexTree({
|
|
178
|
+
* kind: "sequence",
|
|
179
|
+
* items: [
|
|
180
|
+
* { kind: "repeat", body: digit, min: 3, max: 3 },
|
|
181
|
+
* { kind: "literal", text: "-" },
|
|
182
|
+
* { kind: "repeat", body: digit, min: 4, max: 4 },
|
|
183
|
+
* ],
|
|
184
|
+
* });
|
|
185
|
+
* phone.match("555-1212"); // true
|
|
186
|
+
* phone.match("call 555-1212"); // false: the whole string must match
|
|
187
|
+
* ```;
|
|
188
|
+
*
|
|
189
|
+
* @param limits - Bounds on the program and on each run; see
|
|
190
|
+
* {@link RegexLimits}.
|
|
191
|
+
* @param budget - Charged for the compilation instead of a fresh allowance of
|
|
192
|
+
* {@link RegexLimits.maxSteps}.
|
|
193
|
+
* @throws {RegexLimitExceededError} If the program outgrows
|
|
194
|
+
* {@link RegexLimits.maxProgramSize}, or compiling outgrows
|
|
195
|
+
* {@link RegexLimits.maxSteps}.
|
|
196
|
+
* @throws {TypeError} If a node is not one of the tree's kinds, or a field has
|
|
197
|
+
* the wrong type.
|
|
198
|
+
* @throws {RangeError} If a repetition's bounds or a cost are not valid.
|
|
199
|
+
* @throws Whatever a caller's own `budget.spend` throws.
|
|
200
|
+
*/
|
|
201
|
+
export function compileRegexTree(tree, limits, budget) {
|
|
202
|
+
const { maxProgramSize, maxSteps } = regexLimits(limits);
|
|
203
|
+
const program = withWorkBudget(budget ?? maxSteps, (work) => compileTreeProgram_(tree, maxProgramSize, work), stepsExceeded_);
|
|
204
|
+
return {
|
|
205
|
+
match: (input) => testProgram(program, input, maxSteps),
|
|
206
|
+
matchWithin: (input, runBudget) => testProgram(program, input, runBudget),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=regex-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regex-tree.js","sourceRoot":"","sources":["../src/regex-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,cAAc,EAAE,MAAM,cAAc,CAAC;AAE/E,OAAO,EAAoB,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EACN,YAAY,EACZ,cAAc,GAEd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAqH9C,wEAAwE;AACxE,MAAM,KAAK,GAAkB,MAAM,CAAC,MAAM,CAAC,CAAC;AAS5C,MAAM,SAAS,GAAG,CAAC,WAAwB,EAAa,EAAE,CAAC,CAAC;IAC3D,IAAI,EAAE,KAAK;IACX,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE;CACpC,CAAC,CAAC;AAEH,6DAA6D;AAC7D,MAAM,QAAQ,GAAyB;IACtC,IAAI,EAAE,MAAM;IACZ,WAAW,EAAE,EAAE,EAAE,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE;CAClE,CAAC;AAEF,SAAS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KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC;QACvD,WAAW,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,SAAS,CAAC;KACzE,CAAC;AACH,CAAC"}
|
package/dist/token.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import type { Span } from "@ac-kit/core";
|
|
2
2
|
export declare enum TokenKind {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
3
|
+
CHAR = 0,
|
|
4
|
+
SHORTHAND = 1,
|
|
5
|
+
DOT = 2,
|
|
6
|
+
CARET = 3,
|
|
7
|
+
DOLLAR = 4,
|
|
8
|
+
STAR = 5,
|
|
9
|
+
PLUS = 6,
|
|
10
|
+
QUESTION = 7,
|
|
11
|
+
PIPE = 8,
|
|
12
|
+
LEFT_PAREN = 9,
|
|
13
|
+
NON_CAPTURING_LEFT_PAREN = 10,
|
|
14
|
+
RIGHT_PAREN = 11,
|
|
15
|
+
LEFT_BRACKET = 12,
|
|
16
|
+
RIGHT_BRACKET = 13,
|
|
17
|
+
HYPHEN = 14,
|
|
18
|
+
LEFT_BRACE = 15,
|
|
19
|
+
RIGHT_BRACE = 16,
|
|
20
|
+
COMMA = 17,
|
|
21
|
+
NUMBER = 18,
|
|
22
|
+
END_OF_PATTERN = 19
|
|
23
23
|
}
|
|
24
24
|
export interface Token {
|
|
25
25
|
readonly kind: TokenKind;
|
package/dist/token.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEzC,oBAAY,SAAS;IACpB,IAAI,IAAA;IACJ,SAAS,IAAA;IACT,GAAG,IAAA;IACH,KAAK,IAAA;IACL,MAAM,IAAA;IACN,IAAI,IAAA;IACJ,IAAI,IAAA;IACJ,QAAQ,IAAA;IACR,IAAI,IAAA;IACJ,
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEzC,oBAAY,SAAS;IACpB,IAAI,IAAA;IACJ,SAAS,IAAA;IACT,GAAG,IAAA;IACH,KAAK,IAAA;IACL,MAAM,IAAA;IACN,IAAI,IAAA;IACJ,IAAI,IAAA;IACJ,QAAQ,IAAA;IACR,IAAI,IAAA;IACJ,UAAU,IAAA;IACV,wBAAwB,KAAA;IACxB,WAAW,KAAA;IACX,YAAY,KAAA;IACZ,aAAa,KAAA;IACb,MAAM,KAAA;IACN,UAAU,KAAA;IACV,WAAW,KAAA;IACX,KAAK,KAAA;IACL,MAAM,KAAA;IACN,cAAc,KAAA;CACd;AAED,MAAM,WAAW,KAAK;IACrB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;CACpB"}
|
package/dist/token.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
export var TokenKind;
|
|
2
2
|
(function (TokenKind) {
|
|
3
|
-
TokenKind[TokenKind["
|
|
4
|
-
TokenKind[TokenKind["
|
|
5
|
-
TokenKind[TokenKind["
|
|
6
|
-
TokenKind[TokenKind["
|
|
7
|
-
TokenKind[TokenKind["
|
|
8
|
-
TokenKind[TokenKind["
|
|
9
|
-
TokenKind[TokenKind["
|
|
10
|
-
TokenKind[TokenKind["
|
|
11
|
-
TokenKind[TokenKind["
|
|
12
|
-
TokenKind[TokenKind["
|
|
13
|
-
TokenKind[TokenKind["
|
|
14
|
-
TokenKind[TokenKind["
|
|
15
|
-
TokenKind[TokenKind["
|
|
16
|
-
TokenKind[TokenKind["
|
|
17
|
-
TokenKind[TokenKind["
|
|
18
|
-
TokenKind[TokenKind["
|
|
19
|
-
TokenKind[TokenKind["
|
|
20
|
-
TokenKind[TokenKind["
|
|
21
|
-
TokenKind[TokenKind["
|
|
22
|
-
TokenKind[TokenKind["
|
|
3
|
+
TokenKind[TokenKind["CHAR"] = 0] = "CHAR";
|
|
4
|
+
TokenKind[TokenKind["SHORTHAND"] = 1] = "SHORTHAND";
|
|
5
|
+
TokenKind[TokenKind["DOT"] = 2] = "DOT";
|
|
6
|
+
TokenKind[TokenKind["CARET"] = 3] = "CARET";
|
|
7
|
+
TokenKind[TokenKind["DOLLAR"] = 4] = "DOLLAR";
|
|
8
|
+
TokenKind[TokenKind["STAR"] = 5] = "STAR";
|
|
9
|
+
TokenKind[TokenKind["PLUS"] = 6] = "PLUS";
|
|
10
|
+
TokenKind[TokenKind["QUESTION"] = 7] = "QUESTION";
|
|
11
|
+
TokenKind[TokenKind["PIPE"] = 8] = "PIPE";
|
|
12
|
+
TokenKind[TokenKind["LEFT_PAREN"] = 9] = "LEFT_PAREN";
|
|
13
|
+
TokenKind[TokenKind["NON_CAPTURING_LEFT_PAREN"] = 10] = "NON_CAPTURING_LEFT_PAREN";
|
|
14
|
+
TokenKind[TokenKind["RIGHT_PAREN"] = 11] = "RIGHT_PAREN";
|
|
15
|
+
TokenKind[TokenKind["LEFT_BRACKET"] = 12] = "LEFT_BRACKET";
|
|
16
|
+
TokenKind[TokenKind["RIGHT_BRACKET"] = 13] = "RIGHT_BRACKET";
|
|
17
|
+
TokenKind[TokenKind["HYPHEN"] = 14] = "HYPHEN";
|
|
18
|
+
TokenKind[TokenKind["LEFT_BRACE"] = 15] = "LEFT_BRACE";
|
|
19
|
+
TokenKind[TokenKind["RIGHT_BRACE"] = 16] = "RIGHT_BRACE";
|
|
20
|
+
TokenKind[TokenKind["COMMA"] = 17] = "COMMA";
|
|
21
|
+
TokenKind[TokenKind["NUMBER"] = 18] = "NUMBER";
|
|
22
|
+
TokenKind[TokenKind["END_OF_PATTERN"] = 19] = "END_OF_PATTERN";
|
|
23
23
|
})(TokenKind || (TokenKind = {}));
|
|
24
24
|
//# sourceMappingURL=token.js.map
|
package/dist/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAEA,MAAM,CAAN,IAAY,SAqBX;AArBD,WAAY,SAAS;IACpB,yCAAI,CAAA;IACJ,mDAAS,CAAA;IACT,uCAAG,CAAA;IACH,2CAAK,CAAA;IACL,6CAAM,CAAA;IACN,yCAAI,CAAA;IACJ,yCAAI,CAAA;IACJ,iDAAQ,CAAA;IACR,yCAAI,CAAA;IACJ,
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAEA,MAAM,CAAN,IAAY,SAqBX;AArBD,WAAY,SAAS;IACpB,yCAAI,CAAA;IACJ,mDAAS,CAAA;IACT,uCAAG,CAAA;IACH,2CAAK,CAAA;IACL,6CAAM,CAAA;IACN,yCAAI,CAAA;IACJ,yCAAI,CAAA;IACJ,iDAAQ,CAAA;IACR,yCAAI,CAAA;IACJ,qDAAU,CAAA;IACV,kFAAwB,CAAA;IACxB,wDAAW,CAAA;IACX,0DAAY,CAAA;IACZ,4DAAa,CAAA;IACb,8CAAM,CAAA;IACN,sDAAU,CAAA;IACV,wDAAW,CAAA;IACX,4CAAK,CAAA;IACL,8CAAM,CAAA;IACN,8DAAc,CAAA;AACf,CAAC,EArBW,SAAS,KAAT,SAAS,QAqBpB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Unicode general category (The Unicode Standard §4.5), by its abbreviation:
|
|
3
|
+
* a main class (`L`, `M`, `N`, `P`, `S`, `Z`, `C`) or one of its subclasses
|
|
4
|
+
* (`Lu`, `Nd`, `Cs`, …).
|
|
5
|
+
*/
|
|
6
|
+
export type UnicodeGeneralCategory = "L" | "Lu" | "Ll" | "Lt" | "Lm" | "Lo" | "M" | "Mn" | "Mc" | "Me" | "N" | "Nd" | "Nl" | "No" | "P" | "Pc" | "Pd" | "Ps" | "Pe" | "Pi" | "Pf" | "Po" | "S" | "Sm" | "Sc" | "Sk" | "So" | "Z" | "Zs" | "Zl" | "Zp" | "C" | "Cc" | "Cf" | "Cs" | "Co" | "Cn";
|
|
7
|
+
/**
|
|
8
|
+
* Whether `name` is the abbreviation of a Unicode general category.
|
|
9
|
+
*
|
|
10
|
+
* O(1).
|
|
11
|
+
*/
|
|
12
|
+
export declare function isUnicodeGeneralCategory(name: string): name is UnicodeGeneralCategory;
|
|
13
|
+
/**
|
|
14
|
+
* Whether `codePoint` has the general category `category`, or one of its
|
|
15
|
+
* subclasses for a main class: the membership `\p{…}` tests.
|
|
16
|
+
*
|
|
17
|
+
* Membership follows the Unicode version of the JavaScript engine. A surrogate
|
|
18
|
+
* code point is `Cs` (and `C`), an unassigned one `Cn`. The test runs on a
|
|
19
|
+
* literal anchored native pattern over one code point, so it cannot backtrack;
|
|
20
|
+
* it builds a one-character string, which the engine shares for U+0000–U+00FF
|
|
21
|
+
* and allocates above.
|
|
22
|
+
*
|
|
23
|
+
* O(1).
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* isInUnicodeGeneralCategory(0x41, "Lu"); // true: "A"
|
|
28
|
+
* isInUnicodeGeneralCategory(0x41, "L"); // true
|
|
29
|
+
* const isLetter = (codePoint: number) =>
|
|
30
|
+
* isInUnicodeGeneralCategory(codePoint, "L");
|
|
31
|
+
* ```;
|
|
32
|
+
*
|
|
33
|
+
* @param codePoint An integer from 0 to 0x10FFFF.
|
|
34
|
+
* @throws {RangeError} If `codePoint` is not one.
|
|
35
|
+
* @throws {TypeError} If `category` is not a general category (a caller without
|
|
36
|
+
* type checking).
|
|
37
|
+
*/
|
|
38
|
+
export declare function isInUnicodeGeneralCategory(codePoint: number, category: UnicodeGeneralCategory): boolean;
|
|
39
|
+
//# sourceMappingURL=unicode-general-category.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unicode-general-category.d.ts","sourceRoot":"","sources":["../src/unicode-general-category.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAC/B,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,CAAC;AAER;;;;GAIG;AACH,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,MAAM,GACV,IAAI,IAAI,sBAAsB,CAEhC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,0BAA0B,CACzC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,sBAAsB,GAC9B,OAAO,CAST"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { SURROGATE_CATEGORY_TEST_, UNICODE_CATEGORY_TESTS_, } from "./_unicode-category.js";
|
|
2
|
+
/**
|
|
3
|
+
* Whether `name` is the abbreviation of a Unicode general category.
|
|
4
|
+
*
|
|
5
|
+
* O(1).
|
|
6
|
+
*/
|
|
7
|
+
export function isUnicodeGeneralCategory(name) {
|
|
8
|
+
return UNICODE_CATEGORY_TESTS_.has(name) || name === "Cs";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Whether `codePoint` has the general category `category`, or one of its
|
|
12
|
+
* subclasses for a main class: the membership `\p{…}` tests.
|
|
13
|
+
*
|
|
14
|
+
* Membership follows the Unicode version of the JavaScript engine. A surrogate
|
|
15
|
+
* code point is `Cs` (and `C`), an unassigned one `Cn`. The test runs on a
|
|
16
|
+
* literal anchored native pattern over one code point, so it cannot backtrack;
|
|
17
|
+
* it builds a one-character string, which the engine shares for U+0000–U+00FF
|
|
18
|
+
* and allocates above.
|
|
19
|
+
*
|
|
20
|
+
* O(1).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* isInUnicodeGeneralCategory(0x41, "Lu"); // true: "A"
|
|
25
|
+
* isInUnicodeGeneralCategory(0x41, "L"); // true
|
|
26
|
+
* const isLetter = (codePoint: number) =>
|
|
27
|
+
* isInUnicodeGeneralCategory(codePoint, "L");
|
|
28
|
+
* ```;
|
|
29
|
+
*
|
|
30
|
+
* @param codePoint An integer from 0 to 0x10FFFF.
|
|
31
|
+
* @throws {RangeError} If `codePoint` is not one.
|
|
32
|
+
* @throws {TypeError} If `category` is not a general category (a caller without
|
|
33
|
+
* type checking).
|
|
34
|
+
*/
|
|
35
|
+
export function isInUnicodeGeneralCategory(codePoint, category) {
|
|
36
|
+
const test = category === "Cs"
|
|
37
|
+
? SURROGATE_CATEGORY_TEST_
|
|
38
|
+
: UNICODE_CATEGORY_TESTS_.get(category);
|
|
39
|
+
if (test === undefined) {
|
|
40
|
+
throw new TypeError(`"${category}" is not a Unicode general category`);
|
|
41
|
+
}
|
|
42
|
+
return test.test(String.fromCodePoint(codePoint));
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=unicode-general-category.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unicode-general-category.js","sourceRoot":"","sources":["../src/unicode-general-category.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,uBAAuB,GACvB,MAAM,wBAAwB,CAAC;AA8ChC;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACvC,IAAY;IAEZ,OAAO,uBAAuB,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,CAAC;AAC3D,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,0BAA0B,CACzC,SAAiB,EACjB,QAAgC;IAEhC,MAAM,IAAI,GACT,QAAQ,KAAK,IAAI;QAChB,CAAC,CAAC,wBAAwB;QAC1B,CAAC,CAAC,uBAAuB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC1C,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,SAAS,CAAC,IAAI,QAAQ,qCAAqC,CAAC,CAAC;IACxE,CAAC;IACD,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -1,19 +1,32 @@
|
|
|
1
1
|
import { type WorkBudget } from "@ac-kit/core";
|
|
2
2
|
import type { Instruction } from "./program.js";
|
|
3
|
+
/**
|
|
4
|
+
* Sorted disjoint inclusive `[from, to, …]` intervals of the flat `bounds`
|
|
5
|
+
* pairs, a pair whose start is above its end dropped. Charges `budget` (when
|
|
6
|
+
* given) one unit per bound times the sort's comparison depth before doing the
|
|
7
|
+
* work. O(n log n) time, O(n) memory.
|
|
8
|
+
*
|
|
9
|
+
* @throws {RangeError} If `bounds` has an odd length.
|
|
10
|
+
* @throws Whatever `budget.spend` throws.
|
|
11
|
+
*/
|
|
12
|
+
export declare function normalizedIntervals_(bounds: readonly number[], budget: WorkBudget | undefined): readonly number[];
|
|
13
|
+
/** Whether `codePoint` is in sorted disjoint inclusive `intervals`. O(log n). */
|
|
14
|
+
export declare const inIntervals_: (intervals: readonly number[], codePoint: number) => boolean;
|
|
15
|
+
/**
|
|
16
|
+
* The membership work of one class test beyond what a thread step pays: the
|
|
17
|
+
* search steps over `intervals` plus one per extra test, less a small
|
|
18
|
+
* constant.
|
|
19
|
+
*/
|
|
20
|
+
export declare const classCost_: (intervals: readonly number[], extraTestCount: number) => number;
|
|
3
21
|
/**
|
|
4
22
|
* Builds the `predicate` instruction of a character class, for both front ends.
|
|
5
23
|
*
|
|
6
24
|
* Code points (or code units) and ranges are merged into sorted disjoint
|
|
7
|
-
* intervals, so a test is one binary search
|
|
8
|
-
* listed; `extraTests` (shorthands, Unicode
|
|
9
|
-
* deduplicated by the caller, so their number is
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* Charges `budget` for the normalisation (one unit per bound times the sort's
|
|
14
|
-
* comparison depth) before doing it, and sets the instruction's `cost` to the
|
|
15
|
-
* membership work of one test beyond a small constant: the search steps plus
|
|
16
|
-
* one per extra test.
|
|
25
|
+
* intervals ({@link normalizedIntervals_}), so a test is one binary search
|
|
26
|
+
* however many items the class listed; `extraTests` (shorthands, Unicode
|
|
27
|
+
* categories) must already be deduplicated by the caller, so their number is
|
|
28
|
+
* bounded by the dialect, not by the pattern. The instruction's `cost` is
|
|
29
|
+
* {@link classCost_}.
|
|
17
30
|
*
|
|
18
31
|
* O(n log n) time and O(n) memory for n bounds.
|
|
19
32
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_class-predicate.d.ts","sourceRoot":"","sources":["../../src/vm/_class-predicate.ts"],"names":[],"mappings":"AACA,OAAO,EAA2B,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAExE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAahD
|
|
1
|
+
{"version":3,"file":"_class-predicate.d.ts","sourceRoot":"","sources":["../../src/vm/_class-predicate.ts"],"names":[],"mappings":"AACA,OAAO,EAA2B,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAExE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAahD;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,MAAM,EAAE,UAAU,GAAG,SAAS,GAC5B,SAAS,MAAM,EAAE,CAoBnB;AAED,iFAAiF;AACjF,eAAO,MAAM,YAAY,cACb,SAAS,MAAM,EAAE,aACjB,MAAM,KACf,OAEwE,CAAC;AAE5E;;;;GAIG;AACH,eAAO,MAAM,UAAU,cACX,SAAS,MAAM,EAAE,kBACZ,MAAM,KACpB,MAID,CAAC;AAEH;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,UAAU,EAAE,aAAa,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,EACpD,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,UAAU,GAChB,OAAO,CAAC,WAAW,EAAE;IAAE,EAAE,EAAE,WAAW,CAAA;CAAE,CAAC,CAmB3C"}
|