-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
regexp.ts
490 lines (367 loc) · 12.4 KB
/
regexp.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
interface String {
match< RE extends RegExp >( regexp: RE ): ReturnType<
RE[ typeof Symbol.match ]
>
matchAll< RE extends RegExp >( regexp: RE ): ReturnType<
RE[ typeof Symbol.matchAll ]
>
}
namespace $ {
type Groups_to_params<T> = {
[P in keyof T]?: T[P] | boolean | undefined;
};
export type $mol_regexp_source =
| number
| string
| RegExp
| { [ key in string ] : $mol_regexp_source }
| readonly[ $mol_regexp_source , ... $mol_regexp_source[] ]
export type $mol_regexp_groups< Source extends $mol_regexp_source >
= Source extends number
? {}
: Source extends string
? {}
: Source extends $mol_regexp_source[]
? $mol_type_merge< $mol_type_intersect< {
[ key in Extract< keyof Source , number > ] : $mol_regexp_groups< Source[ key ] >
}[ Extract< keyof Source , number > ] > >
: Source extends RegExp
? Record< string, string > extends NonNullable< NonNullable< ReturnType< Source['exec'] > >[ 'groups' ] >
? {}
: NonNullable< NonNullable< ReturnType< Source['exec'] > >[ 'groups' ] >
: Source extends { readonly [ key in string ] : $mol_regexp_source }
? $mol_type_merge< $mol_type_intersect< {
[ key in keyof Source ] :
$mol_type_merge<
& $mol_type_override<
{
readonly [ k in Extract< keyof Source , string > ]: string
},
{
readonly [ k in key ]:
Source[ key ] extends string
? Source[ key ]
: string
}
>
& $mol_regexp_groups< Source[ key ] >
>
}[ keyof Source ] > >
: never
/** Type safe reguar expression builder */
export class $mol_regexp< Groups extends Record< string , string > > extends RegExp {
/** Prefer to use $mol_regexp.from */
constructor( source : string , flags : string = 'gsu' , readonly groups : ( Extract< keyof Groups , string > )[] = [] ) {
super( source , flags )
}
*[Symbol.matchAll] (str:string): RegExpStringIterator< RegExpMatchArray & $mol_type_override< RegExpMatchArray, { groups?: { [ key in keyof Groups ] : string } } > > {
const index = this.lastIndex
this.lastIndex = 0
try {
while ( this.lastIndex < str.length ) {
const found = this.exec(str)
if( !found ) break
yield found
}
} finally {
this.lastIndex = index
}
}
/** Parses input and returns found capture groups or null */
[ Symbol.match ]( str : string ): null | RegExpMatchArray {
const res = [ ... this[Symbol.matchAll]( str ) ].filter( r => r.groups ).map( r => r[0] )
if( !res.length ) return null
return res as RegExpMatchArray
}
/** Splits string by regexp edges */
[ Symbol.split ]( str : string ): string[] {
const res = [] as string[]
let token_last = null
for( let token of this[Symbol.matchAll]( str ) ) {
if( token.groups && ( token_last ? token_last.groups : true ) ) res.push( '' )
res.push( token[0] )
token_last = token
}
if( !res.length ) res.push( '' )
return res
}
test( str : string ): boolean {
return Boolean( str.match( this) )
}
exec( str : string ): RegExpExecArray & $mol_type_override< RegExpExecArray , { groups?: { [ key in keyof Groups ] : string } } > | null {
const from = this.lastIndex
if( from >= str.length ) return null
const res = super.exec( str )
if( res === null ) {
this.lastIndex = str.length
if( !str ) return null
return Object.assign( [ str.slice( from ) ], {
index: from,
input: str,
} ) as any
}
if( from === this.lastIndex ) {
$mol_fail( new Error( 'Captured empty substring' ) )
}
type Token = { [ key in keyof Groups ] : string } & { [ key : number ] : string }
const groups = {} as Token
const skipped = str.slice( from , this.lastIndex - res[0].length )
if( skipped ) {
this.lastIndex = this.lastIndex - res[0].length
return Object.assign( [ skipped ], {
index: from,
input: res.input,
} ) as any
}
for( let i = 0 ; i < this.groups.length ; ++i ) {
const group = this.groups[ i ]
groups[ group ] = groups[ group ] || res[ i + 1 ] || '' as any
}
return Object.assign( res, { groups } )
}
generate(
params: Groups_to_params< Groups >
): string | null {
return null
}
get native() {
return new RegExp( this.source, this.flags )
}
/** Makes regexp that non-greedy repeats this pattern from min to max count */
static repeat<
Source extends $mol_regexp_source
>(
source : Source ,
min = 0 ,
max = Number.POSITIVE_INFINITY ,
) : $mol_regexp< $mol_regexp_groups< Source > > {
const regexp = $mol_regexp.from( source )
const upper = Number.isFinite( max ) ? max : ''
const str = `(?:${ regexp.source }){${ min },${ upper }}?`
const regexp2 = new $mol_regexp( str , regexp.flags , regexp.groups )
regexp2.generate = params => {
const res = regexp.generate( params )
if( res ) return res
if( min > 0 ) return res
return ''
}
return regexp2
}
/** Makes regexp that greedy repeats this pattern from min to max count */
static repeat_greedy<
Source extends $mol_regexp_source
>(
source : Source ,
min = 0 ,
max = Number.POSITIVE_INFINITY ,
) : $mol_regexp< $mol_regexp_groups< Source > > {
const regexp = $mol_regexp.from( source )
const upper = Number.isFinite( max ) ? max : ''
const str = `(?:${ regexp.source }){${ min },${ upper }}`
const regexp2 = new $mol_regexp( str , regexp.flags , regexp.groups )
regexp2.generate = params => {
const res = regexp.generate( params )
if( res ) return res
if( min > 0 ) return res
return ''
}
return regexp2
}
/** Makes regexp that match any of options */
static vary<
Sources extends readonly $mol_regexp_source[]
>(
sources : Sources ,
) {
const groups = [] as string[]
const chunks = sources.map( source => {
const regexp = $mol_regexp.from( source )
groups.push( ... regexp.groups )
return regexp.source
} ) as any as readonly[ $mol_regexp_source , ... $mol_regexp_source[] ]
return new $mol_regexp< $mol_regexp_groups< Sources[number] > >(
`(?:${ chunks.join('|') })` ,
'' ,
groups as any[] ,
)
}
/** Makes regexp that allow absent of this pattern */
static optional<
Source extends $mol_regexp_source
>( source : Source ) {
return $mol_regexp.repeat_greedy( source , 0 , 1 )
}
/** Makes regexp that look ahead for pattern */
static force_after( source : $mol_regexp_source ) {
const regexp = $mol_regexp.from( source )
return new $mol_regexp(
`(?=${ regexp.source })` ,
regexp.flags ,
regexp.groups ,
)
}
/** Makes regexp that look ahead for pattern */
static forbid_after( source : $mol_regexp_source ) {
const regexp = $mol_regexp.from( source )
return new $mol_regexp(
`(?!${ regexp.source })` ,
regexp.flags ,
regexp.groups ,
)
}
/** Converts some js values to regexp */
static from<
Source extends $mol_regexp_source
>(
source : Source ,
{ ignoreCase , multiline } : Partial< Pick< RegExp , 'ignoreCase' | 'multiline' > > = {
ignoreCase : false ,
multiline : false ,
} ,
) : $mol_regexp< $mol_regexp_groups< Source > > {
let flags = 'gsu'
if( multiline ) flags += 'm'
if( ignoreCase ) flags += 'i'
if( typeof source === 'number' ) {
const src = `\\u{${ source.toString(16) }}`
const regexp = new $mol_regexp< $mol_regexp_groups< Source > >( src , flags )
regexp.generate = ()=> src
return regexp
} if( typeof source === 'string' ) {
const src = source.replace( /[.*+?^${}()|[\]\\]/g , '\\$&' )
const regexp = new $mol_regexp< $mol_regexp_groups< Source > >( src , flags )
regexp.generate = ()=> source
return regexp
} else if( source instanceof $mol_regexp ) {
const regexp = new $mol_regexp<any>( source.source, flags, source.groups )
regexp.generate = params => source.generate( params )
return regexp
} if( source instanceof RegExp ) {
const test = new RegExp( '|' + source.source )
const groups = Array.from(
{ length : test.exec('')!.length - 1 } ,
( _ , i )=> String( i + 1 ) ,
)
const regexp = new $mol_regexp< $mol_regexp_groups< Source > >(
source.source ,
source.flags ,
groups as any ,
)
regexp.generate = ()=> ''
return regexp
} if( Array.isArray( source ) ) {
const patterns = source.map( src => Array.isArray( src )
? $mol_regexp.optional( src as any )
: $mol_regexp.from( src )
)
const chunks = patterns.map( pattern => pattern.source )
const groups = [] as ( Extract< keyof $mol_regexp_groups< Source > , string > )[]
let index = 0
for( const pattern of patterns ) {
for( let group of pattern.groups ) {
if( Number( group ) >= 0 ) {
groups.push( String( index ++ ) as any )
} else {
groups.push( group )
}
}
}
const regexp = new $mol_regexp( chunks.join( '' ) , flags , groups )
regexp.generate = params => {
let res = ''
for( const pattern of patterns ) {
let sub = pattern.generate( params )
if( sub === null ) return ''
res += sub
}
return res
}
return regexp
} else {
const groups = [] as string[]
const chunks = Object.keys( source ).map( name => {
groups.push( name )
const regexp = $mol_regexp.from( (source as any)[ name ] )
groups.push( ... regexp.groups )
return `(${regexp.source})`
} ) as any as readonly[ $mol_regexp_source , ... $mol_regexp_source[] ]
const regexp = new $mol_regexp< $mol_regexp_groups< Source > >(
`(?:${ chunks.join('|') })` ,
flags ,
groups as any[] ,
)
const validator = new RegExp( '^' + regexp.source + '$', flags )
regexp.generate = (params: any) => {
for( let option in source ) {
if( option in params ) {
if( typeof params[ option ] === 'boolean' ) {
if( !params[ option as any ] ) continue
} else {
const str = String( params[ option ] )
if( str.match( validator ) ) return str
$mol_fail( new Error( `Wrong param: ${option}=${str}` ) )
}
} else {
if( typeof (source as any)[ option ] !== 'object' ) continue
}
const res = $mol_regexp.from( (source as any)[ option ] ).generate( params )
if( res ) return res
}
return null
}
return regexp
}
}
/** Makes regexp which includes only unicode category */
static unicode_only( ... category: $mol_unicode_category ) {
return new $mol_regexp(
`\\p{${ category.join( '=' ) }}`
)
}
/** Makes regexp which excludes unicode category */
static unicode_except( ... category: $mol_unicode_category ) {
return new $mol_regexp(
`\\P{${ category.join( '=' ) }}`
)
}
static char_range(
from: number,
to: number,
): $mol_regexp<{}> {
return new $mol_regexp(
`${ $mol_regexp.from( from ).source }-${ $mol_regexp.from( to ).source }`
)
}
static char_only(
... allowed: readonly [ $mol_regexp_source, ... $mol_regexp_source[] ]
): $mol_regexp<{}> {
const regexp = allowed.map( f => $mol_regexp.from( f ).source ).join('')
return new $mol_regexp( `[${ regexp }]` )
}
static char_except(
... forbidden: readonly [ $mol_regexp_source, ... $mol_regexp_source[] ]
): $mol_regexp<{}> {
const regexp = forbidden.map( f => $mol_regexp.from( f ).source ).join('')
return new $mol_regexp( `[^${ regexp }]` )
}
static decimal_only = $mol_regexp.from( /\d/gsu )
static decimal_except = $mol_regexp.from( /\D/gsu )
static latin_only = $mol_regexp.from( /\w/gsu )
static latin_except = $mol_regexp.from( /\W/gsu )
static space_only = $mol_regexp.from( /\s/gsu )
static space_except = $mol_regexp.from( /\S/gsu )
static word_break_only = $mol_regexp.from( /\b/gsu )
static word_break_except = $mol_regexp.from( /\B/gsu )
static tab = $mol_regexp.from( /\t/gsu )
static slash_back = $mol_regexp.from( /\\/gsu )
static nul = $mol_regexp.from( /\0/gsu )
static char_any = $mol_regexp.from( /./gsu )
static begin = $mol_regexp.from( /^/gsu )
static end = $mol_regexp.from( /$/gsu )
static or = $mol_regexp.from( /|/gsu )
static line_end = $mol_regexp.from({
win_end: [ [ '\r' ], '\n' ],
mac_end: '\r',
})
}
}