-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathindex.js
901 lines (752 loc) · 30.4 KB
/
index.js
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
const assert = (condition, message) => {
if (!condition) {
throw new Error(message);
}
};
const asType = (type, opts = {}) => value => ({ type, value, ...opts });
const deferredType = (type, opts = {}) => {
const typeFn = asType (type, opts);
return typeFn (typeFn);
};
const deepCopy = o => {
if (Array.isArray(o)) {
return o.map(deepCopy);
}
if (Object.prototype.toString.call(o) === '[object Object]') {
return Object.entries(o).reduce((acc, [k, v]) => {
acc[k] = deepCopy(v);
return acc;
}, {});
}
return o;
}
const partition = (pred, a) => a.reduce((acc, cur) => {
if (pred(cur)) {
acc[0].push(cur);
} else {
acc[1].push(cur);
}
return acc;
}, [[], []]);
const specialChars = '\\.^$|?*+()[]{}-'.split('');
const replaceAll = (s, find, replace) => s.replace(new RegExp(`\\${find}`, 'g'), replace);
const escapeSpecial = s => specialChars.reduce((acc, char) => replaceAll(acc, char, `\\${char}`), s);
const namedGroupRegex = /^[a-z]+\w*$/i;
const singleUnicodeCharRegex = /^[^]$/u;
const controlCharRegex = /^[a-z]$/i;
const hexadecimalStringRegex = /^[0-9a-f]+$/i;
const quantifierTable = {
oneOrMore: '+',
oneOrMoreLazy: '+?',
zeroOrMore: '*',
zeroOrMoreLazy: '*?',
optional: '?',
exactly: times => `{${times}}`,
atLeast: times => `{${times},}`,
atLeastLazy: times => `{${times},}?`,
between: times => `{${times[0]},${times[1]}}`,
betweenLazy: times => `{${times[0]},${times[1]}}?`,
}
const applySubexpressionDefaults = expr => {
const out = { ...expr };
out.namespace = ('namespace' in out) ? out.namespace : '';
out.ignoreFlags = ('ignoreFlags' in out) ? out.ignoreFlags : true;
out.ignoreStartAndEnd = ('ignoreStartAndEnd' in out) ? out.ignoreStartAndEnd : true;
assert(typeof out.namespace === 'string', 'namespace must be a string');
assert(typeof out.ignoreFlags === 'boolean', 'ignoreFlags must be a boolean');
assert(typeof out.ignoreStartAndEnd === 'boolean', 'ignoreStartAndEnd must be a boolean');
return out;
}
const t = {
root: asType('root') (),
noop: asType('noop') (),
startOfInput: asType('startOfInput') (),
endOfInput: asType('endOfInput') (),
anyChar: asType('anyChar') (),
whitespaceChar: asType('whitespaceChar', { classCompatible: true }) (),
nonWhitespaceChar: asType('nonWhitespaceChar', { classCompatible: true }) (),
digit: asType('digit', { classCompatible: true }) (),
nonDigit: asType('nonDigit', { classCompatible: true }) (),
word: asType('word', { classCompatible: true }) (),
nonWord: asType('nonWord', { classCompatible: true }) (),
wordBoundary: asType('wordBoundary') (),
nonWordBoundary: asType('nonWordBoundary') (),
newline: asType('newline',{ classCompatible: true }) (),
carriageReturn: asType('carriageReturn', { classCompatible: true }) (),
tab: asType('tab', { classCompatible: true }) (),
verticalTab: asType('verticalTab', { classCompatible: true }) (),
formFeed: asType('formFeed', { classCompatible: true }) (),
backspace: asType('backspace', { classCompatible: true }) (),
nullByte: asType('nullByte', { classCompatible: true }) (),
anyOfChars: asType('anyOfChars', { classCompatible: true }),
anythingButString: asType('anythingButString'),
anythingButChars: asType('anythingButChars'),
anythingButRange: asType('anythingButRange'),
anythingBut: deferredType('anythingBut', { containsChildren: true }),
char: asType('char', { classCompatible: true }),
range: asType('range', { classCompatible: true }),
string: asType('string', { quantifierRequiresGroup: true }),
controlChar: asType('controlChar', { classCompatible: true }),
hexCode: asType('hexCode', { classCompatible: true }),
utf16Code: asType('utf16Code', { classCompatible: true }),
unicodeCharCode: asType('unicodeCharCode', { classCompatible: true }),
unicodeProperty: asType('unicodeProperty', { classCompatible: true }),
notUnicodeProperty: asType('notUnicodeProperty', { classCompatible: true }),
namedBackreference: name => deferredType('namedBackreference', { name }),
backreference: index => deferredType('backreference', { index }),
capture: deferredType('capture', { containsChildren: true }),
subexpression: asType('subexpression', { containsChildren: true, quantifierRequiresGroup: true }),
namedCapture: name => deferredType('namedCapture', { name, containsChildren: true }),
group: deferredType('group', { containsChildren: true }),
anyOf: deferredType('anyOf', { containsChildren: true }),
assertAhead: deferredType('assertAhead', { containsChildren: true }),
assertNotAhead: deferredType('assertNotAhead', { containsChildren: true }),
assertBehind: deferredType('assertBehind', { containsChildren: true }),
assertNotBehind: deferredType('assertNotBehind', { containsChildren: true }),
exactly: times => deferredType('exactly', { times, containsChild: true }),
atLeast: times => deferredType('atLeast', { times, containsChild: true }),
atLeastLazy: times => deferredType('atLeastLazy', { times, containsChild: true }),
between: (x, y) => deferredType('between', { times: [x, y], containsChild: true }),
betweenLazy: (x, y) => deferredType('betweenLazy', { times: [x, y], containsChild: true }),
zeroOrMore: deferredType('zeroOrMore', { containsChild: true }),
zeroOrMoreLazy: deferredType('zeroOrMoreLazy', { containsChild: true }),
oneOrMore: deferredType('oneOrMore', { containsChild: true }),
oneOrMoreLazy: deferredType('oneOrMoreLazy', { containsChild: true }),
optional: deferredType('optional', { containsChild: true }),
}
const isFusable = element => {
return element.classCompatible;
};
const fuseElements = elements => {
const [fusables, rest] = partition(isFusable, elements);
const fused = fusables.map(el => {
if (el.type === 'char' || el.type === 'anyOfChars') {
return el.value;
} else if (el.type === 'range') {
return `${el.value[0]}-${el.value[1]}`
} else if (el.type === 'backspace') {
return `\\b`;
}
return SuperExpressive[evaluate](el);;
}).join('');
return [fused, rest];
}
const createStackFrame = type => ({ type, quantifier: null, elements: [] });
// Symbols are used to create private methods
const clone = Symbol('clone');
const getCurrentFrame = Symbol('getCurrentFrame');
const getCurrentElementArray = Symbol('getCurrentElementArray');
const applyQuantifier = Symbol('applyQuantifier');
const evaluate = Symbol('evaluate');
const getRegexPatternAndFlags = Symbol('getRegexBody');
const matchElement = Symbol('matchElement');
const frameCreatingElement = Symbol('frameCreatingElement');
const quantifierElement = Symbol('quantifierElement');
const mergeSubexpression = Symbol('mergeSubexpression');
const trackNamedGroup = Symbol('trackNamedGroup');
class SuperExpressive {
constructor() {
this.state = {
hasDefinedStart: false,
hasDefinedEnd: false,
flags: {
d: false,
g: false,
y: false,
m: false,
i: false,
u: false,
s: false
},
stack: [createStackFrame(t.root)],
namedGroups: [],
totalCaptureGroups: 0
}
}
get allowMultipleMatches() {
const next = this[clone]();
next.state.flags.g = true;
return next;
}
get lineByLine() {
const next = this[clone]();
next.state.flags.m = true;
return next;
}
get caseInsensitive() {
const next = this[clone]();
next.state.flags.i = true;
return next;
}
get generateIndices() {
const next = this[clone]();
next.state.flags.d = true;
return next;
}
get sticky() {
const next = this[clone]();
next.state.flags.y = true;
return next;
}
get unicode() {
const next = this[clone]();
next.state.flags.u = true;
return next;
}
get singleLine() {
const next = this[clone]();
next.state.flags.s = true;
return next;
}
[matchElement](typeFn) {
const next = this[clone]();
next[getCurrentElementArray]().push(next[applyQuantifier](typeFn));
return next;
}
get anyChar() { return this[matchElement](t.anyChar); }
get whitespaceChar() { return this[matchElement](t.whitespaceChar); }
get nonWhitespaceChar() { return this[matchElement](t.nonWhitespaceChar); }
get digit() { return this[matchElement](t.digit); }
get nonDigit() { return this[matchElement](t.nonDigit); }
get word() { return this[matchElement](t.word); }
get nonWord() { return this[matchElement](t.nonWord); }
get wordBoundary() { return this[matchElement](t.wordBoundary); }
get nonWordBoundary() { return this[matchElement](t.nonWordBoundary); }
get newline() { return this[matchElement](t.newline); }
get carriageReturn() { return this[matchElement](t.carriageReturn); }
get tab() { return this[matchElement](t.tab); }
get verticalTab() { return this[matchElement](t.verticalTab); }
get formFeed() { return this[matchElement](t.formFeed); }
get backspace() { return this[matchElement](t.backspace); }
get nullByte() { return this[matchElement](t.nullByte); }
namedBackreference(name) {
assert(
this.state.namedGroups.includes(name),
`no capture group called "${name}" exists (create one with .namedCapture())`
);
return this[matchElement](t.namedBackreference(name));
}
backreference(index) {
assert(typeof index === 'number', 'index must be a number');
assert(
index > 0 && index <= this.state.totalCaptureGroups,
`invalid index ${index}. There are ${this.state.totalCaptureGroups} capture groups on this SuperExpression`
);
return this[matchElement](t.backreference(index));
}
[frameCreatingElement](typeFn) {
const next = this[clone]();
const newFrame = createStackFrame(typeFn);
next.state.stack.push(newFrame);
return next;
}
get anythingBut() { return this[frameCreatingElement](t.anythingBut); }
get anyOf() { return this[frameCreatingElement](t.anyOf); }
get group() { return this[frameCreatingElement](t.group); }
get assertAhead() { return this[frameCreatingElement](t.assertAhead); }
get assertNotAhead() { return this[frameCreatingElement](t.assertNotAhead); }
get assertBehind() { return this[frameCreatingElement](t.assertBehind); }
get assertNotBehind() { return this[frameCreatingElement](t.assertNotBehind); }
get capture() {
const next = this[clone]();
const newFrame = createStackFrame(t.capture);
next.state.stack.push(newFrame);
next.state.totalCaptureGroups++;
return next;
}
[trackNamedGroup](name) {
assert(typeof name === 'string', `name must be a string (got ${name})`);
assert(name.length > 0, `name must be at least one character`);
assert(!this.state.namedGroups.includes(name), `cannot use ${name} again for a capture group`);
assert(namedGroupRegex.test(name), `name "${name}" is not valid (only letters, numbers, and underscores)`);
this.state.namedGroups.push(name);
}
namedCapture(name) {
const next = this[clone]();
const newFrame = createStackFrame(t.namedCapture(name));
next[trackNamedGroup](name);
next.state.stack.push(newFrame);
next.state.totalCaptureGroups++;
return next;
}
[quantifierElement](typeFnName) {
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
if (currentFrame.quantifier) {
throw new Error(`cannot quantify regular expression with "${typeFnName}" because it's already being quantified with "${currentFrame.quantifier.type}"`);
}
currentFrame.quantifier = t[typeFnName];
return next;
}
get optional() { return this[quantifierElement]('optional'); }
get zeroOrMore() { return this[quantifierElement]('zeroOrMore'); }
get zeroOrMoreLazy() { return this[quantifierElement]('zeroOrMoreLazy'); }
get oneOrMore() { return this[quantifierElement]('oneOrMore'); }
get oneOrMoreLazy() { return this[quantifierElement]('oneOrMoreLazy'); }
exactly(n) {
assert(Number.isInteger(n) && n > 0, `n must be a positive integer (got ${n})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
if (currentFrame.quantifier) {
throw new Error(`cannot quantify regular expression with "exactly" because it's already being quantified with "${currentFrame.quantifier.type}"`);
}
currentFrame.quantifier = t.exactly(n);
return next;
}
atLeast(n) {
assert(Number.isInteger(n) && n > 0, `n must be a positive integer (got ${n})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
if (currentFrame.quantifier) {
throw new Error(`cannot quantify regular expression with "atLeast" because it's already being quantified with "${currentFrame.quantifier.type}"`);
}
currentFrame.quantifier = t.atLeast(n);
return next;
}
atLeastLazy(n) {
assert(Number.isInteger(n) && n > 0, `n must be a positive integer (got ${n})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
if (currentFrame.quantifier) {
throw new Error(`cannot quantify regular expression with "atLeastLazy" because it's already being quantified with "${currentFrame.quantifier.type}"`);
}
currentFrame.quantifier = t.atLeastLazy(n);
return next;
}
between(x, y) {
assert(Number.isInteger(x) && x >= 0, `x must be an integer (got ${x})`);
assert(Number.isInteger(y) && y > 0, `y must be an integer greater than 0 (got ${y})`);
assert(x < y, `x must be less than y (x = ${x}, y = ${y})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
if (currentFrame.quantifier) {
throw new Error(`cannot quantify regular expression with "between" because it's already being quantified with "${currentFrame.quantifier.type}"`);
}
currentFrame.quantifier = t.between(x, y);
return next;
}
betweenLazy(x, y) {
assert(Number.isInteger(x) && x >= 0, `x must be an integer (got ${x})`);
assert(Number.isInteger(y) && y > 0, `y must be an integer greater than 0 (got ${y})`);
assert(x < y, `x must be less than y (x = ${x}, y = ${y})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
if (currentFrame.quantifier) {
throw new Error(`cannot quantify regular expression with "betweenLazy" because it's already being quantified with "${currentFrame.quantifier.type}"`);
}
currentFrame.quantifier = t.betweenLazy(x, y);
return next;
}
get startOfInput() {
assert(!this.state.hasDefinedStart, 'This regex already has a defined start of input');
assert(!this.state.hasDefinedEnd, 'Cannot define the start of input after the end of input');
const next = this[clone]();
next.state.hasDefinedStart = true;
next[getCurrentElementArray]().push(t.startOfInput);
return next;
}
get endOfInput() {
assert(!this.state.hasDefinedEnd, 'This regex already has a defined end of input');
const next = this[clone]();
next.state.hasDefinedEnd = true;
next[getCurrentElementArray]().push(t.endOfInput);
return next;
}
anyOfChars(s) {
const next = this[clone]();
const elementValue = t.anyOfChars(escapeSpecial(s));
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](elementValue));
return next;
}
end() {
assert(this.state.stack.length > 1, 'Cannot call end while building the root expression.');
const next = this[clone]();
const oldFrame = next.state.stack.pop();
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](oldFrame.type.value(oldFrame.elements)));
return next;
}
anythingButString(str) {
assert(typeof str === 'string', `str must be a string (got ${str})`);
assert(str.length > 0, `str must have least one character`);
const next = this[clone]();
const elementValue = t.anythingButString(escapeSpecial(str));
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](elementValue));
return next;
}
anythingButChars(chars) {
assert(typeof chars === 'string', `chars must be a string (got ${chars})`);
assert(chars.length > 0, `chars must have at least one character`);
const next = this[clone]();
const elementValue = t.anythingButChars(escapeSpecial(chars));
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](elementValue));
return next;
}
anythingButRange(a, b) {
const strA = a.toString();
const strB = b.toString();
assert(strA.length === 1, `a must be a single character or number (got ${strA})`);
assert(strB.length === 1, `b must be a single character or number (got ${strB})`);
assert(strA.charCodeAt(0) < strB.charCodeAt(0), `a must have a smaller character value than b (a = ${strA.charCodeAt(0)}, b = ${strB.charCodeAt(0)})`);
const next = this[clone]();
const elementValue = t.anythingButRange([a, b]);
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](elementValue));
return next;
}
string(s) {
assert(typeof s === 'string', `s must be a string (got ${s})`);
assert(s.length > 0, `s cannot be an empty string`);
const next = this[clone]();
const elementValue = s.length > 1 ? t.string(escapeSpecial(s)) : t.char(escapeSpecial(s));
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](elementValue));
return next;
}
char(c) {
assert(typeof c === 'string', `c must be a string (got ${c})`);
assert(c.length === 1, `char() can only be called with a single character (got ${c})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](t.char(escapeSpecial(c))));
return next;
}
controlChar(c) {
assert(typeof c === 'string', `c must be a string (got ${c})`);
assert(controlCharRegex.test(c), `controlChar() can only be called with a single character from a-z (got ${c})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](t.controlChar(c.toUpperCase())));
return next;
}
hexCode(hex) {
assert(typeof hex === 'string', `hex must be a string (got ${hex})`);
assert(hex.length === 2, `hexCode() can only be called with a 2 character string (got ${hex})`);
assert(hexadecimalStringRegex.test(hex), `hex can only contain hexadecimal characters (got ${hex})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](t.hexCode(hex)));
return next;
}
utf16Code(hex) {
assert(typeof hex === 'string', `hex must be a string (got ${hex})`);
assert(hex.length === 4, `utf16Code() can only be called with a 4 character string (got ${hex})`);
assert(hexadecimalStringRegex.test(hex), `hex can only contain hexadecimal characters (got ${hex})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](t.utf16Code(hex)));
return next;
}
unicodeCharCode(hex) {
assert(typeof hex === 'string', `hex must be a string (got ${hex})`);
assert(hex.length === 4 || hex.length === 5, `unicodeCharCode() can only be called with a 4 or 5 character string (got ${hex})`);
assert(hexadecimalStringRegex.test(hex), `hex can only contain hexadecimal characters (got ${hex})`);
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
next.state.flags.u = true;
currentFrame.elements.push(next[applyQuantifier](t.unicodeCharCode(hex)));
return next;
}
unicodeProperty(property) {
assert(typeof property === 'string', `property must be a string (got ${property})`);
try { RegExp(`\\p{${property}}`, 'u');
} catch {
throw new Error(
`Property is not a valid Unicode property (got ${property}). ` +
`For valid properties see: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape`
);
}
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
next.state.flags.u = true;
currentFrame.elements.push(next[applyQuantifier](t.unicodeProperty(property)));
return next;
}
notUnicodeProperty(property) {
assert(typeof property === 'string', `property must be a string (got ${property})`);
try { RegExp(`\\P{${property}}`, 'u');
} catch {
throw new Error(
`Property is not a valid Unicode property (got ${property}). ` +
`For valid properties see: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape`
);
}
const next = this[clone]();
const currentFrame = next[getCurrentFrame]();
next.state.flags.u = true;
currentFrame.elements.push(next[applyQuantifier](t.notUnicodeProperty(property)));
return next;
}
range(a, b) {
const strA = a.toString();
const strB = b.toString();
assert(singleUnicodeCharRegex.test(strA), `a must be a single character or number (got ${strA})`);
assert(singleUnicodeCharRegex.test(strB), `b must be a single character or number (got ${strB})`);
assert(strA.codePointAt(0) < strB.codePointAt(0), `a must have a smaller character value than b (a = ${strA.codePointAt(0)}, b = ${strB.codePointAt(0)})`);
const next = this[clone]();
const elementValue = t.range([strA, strB]);
const currentFrame = next[getCurrentFrame]();
if (strA.length > 1 || strB.length > 1) {
next.state.flags.u = true;
}
currentFrame.elements.push(next[applyQuantifier](elementValue));
return next;
}
static [mergeSubexpression](el, options, parent, incrementCaptureGroups) {
let nextEl = deepCopy(el);
if (nextEl.type === 'backreference') {
nextEl.index += parent.state.totalCaptureGroups;
}
if (nextEl.type === 'capture') {
incrementCaptureGroups();
}
if (nextEl.type === 'namedCapture') {
const groupName = options.namespace
? `${options.namespace}${nextEl.name}`
: nextEl.name;
parent[trackNamedGroup](groupName);
nextEl.name = groupName;
}
if (nextEl.type === 'namedBackreference') {
nextEl.name = options.namespace
? `${options.namespace}${nextEl.name}`
: nextEl.name;
}
if (nextEl.containsChild) {
nextEl.value = SuperExpressive[mergeSubexpression](
nextEl.value,
options,
parent,
incrementCaptureGroups
);
} else if (nextEl.containsChildren) {
nextEl.value = nextEl.value.map(e =>
SuperExpressive[mergeSubexpression](
e,
options,
parent,
incrementCaptureGroups
)
);
}
if (nextEl.type === 'startOfInput') {
if (options.ignoreStartAndEnd) {
return t.noop;
}
assert(
!parent.state.hasDefinedStart,
'The parent regex already has a defined start of input. ' +
'You can ignore a subexpressions startOfInput/endOfInput markers with the ignoreStartAndEnd option'
);
assert(
!parent.state.hasDefinedEnd,
'The parent regex already has a defined end of input. ' +
'You can ignore a subexpressions startOfInput/endOfInput markers with the ignoreStartAndEnd option'
);
parent.state.hasDefinedStart = true;
}
if (nextEl.type === 'endOfInput') {
if (options.ignoreStartAndEnd) {
return t.noop;
}
assert(
!parent.state.hasDefinedEnd,
'The parent regex already has a defined start of input. ' +
'You can ignore a subexpressions startOfInput/endOfInput markers with the ignoreStartAndEnd option'
);
parent.state.hasDefinedEnd = true;
}
return nextEl;
}
subexpression(expr, opts = {}) {
assert(expr instanceof SuperExpressive, `expr must be a SuperExpressive instance`);
assert(
expr.state.stack.length === 1,
'Cannot call subexpression with a not yet fully specified regex object.' +
`\n(Try adding a .end() call to match the "${expr[getCurrentFrame]().type.type}" on the subexpression)\n`
);
const options = applySubexpressionDefaults(opts);
const exprNext = expr[clone]();
const next = this[clone]();
let additionalCaptureGroups = 0;
const exprFrame = exprNext[getCurrentFrame]();
exprFrame.elements = exprFrame.elements.map(e =>
SuperExpressive[mergeSubexpression](
e,
options,
next,
() => additionalCaptureGroups++
)
);
next.state.totalCaptureGroups += additionalCaptureGroups;
if (!options.ignoreFlags) {
Object.entries(exprNext.state.flags).forEach(([flagName, enabled]) => {
next.state.flags[flagName] = enabled || next.state.flags[flagName];
});
}
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(next[applyQuantifier](t.subexpression(exprFrame.elements)));
return next;
}
toRegexString() {
const {pattern, flags} = this[getRegexPatternAndFlags]();
return `/${pattern}/${flags}`;
}
toRegex() {
const {pattern, flags} = this[getRegexPatternAndFlags]();
return new RegExp(pattern, flags);
}
[getRegexPatternAndFlags]() {
assert(
this.state.stack.length === 1,
'Cannot compute the value of a not yet fully specified regex object.' +
`\n(Try adding a .end() call to match the "${this[getCurrentFrame]().type.type}")\n`
);
const pattern = this[getCurrentElementArray]().map(SuperExpressive[evaluate]).join('');
const flags = Object.entries(this.state.flags).map(([name, isOn]) => isOn ? name : '');
return {
pattern: pattern === '' ? '(?:)' : pattern,
flags: flags.sort().join('')
};
}
[applyQuantifier](element) {
const currentFrame = this[getCurrentFrame]();
if (currentFrame.quantifier) {
const wrapped = currentFrame.quantifier.value(element);
currentFrame.quantifier = null;
return wrapped;
}
return element;
}
[getCurrentFrame]() {
return this.state.stack[this.state.stack.length - 1];
}
[getCurrentElementArray]() {
return this[getCurrentFrame]().elements;
}
[clone]() {
const next = new SuperExpressive();
next.state = deepCopy(this.state);
return next;
}
static [evaluate](el) {
switch (el.type) {
case 'noop': return '';
case 'anyChar': return '.';
case 'whitespaceChar': return '\\s';
case 'nonWhitespaceChar': return '\\S';
case 'digit': return '\\d';
case 'nonDigit': return '\\D';
case 'word': return '\\w';
case 'nonWord': return '\\W';
case 'wordBoundary': return '\\b';
case 'nonWordBoundary': return '\\B';
case 'startOfInput': return '^';
case 'endOfInput': return '$';
case 'newline': return '\\n';
case 'carriageReturn': return '\\r';
case 'tab': return '\\t';
case 'verticalTab': return '\\v';
case 'formFeed': return '\\f';
case 'backspace': return '[\\b]';
case 'nullByte': return '\\0';
case 'string': return el.value;
case 'char': return el.value;
case 'controlChar': return `\\c${el.value}`;
case 'hexCode': return `\\x${el.value}`;
case 'utf16Code': return `\\u${el.value}`;
case 'unicodeCharCode': return `\\u{${el.value}}`;
case 'unicodeProperty': return `\\p{${el.value}}`;
case 'notUnicodeProperty': return `\\P{${el.value}}`;
case 'range': return `[${el.value[0]}-${el.value[1]}]`;
case 'anythingButRange': return `[^${el.value[0]}-${el.value[1]}]`;
case 'anyOfChars': return `[${el.value}]`;
case 'anythingButChars': return `[^${el.value}]`;
case 'namedBackreference': return `\\k<${el.name}>`;
case 'backreference': return `\\${el.index}`;
case 'subexpression': return el.value.map(SuperExpressive[evaluate]).join('');
case 'optional':
case 'zeroOrMore':
case 'zeroOrMoreLazy':
case 'oneOrMore':
case 'oneOrMoreLazy': {
const inner = SuperExpressive[evaluate](el.value);
const withGroup = el.value.quantifierRequiresGroup
? `(?:${inner})`
: inner;
const symbol = quantifierTable[el.type];
return `${withGroup}${symbol}`;
}
case 'betweenLazy':
case 'between':
case 'atLeast':
case 'atLeastLazy':
case 'exactly': {
const inner = SuperExpressive[evaluate](el.value);
const withGroup = el.value.quantifierRequiresGroup
? `(?:${inner})`
: inner;
return `${withGroup}${quantifierTable[el.type](el.times)}`;
}
case 'anythingButString': {
const chars = el.value.split('').map(c => `[^${c}]`).join('');
return `(?:${chars})`;
}
case 'assertAhead': {
const evaluated = el.value.map(SuperExpressive[evaluate]).join('');
return `(?=${evaluated})`;
}
case 'assertBehind': {
const evaluated = el.value.map(SuperExpressive[evaluate]).join('');
return `(?<=${evaluated})`;
}
case 'assertNotAhead': {
const evaluated = el.value.map(SuperExpressive[evaluate]).join('');
return `(?!${evaluated})`;
}
case 'assertNotBehind': {
const evaluated = el.value.map(SuperExpressive[evaluate]).join('');
return `(?<!${evaluated})`;
}
case 'anythingBut': {
const [fused, rest] = fuseElements(el.value);
if (!rest.length) {
return `[^${fused}]`;
}
const evaluatedRest = rest.map(SuperExpressive[evaluate]);
return `(?:(?!${evaluatedRest.join('|')})[^${fused}])`;
}
case 'anyOf': {
const [fused, rest] = fuseElements(el.value);
if (!rest.length) {
return `[${fused}]`;
}
const evaluatedRest = rest.map(SuperExpressive[evaluate]);
const separator = (evaluatedRest.length > 0 && fused.length > 0) ? '|' : '';
return `(?:${evaluatedRest.join('|')}${separator}${fused ? `[${fused}]` : ''})`;
}
case 'capture': {
const evaluated = el.value.map(SuperExpressive[evaluate]);
return `(${evaluated.join('')})`;
}
case 'namedCapture': {
const evaluated = el.value.map(SuperExpressive[evaluate]);
return `(?<${el.name}>${evaluated.join('')})`;
}
case 'group': {
const evaluated = el.value.map(SuperExpressive[evaluate]);
return `(?:${evaluated.join('')})`;
}
default: {
throw new Error(`Can't process unsupported element type: ${el.type}`);
}
}
}
static create() {
return new SuperExpressive();
}
}
module.exports = SuperExpressive.create;