@criogaid/pi-hashline-edit 0.6.0 → 0.7.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/README.md +161 -304
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +10 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +189 -60
- package/src/core/apply.ts +110 -74
- package/src/core/case-fold.test.ts +40 -0
- package/src/core/case-fold.ts +81 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +170 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/lines.ts +7 -0
- package/src/core/regex.ts +21 -0
- package/src/core/replace-schema.ts +34 -0
- package/src/core/replace.ts +37 -54
- package/src/core/search-analysis.test.ts +199 -0
- package/src/core/search-analysis.ts +375 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +16 -11
- package/src/index.ts +25 -15
- package/src/integration/config-registration.test.ts +30 -24
- package/src/integration/forget-grep.test.ts +52 -2
- package/src/integration/fusion-dispatch.test.ts +21 -105
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +280 -0
- package/src/integration/grep-js-completeness.test.ts +208 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-path-matrix.test.ts +132 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +410 -0
- package/src/integration/grep-rg.test.ts +220 -402
- package/src/integration/grep-scan-budget.test.ts +89 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +57 -1
- package/src/integration/model-budget-lifecycle.test.ts +128 -0
- package/src/integration/mutation-file-types.test.ts +12 -25
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +123 -0
- package/src/integration/publication-read-changes.test.ts +344 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +33 -0
- package/src/integration/replace-worker-boundary.test.ts +107 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +55 -0
- package/src/pi/action-fusion.test.ts +253 -103
- package/src/pi/action-fusion.ts +116 -290
- package/src/pi/anchor-format.ts +9 -0
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +43 -0
- package/src/pi/argument-error.ts +78 -0
- package/src/pi/argument-format.test.ts +208 -0
- package/src/pi/argument-validation.test.ts +742 -45
- package/src/pi/argument-validation.ts +56 -59
- package/src/pi/budgets.ts +45 -16
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +14 -10
- package/src/pi/config.ts +21 -7
- package/src/pi/diagnostic-buffer.test.ts +52 -16
- package/src/pi/diagnostic-buffer.ts +11 -14
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +100 -93
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +362 -315
- package/src/pi/extension-loading.test.ts +14 -0
- package/src/pi/extension-loading.testing.ts +3 -0
- package/src/pi/failure-context.test.ts +220 -88
- package/src/pi/failure-context.ts +161 -180
- package/src/pi/failure-path.test.ts +85 -88
- package/src/pi/file-commit.test.ts +78 -45
- package/src/pi/file-commit.ts +218 -151
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/forget-tool.ts +189 -90
- package/src/pi/forget.test.ts +309 -22
- package/src/pi/forget.testing.ts +41 -113
- package/src/pi/fusion-card.test.ts +40 -14
- package/src/pi/fusion-card.ts +84 -50
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +193 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-output.ts +181 -138
- package/src/pi/grep-render.ts +28 -25
- package/src/pi/grep-scope.ts +61 -53
- package/src/pi/grep-search.ts +299 -242
- package/src/pi/grep-tool.ts +150 -140
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +631 -632
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +5 -5
- package/src/pi/mutation-result.test.ts +21 -74
- package/src/pi/mutation-result.ts +74 -87
- package/src/pi/mutation-runner.ts +83 -75
- package/src/pi/output-budgets.test.ts +166 -0
- package/src/pi/path.ts +7 -0
- package/src/pi/read-tool.ts +151 -137
- package/src/pi/read.test.ts +39 -33
- package/src/pi/regex-worker-contract.ts +73 -0
- package/src/pi/regex-worker.ts +101 -0
- package/src/pi/registration.test.ts +111 -92
- package/src/pi/render.test.ts +193 -19
- package/src/pi/render.ts +137 -29
- package/src/pi/replace-regex.ts +11 -44
- package/src/pi/replace-tool.ts +38 -145
- package/src/pi/replace-worker-contract.ts +19 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +93 -160
- package/src/pi/report-cause.ts +99 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +512 -0
- package/src/pi/report.test.ts +276 -0
- package/src/pi/report.ts +288 -0
- package/src/pi/result-safety.test.ts +122 -427
- package/src/pi/rg-process.test.ts +78 -79
- package/src/pi/rg-process.ts +124 -177
- package/src/pi/rg-regex.test.ts +50 -0
- package/src/pi/rg-regex.ts +709 -0
- package/src/pi/text-stream.test.ts +67 -38
- package/src/pi/text-stream.ts +96 -75
- package/src/pi/tool-call.testing.ts +3 -39
- package/src/pi/write-tool.ts +45 -34
- package/src/pi/write.test.ts +45 -37
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +308 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Line-candidate translation: each grep pattern becomes an rg pattern that matches, over a
|
|
3
|
+
* physical line's original bytes, every line whose LF view the JavaScript pattern matches,
|
|
4
|
+
* and possibly more. Native RegExp re-checks only those lines, so a construct is translated
|
|
5
|
+
* exactly where its meaning is the same on both sides and otherwise widened: an assertion
|
|
6
|
+
* may be dropped and an element may match more text, but nothing a match needs is removed.
|
|
7
|
+
*
|
|
8
|
+
* Original line bytes differ from the LF view in three ways, each widened here: a CRLF line
|
|
9
|
+
* keeps its CR before the line feed, malformed UTF-8 is U+FFFD in the view, and without u a
|
|
10
|
+
* consuming element can match half of a supplementary character.
|
|
11
|
+
*
|
|
12
|
+
* @module pi-hashline-edit/pi
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { visitRegExpAST, type AST } from "@eslint-community/regexpp";
|
|
16
|
+
import {
|
|
17
|
+
MAX_UNIT,
|
|
18
|
+
MAX_SCALAR,
|
|
19
|
+
normalize,
|
|
20
|
+
complement,
|
|
21
|
+
subtract,
|
|
22
|
+
intersect,
|
|
23
|
+
total,
|
|
24
|
+
contains,
|
|
25
|
+
nativeCharacterRanges,
|
|
26
|
+
foldRanges,
|
|
27
|
+
membership,
|
|
28
|
+
type Ranges,
|
|
29
|
+
classStrings,
|
|
30
|
+
type CharacterNode,
|
|
31
|
+
surrogatePair,
|
|
32
|
+
} from "../core/character-set.ts";
|
|
33
|
+
import { caseVariants } from "../core/case-fold.ts";
|
|
34
|
+
import { parseQueryPattern } from "../core/search-analysis.ts";
|
|
35
|
+
import type { GrepQuery } from "../core/search.ts";
|
|
36
|
+
|
|
37
|
+
const LF = 0x0a;
|
|
38
|
+
const REPLACEMENT = 0xfffd;
|
|
39
|
+
const SURROGATES: Ranges = [[0xd800, 0xdfff]];
|
|
40
|
+
const SUPPLEMENTARY: Ranges = [[0x10000, MAX_SCALAR]];
|
|
41
|
+
/** Repetition counts above this become unbounded, keeping rg's compiled size small. */
|
|
42
|
+
const MAX_EXACT_REPEAT = 16;
|
|
43
|
+
/** Bound expanded class strings by total code points, counting an empty string as one. */
|
|
44
|
+
const MAX_EXPANDED_STRING_POINTS = 256;
|
|
45
|
+
/** Bound emitted string alternatives; oversized sets retain necessary prefixes. */
|
|
46
|
+
const MAX_STRING_PATTERN_UNITS = 8192;
|
|
47
|
+
const MAX_STRING_PREFIX_POINTS = 16;
|
|
48
|
+
/** Leave room for rg's arguments and paths on hosts with small command-line limits. */
|
|
49
|
+
const MAX_LINE_PATTERN_UNITS = 24 * 1024;
|
|
50
|
+
|
|
51
|
+
/** The bytes a lossy decoder shows as U+FFFD: every byte of a malformed sequence is 0x80–0xFF. */
|
|
52
|
+
const MALFORMED = "(?-u:[\\x80-\\xff])+";
|
|
53
|
+
/** Any text within one line, malformed bytes included. */
|
|
54
|
+
const ANY_TEXT = "(?-u:[^\\n])*";
|
|
55
|
+
const NEVER = "[^\\x{0}-\\x{10ffff}]";
|
|
56
|
+
/** Characters after which, or before which, JavaScript's m-mode ^ and $ hold inside a line. */
|
|
57
|
+
const TERMINATORS = "[\\r\\x{2028}\\x{2029}]";
|
|
58
|
+
|
|
59
|
+
interface Context {
|
|
60
|
+
readonly ignoreCase: boolean;
|
|
61
|
+
/** The i flag, which modifier groups override. */
|
|
62
|
+
readonly flagIgnoreCase: boolean;
|
|
63
|
+
readonly flagDotAll: boolean;
|
|
64
|
+
/** u or v: elements consume code points, not UTF-16 units. */
|
|
65
|
+
readonly unicode: boolean;
|
|
66
|
+
readonly unicodeSets: boolean;
|
|
67
|
+
readonly multilineAnchors: boolean;
|
|
68
|
+
/** Translating a group as the text a backreference repeats, away from where it matched. */
|
|
69
|
+
readonly repeated: boolean;
|
|
70
|
+
readonly bindings: ReadonlyMap<AST.CapturingGroup, string>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Whether nothing consumed can precede or follow an element within its pattern's match. */
|
|
74
|
+
interface Position {
|
|
75
|
+
readonly first: boolean;
|
|
76
|
+
readonly last: boolean;
|
|
77
|
+
}
|
|
78
|
+
const INNER: Position = { first: false, last: false };
|
|
79
|
+
|
|
80
|
+
/** Bound assertion alternatives and correlated capture expansion. */
|
|
81
|
+
const MAX_ANALYSIS_BRANCHES = 16;
|
|
82
|
+
const MAX_FIXED_UNITS = 64;
|
|
83
|
+
interface FixedSequence {
|
|
84
|
+
readonly positions: readonly Ranges[];
|
|
85
|
+
readonly exact: boolean;
|
|
86
|
+
}
|
|
87
|
+
type Constraints = ReadonlyMap<AST.Element, readonly Ranges[]>;
|
|
88
|
+
|
|
89
|
+
function fixedAlternatives(
|
|
90
|
+
nodes: readonly AST.Alternative[],
|
|
91
|
+
ctx: Context,
|
|
92
|
+
): FixedSequence | undefined {
|
|
93
|
+
const branches: FixedSequence[] = [];
|
|
94
|
+
for (const node of nodes) {
|
|
95
|
+
const positions: Ranges[] = [];
|
|
96
|
+
let exact = true;
|
|
97
|
+
for (const child of node.elements) {
|
|
98
|
+
const fixed = fixedSequence(child, ctx);
|
|
99
|
+
if (!fixed) return undefined;
|
|
100
|
+
positions.push(...fixed.positions);
|
|
101
|
+
exact &&= fixed.exact;
|
|
102
|
+
if (positions.length > MAX_FIXED_UNITS) return undefined;
|
|
103
|
+
}
|
|
104
|
+
branches.push({ positions, exact });
|
|
105
|
+
}
|
|
106
|
+
if (!branches.every((branch) => branch.positions.length === branches[0].positions.length))
|
|
107
|
+
return undefined;
|
|
108
|
+
return {
|
|
109
|
+
positions: branches[0].positions.map((_, index) =>
|
|
110
|
+
normalize(branches.flatMap((branch) => branch.positions[index])),
|
|
111
|
+
),
|
|
112
|
+
exact:
|
|
113
|
+
branches.every((branch) => branch.exact) &&
|
|
114
|
+
(branches.length === 1 || branches[0].positions.length <= 1),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function fixedSequence(node: AST.Element, ctx: Context): FixedSequence | undefined {
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case "Character":
|
|
121
|
+
case "CharacterClass":
|
|
122
|
+
case "ExpressionCharacterClass":
|
|
123
|
+
case "CharacterSet":
|
|
124
|
+
if (classStrings(node)?.length !== 0) return undefined;
|
|
125
|
+
return { positions: [scalarRanges(node, ctx)], exact: true };
|
|
126
|
+
case "Assertion":
|
|
127
|
+
return { positions: [], exact: false };
|
|
128
|
+
case "Backreference":
|
|
129
|
+
return undefined;
|
|
130
|
+
case "Group":
|
|
131
|
+
return fixedAlternatives(node.alternatives, modified(node, ctx));
|
|
132
|
+
case "CapturingGroup": {
|
|
133
|
+
const bound = ctx.bindings.get(node);
|
|
134
|
+
if (bound !== undefined)
|
|
135
|
+
return {
|
|
136
|
+
positions: [...(ctx.unicode ? [...bound] : bound.split(""))].map((char) => [
|
|
137
|
+
[char.codePointAt(0)!, char.codePointAt(0)!],
|
|
138
|
+
]),
|
|
139
|
+
exact: true,
|
|
140
|
+
};
|
|
141
|
+
return fixedAlternatives(node.alternatives, ctx);
|
|
142
|
+
}
|
|
143
|
+
case "Quantifier": {
|
|
144
|
+
if (node.max === 0) return { positions: [], exact: true };
|
|
145
|
+
if (node.min !== node.max || node.max > MAX_FIXED_UNITS) return undefined;
|
|
146
|
+
const inner = fixedSequence(node.element, ctx);
|
|
147
|
+
if (!inner || inner.positions.length * node.max > MAX_FIXED_UNITS) return undefined;
|
|
148
|
+
return {
|
|
149
|
+
positions: Array.from({ length: node.max }, () => inner.positions).flat(),
|
|
150
|
+
exact: inner.exact,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface Neighbour {
|
|
157
|
+
readonly node: AST.Element;
|
|
158
|
+
readonly index: number;
|
|
159
|
+
readonly ranges: Ranges;
|
|
160
|
+
}
|
|
161
|
+
function neighbours(
|
|
162
|
+
elements: readonly AST.Element[],
|
|
163
|
+
from: number,
|
|
164
|
+
direction: number,
|
|
165
|
+
count: number,
|
|
166
|
+
ctx: Context,
|
|
167
|
+
plan: Constraints,
|
|
168
|
+
): Neighbour[] {
|
|
169
|
+
const result: Neighbour[] = [];
|
|
170
|
+
for (
|
|
171
|
+
let index = from + direction;
|
|
172
|
+
index >= 0 && index < elements.length && result.length < count;
|
|
173
|
+
index += direction
|
|
174
|
+
) {
|
|
175
|
+
const node = elements[index];
|
|
176
|
+
const fixed = fixedSequence(node, ctx);
|
|
177
|
+
if (!fixed) break;
|
|
178
|
+
const positions = plan.get(node) ?? fixed.positions;
|
|
179
|
+
for (
|
|
180
|
+
let at = direction > 0 ? 0 : positions.length - 1;
|
|
181
|
+
at >= 0 && at < positions.length && result.length < count;
|
|
182
|
+
at += direction
|
|
183
|
+
)
|
|
184
|
+
result.push({ node, index: at, ranges: positions[at] });
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function restrict(
|
|
190
|
+
plan: Constraints,
|
|
191
|
+
neighbour: Neighbour,
|
|
192
|
+
allowed: Ranges,
|
|
193
|
+
ctx: Context,
|
|
194
|
+
): Constraints {
|
|
195
|
+
const result = new Map(plan);
|
|
196
|
+
const positions = [
|
|
197
|
+
...(result.get(neighbour.node) ?? fixedSequence(neighbour.node, ctx)!.positions),
|
|
198
|
+
];
|
|
199
|
+
positions[neighbour.index] = intersect(positions[neighbour.index], allowed);
|
|
200
|
+
result.set(neighbour.node, positions);
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Apply only proved constraints; a negative test needs an exact forbidden set. */
|
|
205
|
+
function assertionPlans(elements: readonly AST.Element[], ctx: Context): readonly Constraints[] {
|
|
206
|
+
let plans: readonly Constraints[] = [new Map()];
|
|
207
|
+
if (ctx.repeated) return plans;
|
|
208
|
+
for (const [index, node] of elements.entries()) {
|
|
209
|
+
if (node.type !== "Assertion") continue;
|
|
210
|
+
if (node.kind === "lookahead" || node.kind === "lookbehind") {
|
|
211
|
+
const fixed = fixedAlternatives(node.alternatives, ctx);
|
|
212
|
+
if (!fixed) continue;
|
|
213
|
+
const direction = node.kind === "lookahead" ? 1 : -1;
|
|
214
|
+
const asserted = direction > 0 ? fixed.positions : [...fixed.positions].reverse();
|
|
215
|
+
if (node.negate && (!fixed.exact || plans.length * asserted.length > MAX_ANALYSIS_BRANCHES))
|
|
216
|
+
continue;
|
|
217
|
+
plans = plans.flatMap((plan) => {
|
|
218
|
+
const adjacent = neighbours(elements, index, direction, asserted.length, ctx, plan);
|
|
219
|
+
if (node.negate) {
|
|
220
|
+
if (adjacent.length !== asserted.length) return [plan];
|
|
221
|
+
return adjacent.map((item, at) =>
|
|
222
|
+
restrict(plan, item, complement(asserted[at], everything(ctx)[0][1]), ctx),
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
let constrained = plan;
|
|
226
|
+
for (const [at, item] of adjacent.entries())
|
|
227
|
+
constrained = restrict(constrained, item, asserted[at], ctx);
|
|
228
|
+
return [constrained];
|
|
229
|
+
});
|
|
230
|
+
} else if (node.kind === "word") {
|
|
231
|
+
const word = nativeCharacterRanges("\\w", nativeFlags(node, ctx));
|
|
232
|
+
plans = plans.map((plan) => {
|
|
233
|
+
const left = neighbours(elements, index, -1, 1, ctx, plan)[0];
|
|
234
|
+
const right = neighbours(elements, index, 1, 1, ctx, plan)[0];
|
|
235
|
+
if (!left || !right) return plan;
|
|
236
|
+
for (const [known, other] of [
|
|
237
|
+
[left, right],
|
|
238
|
+
[right, left],
|
|
239
|
+
]) {
|
|
240
|
+
const isWord = subtract(known.ranges, word).length === 0;
|
|
241
|
+
const isNonWord = intersect(known.ranges, word).length === 0;
|
|
242
|
+
if (isWord || isNonWord)
|
|
243
|
+
plan = restrict(
|
|
244
|
+
plan,
|
|
245
|
+
other,
|
|
246
|
+
isWord === node.negate ? word : complement(word, everything(ctx)[0][1]),
|
|
247
|
+
ctx,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
return plan;
|
|
251
|
+
});
|
|
252
|
+
} else {
|
|
253
|
+
const direction = node.kind === "start" ? -1 : 1;
|
|
254
|
+
plans = plans.map((plan) => {
|
|
255
|
+
const neighbour = neighbours(elements, index, direction, 1, ctx, plan)[0];
|
|
256
|
+
if (!neighbour) return plan;
|
|
257
|
+
const terminators = nativeCharacterRanges("[\\n\\r\\u2028\\u2029]", ctx.unicode ? "u" : "");
|
|
258
|
+
return restrict(
|
|
259
|
+
plan,
|
|
260
|
+
neighbour,
|
|
261
|
+
node.kind === "start" && !ctx.multilineAnchors ? [] : terminators,
|
|
262
|
+
ctx,
|
|
263
|
+
);
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return plans;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const intersects = (ranges: Ranges, other: Ranges) => intersect(ranges, other).length !== 0;
|
|
271
|
+
|
|
272
|
+
/** Everything one element can consume: one code point with u, one UTF-16 unit without. */
|
|
273
|
+
const everything = (ctx: Context): Ranges => (ctx.unicode ? [[0, MAX_SCALAR]] : [[0, MAX_UNIT]]);
|
|
274
|
+
|
|
275
|
+
const folded = (ranges: Ranges, ctx: Context): Ranges =>
|
|
276
|
+
ctx.ignoreCase ? foldRanges(ranges, ctx.unicode) : ranges;
|
|
277
|
+
|
|
278
|
+
const escape = (point: number) =>
|
|
279
|
+
/^[0-9A-Za-z_]$/.test(String.fromCodePoint(point))
|
|
280
|
+
? String.fromCodePoint(point)
|
|
281
|
+
: `\\x{${point.toString(16)}}`;
|
|
282
|
+
|
|
283
|
+
function classPattern(ranges: Ranges): string {
|
|
284
|
+
if (ranges.length === 1 && ranges[0][0] === ranges[0][1]) return escape(ranges[0][0]);
|
|
285
|
+
return `[${ranges.map(([low, high]) => (low === high ? escape(low) : `${escape(low)}-${escape(high)}`)).join("")}]`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* One consuming element over the units or code points it can match. U+FFFD may be malformed
|
|
290
|
+
* bytes. Without u, a surrogate may be half of a supplementary character: the element then
|
|
291
|
+
* consumes the whole character or nothing, and its neighbour consumes the other half.
|
|
292
|
+
*/
|
|
293
|
+
function unit(ranges: Ranges, ctx: Context): string {
|
|
294
|
+
const halves = !ctx.unicode && intersects(ranges, SURROGATES);
|
|
295
|
+
let scalars = subtract(ranges, [[LF, LF], ...SURROGATES]);
|
|
296
|
+
if (halves) scalars = normalize([...scalars, ...SUPPLEMENTARY]);
|
|
297
|
+
const alternatives = [
|
|
298
|
+
...(scalars.length ? [classPattern(scalars)] : []),
|
|
299
|
+
...(contains(ranges, REPLACEMENT) ? [MALFORMED] : []),
|
|
300
|
+
];
|
|
301
|
+
if (!alternatives.length) return NEVER;
|
|
302
|
+
const body = alternatives.length === 1 ? alternatives[0] : `(?:${alternatives.join("|")})`;
|
|
303
|
+
return halves ? `(?:${body})?` : body;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function nativeFlags(node: AST.Node, ctx: Context): string {
|
|
307
|
+
let dotAll = ctx.flagDotAll;
|
|
308
|
+
for (let parent = node.parent; parent; parent = parent.parent) {
|
|
309
|
+
if (parent.type !== "Group" || !parent.modifiers) continue;
|
|
310
|
+
if (parent.modifiers.add.dotAll) {
|
|
311
|
+
dotAll = true;
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
if (parent.modifiers.remove?.dotAll) {
|
|
315
|
+
dotAll = false;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return `${ignoreCaseAt(node, ctx) ? "i" : ""}${dotAll ? "s" : ""}${ctx.unicodeSets ? "v" : ctx.unicode ? "u" : ""}`;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function scalarRanges(node: CharacterNode, ctx: Context): Ranges {
|
|
323
|
+
const ranges = nativeCharacterRanges(node.raw, nativeFlags(node, ctx));
|
|
324
|
+
// Case comparison at a reference can widen its capture's original character set.
|
|
325
|
+
return ctx.repeated && ctx.ignoreCase && !ignoreCaseAt(node, ctx) ? folded(ranges, ctx) : ranges;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const choice = (patterns: readonly string[]): string =>
|
|
329
|
+
patterns.length === 0 ? NEVER : patterns.length === 1 ? patterns[0] : `(?:${patterns.join("|")})`;
|
|
330
|
+
|
|
331
|
+
/** Factor common prefixes without dropping an alternative or cutting a code point escape. */
|
|
332
|
+
function wordTree(words: readonly (readonly number[])[], ctx: Context): string {
|
|
333
|
+
const branches = new Map<number, number[][]>();
|
|
334
|
+
let empty = false;
|
|
335
|
+
for (const word of words) {
|
|
336
|
+
if (!word.length) {
|
|
337
|
+
empty = true;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const branch = branches.get(word[0]) ?? [];
|
|
341
|
+
branch.push(word.slice(1));
|
|
342
|
+
branches.set(word[0], branch);
|
|
343
|
+
}
|
|
344
|
+
const patterns = [...branches].map(
|
|
345
|
+
([point, tails]) => unit(folded([[point, point]], ctx), ctx) + wordTree(tails, ctx),
|
|
346
|
+
);
|
|
347
|
+
if (empty) patterns.push("");
|
|
348
|
+
return choice(patterns);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function stringPattern(strings: readonly string[], ctx: Context): string {
|
|
352
|
+
const words = [...new Set(strings)].map((text) => [...text].map((char) => char.codePointAt(0)!));
|
|
353
|
+
const size = words.reduce((sum, word) => sum + Math.max(1, word.length), 0);
|
|
354
|
+
if (size <= MAX_EXPANDED_STRING_POINTS) return wordTree(words, ctx);
|
|
355
|
+
const shortest = words.reduce((length, word) => Math.min(length, word.length), Infinity);
|
|
356
|
+
if (!shortest) return ANY_TEXT;
|
|
357
|
+
// A suffix survives only where it cannot overlap the retained prefix.
|
|
358
|
+
const suffix: number[] = [];
|
|
359
|
+
for (
|
|
360
|
+
let index = 1;
|
|
361
|
+
index <= Math.min(MAX_STRING_PREFIX_POINTS, Math.floor(shortest / 2));
|
|
362
|
+
index++
|
|
363
|
+
) {
|
|
364
|
+
const point = words[0].at(-index)!;
|
|
365
|
+
if (!words.every((word) => word.at(-index) === point)) break;
|
|
366
|
+
suffix.unshift(point);
|
|
367
|
+
}
|
|
368
|
+
for (
|
|
369
|
+
let length = Math.min(MAX_STRING_PREFIX_POINTS, shortest - suffix.length);
|
|
370
|
+
length >= 1;
|
|
371
|
+
length = Math.floor(length / 2)
|
|
372
|
+
) {
|
|
373
|
+
const prefixes = words.map((word) => word.slice(0, length));
|
|
374
|
+
const pattern = wordTree(prefixes, ctx) + ANY_TEXT + wordTree([suffix], ctx);
|
|
375
|
+
if (pattern.length <= MAX_STRING_PATTERN_UNITS) return pattern;
|
|
376
|
+
}
|
|
377
|
+
return ANY_TEXT;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function characterPattern(node: CharacterNode, ctx: Context): string {
|
|
381
|
+
const strings = classStrings(node);
|
|
382
|
+
if (!strings) return ANY_TEXT;
|
|
383
|
+
const ranges = scalarRanges(node, ctx);
|
|
384
|
+
const accepts = membership(node.raw, nativeFlags(node, ctx));
|
|
385
|
+
const accepted = strings.filter((value) => accepts.test(value));
|
|
386
|
+
return choice([
|
|
387
|
+
...(ranges.length ? [unit(ranges, ctx)] : []),
|
|
388
|
+
...(accepted.length
|
|
389
|
+
? [stringPattern(accepted, { ...ctx, ignoreCase: ctx.ignoreCase || ignoreCaseAt(node, ctx) })]
|
|
390
|
+
: []),
|
|
391
|
+
]);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function assertion(node: AST.Assertion, ctx: Context, position: Position): string {
|
|
395
|
+
// Repeated text held its assertions where the group matched, not where it repeats.
|
|
396
|
+
if (ctx.repeated) return "";
|
|
397
|
+
switch (node.kind) {
|
|
398
|
+
case "start":
|
|
399
|
+
// A line's first byte starts its view; with m, ^ also follows a terminator inside the
|
|
400
|
+
// line, which the match may consume only when nothing before ^ belongs to it.
|
|
401
|
+
if (!ctx.multilineAnchors) return "^";
|
|
402
|
+
return position.first ? `(?:^|${TERMINATORS})` : "";
|
|
403
|
+
case "end":
|
|
404
|
+
// A CRLF line keeps its CR before the line feed, which the match may consume only when
|
|
405
|
+
// nothing after $ belongs to it; with m, $ also precedes a terminator inside the line.
|
|
406
|
+
if (!position.last) return "";
|
|
407
|
+
return ctx.multilineAnchors ? `(?:${TERMINATORS}|\\r?$)` : "\\r?$";
|
|
408
|
+
case "word":
|
|
409
|
+
// JavaScript and rg agree on ASCII word boundaries over valid text and malformed bytes.
|
|
410
|
+
// With u and i, \b also sees U+017F and U+212A; without u, \B holds between the two
|
|
411
|
+
// halves of a supplementary character, which rg consumes as one.
|
|
412
|
+
if (ctx.ignoreCase && ctx.unicode) return "";
|
|
413
|
+
if (node.negate) return ctx.unicode ? "(?-u:\\B)" : "";
|
|
414
|
+
return "(?-u:\\b)";
|
|
415
|
+
default:
|
|
416
|
+
if (!node.negate && position.first && position.last)
|
|
417
|
+
return alternatives(node.alternatives, ctx, INNER);
|
|
418
|
+
// Lookarounds consume nothing; dropping one only widens the candidates.
|
|
419
|
+
return "";
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function quantifier(node: AST.Quantifier, ctx: Context): string {
|
|
424
|
+
if (node.max === 0) return "";
|
|
425
|
+
const inner = element(node.element, ctx, INNER);
|
|
426
|
+
if (!inner) return "";
|
|
427
|
+
const group = `(?:${inner})`;
|
|
428
|
+
if (node.max === Infinity) {
|
|
429
|
+
if (node.min === 0) return `${group}*`;
|
|
430
|
+
if (node.min === 1) return `${group}+`;
|
|
431
|
+
return node.min <= MAX_EXACT_REPEAT ? `${group}{${node.min},}` : `${group}+`;
|
|
432
|
+
}
|
|
433
|
+
if (node.max <= MAX_EXACT_REPEAT) return `${group}{${node.min},${node.max}}`;
|
|
434
|
+
return node.min === 0 ? `${group}*` : `${group}+`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** A modifier group's i and m; its s needs nothing, since every . already matches anything. */
|
|
438
|
+
function modified(node: AST.Group, ctx: Context): Context {
|
|
439
|
+
const { add, remove } = node.modifiers ?? {};
|
|
440
|
+
if (!add) return ctx;
|
|
441
|
+
return {
|
|
442
|
+
...ctx,
|
|
443
|
+
// A capture's modifiers cannot remove case variants allowed where its text is repeated.
|
|
444
|
+
ignoreCase: add.ignoreCase || ((ctx.repeated || !remove?.ignoreCase) && ctx.ignoreCase),
|
|
445
|
+
multilineAnchors: add.multiline || (!remove?.multiline && ctx.multilineAnchors),
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** ignoreCase where a node appears: the innermost modifier group that sets it decides. */
|
|
450
|
+
function ignoreCaseAt(node: AST.Node, ctx: Context): boolean {
|
|
451
|
+
for (let parent = node.parent; parent; parent = parent.parent)
|
|
452
|
+
if (parent.type === "Group" && parent.modifiers) {
|
|
453
|
+
if (parent.modifiers.add.ignoreCase) return true;
|
|
454
|
+
if (parent.modifiers.remove?.ignoreCase) return false;
|
|
455
|
+
}
|
|
456
|
+
return ctx.flagIgnoreCase;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Whether a group has always matched when its backreference is reached: the group precedes
|
|
461
|
+
* the reference in one alternative, and nothing between the group and that alternative can
|
|
462
|
+
* skip it (another alternative, a quantifier that allows zero, or a lookaround).
|
|
463
|
+
*/
|
|
464
|
+
function participates(group: AST.CapturingGroup, reference: AST.Backreference): boolean {
|
|
465
|
+
const ancestors = new Set<AST.Node>();
|
|
466
|
+
for (let node: AST.Node | null = group; node; node = node.parent) ancestors.add(node);
|
|
467
|
+
let referenceChild: AST.Node = reference;
|
|
468
|
+
let common: AST.Node | null = reference.parent;
|
|
469
|
+
while (common && !ancestors.has(common)) {
|
|
470
|
+
referenceChild = common;
|
|
471
|
+
common = common.parent;
|
|
472
|
+
}
|
|
473
|
+
if (common?.type !== "Alternative") return false;
|
|
474
|
+
let groupChild: AST.Node = group;
|
|
475
|
+
for (let node: AST.Node = group.parent; node !== common; node = node.parent!) {
|
|
476
|
+
const kept =
|
|
477
|
+
node.type === "Alternative" ||
|
|
478
|
+
((node.type === "Group" || node.type === "CapturingGroup") &&
|
|
479
|
+
node.alternatives.length === 1) ||
|
|
480
|
+
(node.type === "Quantifier" && node.min >= 1);
|
|
481
|
+
if (!kept) return false;
|
|
482
|
+
groupChild = node;
|
|
483
|
+
}
|
|
484
|
+
const elements: readonly AST.Node[] = common.elements;
|
|
485
|
+
return elements.indexOf(groupChild) < elements.indexOf(referenceChild);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* A backreference repeats text its group matched, compared caselessly where the reference
|
|
490
|
+
* is: the group's pattern under the case rule of either place matches it. A group that may
|
|
491
|
+
* not have matched leaves the reference empty.
|
|
492
|
+
*/
|
|
493
|
+
function backreference(node: AST.Backreference, ctx: Context): string {
|
|
494
|
+
if (!node.ambiguous) {
|
|
495
|
+
const bound = ctx.bindings.get(node.resolved);
|
|
496
|
+
if (bound !== undefined) return literalWord(bound, ctx);
|
|
497
|
+
}
|
|
498
|
+
// A reference inside repeated text could repeat itself.
|
|
499
|
+
if (ctx.repeated) return ANY_TEXT;
|
|
500
|
+
const groups = node.ambiguous ? node.resolved : [node.resolved];
|
|
501
|
+
const copies = groups.map((group) =>
|
|
502
|
+
alternatives(
|
|
503
|
+
group.alternatives,
|
|
504
|
+
{ ...ctx, ignoreCase: ctx.ignoreCase || ignoreCaseAt(group, ctx), repeated: true },
|
|
505
|
+
INNER,
|
|
506
|
+
),
|
|
507
|
+
);
|
|
508
|
+
const text = copies.length === 1 ? copies[0] : `(?:${copies.join("|")})`;
|
|
509
|
+
if (!text) return "";
|
|
510
|
+
return !node.ambiguous && participates(node.resolved, node) ? text : `(?:${text})?`;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function element(node: AST.Element, ctx: Context, position: Position): string {
|
|
514
|
+
switch (node.type) {
|
|
515
|
+
case "Character":
|
|
516
|
+
if (node.value === LF) return NEVER;
|
|
517
|
+
return unit(folded([[node.value, node.value]], ctx), ctx);
|
|
518
|
+
case "CharacterClass":
|
|
519
|
+
case "ExpressionCharacterClass":
|
|
520
|
+
case "CharacterSet":
|
|
521
|
+
return characterPattern(node, ctx);
|
|
522
|
+
case "Assertion":
|
|
523
|
+
return assertion(node, ctx, position);
|
|
524
|
+
case "Backreference":
|
|
525
|
+
return backreference(node, ctx);
|
|
526
|
+
case "Group":
|
|
527
|
+
return alternatives(node.alternatives, modified(node, ctx), position);
|
|
528
|
+
case "CapturingGroup":
|
|
529
|
+
return ctx.bindings.has(node)
|
|
530
|
+
? literalWord(ctx.bindings.get(node)!, { ...ctx, ignoreCase: false })
|
|
531
|
+
: alternatives(node.alternatives, ctx, position);
|
|
532
|
+
case "Quantifier":
|
|
533
|
+
return quantifier(node, ctx);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function alternatives(nodes: readonly AST.Alternative[], ctx: Context, position: Position): string {
|
|
538
|
+
return choice(
|
|
539
|
+
nodes.flatMap(({ elements }) =>
|
|
540
|
+
assertionPlans(elements, ctx).map((plan) => {
|
|
541
|
+
let pattern = "";
|
|
542
|
+
for (let index = 0; index < elements.length; index++) {
|
|
543
|
+
const constrained = plan.get(elements[index]);
|
|
544
|
+
if (constrained) {
|
|
545
|
+
pattern += constrained.map((ranges) => unit(ranges, ctx)).join("");
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
const pair =
|
|
549
|
+
ctx.unicode || plan.has(elements[index + 1])
|
|
550
|
+
? undefined
|
|
551
|
+
: surrogatePair(elements[index], elements[index + 1]);
|
|
552
|
+
if (pair !== undefined) {
|
|
553
|
+
pattern += classPattern([[pair, pair]]);
|
|
554
|
+
index++;
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
pattern += element(elements[index], ctx, {
|
|
558
|
+
first: position.first && index === 0,
|
|
559
|
+
last: position.last && index === elements.length - 1,
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
return pattern;
|
|
563
|
+
}),
|
|
564
|
+
),
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function literalWord(text: string, ctx: Context): string {
|
|
569
|
+
return [...text]
|
|
570
|
+
.map((char) => {
|
|
571
|
+
const point = char.codePointAt(0)!;
|
|
572
|
+
return !ctx.unicode && point > MAX_UNIT
|
|
573
|
+
? classPattern([[point, point]])
|
|
574
|
+
: unit(folded([[point, point]], ctx), ctx);
|
|
575
|
+
})
|
|
576
|
+
.join("");
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function joinWords(
|
|
580
|
+
left: readonly string[],
|
|
581
|
+
right: readonly string[],
|
|
582
|
+
): readonly string[] | undefined {
|
|
583
|
+
if (left.length * right.length > MAX_ANALYSIS_BRANCHES) return undefined;
|
|
584
|
+
const words = [...new Set(left.flatMap((a) => right.map((b) => a + b)))];
|
|
585
|
+
return words.some((word) => word.length > MAX_FIXED_UNITS) ? undefined : words;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function finiteWords(node: AST.Element, ctx: Context): readonly string[] | undefined {
|
|
589
|
+
if (node.type === "Assertion" || node.type === "Backreference") return undefined;
|
|
590
|
+
if (node.type === "Group" || node.type === "CapturingGroup") {
|
|
591
|
+
const result: string[] = [];
|
|
592
|
+
for (const branch of node.alternatives) {
|
|
593
|
+
let words: readonly string[] = [""];
|
|
594
|
+
for (const child of branch.elements) {
|
|
595
|
+
const part = finiteWords(child, ctx);
|
|
596
|
+
const joined = part && joinWords(words, part);
|
|
597
|
+
if (!joined) return undefined;
|
|
598
|
+
words = joined;
|
|
599
|
+
}
|
|
600
|
+
result.push(...words);
|
|
601
|
+
if (result.length > MAX_ANALYSIS_BRANCHES) return undefined;
|
|
602
|
+
}
|
|
603
|
+
return [...new Set(result)];
|
|
604
|
+
}
|
|
605
|
+
if (node.type === "Quantifier") {
|
|
606
|
+
if (node.max > MAX_ANALYSIS_BRANCHES) return undefined;
|
|
607
|
+
if (!node.max) return [""];
|
|
608
|
+
const inner = finiteWords(node.element, ctx);
|
|
609
|
+
if (!inner) return undefined;
|
|
610
|
+
let words: readonly string[] = [""];
|
|
611
|
+
const result: string[] = node.min === 0 ? [""] : [];
|
|
612
|
+
for (let count = 1; count <= node.max; count++) {
|
|
613
|
+
const joined = joinWords(words, inner);
|
|
614
|
+
if (!joined) return undefined;
|
|
615
|
+
words = joined;
|
|
616
|
+
if (count >= node.min) result.push(...words);
|
|
617
|
+
if (result.length > MAX_ANALYSIS_BRANCHES) return undefined;
|
|
618
|
+
}
|
|
619
|
+
return [...new Set(result)];
|
|
620
|
+
}
|
|
621
|
+
const ranges = scalarRanges(node, ctx);
|
|
622
|
+
if (total(ranges) > MAX_ANALYSIS_BRANCHES) return undefined;
|
|
623
|
+
const result: string[] = [];
|
|
624
|
+
for (const [low, high] of ranges)
|
|
625
|
+
for (let point = low; point <= high; point++) result.push(String.fromCodePoint(point));
|
|
626
|
+
const strings = classStrings(node);
|
|
627
|
+
if (!strings) return undefined;
|
|
628
|
+
const accepts = membership(node.raw, nativeFlags(node, ctx));
|
|
629
|
+
for (const value of strings) {
|
|
630
|
+
if (!accepts.test(value)) continue;
|
|
631
|
+
let words: readonly string[] = [""];
|
|
632
|
+
for (const char of value) {
|
|
633
|
+
const variants = ignoreCaseAt(node, ctx) ? caseVariants(char, ctx.unicode) : [char];
|
|
634
|
+
const joined = joinWords(words, variants);
|
|
635
|
+
if (!joined) return undefined;
|
|
636
|
+
words = joined;
|
|
637
|
+
}
|
|
638
|
+
result.push(...words);
|
|
639
|
+
if (result.length > MAX_ANALYSIS_BRANCHES) return undefined;
|
|
640
|
+
}
|
|
641
|
+
return [...new Set(result)];
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** Enumerate only captures whose value is stable at every reference. */
|
|
645
|
+
function bindingContexts(parsed: AST.Pattern, ctx: Context): readonly Context[] {
|
|
646
|
+
let contexts = [ctx];
|
|
647
|
+
visitRegExpAST(parsed, {
|
|
648
|
+
onCapturingGroupEnter(group) {
|
|
649
|
+
if (
|
|
650
|
+
!group.references.length ||
|
|
651
|
+
group.references.some((ref) => ref.ambiguous || !participates(group, ref))
|
|
652
|
+
)
|
|
653
|
+
return;
|
|
654
|
+
for (let parent: AST.Node | null = group.parent; parent; parent = parent.parent)
|
|
655
|
+
if (parent.type === "Assertion" || parent.type === "Quantifier") return;
|
|
656
|
+
let nested = false;
|
|
657
|
+
visitRegExpAST(group, {
|
|
658
|
+
onCapturingGroupEnter(node) {
|
|
659
|
+
if (node !== group) nested = true;
|
|
660
|
+
},
|
|
661
|
+
});
|
|
662
|
+
if (nested) return;
|
|
663
|
+
const words = finiteWords(group, ctx);
|
|
664
|
+
if (!words?.length || words.length * contexts.length > MAX_ANALYSIS_BRANCHES) return;
|
|
665
|
+
contexts = contexts.flatMap((context) =>
|
|
666
|
+
words.map((word) => ({
|
|
667
|
+
...context,
|
|
668
|
+
bindings: new Map([...context.bindings, [group, word]]),
|
|
669
|
+
})),
|
|
670
|
+
);
|
|
671
|
+
},
|
|
672
|
+
});
|
|
673
|
+
return contexts;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* rg patterns, one per query pattern, whose matching lines include every line the query
|
|
678
|
+
* matches without multiline; undefined when parsing or translation exceeds supported budgets.
|
|
679
|
+
*/
|
|
680
|
+
export function lineCandidatePatterns(query: GrepQuery): string[] | undefined {
|
|
681
|
+
const ctx: Context = {
|
|
682
|
+
ignoreCase: query.flags.includes("i"),
|
|
683
|
+
flagIgnoreCase: query.flags.includes("i"),
|
|
684
|
+
flagDotAll: query.flags.includes("s"),
|
|
685
|
+
unicode: query.flags.includes("u") || query.flags.includes("v"),
|
|
686
|
+
unicodeSets: query.flags.includes("v"),
|
|
687
|
+
multilineAnchors: query.flags.includes("m"),
|
|
688
|
+
repeated: false,
|
|
689
|
+
bindings: new Map(),
|
|
690
|
+
};
|
|
691
|
+
const patterns: string[] = [];
|
|
692
|
+
for (const pattern of query.patterns) {
|
|
693
|
+
const parsed = parseQueryPattern(pattern, query.literal, query.flags);
|
|
694
|
+
if (!parsed) return undefined;
|
|
695
|
+
const translated = choice(
|
|
696
|
+
bindingContexts(parsed, ctx).map((context) =>
|
|
697
|
+
alternatives(parsed.alternatives, context, { first: true, last: true }),
|
|
698
|
+
),
|
|
699
|
+
);
|
|
700
|
+
if (
|
|
701
|
+
translated.length + patterns.reduce((sum, value) => sum + value.length, 0) >
|
|
702
|
+
MAX_LINE_PATTERN_UNITS
|
|
703
|
+
)
|
|
704
|
+
return undefined;
|
|
705
|
+
// A sticky match starts where the line starts.
|
|
706
|
+
patterns.push(query.flags.includes("y") ? `^(?:${translated})` : translated);
|
|
707
|
+
}
|
|
708
|
+
return patterns;
|
|
709
|
+
}
|