clarity-pattern-parser 10.0.5 → 10.0.7
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/dist/index.browser.js +44 -77
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +44 -77
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +44 -77
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +0 -11
- package/dist/patterns/CursorHistory.d.ts +0 -7
- package/dist/patterns/DepthCache.d.ts +6 -0
- package/package.json +1 -1
- package/src/patterns/Cursor.ts +1 -51
- package/src/patterns/CursorHistory.ts +0 -16
- package/src/patterns/DepthCache.ts +26 -0
- package/src/patterns/FiniteRepeat.ts +0 -5
- package/src/patterns/InfiniteRepeat.ts +0 -5
- package/src/patterns/Literal.ts +0 -4
- package/src/patterns/Not.ts +0 -3
- package/src/patterns/Optional.ts +0 -4
- package/src/patterns/Options.test.ts +2 -3
- package/src/patterns/Options.ts +19 -10
- package/src/patterns/Regex.ts +0 -3
- package/src/patterns/Sequence.ts +11 -4
package/dist/index.browser.js
CHANGED
|
@@ -285,7 +285,6 @@
|
|
|
285
285
|
this._patterns = [];
|
|
286
286
|
this._nodes = [];
|
|
287
287
|
this._errors = [];
|
|
288
|
-
this._trace = [];
|
|
289
288
|
}
|
|
290
289
|
get isRecording() {
|
|
291
290
|
return this._isRecording;
|
|
@@ -314,9 +313,6 @@
|
|
|
314
313
|
get patterns() {
|
|
315
314
|
return this._patterns;
|
|
316
315
|
}
|
|
317
|
-
get trace() {
|
|
318
|
-
return this._trace;
|
|
319
|
-
}
|
|
320
316
|
recordMatch(pattern, node) {
|
|
321
317
|
if (this._isRecording) {
|
|
322
318
|
this._patterns.push(pattern);
|
|
@@ -371,21 +367,8 @@
|
|
|
371
367
|
resolveError() {
|
|
372
368
|
this._currentError = null;
|
|
373
369
|
}
|
|
374
|
-
pushStackTrace(trace) {
|
|
375
|
-
if (this._isRecording) {
|
|
376
|
-
this._trace.push(trace);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
370
|
}
|
|
380
371
|
|
|
381
|
-
class CyclicalParseError extends Error {
|
|
382
|
-
constructor(patternId, patternName, cursorIndex) {
|
|
383
|
-
super("Cyclical Parse Error");
|
|
384
|
-
this.patternId = patternId;
|
|
385
|
-
this.patternName = patternName;
|
|
386
|
-
this.cursorIndex = cursorIndex;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
372
|
class Cursor {
|
|
390
373
|
get text() {
|
|
391
374
|
return this._text;
|
|
@@ -440,7 +423,6 @@
|
|
|
440
423
|
this._index = 0;
|
|
441
424
|
this._length = [...text].length;
|
|
442
425
|
this._history = new CursorHistory();
|
|
443
|
-
this._stackTrace = [];
|
|
444
426
|
}
|
|
445
427
|
hasNext() {
|
|
446
428
|
return this._index + 1 < this._length;
|
|
@@ -490,35 +472,6 @@
|
|
|
490
472
|
stopRecording() {
|
|
491
473
|
this._history.stopRecording();
|
|
492
474
|
}
|
|
493
|
-
startParseWith(pattern) {
|
|
494
|
-
const trace = {
|
|
495
|
-
pattern,
|
|
496
|
-
cursorIndex: this.index
|
|
497
|
-
};
|
|
498
|
-
const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
|
|
499
|
-
if (hasCycle) {
|
|
500
|
-
throw new CyclicalParseError(pattern.id, pattern.name, this.index);
|
|
501
|
-
}
|
|
502
|
-
this._history.pushStackTrace(trace);
|
|
503
|
-
this._stackTrace.push(trace);
|
|
504
|
-
}
|
|
505
|
-
endParse() {
|
|
506
|
-
this._stackTrace.pop();
|
|
507
|
-
}
|
|
508
|
-
audit() {
|
|
509
|
-
return this._history.trace.map(t => {
|
|
510
|
-
const onChar = this.getChars(t.cursorIndex, t.cursorIndex);
|
|
511
|
-
const restChars = this.getChars(t.cursorIndex + 1, t.cursorIndex + 5);
|
|
512
|
-
const context = `{${t.cursorIndex}}[${onChar}]${restChars}`;
|
|
513
|
-
return `${this._buildPatternContext(t.pattern)}-->${context}`;
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
_buildPatternContext(pattern) {
|
|
517
|
-
if (pattern.parent != null) {
|
|
518
|
-
return `${pattern.parent.name}.${pattern.name}`;
|
|
519
|
-
}
|
|
520
|
-
return pattern.name;
|
|
521
|
-
}
|
|
522
475
|
}
|
|
523
476
|
|
|
524
477
|
let idIndex$9 = 0;
|
|
@@ -574,18 +527,15 @@
|
|
|
574
527
|
};
|
|
575
528
|
}
|
|
576
529
|
parse(cursor) {
|
|
577
|
-
cursor.startParseWith(this);
|
|
578
530
|
this._firstIndex = cursor.index;
|
|
579
531
|
const passed = this._tryToParse(cursor);
|
|
580
532
|
if (passed) {
|
|
581
533
|
cursor.resolveError();
|
|
582
534
|
const node = this._createNode();
|
|
583
535
|
cursor.recordMatch(this, node);
|
|
584
|
-
cursor.endParse();
|
|
585
536
|
return node;
|
|
586
537
|
}
|
|
587
538
|
cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
|
|
588
|
-
cursor.endParse();
|
|
589
539
|
return null;
|
|
590
540
|
}
|
|
591
541
|
_tryToParse(cursor) {
|
|
@@ -714,11 +664,9 @@
|
|
|
714
664
|
};
|
|
715
665
|
}
|
|
716
666
|
parse(cursor) {
|
|
717
|
-
cursor.startParseWith(this);
|
|
718
667
|
this._firstIndex = cursor.index;
|
|
719
668
|
this.resetState(cursor);
|
|
720
669
|
this.tryToParse(cursor);
|
|
721
|
-
cursor.endParse();
|
|
722
670
|
return this._node;
|
|
723
671
|
}
|
|
724
672
|
resetState(cursor) {
|
|
@@ -934,6 +882,33 @@
|
|
|
934
882
|
return patterns.map(p => p.clone());
|
|
935
883
|
}
|
|
936
884
|
|
|
885
|
+
class DepthCache {
|
|
886
|
+
constructor() {
|
|
887
|
+
this._depthMap = {};
|
|
888
|
+
}
|
|
889
|
+
getDepth(name, cursorIndex) {
|
|
890
|
+
if (this._depthMap[name] == null) {
|
|
891
|
+
this._depthMap[name] = {};
|
|
892
|
+
}
|
|
893
|
+
if (this._depthMap[name][cursorIndex] == null) {
|
|
894
|
+
this._depthMap[name][cursorIndex] = 0;
|
|
895
|
+
}
|
|
896
|
+
return this._depthMap[name][cursorIndex];
|
|
897
|
+
}
|
|
898
|
+
incrementDepth(name, cursorIndex) {
|
|
899
|
+
const depth = this.getDepth(name, cursorIndex);
|
|
900
|
+
this._depthMap[name][cursorIndex] = depth + 1;
|
|
901
|
+
}
|
|
902
|
+
decrementDepth(name, cursorIndex) {
|
|
903
|
+
const depth = this.getDepth(name, cursorIndex);
|
|
904
|
+
this._depthMap[name][cursorIndex] = depth - 1;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/*
|
|
909
|
+
The following is created to reduce the overhead of recursion check.
|
|
910
|
+
*/
|
|
911
|
+
const depthCache$1 = new DepthCache();
|
|
937
912
|
let idIndex$6 = 0;
|
|
938
913
|
class Options {
|
|
939
914
|
get id() {
|
|
@@ -988,30 +963,30 @@
|
|
|
988
963
|
};
|
|
989
964
|
}
|
|
990
965
|
parse(cursor) {
|
|
991
|
-
|
|
966
|
+
// This is a cache to help with speed
|
|
967
|
+
this._firstIndex = cursor.index;
|
|
968
|
+
depthCache$1.incrementDepth(this._id, this._firstIndex);
|
|
992
969
|
this._firstIndex = cursor.index;
|
|
993
970
|
const node = this._tryToParse(cursor);
|
|
971
|
+
depthCache$1.decrementDepth(this._id, this._firstIndex);
|
|
994
972
|
if (node != null) {
|
|
995
973
|
cursor.moveTo(node.lastIndex);
|
|
996
974
|
cursor.resolveError();
|
|
997
|
-
cursor.endParse();
|
|
998
975
|
return node;
|
|
999
976
|
}
|
|
1000
977
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
1001
|
-
cursor.endParse();
|
|
1002
978
|
return null;
|
|
1003
979
|
}
|
|
1004
980
|
_tryToParse(cursor) {
|
|
981
|
+
if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
|
|
982
|
+
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
983
|
+
return null;
|
|
984
|
+
}
|
|
1005
985
|
const results = [];
|
|
1006
986
|
for (const pattern of this._children) {
|
|
1007
987
|
cursor.moveTo(this._firstIndex);
|
|
1008
988
|
let result = null;
|
|
1009
|
-
|
|
1010
|
-
result = pattern.parse(cursor);
|
|
1011
|
-
}
|
|
1012
|
-
catch (_a) {
|
|
1013
|
-
continue;
|
|
1014
|
-
}
|
|
989
|
+
result = pattern.parse(cursor);
|
|
1015
990
|
if (this._isGreedy) {
|
|
1016
991
|
results.push(result);
|
|
1017
992
|
}
|
|
@@ -1123,7 +1098,6 @@
|
|
|
1123
1098
|
}
|
|
1124
1099
|
}
|
|
1125
1100
|
parse(cursor) {
|
|
1126
|
-
cursor.startParseWith(this);
|
|
1127
1101
|
const startIndex = cursor.index;
|
|
1128
1102
|
const nodes = [];
|
|
1129
1103
|
const modulo = this._hasDivider ? 2 : 1;
|
|
@@ -1162,19 +1136,16 @@
|
|
|
1162
1136
|
const lastIndex = cursor.index;
|
|
1163
1137
|
cursor.moveTo(startIndex);
|
|
1164
1138
|
cursor.recordErrorAt(startIndex, lastIndex, this);
|
|
1165
|
-
cursor.endParse();
|
|
1166
1139
|
return null;
|
|
1167
1140
|
}
|
|
1168
1141
|
if (nodes.length === 0 && !cursor.hasError) {
|
|
1169
1142
|
cursor.moveTo(startIndex);
|
|
1170
|
-
cursor.endParse();
|
|
1171
1143
|
return null;
|
|
1172
1144
|
}
|
|
1173
1145
|
const firstIndex = nodes[0].firstIndex;
|
|
1174
1146
|
const lastIndex = nodes[nodes.length - 1].lastIndex;
|
|
1175
1147
|
cursor.resolveError();
|
|
1176
1148
|
cursor.moveTo(lastIndex);
|
|
1177
|
-
cursor.endParse();
|
|
1178
1149
|
return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
|
|
1179
1150
|
}
|
|
1180
1151
|
test(text) {
|
|
@@ -1320,7 +1291,6 @@
|
|
|
1320
1291
|
};
|
|
1321
1292
|
}
|
|
1322
1293
|
parse(cursor) {
|
|
1323
|
-
cursor.startParseWith(this);
|
|
1324
1294
|
this._firstIndex = cursor.index;
|
|
1325
1295
|
this._nodes = [];
|
|
1326
1296
|
const passed = this._tryToParse(cursor);
|
|
@@ -1331,15 +1301,12 @@
|
|
|
1331
1301
|
cursor.moveTo(node.lastIndex);
|
|
1332
1302
|
cursor.recordMatch(this, node);
|
|
1333
1303
|
}
|
|
1334
|
-
cursor.endParse();
|
|
1335
1304
|
return node;
|
|
1336
1305
|
}
|
|
1337
1306
|
if (this._min > 0) {
|
|
1338
|
-
cursor.endParse();
|
|
1339
1307
|
return null;
|
|
1340
1308
|
}
|
|
1341
1309
|
cursor.resolveError();
|
|
1342
|
-
cursor.endParse();
|
|
1343
1310
|
return null;
|
|
1344
1311
|
}
|
|
1345
1312
|
_meetsMin() {
|
|
@@ -1624,6 +1591,7 @@
|
|
|
1624
1591
|
return filteredNodes;
|
|
1625
1592
|
}
|
|
1626
1593
|
|
|
1594
|
+
const depthCache = new DepthCache();
|
|
1627
1595
|
let idIndex$2 = 0;
|
|
1628
1596
|
class Sequence {
|
|
1629
1597
|
get id() {
|
|
@@ -1678,22 +1646,26 @@
|
|
|
1678
1646
|
};
|
|
1679
1647
|
}
|
|
1680
1648
|
parse(cursor) {
|
|
1681
|
-
|
|
1649
|
+
// This is a cache to help with speed
|
|
1682
1650
|
this._firstIndex = cursor.index;
|
|
1651
|
+
depthCache.incrementDepth(this._id, this._firstIndex);
|
|
1683
1652
|
this._nodes = [];
|
|
1684
1653
|
const passed = this.tryToParse(cursor);
|
|
1654
|
+
depthCache.decrementDepth(this._id, this._firstIndex);
|
|
1685
1655
|
if (passed) {
|
|
1686
1656
|
const node = this.createNode(cursor);
|
|
1687
1657
|
if (node !== null) {
|
|
1688
1658
|
cursor.recordMatch(this, node);
|
|
1689
1659
|
}
|
|
1690
|
-
cursor.endParse();
|
|
1691
1660
|
return node;
|
|
1692
1661
|
}
|
|
1693
|
-
cursor.endParse();
|
|
1694
1662
|
return null;
|
|
1695
1663
|
}
|
|
1696
1664
|
tryToParse(cursor) {
|
|
1665
|
+
if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
|
|
1666
|
+
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
1667
|
+
return false;
|
|
1668
|
+
}
|
|
1697
1669
|
let passed = false;
|
|
1698
1670
|
for (let i = 0; i < this._children.length; i++) {
|
|
1699
1671
|
const runningCursorIndex = cursor.index;
|
|
@@ -1932,17 +1904,14 @@
|
|
|
1932
1904
|
};
|
|
1933
1905
|
}
|
|
1934
1906
|
parse(cursor) {
|
|
1935
|
-
cursor.startParseWith(this);
|
|
1936
1907
|
const firstIndex = cursor.index;
|
|
1937
1908
|
const node = this._children[0].parse(cursor);
|
|
1938
1909
|
if (cursor.hasError) {
|
|
1939
1910
|
cursor.resolveError();
|
|
1940
1911
|
cursor.moveTo(firstIndex);
|
|
1941
|
-
cursor.endParse();
|
|
1942
1912
|
return null;
|
|
1943
1913
|
}
|
|
1944
1914
|
else {
|
|
1945
|
-
cursor.endParse();
|
|
1946
1915
|
return node;
|
|
1947
1916
|
}
|
|
1948
1917
|
}
|
|
@@ -2247,7 +2216,6 @@
|
|
|
2247
2216
|
};
|
|
2248
2217
|
}
|
|
2249
2218
|
parse(cursor) {
|
|
2250
|
-
cursor.startParseWith(this);
|
|
2251
2219
|
const firstIndex = cursor.index;
|
|
2252
2220
|
this._children[0].parse(cursor);
|
|
2253
2221
|
if (cursor.hasError) {
|
|
@@ -2259,7 +2227,6 @@
|
|
|
2259
2227
|
cursor.resolveError();
|
|
2260
2228
|
cursor.recordErrorAt(firstIndex, firstIndex, this);
|
|
2261
2229
|
}
|
|
2262
|
-
cursor.endParse();
|
|
2263
2230
|
return null;
|
|
2264
2231
|
}
|
|
2265
2232
|
clone(name = this._name) {
|