libphonenumber-js 1.9.39 → 1.9.40
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/CHANGELOG.md +5 -0
- package/build/AsYouType.test.js +25 -3
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternMatcher.js +435 -0
- package/build/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
- package/build/AsYouTypeFormatter.PatternMatcher.test.js +447 -0
- package/build/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
- package/build/AsYouTypeFormatter.js +45 -10
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/es6/AsYouType.test.js +25 -3
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternMatcher.js +428 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js +442 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
- package/es6/AsYouTypeFormatter.js +44 -10
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/package.json +1 -1
- package/source/AsYouType.test.js +26 -3
- package/source/AsYouTypeFormatter.PatternMatcher.js +343 -0
- package/source/AsYouTypeFormatter.PatternMatcher.test.js +596 -0
- package/source/AsYouTypeFormatter.js +47 -9
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
2
|
+
|
|
3
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
4
|
+
|
|
5
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
6
|
+
|
|
7
|
+
var PatternMatcher =
|
|
8
|
+
/*#__PURE__*/
|
|
9
|
+
function () {
|
|
10
|
+
function PatternMatcher(pattern) {
|
|
11
|
+
_classCallCheck(this, PatternMatcher);
|
|
12
|
+
|
|
13
|
+
this.matchTree = new PatternParser().parse(pattern); // console.log(JSON.stringify(this.matchTree, null, 2))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_createClass(PatternMatcher, [{
|
|
17
|
+
key: "match",
|
|
18
|
+
value: function match(string) {
|
|
19
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
20
|
+
allowOverflow = _ref.allowOverflow;
|
|
21
|
+
|
|
22
|
+
if (!string) {
|
|
23
|
+
throw new Error('String is required');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var result = _match(string.split(''), this.matchTree, true);
|
|
27
|
+
|
|
28
|
+
if (result && result.match) {
|
|
29
|
+
delete result.matchedChars;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (result && result.overflow) {
|
|
33
|
+
if (!allowOverflow) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
}]);
|
|
41
|
+
|
|
42
|
+
return PatternMatcher;
|
|
43
|
+
}();
|
|
44
|
+
|
|
45
|
+
export { PatternMatcher as default };
|
|
46
|
+
|
|
47
|
+
function _match(characters, tree, last) {
|
|
48
|
+
if (typeof tree === 'string') {
|
|
49
|
+
if (last) {
|
|
50
|
+
if (characters.length > tree.length) {
|
|
51
|
+
return {
|
|
52
|
+
overflow: true
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
var characterString = characters.join('');
|
|
58
|
+
|
|
59
|
+
if (tree.indexOf(characterString) === 0) {
|
|
60
|
+
if (characters.length === tree.length) {
|
|
61
|
+
return {
|
|
62
|
+
match: true,
|
|
63
|
+
matchedChars: characters
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
partialMatch: true // matchedChars: characters
|
|
69
|
+
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (characterString.indexOf(tree) === 0) {
|
|
74
|
+
return {
|
|
75
|
+
match: true,
|
|
76
|
+
matchedChars: characters.slice(0, tree.length)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (Array.isArray(tree)) {
|
|
84
|
+
var restCharacters = characters.slice();
|
|
85
|
+
var i = 0;
|
|
86
|
+
|
|
87
|
+
while (i < tree.length) {
|
|
88
|
+
var subtree = tree[i];
|
|
89
|
+
|
|
90
|
+
var result = _match(restCharacters, subtree, last && i === tree.length - 1);
|
|
91
|
+
|
|
92
|
+
if (!result || result.overflow) {
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (result.match) {
|
|
97
|
+
// Continue with the next subtree with the rest of the characters.
|
|
98
|
+
restCharacters = restCharacters.slice(result.matchedChars.length);
|
|
99
|
+
|
|
100
|
+
if (restCharacters.length === 0) {
|
|
101
|
+
if (i === tree.length - 1) {
|
|
102
|
+
return {
|
|
103
|
+
match: true,
|
|
104
|
+
matchedChars: characters
|
|
105
|
+
};
|
|
106
|
+
} else {
|
|
107
|
+
return {
|
|
108
|
+
partialMatch: true // matchedChars: characters
|
|
109
|
+
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} else if (result.partialMatch) {
|
|
114
|
+
return {
|
|
115
|
+
partialMatch: true // matchedChars: characters
|
|
116
|
+
|
|
117
|
+
};
|
|
118
|
+
} else {
|
|
119
|
+
throw new Error("Unsupported match result:\n".concat(JSON.stringify(result, null, 2)));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
i++;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (last) {
|
|
126
|
+
return {
|
|
127
|
+
overflow: true
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
match: true,
|
|
133
|
+
matchedChars: characters.slice(0, characters.length - restCharacters.length)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
switch (tree.op) {
|
|
138
|
+
case '|':
|
|
139
|
+
var partialMatch;
|
|
140
|
+
|
|
141
|
+
for (var _iterator = tree.args, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|
142
|
+
var _ref2;
|
|
143
|
+
|
|
144
|
+
if (_isArray) {
|
|
145
|
+
if (_i >= _iterator.length) break;
|
|
146
|
+
_ref2 = _iterator[_i++];
|
|
147
|
+
} else {
|
|
148
|
+
_i = _iterator.next();
|
|
149
|
+
if (_i.done) break;
|
|
150
|
+
_ref2 = _i.value;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
var branch = _ref2;
|
|
154
|
+
|
|
155
|
+
var _result = _match(characters, branch, last);
|
|
156
|
+
|
|
157
|
+
if (_result) {
|
|
158
|
+
if (_result.overflow) {
|
|
159
|
+
return _result;
|
|
160
|
+
} else if (_result.match) {
|
|
161
|
+
return {
|
|
162
|
+
match: true,
|
|
163
|
+
matchedChars: _result.matchedChars
|
|
164
|
+
};
|
|
165
|
+
} else if (_result.partialMatch) {
|
|
166
|
+
partialMatch = true;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (partialMatch) {
|
|
172
|
+
return {
|
|
173
|
+
partialMatch: true // matchedChars: ...
|
|
174
|
+
|
|
175
|
+
};
|
|
176
|
+
} // Not even a partial match.
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
return;
|
|
180
|
+
|
|
181
|
+
case '[]':
|
|
182
|
+
for (var _iterator2 = tree.args, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
|
183
|
+
var _ref3;
|
|
184
|
+
|
|
185
|
+
if (_isArray2) {
|
|
186
|
+
if (_i2 >= _iterator2.length) break;
|
|
187
|
+
_ref3 = _iterator2[_i2++];
|
|
188
|
+
} else {
|
|
189
|
+
_i2 = _iterator2.next();
|
|
190
|
+
if (_i2.done) break;
|
|
191
|
+
_ref3 = _i2.value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
var _char = _ref3;
|
|
195
|
+
|
|
196
|
+
if (characters[0] === _char) {
|
|
197
|
+
if (characters.length === 1) {
|
|
198
|
+
return {
|
|
199
|
+
match: true,
|
|
200
|
+
matchedChars: characters
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (last) {
|
|
205
|
+
return {
|
|
206
|
+
overflow: true
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
match: true,
|
|
212
|
+
matchedChars: [_char]
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
} // No character matches.
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
return;
|
|
219
|
+
|
|
220
|
+
default:
|
|
221
|
+
throw new Error("Unsupported instruction tree: ".concat(tree));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
var OPERATOR = new RegExp( // any of:
|
|
226
|
+
'(' + // or operator
|
|
227
|
+
'\\|' + // or
|
|
228
|
+
'|' + // or group start
|
|
229
|
+
'\\(\\?\\:' + // or
|
|
230
|
+
'|' + // or group end
|
|
231
|
+
'\\)' + // or
|
|
232
|
+
'|' + // one-of set start
|
|
233
|
+
'\\[' + // or
|
|
234
|
+
'|' + // one-of set end
|
|
235
|
+
'\\]' + ')');
|
|
236
|
+
|
|
237
|
+
var PatternParser =
|
|
238
|
+
/*#__PURE__*/
|
|
239
|
+
function () {
|
|
240
|
+
function PatternParser() {
|
|
241
|
+
_classCallCheck(this, PatternParser);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
_createClass(PatternParser, [{
|
|
245
|
+
key: "parse",
|
|
246
|
+
value: function parse(pattern) {
|
|
247
|
+
this.context = [{
|
|
248
|
+
or: true,
|
|
249
|
+
instructions: []
|
|
250
|
+
}];
|
|
251
|
+
this.parsePattern(pattern);
|
|
252
|
+
|
|
253
|
+
if (this.context.length !== 1) {
|
|
254
|
+
throw new Error('Non-finalized contexts left when pattern parse ended');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
var _this$context$ = this.context[0],
|
|
258
|
+
branches = _this$context$.branches,
|
|
259
|
+
instructions = _this$context$.instructions;
|
|
260
|
+
|
|
261
|
+
if (branches) {
|
|
262
|
+
return [{
|
|
263
|
+
op: '|',
|
|
264
|
+
args: branches.concat([instructions])
|
|
265
|
+
}];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (instructions.length === 0) {
|
|
269
|
+
throw new Error('Pattern is required');
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return instructions;
|
|
273
|
+
}
|
|
274
|
+
}, {
|
|
275
|
+
key: "startContext",
|
|
276
|
+
value: function startContext(context) {
|
|
277
|
+
this.context.push(context);
|
|
278
|
+
}
|
|
279
|
+
}, {
|
|
280
|
+
key: "endContext",
|
|
281
|
+
value: function endContext() {
|
|
282
|
+
this.context.pop();
|
|
283
|
+
}
|
|
284
|
+
}, {
|
|
285
|
+
key: "getContext",
|
|
286
|
+
value: function getContext() {
|
|
287
|
+
return this.context[this.context.length - 1];
|
|
288
|
+
}
|
|
289
|
+
}, {
|
|
290
|
+
key: "parsePattern",
|
|
291
|
+
value: function parsePattern(pattern) {
|
|
292
|
+
if (!pattern) {
|
|
293
|
+
throw new Error('Empty pattern passed');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
var match = pattern.match(OPERATOR);
|
|
297
|
+
|
|
298
|
+
if (!match) {
|
|
299
|
+
this.getContext().instructions = this.getContext().instructions.concat(pattern.split(''));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
var operator = match[1];
|
|
304
|
+
var before = pattern.slice(0, match.index);
|
|
305
|
+
var rightPart = pattern.slice(match.index + operator.length);
|
|
306
|
+
|
|
307
|
+
switch (operator) {
|
|
308
|
+
case '(?:':
|
|
309
|
+
if (before) {
|
|
310
|
+
this.parsePattern(before);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.startContext({
|
|
314
|
+
or: true,
|
|
315
|
+
instructions: [],
|
|
316
|
+
branches: []
|
|
317
|
+
});
|
|
318
|
+
break;
|
|
319
|
+
|
|
320
|
+
case ')':
|
|
321
|
+
if (!this.getContext().or) {
|
|
322
|
+
throw new Error('")" operator must be preceded by "(?:" operator');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (before) {
|
|
326
|
+
this.parsePattern(before);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (this.getContext().instructions.length === 0) {
|
|
330
|
+
throw new Error('No instructions found after "|" operator in an "or" group');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
var _this$getContext = this.getContext(),
|
|
334
|
+
branches = _this$getContext.branches;
|
|
335
|
+
|
|
336
|
+
branches.push(this.getContext().instructions);
|
|
337
|
+
this.endContext();
|
|
338
|
+
this.getContext().instructions.push({
|
|
339
|
+
op: '|',
|
|
340
|
+
args: branches
|
|
341
|
+
});
|
|
342
|
+
break;
|
|
343
|
+
|
|
344
|
+
case '|':
|
|
345
|
+
if (!this.getContext().or) {
|
|
346
|
+
throw new Error('"|" operator can only be used inside "or" groups');
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (before) {
|
|
350
|
+
this.parsePattern(before);
|
|
351
|
+
} // The top-level is an implicit "or" group, if required.
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
if (!this.getContext().branches) {
|
|
355
|
+
if (this.context.length === 1) {
|
|
356
|
+
this.getContext().branches = [];
|
|
357
|
+
} else {
|
|
358
|
+
throw new Error('"branches" not found in an "or" group context');
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
this.getContext().branches.push(this.getContext().instructions);
|
|
363
|
+
this.getContext().instructions = [];
|
|
364
|
+
break;
|
|
365
|
+
|
|
366
|
+
case '[':
|
|
367
|
+
if (before) {
|
|
368
|
+
this.parsePattern(before);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
this.startContext({
|
|
372
|
+
oneOfSet: true
|
|
373
|
+
});
|
|
374
|
+
break;
|
|
375
|
+
|
|
376
|
+
case ']':
|
|
377
|
+
if (!this.getContext().oneOfSet) {
|
|
378
|
+
throw new Error('"]" operator must be preceded by "[" operator');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
this.endContext();
|
|
382
|
+
this.getContext().instructions.push({
|
|
383
|
+
op: '[]',
|
|
384
|
+
args: parseOneOfSet(before)
|
|
385
|
+
});
|
|
386
|
+
break;
|
|
387
|
+
|
|
388
|
+
default:
|
|
389
|
+
throw new Error("Unknown operator: ".concat(operator));
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (rightPart) {
|
|
393
|
+
this.parsePattern(rightPart);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}]);
|
|
397
|
+
|
|
398
|
+
return PatternParser;
|
|
399
|
+
}();
|
|
400
|
+
|
|
401
|
+
function parseOneOfSet(pattern) {
|
|
402
|
+
var values = [];
|
|
403
|
+
var i = 0;
|
|
404
|
+
|
|
405
|
+
while (i < pattern.length) {
|
|
406
|
+
if (pattern[i] === '-') {
|
|
407
|
+
if (i === 0 || i === pattern.length - 1) {
|
|
408
|
+
throw new Error("Couldn't parse a one-of set pattern: ".concat(pattern));
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
var prevValue = pattern[i - 1].charCodeAt(0) + 1;
|
|
412
|
+
var nextValue = pattern[i + 1].charCodeAt(0) - 1;
|
|
413
|
+
var value = prevValue;
|
|
414
|
+
|
|
415
|
+
while (value <= nextValue) {
|
|
416
|
+
values.push(String.fromCharCode(value));
|
|
417
|
+
value++;
|
|
418
|
+
}
|
|
419
|
+
} else {
|
|
420
|
+
values.push(pattern[i]);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
i++;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return values;
|
|
427
|
+
}
|
|
428
|
+
//# sourceMappingURL=AsYouTypeFormatter.PatternMatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../source/AsYouTypeFormatter.PatternMatcher.js"],"names":["PatternMatcher","pattern","matchTree","PatternParser","parse","string","allowOverflow","Error","result","match","split","matchedChars","overflow","characters","tree","last","length","characterString","join","indexOf","partialMatch","slice","Array","isArray","restCharacters","i","subtree","JSON","stringify","op","args","branch","char","OPERATOR","RegExp","context","or","instructions","parsePattern","branches","concat","push","pop","getContext","operator","before","index","rightPart","startContext","endContext","oneOfSet","parseOneOfSet","values","prevValue","charCodeAt","nextValue","value","String","fromCharCode"],"mappings":";;;;;;IAAqBA,c;;;AACpB,0BAAYC,OAAZ,EAAqB;AAAA;;AACpB,SAAKC,SAAL,GAAiB,IAAIC,aAAJ,GAAoBC,KAApB,CAA0BH,OAA1B,CAAjB,CADoB,CAEpB;AACA;;;;0BAEKI,M,EAAgC;AAAA,qFAAJ,EAAI;AAAA,UAAtBC,aAAsB,QAAtBA,aAAsB;;AACrC,UAAI,CAACD,MAAL,EAAa;AACZ,cAAM,IAAIE,KAAJ,CAAU,oBAAV,CAAN;AACA;;AACD,UAA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;AACDA,IAAAA,CAAC;AACD;;AACD,SAAO2B,MAAP;AACA","sourcesContent":["export default class PatternMatcher {\r\n\tconstructor(pattern) {\r\n\t\tthis.matchTree = new PatternParser().parse(pattern)\r\n\t\t// console.log(JSON.stringify(this.matchTree, null, 2))\r\n\t}\r\n\r\n\tmatch(string, { allowOverflow } = {}) {\r\n\t\tif (!string) {\r\n\t\t\tthrow new Error('String is required')\r\n\t\t}\r\n\t\tconst result = match(string.split(''), this.matchTree, true)\r\n\t\tif (result && result.match) {\r\n\t\t\tdelete result.matchedChars\r\n\t\t}\r\n\t\tif (result && result.overflow) {\r\n\t\t\tif (!allowOverflow) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn result\r\n\t}\r\n}\r\n\r\nfunction match(characters, tree, last) {\r\n\tif (typeof tree === 'string') {\r\n\t\tif (last) {\r\n\t\t\tif (characters.length > tree.length) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\toverflow: true\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tconst characterString = characters.join('')\r\n\t\tif (tree.indexOf(characterString) === 0) {\r\n\t\t\tif (characters.length === tree.length) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\tmatch: true,\r\n\t\t\t\t\tmatchedChars: characters\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn {\r\n\t\t\t\tpartialMatch: true,\r\n\t\t\t\t// matchedChars: characters\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (characterString.indexOf(tree) === 0) {\r\n\t\t\treturn {\r\n\t\t\t\tmatch: true,\r\n\t\t\t\tmatchedChars: characters.slice(0, tree.length)\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn\r\n\t}\r\n\r\n\tif (Array.isArray(tree)) {\r\n\t\tlet restCharacters = characters.slice()\r\n\t\tlet i = 0\r\n\t\twhile (i < tree.length) {\r\n\t\t\tconst subtree = tree[i]\r\n\t\t\tconst result = match(restCharacters, subtree, last && (i === tree.length - 1))\r\n\t\t\tif (!result || result.overflow) {\r\n\t\t\t\treturn result\r\n\t\t\t}\r\n\t\t\tif (result.match) {\r\n\t\t\t\t// Continue with the next subtree with the rest of the characters.\r\n\t\t\t\trestCharacters = restCharacters.slice(result.matchedChars.length)\r\n\t\t\t\tif (restCharacters.length === 0) {\r\n\t\t\t\t\tif (i === tree.length - 1) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\t\tmatchedChars: characters\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tpartialMatch: true,\r\n\t\t\t\t\t\t\t// matchedChars: characters\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} else if (result.partialMatch) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\tpartialMatch: true,\r\n\t\t\t\t\t// matchedChars: characters\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tthrow new Error(`Unsupported match result:\\n${JSON.stringify(result, null, 2)}`)\r\n\t\t\t}\r\n\t\t\ti++\r\n\t\t}\r\n\t\tif (last) {\r\n\t\t\treturn {\r\n\t\t\t\toverflow: true\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn {\r\n\t\t\tmatch: true,\r\n\t\t\tmatchedChars: characters.slice(0, characters.length - restCharacters.length)\r\n\t\t}\r\n\t}\r\n\r\n\tswitch (tree.op) {\r\n\t\tcase '|':\r\n\t\t\tlet partialMatch\r\n\t\t\tfor (const branch of tree.args) {\r\n\t\t\t\tconst result = match(characters, branch, last)\r\n\t\t\t\tif (result) {\r\n\t\t\t\t\tif (result.overflow) {\r\n\t\t\t\t\t\treturn result\r\n\t\t\t\t\t} else if (result.match) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\t\tmatchedChars: result.matchedChars\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else if (result.partialMatch) {\r\n\t\t\t\t\t\tpartialMatch = true\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (partialMatch) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\tpartialMatch: true,\r\n\t\t\t\t\t// matchedChars: ...\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// Not even a partial match.\r\n\t\t\treturn\r\n\r\n\t\tcase '[]':\r\n\t\t\tfor (const char of tree.args) {\r\n\t\t\t\tif (characters[0] === char) {\r\n\t\t\t\t\tif (characters.length === 1) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\t\tmatchedChars: characters\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif (last) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\toverflow: true\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\tmatchedChars: [char]\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// No character matches.\r\n\t\t\treturn\r\n\r\n\t\tdefault:\r\n\t\t\tthrow new Error(`Unsupported instruction tree: ${tree}`)\r\n\t}\r\n}\r\n\r\nconst OPERATOR = new RegExp(\r\n\t// any of:\r\n\t'(' +\r\n\t\t// or operator\r\n\t\t'\\\\|' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// or group start\r\n\t\t'\\\\(\\\\?\\\\:' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// or group end\r\n\t\t'\\\\)' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// one-of set start\r\n\t\t'\\\\[' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// one-of set end\r\n\t\t'\\\\]' +\r\n\t')'\r\n)\r\n\r\nclass PatternParser {\r\n\tparse(pattern) {\r\n\t\tthis.context = [{\r\n\t\t\tor: true,\r\n\t\t\tinstructions: []\r\n\t\t}]\r\n\r\n\t\tthis.parsePattern(pattern)\r\n\r\n\t\tif (this.context.length !== 1) {\r\n\t\t\tthrow new Error('Non-finalized contexts left when pattern parse ended')\r\n\t\t}\r\n\r\n\t\tconst { branches, instructions } = this.context[0]\r\n\r\n\t\tif (branches) {\r\n\t\t\treturn [{\r\n\t\t\t\top: '|',\r\n\t\t\t\targs: branches.concat([instructions])\r\n\t\t\t}]\r\n\t\t}\r\n\r\n\t\tif (instructions.length === 0) {\r\n\t\t\tthrow new Error('Pattern is required')\r\n\t\t}\r\n\r\n\t\treturn instructions\r\n\t}\r\n\r\n\tstartContext(context) {\r\n\t\tthis.context.push(context)\r\n\t}\r\n\r\n\tendContext() {\r\n\t\tthis.context.pop()\r\n\t}\r\n\r\n\tgetContext() {\r\n\t\treturn this.context[this.context.length - 1]\r\n\t}\r\n\r\n\tparsePattern(pattern) {\r\n\t\tif (!pattern) {\r\n\t\t\tthrow new Error('Empty pattern passed')\r\n\t\t}\r\n\r\n\t\tconst match = pattern.match(OPERATOR)\r\n\t\tif (!match) {\r\n\t\t\tthis.getContext().instructions = this.getContext().instructions.concat(\r\n\t\t\t\tpattern.split('')\r\n\t\t\t)\r\n\t\t\treturn\r\n\t\t}\r\n\r\n\t\tconst operator = match[1]\r\n\t\tconst before = pattern.slice(0, match.index)\r\n\t\tconst rightPart = pattern.slice(match.index + operator.length)\r\n\r\n\t\tswitch (operator) {\r\n\t\t\tcase '(?:':\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\tthis.startContext({\r\n\t\t\t\t\tor: true,\r\n\t\t\t\t\tinstructions: [],\r\n\t\t\t\t\tbranches: []\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase ')':\r\n\t\t\t\tif (!this.getContext().or) {\r\n\t\t\t\t\tthrow new Error('\")\" operator must be preceded by \"(?:\" operator')\r\n\t\t\t\t}\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\tif (this.getContext().instructions.length === 0) {\r\n\t\t\t\t\tthrow new Error('No instructions found after \"|\" operator in an \"or\" group')\r\n\t\t\t\t}\r\n\t\t\t\tconst { branches } = this.getContext()\r\n\t\t\t\tbranches.push(\r\n\t\t\t\t\tthis.getContext().instructions\r\n\t\t\t\t)\r\n\t\t\t\tthis.endContext()\r\n\t\t\t\tthis.getContext().instructions.push({\r\n\t\t\t\t\top: '|',\r\n\t\t\t\t\targs: branches\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase '|':\r\n\t\t\t\tif (!this.getContext().or) {\r\n\t\t\t\t\tthrow new Error('\"|\" operator can only be used inside \"or\" groups')\r\n\t\t\t\t}\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\t// The top-level is an implicit \"or\" group, if required.\r\n\t\t\t\tif (!this.getContext().branches) {\r\n\t\t\t\t\tif (this.context.length === 1) {\r\n\t\t\t\t\t\tthis.getContext().branches = []\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\tthrow new Error('\"branches\" not found in an \"or\" group context')\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tthis.getContext().branches.push(\r\n\t\t\t\t\tthis.getContext().instructions\r\n\t\t\t\t)\r\n\t\t\t\tthis.getContext().instructions = []\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase '[':\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\tthis.startContext({\r\n\t\t\t\t\toneOfSet: true\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase ']':\r\n\t\t\t\tif (!this.getContext().oneOfSet) {\r\n\t\t\t\t\tthrow new Error('\"]\" operator must be preceded by \"[\" operator')\r\n\t\t\t\t}\r\n\t\t\t\tthis.endContext()\r\n\t\t\t\tthis.getContext().instructions.push({\r\n\t\t\t\t\top: '[]',\r\n\t\t\t\t\targs: parseOneOfSet(before)\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tdefault:\r\n\t\t\t\tthrow new Error(`Unknown operator: ${operator}`)\r\n\t\t}\r\n\r\n\t\tif (rightPart) {\r\n\t\t\tthis.parsePattern(rightPart)\r\n\t\t}\r\n\t}\r\n}\r\n\r\nfunction parseOneOfSet(pattern) {\r\n\tconst values = []\r\n\tlet i = 0\r\n\twhile (i < pattern.length) {\r\n\t\tif (pattern[i] === '-') {\r\n\t\t\tif (i === 0 || i === pattern.length - 1) {\r\n\t\t\t\tthrow new Error(`Couldn't parse a one-of set pattern: ${pattern}`)\r\n\t\t\t}\r\n\t\t\tconst prevValue = pattern[i - 1].charCodeAt(0) + 1\r\n\t\t\tconst nextValue = pattern[i + 1].charCodeAt(0) - 1\r\n\t\t\tlet value = prevValue\r\n\t\t\twhile (value <= nextValue) {\r\n\t\t\t\tvalues.push(String.fromCharCode(value))\r\n\t\t\t\tvalue++\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tvalues.push(pattern[i])\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\treturn values\r\n}"],"file":"AsYouTypeFormatter.PatternMatcher.js"}
|