-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.d.ts
315 lines (269 loc) · 9.04 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
declare module 'regexp-tree/ast' {
export interface AstClassMap {
'RegExp': AstRegExp;
'Disjunction': Disjunction;
'Alternative': Alternative;
'Assertion': Assertion;
'Char': Char;
'CharacterClass': CharacterClass;
'ClassRange': ClassRange;
'Backreference': Backreference;
'Group': Group;
'Repetition': Repetition;
'Quantifier': Quantifier;
'UnicodeProperty': UnicodeProperty;
}
export type AstClass = keyof AstClassMap;
export type AstNode = AstClassMap[AstClass];
export type AstNodeLocation = {
line: number;
column: number;
offset: number;
};
export interface Base<T extends AstClass> {
type: T;
loc?: {
source: string;
start: AstNodeLocation;
end: AstNodeLocation;
};
}
export interface SimpleChar extends Base<'Char'> {
value: string;
kind: 'simple';
escaped?: true;
codePoint: number;
}
export interface SpecialChar extends Base<'Char'> {
value: string;
kind: 'meta' | 'control' | 'hex' | 'decimal' | 'oct' | 'unicode';
codePoint: number;
}
export type Char =
| SimpleChar
| SpecialChar;
export interface ClassRange extends Base<'ClassRange'> {
from: Char;
to: Char;
}
export interface CharacterClass extends Base<'CharacterClass'> {
negative?: true;
expressions: (Char | ClassRange | UnicodeProperty)[];
}
export interface Alternative extends Base<'Alternative'> {
expressions: Expression[];
}
export interface Disjunction extends Base<'Disjunction'> {
left: Expression | null;
right: Expression | null;
}
export interface CapturingGroup extends Base<'Group'> {
capturing: true;
number: number;
name?: string;
nameRaw?: string;
expression: Expression | null;
}
export interface NoncapturingGroup extends Base<'Group'> {
capturing: false;
expression: Expression | null;
}
export type Group =
| CapturingGroup
| NoncapturingGroup;
export interface NumericBackreference extends Base<'Backreference'> {
kind: 'number';
number: number;
reference: number;
}
export interface NamedBackreference extends Base<'Backreference'> {
kind: 'name';
number: number;
reference: string;
referenceRaw: string;
}
export type Backreference =
| NumericBackreference
| NamedBackreference;
export interface Repetition extends Base<'Repetition'> {
expression: Expression;
quantifier: Quantifier;
}
export interface SimpleQuantifier extends Base<'Quantifier'> {
kind: '+' | '*' | '?';
greedy: boolean;
}
export interface RangeQuantifier extends Base<'Quantifier'> {
kind: 'Range';
from: number;
to?: number;
greedy: boolean;
}
export type Quantifier =
| SimpleQuantifier
| RangeQuantifier;
export interface SimpleAssertion extends Base<'Assertion'> {
kind: '^' | '$' | '\\b' | '\\B';
}
export interface LookaroundAssertion extends Base<'Assertion'> {
kind: 'Lookahead' | 'Lookbehind';
negative?: true;
assertion: Expression | null;
}
export type Assertion =
| SimpleAssertion
| LookaroundAssertion;
export interface UnicodeProperty extends Base<'UnicodeProperty'> {
name: string;
value: string;
negative?: true;
shorthand?: true;
binary?: true;
canonicalName?: string;
canonicalValue?: string;
}
export type Expression =
| Char
| CharacterClass
| Alternative
| Disjunction
| Group
| Backreference
| Repetition
| Assertion
| UnicodeProperty;
export interface AstRegExp extends Base<'RegExp'> {
body: Expression | null;
flags: string;
}
}
declare module 'regexp-tree' {
import {
AstRegExp,
AstNode,
AstClass,
AstClassMap
} from 'regexp-tree/ast'
export interface ParserOptions {
captureLocations?: boolean;
allowGroupNameDuplicates?: boolean;
}
/**
* Parses a regexp string, producing an AST.
*
* @param regexp a regular expression in different formats: string, AST, RegExp.
* @param options parsing options for this parse call.
*/
export function parse(regexp: string | RegExp, options?: ParserOptions): AstRegExp;
/**
* Generates a RegExp string from an AST.
*/
export function generate(ast: AstNode | null | undefined): string;
/**
* Creates a RegExp object from a regexp string.
*/
export function toRegExp(regexp: string): RegExp;
export interface NodePath<T extends AstNode = AstNode> {
node: T;
parent: AstNode | null;
parentPath: NodePath | null;
property: string | null;
index: number | null;
getParent(): NodePath | null;
getChild(n?: number): NodePath | null;
getPreviousSibling(): NodePath | null;
getNextSibling(): NodePath | null;
setChild<T extends AstNode>(node: T | null, index?: number | null, property?: string | null): NodePath<T> | null;
appendChild<T extends AstNode>(node: T | null, property?: string | null): NodePath<T> | null;
insertChildAt<T extends AstNode>(node: T | null, index: number, property?: string | null): void;
replace<T extends AstNode>(node: T): NodePath<T> | null;
update(nodeProps: Partial<T>): void;
remove(): void;
isRemoved(): boolean;
hasEqualSource(path: NodePath<T>): boolean;
jsonEncode(options?: { format?: string | number, useLoc?: boolean }): string;
}
export type NodeTraversalCallback<T extends AstNode = AstNode> = (node: T, parent: NodePath | null, property?: string, index?: number) => void | boolean;
export interface NodeTraversalCallbacks<T extends AstNode = AstNode> {
pre?: NodeTraversalCallback<T>;
post?: NodeTraversalCallback<T>;
}
export type SpecificNodeTraversalHandlers = {
[P in AstClass]?: NodeTraversalCallback<AstClassMap[P]> | NodeTraversalCallbacks<AstClassMap[P]>;
};
export interface NodeTraversalHandlers<T extends AstNode = AstNode> extends SpecificNodeTraversalHandlers {
'*'?: NodeTraversalCallback;
shouldRun?(ast: T): boolean;
init?(ast: T): void;
}
export type TraversalCallback<T extends AstNode = AstNode> = (path: NodePath<T>) => void | boolean;
export interface TraversalCallbacks<T extends AstNode = AstNode> {
pre?: TraversalCallback<T>;
post?: TraversalCallback<T>;
}
export type SpecificTraversalHandlers = {
[P in AstClass]?: TraversalCallback<AstClassMap[P]> | TraversalCallbacks<AstClassMap[P]>;
};
export interface TraversalHandlers<T extends AstNode = AstNode> extends SpecificTraversalHandlers {
'*'?: TraversalCallback;
shouldRun?(ast: T): boolean;
init?(ast: T): void;
}
/**
* Traverses a RegExp AST.
*
* @param handlers Each `handler` is an object containing handler function for needed
* node types. The value for a node type may also be an object with functions pre and post.
* This enables more context-aware analyses, e.g. measuring star height.
*
* @example
* regexpTree.traverse(ast, {
* onChar(node) {
* ...
* },
* });
*/
export function traverse<T extends AstNode>(ast: T, handlers: NodeTraversalHandlers<T> | ReadonlyArray<NodeTraversalHandlers<T>>, options: { asNodes: true }): void;
export function traverse<T extends AstNode>(ast: T, handlers: TraversalHandlers<T> | ReadonlyArray<TraversalHandlers<T>>, options?: { asNodes?: false }): void;
export type TransformHandlers<T extends AstNode = AstNode> = TraversalHandlers<T>;
export class TransformResult<T extends AstNode, E = unknown> {
private _ast;
private _source;
private _string;
private _regexp;
private _extra;
constructor(ast: T, extra?: E);
getAST(): T;
setExtra(extra: E): void;
getExtra(): E;
toRegExp(): RegExp;
getSource(): string;
getFlags(): string;
toString(): string;
}
/**
* Transforms a regular expression.
*
* A regexp can be passed in different formats (string, regexp or AST),
* applying a set of transformations. It is a convenient wrapper
* on top of "parse-traverse-generate" tool chain.
*/
export function transform<T extends AstNode>(ast: T, handlers: TraversalHandlers<T> | ReadonlyArray<TraversalHandlers<T>>): TransformResult<T>;
export function transform(regexp: string | RegExp, handlers: TransformHandlers<AstRegExp> | ReadonlyArray<TransformHandlers<AstRegExp>>): TransformResult<AstRegExp>;
/**
* Optimizes a regular expression by replacing some
* sub-expressions with their idiomatic patterns.
*/
export function optimize<T extends AstNode>(ast: T, whitelist?: string[]): TransformResult<T>;
export function optimize(regexp: string | RegExp, whitelist?: string[]): TransformResult<AstRegExp>;
/**
* Translates a regular expression in new syntax or in new format
* into equivalent expressions in old syntax.
*/
export function compatTranspile<T extends AstNode>(ast: T, whitelist?: string[]): TransformResult<T>;
export function compatTranspile(regexp: string | RegExp, whitelist?: string[]): TransformResult<AstRegExp>;
/**
* Executes a regular expression on a string.
*/
export function exec(re: string | RegExp, string: string): RegExpExecArray;
}