forked from francisrstokes/super-expressive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
548 lines (463 loc) · 17.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
const asType = (type, opts = {}) => value => ({ type, value, ...opts });
const deferredType = type => {
const typeFn = asType (type);
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 quantifierTable = {
oneOrMore: '+',
oneOrMoreLazy: '+?',
zeroOrMore: '*',
zeroOrMoreLazy: '*?',
optional: '?',
exactly: metadata => `{${metadata}}`,
atLeast: metadata => `{${metadata},}`,
between: metadata => `{${metadata[0]},${metadata[1]}}`,
betweenLazy: metadata => `{${metadata[0]},${metadata[1]}}?`,
}
const t = {
root: asType('root') (),
startOfInput: asType('startOfInput') (),
endOfInput: asType('endOfInput') (),
capture: deferredType('capture'),
group: deferredType('group'),
anyOf: deferredType('anyOf'),
assertAhead: deferredType('assertAhead'),
assertNotAhead: deferredType('assertNotAhead'),
exactly: times => ({ ...deferredType('exactly'), metadata: times }),
atLeast: times => ({ ...deferredType('atLeast'), metadata: times }),
between: (x, y) => ({ ...deferredType('between'), metadata: [x, y] }),
betweenLazy: (x, y) => ({ ...deferredType('betweenLazy'), metadata: [x, y] }),
anyChar: asType('anyChar') (),
whitespaceChar: asType('whitespaceChar') (),
nonWhitespaceChar: asType('nonWhitespaceChar') (),
digit: asType('digit') (),
nonDigit: asType('nonDigit') (),
word: asType('word') (),
nonWord: asType('nonWord') (),
nonWord: asType('nonWord') (),
wordBoundary: asType('wordBoundary') (),
nonWordBoundary: asType('nonWordBoundary') (),
newline: asType('newline') (),
carriageReturn: asType('carriageReturn') (),
tab: asType('tab') (),
nullByte: asType('nullByte') (),
string: asType('string', { quantifierRequiresGroup: true }),
anyOfChars: asType('anyOfChars'),
anythingButString: asType('anythingButString'),
anythingButChars: asType('anythingButChars'),
anythingButRange: asType('anythingButRange'),
char: asType('char'),
range: asType('range'),
zeroOrMore: deferredType('zeroOrMore'),
zeroOrMoreLazy: deferredType('zeroOrMoreLazy'),
oneOrMore: deferredType('oneOrMore'),
oneOrMoreLazy: deferredType('oneOrMoreLazy'),
optional: deferredType('optional'),
}
const isFusable = element => {
return element.type === 'range' ||
element.type === 'char' ||
element.type === 'anyOfChars';
};
const fuseElements = elements => {
const [fusables, rest] = partition(isFusable, elements);
const fused = fusables.map(el => {
if (el.type === 'char' || el.type === 'anyOfChars') {
return el.value;
}
return `${el.value[0]}-${el.value[1]}`;
}).join('');
return [fused, rest];
}
const createStackFrame = type => ({ type, quantifier: null, elements: [] });
const assert = (condition, message) => {
if (!condition) {
throw new Error(message);
}
};
// 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');
class SuperExpressive {
constructor() {
this.state = {
hasDefinedStart: false,
hasDefinedEnd: false,
flags: {
g: false,
y: false,
m: false,
i: false,
u: false,
s: false
},
stack: [createStackFrame(t.root)],
namedGroups: []
}
}
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 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 nullByte() { return this[matchElement](t.nullByte); }
[frameCreatingElement](typeFn) {
const next = this[clone]();
const newFrame = createStackFrame(typeFn);
next.state.stack.push(newFrame);
return next;
}
get anyOf() { return this[frameCreatingElement](t.anyOf); }
get capture() { return this[frameCreatingElement](t.capture); }
get group() { return this[frameCreatingElement](t.group); }
get assertAhead() { return this[frameCreatingElement](t.assertAhead); }
get assertNotAhead() { return this[frameCreatingElement](t.assertNotAhead); }
[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;
}
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() {
if (this.state.hasDefinedEnd) {
throw new Error('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() {
const next = this[clone]();
if (next.state.stack.length === 1) {
throw new Error(`Cannot call end while building the root expression.`);
}
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(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;
}
range(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.range([strA, strB]);
const currentFrame = next[getCurrentFrame]();
currentFrame.elements.push(this[applyQuantifier](elementValue));
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 : '').join('');
return {
pattern: pattern === '' ? '(?:)' : pattern,
flags
};
}
[applyQuantifier](element) {
const currentFrame = this[getCurrentFrame]();
if (currentFrame.quantifier) {
const wrapped = currentFrame.quantifier.value(element);
wrapped.metadata = currentFrame.quantifier.metadata;
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 '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 'nullByte': return '\\0';
case 'string': return el.value;
case 'char': return 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 '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 'exactly': {
const inner = SuperExpressive[evaluate](el.value);
const withGroup = el.value.quantifierRequiresGroup
? `(?:${inner})`
: inner;
return `${withGroup}${quantifierTable[el.type](el.metadata)}`;
}
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 'assertNotAhead': {
const evaluated = el.value.map(SuperExpressive[evaluate]).join('');
return `(?!${evaluated})`;
}
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 '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;