easy-regex-lib 0.1.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 +546 -0
- package/dist/index.cjs +1037 -0
- package/dist/index.d.cts +308 -0
- package/dist/index.d.ts +308 -0
- package/dist/index.js +972 -0
- package/package.json +39 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/** JSON schema version for serialized patterns. */
|
|
2
|
+
declare const PATTERN_SCHEMA_VERSION: 1;
|
|
3
|
+
type PatternFlags = {
|
|
4
|
+
/** Case-insensitive matching (`i`) */
|
|
5
|
+
ignoreCase?: boolean;
|
|
6
|
+
/** `m` — ^ and $ match line boundaries */
|
|
7
|
+
multiline?: boolean;
|
|
8
|
+
/** `s` — dot matches newline */
|
|
9
|
+
dotAll?: boolean;
|
|
10
|
+
/** `u` — Unicode mode */
|
|
11
|
+
unicode?: boolean;
|
|
12
|
+
/** Global (`g`) */
|
|
13
|
+
global?: boolean;
|
|
14
|
+
/** Sticky (`y`) */
|
|
15
|
+
sticky?: boolean;
|
|
16
|
+
};
|
|
17
|
+
type LetterCase = "lower" | "upper" | "both";
|
|
18
|
+
type Pattern = {
|
|
19
|
+
kind: "Literal";
|
|
20
|
+
value: string;
|
|
21
|
+
} | {
|
|
22
|
+
kind: "Any";
|
|
23
|
+
} | {
|
|
24
|
+
kind: "Digit";
|
|
25
|
+
unicode: boolean;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "Word";
|
|
28
|
+
unicode: boolean;
|
|
29
|
+
} | {
|
|
30
|
+
kind: "Whitespace";
|
|
31
|
+
unicode: boolean;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "Letter";
|
|
34
|
+
letterCase: LetterCase;
|
|
35
|
+
unicode: boolean;
|
|
36
|
+
} | {
|
|
37
|
+
kind: "HexDigit";
|
|
38
|
+
uppercaseOnly?: boolean;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "Start";
|
|
41
|
+
} | {
|
|
42
|
+
kind: "End";
|
|
43
|
+
} | {
|
|
44
|
+
kind: "WordBoundary";
|
|
45
|
+
} | {
|
|
46
|
+
kind: "Sequence";
|
|
47
|
+
children: readonly Pattern[];
|
|
48
|
+
} | {
|
|
49
|
+
kind: "Choice";
|
|
50
|
+
children: readonly Pattern[];
|
|
51
|
+
} | {
|
|
52
|
+
kind: "Repeat";
|
|
53
|
+
child: Pattern;
|
|
54
|
+
min: number;
|
|
55
|
+
max: number;
|
|
56
|
+
greedy: boolean;
|
|
57
|
+
} | {
|
|
58
|
+
kind: "Optional";
|
|
59
|
+
child: Pattern;
|
|
60
|
+
greedy: boolean;
|
|
61
|
+
} | {
|
|
62
|
+
kind: "NamedGroup";
|
|
63
|
+
name: string;
|
|
64
|
+
child: Pattern;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "NonCapturing";
|
|
67
|
+
child: Pattern;
|
|
68
|
+
} | {
|
|
69
|
+
kind: "RawRegex";
|
|
70
|
+
source: string;
|
|
71
|
+
/** Engine flags merged at compile time */
|
|
72
|
+
flags?: PatternFlags;
|
|
73
|
+
/** Documentation only — core never trusts user input */
|
|
74
|
+
trusted?: boolean;
|
|
75
|
+
};
|
|
76
|
+
declare function seq(...children: Pattern[]): Pattern;
|
|
77
|
+
declare function alt(...children: Pattern[]): Pattern;
|
|
78
|
+
declare function repeat(child: Pattern, min: number, max: number, greedy?: boolean): Pattern;
|
|
79
|
+
declare function optional(child: Pattern, greedy?: boolean): Pattern;
|
|
80
|
+
declare function namedGroup(name: string, child: Pattern): Pattern;
|
|
81
|
+
declare function nonCapturing(child: Pattern): Pattern;
|
|
82
|
+
declare function literal(value: string): Pattern;
|
|
83
|
+
declare function optimize(pattern: Pattern): Pattern;
|
|
84
|
+
|
|
85
|
+
type DigitOpts = {
|
|
86
|
+
unicode?: boolean;
|
|
87
|
+
};
|
|
88
|
+
type LetterOpts = {
|
|
89
|
+
case?: LetterCase;
|
|
90
|
+
unicode?: boolean;
|
|
91
|
+
};
|
|
92
|
+
type Quantified = Pattern & {
|
|
93
|
+
exactly(n: number): Pattern;
|
|
94
|
+
atLeast(n: number): Pattern;
|
|
95
|
+
atMost(n: number): Pattern;
|
|
96
|
+
between(min: number, max: number): Pattern;
|
|
97
|
+
oneOrMore(): Pattern;
|
|
98
|
+
zeroOrMore(): Pattern;
|
|
99
|
+
maybe(): Pattern;
|
|
100
|
+
};
|
|
101
|
+
declare function anyChar(): Quantified;
|
|
102
|
+
declare function digit(opts?: DigitOpts): Quantified;
|
|
103
|
+
declare function word(opts?: DigitOpts): Quantified;
|
|
104
|
+
declare function whitespace(opts?: DigitOpts): Quantified;
|
|
105
|
+
declare function letter(opts?: LetterOpts): Quantified;
|
|
106
|
+
declare function hexDigit(opts?: {
|
|
107
|
+
uppercaseOnly?: boolean;
|
|
108
|
+
}): Quantified;
|
|
109
|
+
declare function start(): Pattern;
|
|
110
|
+
declare function end(): Pattern;
|
|
111
|
+
declare function wordBoundary(): Pattern;
|
|
112
|
+
/** Escape hatch: embed raw regex fragment (analyzer flags this). */
|
|
113
|
+
declare function raw(source: string, flags?: PatternFlags): Pattern;
|
|
114
|
+
declare function dash(): Pattern;
|
|
115
|
+
declare function underscore(): Pattern;
|
|
116
|
+
/** Dot literal */
|
|
117
|
+
declare function dot(): Pattern;
|
|
118
|
+
/** Integer sequence (no sign) — digits only */
|
|
119
|
+
declare function integer(): Pattern;
|
|
120
|
+
/** Matches `true` or `false` literally */
|
|
121
|
+
declare function booleanLiteral(): Pattern;
|
|
122
|
+
|
|
123
|
+
type CompileTarget = "javascript";
|
|
124
|
+
type CompileOptions = {
|
|
125
|
+
target?: CompileTarget;
|
|
126
|
+
/** Base flags for the resulting RegExp */
|
|
127
|
+
flags?: PatternFlags;
|
|
128
|
+
/** When true, strip capturing groups (for simulation / diagnostics) */
|
|
129
|
+
nonCapturing?: boolean;
|
|
130
|
+
};
|
|
131
|
+
type CompileResult = {
|
|
132
|
+
pattern: string;
|
|
133
|
+
flags: string;
|
|
134
|
+
warnings: CompileWarning[];
|
|
135
|
+
};
|
|
136
|
+
type CompileWarning = {
|
|
137
|
+
code: string;
|
|
138
|
+
message: string;
|
|
139
|
+
};
|
|
140
|
+
declare function compilePattern(root: Pattern, options?: CompileOptions): CompileResult;
|
|
141
|
+
declare function toRegExp(root: Pattern, options?: CompileOptions): RegExp;
|
|
142
|
+
|
|
143
|
+
type ExplainClause = {
|
|
144
|
+
id: string;
|
|
145
|
+
text: string;
|
|
146
|
+
};
|
|
147
|
+
type ExplainResult = {
|
|
148
|
+
clauses: ExplainClause[];
|
|
149
|
+
summary: string;
|
|
150
|
+
};
|
|
151
|
+
/** Produce human-readable clauses and a short summary. */
|
|
152
|
+
declare function explainPattern(root: Pattern): ExplainResult;
|
|
153
|
+
|
|
154
|
+
type DiagnoseOk = {
|
|
155
|
+
ok: true;
|
|
156
|
+
match?: string;
|
|
157
|
+
index?: number;
|
|
158
|
+
groups?: Record<string, string>;
|
|
159
|
+
};
|
|
160
|
+
type DiagnoseFail = {
|
|
161
|
+
ok: false;
|
|
162
|
+
index: number;
|
|
163
|
+
message: string;
|
|
164
|
+
expected?: string;
|
|
165
|
+
};
|
|
166
|
+
type DiagnoseResult = DiagnoseOk | DiagnoseFail;
|
|
167
|
+
declare function diagnose(root: Pattern, input: string, options?: CompileOptions): DiagnoseResult;
|
|
168
|
+
|
|
169
|
+
type AnalysisFinding = {
|
|
170
|
+
severity: "info" | "warn" | "error";
|
|
171
|
+
code: string;
|
|
172
|
+
message: string;
|
|
173
|
+
};
|
|
174
|
+
/** Lightweight static hints (not a full ReDoS solver). */
|
|
175
|
+
declare function analyzePattern(root: Pattern): AnalysisFinding[];
|
|
176
|
+
|
|
177
|
+
type SerializedPattern = {
|
|
178
|
+
schemaVersion: typeof PATTERN_SCHEMA_VERSION;
|
|
179
|
+
pattern: JsonPattern;
|
|
180
|
+
};
|
|
181
|
+
type JsonPattern = {
|
|
182
|
+
kind: "Literal";
|
|
183
|
+
value: string;
|
|
184
|
+
} | {
|
|
185
|
+
kind: "Any";
|
|
186
|
+
} | {
|
|
187
|
+
kind: "Digit";
|
|
188
|
+
unicode: boolean;
|
|
189
|
+
} | {
|
|
190
|
+
kind: "Word";
|
|
191
|
+
unicode: boolean;
|
|
192
|
+
} | {
|
|
193
|
+
kind: "Whitespace";
|
|
194
|
+
unicode: boolean;
|
|
195
|
+
} | {
|
|
196
|
+
kind: "Letter";
|
|
197
|
+
letterCase: LetterCase;
|
|
198
|
+
unicode: boolean;
|
|
199
|
+
} | {
|
|
200
|
+
kind: "HexDigit";
|
|
201
|
+
uppercaseOnly?: boolean;
|
|
202
|
+
} | {
|
|
203
|
+
kind: "Start";
|
|
204
|
+
} | {
|
|
205
|
+
kind: "End";
|
|
206
|
+
} | {
|
|
207
|
+
kind: "WordBoundary";
|
|
208
|
+
} | {
|
|
209
|
+
kind: "Sequence";
|
|
210
|
+
children: JsonPattern[];
|
|
211
|
+
} | {
|
|
212
|
+
kind: "Choice";
|
|
213
|
+
children: JsonPattern[];
|
|
214
|
+
} | {
|
|
215
|
+
kind: "Repeat";
|
|
216
|
+
child: JsonPattern;
|
|
217
|
+
min: number;
|
|
218
|
+
max: number | "__inf__";
|
|
219
|
+
greedy: boolean;
|
|
220
|
+
} | {
|
|
221
|
+
kind: "Optional";
|
|
222
|
+
child: JsonPattern;
|
|
223
|
+
greedy: boolean;
|
|
224
|
+
} | {
|
|
225
|
+
kind: "NamedGroup";
|
|
226
|
+
name: string;
|
|
227
|
+
child: JsonPattern;
|
|
228
|
+
} | {
|
|
229
|
+
kind: "NonCapturing";
|
|
230
|
+
child: JsonPattern;
|
|
231
|
+
} | {
|
|
232
|
+
kind: "RawRegex";
|
|
233
|
+
source: string;
|
|
234
|
+
flags?: PatternFlags;
|
|
235
|
+
trusted?: boolean;
|
|
236
|
+
};
|
|
237
|
+
declare function serializePattern(root: Pattern): SerializedPattern;
|
|
238
|
+
declare function deserializePattern(data: unknown): Pattern;
|
|
239
|
+
declare function patternToJsonString(root: Pattern, space?: number): string;
|
|
240
|
+
declare function patternFromJsonString(text: string): Pattern;
|
|
241
|
+
|
|
242
|
+
/** Compile any AST into an executable pattern wrapper. */
|
|
243
|
+
declare function compile<Captures extends Record<string, string> = Record<string, never>>(ast: Pattern, options?: CompileOptions): CompiledPattern<Captures>;
|
|
244
|
+
declare class CompiledPattern<Captures extends Record<string, string> = Record<string, never>> {
|
|
245
|
+
readonly ast: Pattern;
|
|
246
|
+
private readonly compileOpts;
|
|
247
|
+
constructor(ast: Pattern, compileOpts?: CompileOptions);
|
|
248
|
+
/** Compiled regex source (body only). */
|
|
249
|
+
get source(): string;
|
|
250
|
+
/** Engine flags string, e.g. `"iu"`. */
|
|
251
|
+
get flags(): string;
|
|
252
|
+
/** Compiler warnings (for example raw-regex notices). */
|
|
253
|
+
get warnings(): CompileWarning[];
|
|
254
|
+
toRegExp(): RegExp;
|
|
255
|
+
test(input: string): boolean;
|
|
256
|
+
exec(input: string): RegExpExecArray | null;
|
|
257
|
+
explain(): ExplainResult;
|
|
258
|
+
diagnose(input: string): DiagnoseResult;
|
|
259
|
+
analyze(): AnalysisFinding[];
|
|
260
|
+
toJSON(): SerializedPattern;
|
|
261
|
+
toJSONString(space?: number): string;
|
|
262
|
+
}
|
|
263
|
+
type MatchBuilderOptions = {
|
|
264
|
+
flags?: PatternFlags;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
declare class MatchBuilder<Captures extends Record<string, string> = Record<string, never>> {
|
|
268
|
+
private readonly defaults;
|
|
269
|
+
private readonly parts;
|
|
270
|
+
constructor(defaults?: MatchBuilderOptions);
|
|
271
|
+
/** Anchor start (`^`). */
|
|
272
|
+
start(): this;
|
|
273
|
+
/** Anchor end (`$`). */
|
|
274
|
+
end(): this;
|
|
275
|
+
boundary(): this;
|
|
276
|
+
/** Literal text segment (escaped on compile). */
|
|
277
|
+
text(value: string): this;
|
|
278
|
+
/** Alias of `text`. */
|
|
279
|
+
literal(value: string): this;
|
|
280
|
+
dash(): this;
|
|
281
|
+
/** Append any composed pattern fragment. */
|
|
282
|
+
take(fragment: Pattern): this;
|
|
283
|
+
digit(): this;
|
|
284
|
+
lettersUpper(): this;
|
|
285
|
+
lettersLower(): this;
|
|
286
|
+
named<K extends string>(name: K, inner: Pattern): MatchBuilder<Captures & Record<K, string>>;
|
|
287
|
+
build(): Pattern;
|
|
288
|
+
compile(options?: CompileOptions): CompiledPattern<Captures>;
|
|
289
|
+
}
|
|
290
|
+
declare function match(opts?: MatchBuilderOptions): MatchBuilder;
|
|
291
|
+
/** Alias matching the conceptual name from the design doc. */
|
|
292
|
+
declare function regex(opts?: MatchBuilderOptions): MatchBuilder;
|
|
293
|
+
|
|
294
|
+
type HexColorOpts = {
|
|
295
|
+
/** Allow `#RGB` shorthand */
|
|
296
|
+
short?: boolean;
|
|
297
|
+
/** Allow `#RRGGBBAA` */
|
|
298
|
+
alpha?: boolean;
|
|
299
|
+
};
|
|
300
|
+
declare const presets: {
|
|
301
|
+
/** RFC 4122 UUID (case-insensitive hex). Version nibble not enforced here. */
|
|
302
|
+
uuid(): Pattern;
|
|
303
|
+
/** Conservative ASCII slug: lowercase letters, digits, single dashes between segments. */
|
|
304
|
+
slug(): Pattern;
|
|
305
|
+
hexColor(opts?: HexColorOpts): Pattern;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
export { type AnalysisFinding, type CompileOptions, type CompileResult, type CompileWarning, CompiledPattern, type DiagnoseFail, type DiagnoseOk, type DiagnoseResult, type DigitOpts, type ExplainClause, type ExplainResult, type HexColorOpts, type LetterCase, type LetterOpts, MatchBuilder, type MatchBuilderOptions, PATTERN_SCHEMA_VERSION, type Pattern, type PatternFlags, type Quantified, type SerializedPattern, alt, analyzePattern, anyChar, booleanLiteral, compile, compilePattern, dash, deserializePattern, diagnose, digit, dot, end, explainPattern, hexDigit, integer, letter, literal, match, namedGroup, nonCapturing, optimize, optional, patternFromJsonString, patternToJsonString, presets, raw, regex, repeat, seq, serializePattern, start, toRegExp, underscore, whitespace, word, wordBoundary };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/** JSON schema version for serialized patterns. */
|
|
2
|
+
declare const PATTERN_SCHEMA_VERSION: 1;
|
|
3
|
+
type PatternFlags = {
|
|
4
|
+
/** Case-insensitive matching (`i`) */
|
|
5
|
+
ignoreCase?: boolean;
|
|
6
|
+
/** `m` — ^ and $ match line boundaries */
|
|
7
|
+
multiline?: boolean;
|
|
8
|
+
/** `s` — dot matches newline */
|
|
9
|
+
dotAll?: boolean;
|
|
10
|
+
/** `u` — Unicode mode */
|
|
11
|
+
unicode?: boolean;
|
|
12
|
+
/** Global (`g`) */
|
|
13
|
+
global?: boolean;
|
|
14
|
+
/** Sticky (`y`) */
|
|
15
|
+
sticky?: boolean;
|
|
16
|
+
};
|
|
17
|
+
type LetterCase = "lower" | "upper" | "both";
|
|
18
|
+
type Pattern = {
|
|
19
|
+
kind: "Literal";
|
|
20
|
+
value: string;
|
|
21
|
+
} | {
|
|
22
|
+
kind: "Any";
|
|
23
|
+
} | {
|
|
24
|
+
kind: "Digit";
|
|
25
|
+
unicode: boolean;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "Word";
|
|
28
|
+
unicode: boolean;
|
|
29
|
+
} | {
|
|
30
|
+
kind: "Whitespace";
|
|
31
|
+
unicode: boolean;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "Letter";
|
|
34
|
+
letterCase: LetterCase;
|
|
35
|
+
unicode: boolean;
|
|
36
|
+
} | {
|
|
37
|
+
kind: "HexDigit";
|
|
38
|
+
uppercaseOnly?: boolean;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "Start";
|
|
41
|
+
} | {
|
|
42
|
+
kind: "End";
|
|
43
|
+
} | {
|
|
44
|
+
kind: "WordBoundary";
|
|
45
|
+
} | {
|
|
46
|
+
kind: "Sequence";
|
|
47
|
+
children: readonly Pattern[];
|
|
48
|
+
} | {
|
|
49
|
+
kind: "Choice";
|
|
50
|
+
children: readonly Pattern[];
|
|
51
|
+
} | {
|
|
52
|
+
kind: "Repeat";
|
|
53
|
+
child: Pattern;
|
|
54
|
+
min: number;
|
|
55
|
+
max: number;
|
|
56
|
+
greedy: boolean;
|
|
57
|
+
} | {
|
|
58
|
+
kind: "Optional";
|
|
59
|
+
child: Pattern;
|
|
60
|
+
greedy: boolean;
|
|
61
|
+
} | {
|
|
62
|
+
kind: "NamedGroup";
|
|
63
|
+
name: string;
|
|
64
|
+
child: Pattern;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "NonCapturing";
|
|
67
|
+
child: Pattern;
|
|
68
|
+
} | {
|
|
69
|
+
kind: "RawRegex";
|
|
70
|
+
source: string;
|
|
71
|
+
/** Engine flags merged at compile time */
|
|
72
|
+
flags?: PatternFlags;
|
|
73
|
+
/** Documentation only — core never trusts user input */
|
|
74
|
+
trusted?: boolean;
|
|
75
|
+
};
|
|
76
|
+
declare function seq(...children: Pattern[]): Pattern;
|
|
77
|
+
declare function alt(...children: Pattern[]): Pattern;
|
|
78
|
+
declare function repeat(child: Pattern, min: number, max: number, greedy?: boolean): Pattern;
|
|
79
|
+
declare function optional(child: Pattern, greedy?: boolean): Pattern;
|
|
80
|
+
declare function namedGroup(name: string, child: Pattern): Pattern;
|
|
81
|
+
declare function nonCapturing(child: Pattern): Pattern;
|
|
82
|
+
declare function literal(value: string): Pattern;
|
|
83
|
+
declare function optimize(pattern: Pattern): Pattern;
|
|
84
|
+
|
|
85
|
+
type DigitOpts = {
|
|
86
|
+
unicode?: boolean;
|
|
87
|
+
};
|
|
88
|
+
type LetterOpts = {
|
|
89
|
+
case?: LetterCase;
|
|
90
|
+
unicode?: boolean;
|
|
91
|
+
};
|
|
92
|
+
type Quantified = Pattern & {
|
|
93
|
+
exactly(n: number): Pattern;
|
|
94
|
+
atLeast(n: number): Pattern;
|
|
95
|
+
atMost(n: number): Pattern;
|
|
96
|
+
between(min: number, max: number): Pattern;
|
|
97
|
+
oneOrMore(): Pattern;
|
|
98
|
+
zeroOrMore(): Pattern;
|
|
99
|
+
maybe(): Pattern;
|
|
100
|
+
};
|
|
101
|
+
declare function anyChar(): Quantified;
|
|
102
|
+
declare function digit(opts?: DigitOpts): Quantified;
|
|
103
|
+
declare function word(opts?: DigitOpts): Quantified;
|
|
104
|
+
declare function whitespace(opts?: DigitOpts): Quantified;
|
|
105
|
+
declare function letter(opts?: LetterOpts): Quantified;
|
|
106
|
+
declare function hexDigit(opts?: {
|
|
107
|
+
uppercaseOnly?: boolean;
|
|
108
|
+
}): Quantified;
|
|
109
|
+
declare function start(): Pattern;
|
|
110
|
+
declare function end(): Pattern;
|
|
111
|
+
declare function wordBoundary(): Pattern;
|
|
112
|
+
/** Escape hatch: embed raw regex fragment (analyzer flags this). */
|
|
113
|
+
declare function raw(source: string, flags?: PatternFlags): Pattern;
|
|
114
|
+
declare function dash(): Pattern;
|
|
115
|
+
declare function underscore(): Pattern;
|
|
116
|
+
/** Dot literal */
|
|
117
|
+
declare function dot(): Pattern;
|
|
118
|
+
/** Integer sequence (no sign) — digits only */
|
|
119
|
+
declare function integer(): Pattern;
|
|
120
|
+
/** Matches `true` or `false` literally */
|
|
121
|
+
declare function booleanLiteral(): Pattern;
|
|
122
|
+
|
|
123
|
+
type CompileTarget = "javascript";
|
|
124
|
+
type CompileOptions = {
|
|
125
|
+
target?: CompileTarget;
|
|
126
|
+
/** Base flags for the resulting RegExp */
|
|
127
|
+
flags?: PatternFlags;
|
|
128
|
+
/** When true, strip capturing groups (for simulation / diagnostics) */
|
|
129
|
+
nonCapturing?: boolean;
|
|
130
|
+
};
|
|
131
|
+
type CompileResult = {
|
|
132
|
+
pattern: string;
|
|
133
|
+
flags: string;
|
|
134
|
+
warnings: CompileWarning[];
|
|
135
|
+
};
|
|
136
|
+
type CompileWarning = {
|
|
137
|
+
code: string;
|
|
138
|
+
message: string;
|
|
139
|
+
};
|
|
140
|
+
declare function compilePattern(root: Pattern, options?: CompileOptions): CompileResult;
|
|
141
|
+
declare function toRegExp(root: Pattern, options?: CompileOptions): RegExp;
|
|
142
|
+
|
|
143
|
+
type ExplainClause = {
|
|
144
|
+
id: string;
|
|
145
|
+
text: string;
|
|
146
|
+
};
|
|
147
|
+
type ExplainResult = {
|
|
148
|
+
clauses: ExplainClause[];
|
|
149
|
+
summary: string;
|
|
150
|
+
};
|
|
151
|
+
/** Produce human-readable clauses and a short summary. */
|
|
152
|
+
declare function explainPattern(root: Pattern): ExplainResult;
|
|
153
|
+
|
|
154
|
+
type DiagnoseOk = {
|
|
155
|
+
ok: true;
|
|
156
|
+
match?: string;
|
|
157
|
+
index?: number;
|
|
158
|
+
groups?: Record<string, string>;
|
|
159
|
+
};
|
|
160
|
+
type DiagnoseFail = {
|
|
161
|
+
ok: false;
|
|
162
|
+
index: number;
|
|
163
|
+
message: string;
|
|
164
|
+
expected?: string;
|
|
165
|
+
};
|
|
166
|
+
type DiagnoseResult = DiagnoseOk | DiagnoseFail;
|
|
167
|
+
declare function diagnose(root: Pattern, input: string, options?: CompileOptions): DiagnoseResult;
|
|
168
|
+
|
|
169
|
+
type AnalysisFinding = {
|
|
170
|
+
severity: "info" | "warn" | "error";
|
|
171
|
+
code: string;
|
|
172
|
+
message: string;
|
|
173
|
+
};
|
|
174
|
+
/** Lightweight static hints (not a full ReDoS solver). */
|
|
175
|
+
declare function analyzePattern(root: Pattern): AnalysisFinding[];
|
|
176
|
+
|
|
177
|
+
type SerializedPattern = {
|
|
178
|
+
schemaVersion: typeof PATTERN_SCHEMA_VERSION;
|
|
179
|
+
pattern: JsonPattern;
|
|
180
|
+
};
|
|
181
|
+
type JsonPattern = {
|
|
182
|
+
kind: "Literal";
|
|
183
|
+
value: string;
|
|
184
|
+
} | {
|
|
185
|
+
kind: "Any";
|
|
186
|
+
} | {
|
|
187
|
+
kind: "Digit";
|
|
188
|
+
unicode: boolean;
|
|
189
|
+
} | {
|
|
190
|
+
kind: "Word";
|
|
191
|
+
unicode: boolean;
|
|
192
|
+
} | {
|
|
193
|
+
kind: "Whitespace";
|
|
194
|
+
unicode: boolean;
|
|
195
|
+
} | {
|
|
196
|
+
kind: "Letter";
|
|
197
|
+
letterCase: LetterCase;
|
|
198
|
+
unicode: boolean;
|
|
199
|
+
} | {
|
|
200
|
+
kind: "HexDigit";
|
|
201
|
+
uppercaseOnly?: boolean;
|
|
202
|
+
} | {
|
|
203
|
+
kind: "Start";
|
|
204
|
+
} | {
|
|
205
|
+
kind: "End";
|
|
206
|
+
} | {
|
|
207
|
+
kind: "WordBoundary";
|
|
208
|
+
} | {
|
|
209
|
+
kind: "Sequence";
|
|
210
|
+
children: JsonPattern[];
|
|
211
|
+
} | {
|
|
212
|
+
kind: "Choice";
|
|
213
|
+
children: JsonPattern[];
|
|
214
|
+
} | {
|
|
215
|
+
kind: "Repeat";
|
|
216
|
+
child: JsonPattern;
|
|
217
|
+
min: number;
|
|
218
|
+
max: number | "__inf__";
|
|
219
|
+
greedy: boolean;
|
|
220
|
+
} | {
|
|
221
|
+
kind: "Optional";
|
|
222
|
+
child: JsonPattern;
|
|
223
|
+
greedy: boolean;
|
|
224
|
+
} | {
|
|
225
|
+
kind: "NamedGroup";
|
|
226
|
+
name: string;
|
|
227
|
+
child: JsonPattern;
|
|
228
|
+
} | {
|
|
229
|
+
kind: "NonCapturing";
|
|
230
|
+
child: JsonPattern;
|
|
231
|
+
} | {
|
|
232
|
+
kind: "RawRegex";
|
|
233
|
+
source: string;
|
|
234
|
+
flags?: PatternFlags;
|
|
235
|
+
trusted?: boolean;
|
|
236
|
+
};
|
|
237
|
+
declare function serializePattern(root: Pattern): SerializedPattern;
|
|
238
|
+
declare function deserializePattern(data: unknown): Pattern;
|
|
239
|
+
declare function patternToJsonString(root: Pattern, space?: number): string;
|
|
240
|
+
declare function patternFromJsonString(text: string): Pattern;
|
|
241
|
+
|
|
242
|
+
/** Compile any AST into an executable pattern wrapper. */
|
|
243
|
+
declare function compile<Captures extends Record<string, string> = Record<string, never>>(ast: Pattern, options?: CompileOptions): CompiledPattern<Captures>;
|
|
244
|
+
declare class CompiledPattern<Captures extends Record<string, string> = Record<string, never>> {
|
|
245
|
+
readonly ast: Pattern;
|
|
246
|
+
private readonly compileOpts;
|
|
247
|
+
constructor(ast: Pattern, compileOpts?: CompileOptions);
|
|
248
|
+
/** Compiled regex source (body only). */
|
|
249
|
+
get source(): string;
|
|
250
|
+
/** Engine flags string, e.g. `"iu"`. */
|
|
251
|
+
get flags(): string;
|
|
252
|
+
/** Compiler warnings (for example raw-regex notices). */
|
|
253
|
+
get warnings(): CompileWarning[];
|
|
254
|
+
toRegExp(): RegExp;
|
|
255
|
+
test(input: string): boolean;
|
|
256
|
+
exec(input: string): RegExpExecArray | null;
|
|
257
|
+
explain(): ExplainResult;
|
|
258
|
+
diagnose(input: string): DiagnoseResult;
|
|
259
|
+
analyze(): AnalysisFinding[];
|
|
260
|
+
toJSON(): SerializedPattern;
|
|
261
|
+
toJSONString(space?: number): string;
|
|
262
|
+
}
|
|
263
|
+
type MatchBuilderOptions = {
|
|
264
|
+
flags?: PatternFlags;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
declare class MatchBuilder<Captures extends Record<string, string> = Record<string, never>> {
|
|
268
|
+
private readonly defaults;
|
|
269
|
+
private readonly parts;
|
|
270
|
+
constructor(defaults?: MatchBuilderOptions);
|
|
271
|
+
/** Anchor start (`^`). */
|
|
272
|
+
start(): this;
|
|
273
|
+
/** Anchor end (`$`). */
|
|
274
|
+
end(): this;
|
|
275
|
+
boundary(): this;
|
|
276
|
+
/** Literal text segment (escaped on compile). */
|
|
277
|
+
text(value: string): this;
|
|
278
|
+
/** Alias of `text`. */
|
|
279
|
+
literal(value: string): this;
|
|
280
|
+
dash(): this;
|
|
281
|
+
/** Append any composed pattern fragment. */
|
|
282
|
+
take(fragment: Pattern): this;
|
|
283
|
+
digit(): this;
|
|
284
|
+
lettersUpper(): this;
|
|
285
|
+
lettersLower(): this;
|
|
286
|
+
named<K extends string>(name: K, inner: Pattern): MatchBuilder<Captures & Record<K, string>>;
|
|
287
|
+
build(): Pattern;
|
|
288
|
+
compile(options?: CompileOptions): CompiledPattern<Captures>;
|
|
289
|
+
}
|
|
290
|
+
declare function match(opts?: MatchBuilderOptions): MatchBuilder;
|
|
291
|
+
/** Alias matching the conceptual name from the design doc. */
|
|
292
|
+
declare function regex(opts?: MatchBuilderOptions): MatchBuilder;
|
|
293
|
+
|
|
294
|
+
type HexColorOpts = {
|
|
295
|
+
/** Allow `#RGB` shorthand */
|
|
296
|
+
short?: boolean;
|
|
297
|
+
/** Allow `#RRGGBBAA` */
|
|
298
|
+
alpha?: boolean;
|
|
299
|
+
};
|
|
300
|
+
declare const presets: {
|
|
301
|
+
/** RFC 4122 UUID (case-insensitive hex). Version nibble not enforced here. */
|
|
302
|
+
uuid(): Pattern;
|
|
303
|
+
/** Conservative ASCII slug: lowercase letters, digits, single dashes between segments. */
|
|
304
|
+
slug(): Pattern;
|
|
305
|
+
hexColor(opts?: HexColorOpts): Pattern;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
export { type AnalysisFinding, type CompileOptions, type CompileResult, type CompileWarning, CompiledPattern, type DiagnoseFail, type DiagnoseOk, type DiagnoseResult, type DigitOpts, type ExplainClause, type ExplainResult, type HexColorOpts, type LetterCase, type LetterOpts, MatchBuilder, type MatchBuilderOptions, PATTERN_SCHEMA_VERSION, type Pattern, type PatternFlags, type Quantified, type SerializedPattern, alt, analyzePattern, anyChar, booleanLiteral, compile, compilePattern, dash, deserializePattern, diagnose, digit, dot, end, explainPattern, hexDigit, integer, letter, literal, match, namedGroup, nonCapturing, optimize, optional, patternFromJsonString, patternToJsonString, presets, raw, regex, repeat, seq, serializePattern, start, toRegExp, underscore, whitespace, word, wordBoundary };
|