clarity-pattern-parser 10.0.5 → 10.0.6
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 +47 -77
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +47 -77
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +47 -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/Options.d.ts +1 -0
- package/dist/patterns/Sequence.d.ts +1 -0
- package/package.json +1 -1
- package/src/patterns/Cursor.ts +1 -51
- package/src/patterns/CursorHistory.ts +0 -16
- 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 +34 -10
- package/src/patterns/Regex.ts +0 -3
- package/src/patterns/Sequence.ts +33 -4
package/dist/index.js
CHANGED
|
@@ -283,7 +283,6 @@ class CursorHistory {
|
|
|
283
283
|
this._patterns = [];
|
|
284
284
|
this._nodes = [];
|
|
285
285
|
this._errors = [];
|
|
286
|
-
this._trace = [];
|
|
287
286
|
}
|
|
288
287
|
get isRecording() {
|
|
289
288
|
return this._isRecording;
|
|
@@ -312,9 +311,6 @@ class CursorHistory {
|
|
|
312
311
|
get patterns() {
|
|
313
312
|
return this._patterns;
|
|
314
313
|
}
|
|
315
|
-
get trace() {
|
|
316
|
-
return this._trace;
|
|
317
|
-
}
|
|
318
314
|
recordMatch(pattern, node) {
|
|
319
315
|
if (this._isRecording) {
|
|
320
316
|
this._patterns.push(pattern);
|
|
@@ -369,21 +365,8 @@ class CursorHistory {
|
|
|
369
365
|
resolveError() {
|
|
370
366
|
this._currentError = null;
|
|
371
367
|
}
|
|
372
|
-
pushStackTrace(trace) {
|
|
373
|
-
if (this._isRecording) {
|
|
374
|
-
this._trace.push(trace);
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
368
|
}
|
|
378
369
|
|
|
379
|
-
class CyclicalParseError extends Error {
|
|
380
|
-
constructor(patternId, patternName, cursorIndex) {
|
|
381
|
-
super("Cyclical Parse Error");
|
|
382
|
-
this.patternId = patternId;
|
|
383
|
-
this.patternName = patternName;
|
|
384
|
-
this.cursorIndex = cursorIndex;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
370
|
class Cursor {
|
|
388
371
|
get text() {
|
|
389
372
|
return this._text;
|
|
@@ -438,7 +421,6 @@ class Cursor {
|
|
|
438
421
|
this._index = 0;
|
|
439
422
|
this._length = [...text].length;
|
|
440
423
|
this._history = new CursorHistory();
|
|
441
|
-
this._stackTrace = [];
|
|
442
424
|
}
|
|
443
425
|
hasNext() {
|
|
444
426
|
return this._index + 1 < this._length;
|
|
@@ -488,35 +470,6 @@ class Cursor {
|
|
|
488
470
|
stopRecording() {
|
|
489
471
|
this._history.stopRecording();
|
|
490
472
|
}
|
|
491
|
-
startParseWith(pattern) {
|
|
492
|
-
const trace = {
|
|
493
|
-
pattern,
|
|
494
|
-
cursorIndex: this.index
|
|
495
|
-
};
|
|
496
|
-
const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
|
|
497
|
-
if (hasCycle) {
|
|
498
|
-
throw new CyclicalParseError(pattern.id, pattern.name, this.index);
|
|
499
|
-
}
|
|
500
|
-
this._history.pushStackTrace(trace);
|
|
501
|
-
this._stackTrace.push(trace);
|
|
502
|
-
}
|
|
503
|
-
endParse() {
|
|
504
|
-
this._stackTrace.pop();
|
|
505
|
-
}
|
|
506
|
-
audit() {
|
|
507
|
-
return this._history.trace.map(t => {
|
|
508
|
-
const onChar = this.getChars(t.cursorIndex, t.cursorIndex);
|
|
509
|
-
const restChars = this.getChars(t.cursorIndex + 1, t.cursorIndex + 5);
|
|
510
|
-
const context = `{${t.cursorIndex}}[${onChar}]${restChars}`;
|
|
511
|
-
return `${this._buildPatternContext(t.pattern)}-->${context}`;
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
_buildPatternContext(pattern) {
|
|
515
|
-
if (pattern.parent != null) {
|
|
516
|
-
return `${pattern.parent.name}.${pattern.name}`;
|
|
517
|
-
}
|
|
518
|
-
return pattern.name;
|
|
519
|
-
}
|
|
520
473
|
}
|
|
521
474
|
|
|
522
475
|
let idIndex$9 = 0;
|
|
@@ -572,18 +525,15 @@ class Literal {
|
|
|
572
525
|
};
|
|
573
526
|
}
|
|
574
527
|
parse(cursor) {
|
|
575
|
-
cursor.startParseWith(this);
|
|
576
528
|
this._firstIndex = cursor.index;
|
|
577
529
|
const passed = this._tryToParse(cursor);
|
|
578
530
|
if (passed) {
|
|
579
531
|
cursor.resolveError();
|
|
580
532
|
const node = this._createNode();
|
|
581
533
|
cursor.recordMatch(this, node);
|
|
582
|
-
cursor.endParse();
|
|
583
534
|
return node;
|
|
584
535
|
}
|
|
585
536
|
cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
|
|
586
|
-
cursor.endParse();
|
|
587
537
|
return null;
|
|
588
538
|
}
|
|
589
539
|
_tryToParse(cursor) {
|
|
@@ -712,11 +662,9 @@ class Regex {
|
|
|
712
662
|
};
|
|
713
663
|
}
|
|
714
664
|
parse(cursor) {
|
|
715
|
-
cursor.startParseWith(this);
|
|
716
665
|
this._firstIndex = cursor.index;
|
|
717
666
|
this.resetState(cursor);
|
|
718
667
|
this.tryToParse(cursor);
|
|
719
|
-
cursor.endParse();
|
|
720
668
|
return this._node;
|
|
721
669
|
}
|
|
722
670
|
resetState(cursor) {
|
|
@@ -986,30 +934,26 @@ class Options {
|
|
|
986
934
|
};
|
|
987
935
|
}
|
|
988
936
|
parse(cursor) {
|
|
989
|
-
cursor.startParseWith(this);
|
|
990
937
|
this._firstIndex = cursor.index;
|
|
991
938
|
const node = this._tryToParse(cursor);
|
|
992
939
|
if (node != null) {
|
|
993
940
|
cursor.moveTo(node.lastIndex);
|
|
994
941
|
cursor.resolveError();
|
|
995
|
-
cursor.endParse();
|
|
996
942
|
return node;
|
|
997
943
|
}
|
|
998
944
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
999
|
-
cursor.endParse();
|
|
1000
945
|
return null;
|
|
1001
946
|
}
|
|
1002
947
|
_tryToParse(cursor) {
|
|
948
|
+
if (this._isBeyondRecursiveLimit()) {
|
|
949
|
+
cursor.recordErrorAt(cursor.index, cursor.index, this);
|
|
950
|
+
return null;
|
|
951
|
+
}
|
|
1003
952
|
const results = [];
|
|
1004
953
|
for (const pattern of this._children) {
|
|
1005
954
|
cursor.moveTo(this._firstIndex);
|
|
1006
955
|
let result = null;
|
|
1007
|
-
|
|
1008
|
-
result = pattern.parse(cursor);
|
|
1009
|
-
}
|
|
1010
|
-
catch (_a) {
|
|
1011
|
-
continue;
|
|
1012
|
-
}
|
|
956
|
+
result = pattern.parse(cursor);
|
|
1013
957
|
if (this._isGreedy) {
|
|
1014
958
|
results.push(result);
|
|
1015
959
|
}
|
|
@@ -1022,6 +966,25 @@ class Options {
|
|
|
1022
966
|
nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
|
|
1023
967
|
return nonNullResults[0] || null;
|
|
1024
968
|
}
|
|
969
|
+
_isBeyondRecursiveLimit() {
|
|
970
|
+
let pattern = this;
|
|
971
|
+
const matches = [];
|
|
972
|
+
while (pattern.parent != null) {
|
|
973
|
+
if (pattern.type !== "options") {
|
|
974
|
+
pattern = pattern.parent;
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
const optionsPattern = pattern;
|
|
978
|
+
if (pattern.id === this.id && optionsPattern._firstIndex === this._firstIndex) {
|
|
979
|
+
matches.push(pattern);
|
|
980
|
+
if (matches.length > 2) {
|
|
981
|
+
return true;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
pattern = pattern.parent;
|
|
985
|
+
}
|
|
986
|
+
return false;
|
|
987
|
+
}
|
|
1025
988
|
getTokens() {
|
|
1026
989
|
const tokens = [];
|
|
1027
990
|
for (const child of this._children) {
|
|
@@ -1121,7 +1084,6 @@ class FiniteRepeat {
|
|
|
1121
1084
|
}
|
|
1122
1085
|
}
|
|
1123
1086
|
parse(cursor) {
|
|
1124
|
-
cursor.startParseWith(this);
|
|
1125
1087
|
const startIndex = cursor.index;
|
|
1126
1088
|
const nodes = [];
|
|
1127
1089
|
const modulo = this._hasDivider ? 2 : 1;
|
|
@@ -1160,19 +1122,16 @@ class FiniteRepeat {
|
|
|
1160
1122
|
const lastIndex = cursor.index;
|
|
1161
1123
|
cursor.moveTo(startIndex);
|
|
1162
1124
|
cursor.recordErrorAt(startIndex, lastIndex, this);
|
|
1163
|
-
cursor.endParse();
|
|
1164
1125
|
return null;
|
|
1165
1126
|
}
|
|
1166
1127
|
if (nodes.length === 0 && !cursor.hasError) {
|
|
1167
1128
|
cursor.moveTo(startIndex);
|
|
1168
|
-
cursor.endParse();
|
|
1169
1129
|
return null;
|
|
1170
1130
|
}
|
|
1171
1131
|
const firstIndex = nodes[0].firstIndex;
|
|
1172
1132
|
const lastIndex = nodes[nodes.length - 1].lastIndex;
|
|
1173
1133
|
cursor.resolveError();
|
|
1174
1134
|
cursor.moveTo(lastIndex);
|
|
1175
|
-
cursor.endParse();
|
|
1176
1135
|
return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
|
|
1177
1136
|
}
|
|
1178
1137
|
test(text) {
|
|
@@ -1318,7 +1277,6 @@ class InfiniteRepeat {
|
|
|
1318
1277
|
};
|
|
1319
1278
|
}
|
|
1320
1279
|
parse(cursor) {
|
|
1321
|
-
cursor.startParseWith(this);
|
|
1322
1280
|
this._firstIndex = cursor.index;
|
|
1323
1281
|
this._nodes = [];
|
|
1324
1282
|
const passed = this._tryToParse(cursor);
|
|
@@ -1329,15 +1287,12 @@ class InfiniteRepeat {
|
|
|
1329
1287
|
cursor.moveTo(node.lastIndex);
|
|
1330
1288
|
cursor.recordMatch(this, node);
|
|
1331
1289
|
}
|
|
1332
|
-
cursor.endParse();
|
|
1333
1290
|
return node;
|
|
1334
1291
|
}
|
|
1335
1292
|
if (this._min > 0) {
|
|
1336
|
-
cursor.endParse();
|
|
1337
1293
|
return null;
|
|
1338
1294
|
}
|
|
1339
1295
|
cursor.resolveError();
|
|
1340
|
-
cursor.endParse();
|
|
1341
1296
|
return null;
|
|
1342
1297
|
}
|
|
1343
1298
|
_meetsMin() {
|
|
@@ -1676,19 +1631,20 @@ class Sequence {
|
|
|
1676
1631
|
};
|
|
1677
1632
|
}
|
|
1678
1633
|
parse(cursor) {
|
|
1679
|
-
cursor.startParseWith(this);
|
|
1680
1634
|
this._firstIndex = cursor.index;
|
|
1681
1635
|
this._nodes = [];
|
|
1636
|
+
if (this._isBeyondRecursiveLimit()) {
|
|
1637
|
+
cursor.recordErrorAt(cursor.index, cursor.index, this);
|
|
1638
|
+
return null;
|
|
1639
|
+
}
|
|
1682
1640
|
const passed = this.tryToParse(cursor);
|
|
1683
1641
|
if (passed) {
|
|
1684
1642
|
const node = this.createNode(cursor);
|
|
1685
1643
|
if (node !== null) {
|
|
1686
1644
|
cursor.recordMatch(this, node);
|
|
1687
1645
|
}
|
|
1688
|
-
cursor.endParse();
|
|
1689
1646
|
return node;
|
|
1690
1647
|
}
|
|
1691
|
-
cursor.endParse();
|
|
1692
1648
|
return null;
|
|
1693
1649
|
}
|
|
1694
1650
|
tryToParse(cursor) {
|
|
@@ -1748,6 +1704,25 @@ class Sequence {
|
|
|
1748
1704
|
}
|
|
1749
1705
|
return passed;
|
|
1750
1706
|
}
|
|
1707
|
+
_isBeyondRecursiveLimit() {
|
|
1708
|
+
let pattern = this;
|
|
1709
|
+
const matches = [];
|
|
1710
|
+
while (pattern.parent != null) {
|
|
1711
|
+
if (pattern.type !== "sequence") {
|
|
1712
|
+
pattern = pattern.parent;
|
|
1713
|
+
continue;
|
|
1714
|
+
}
|
|
1715
|
+
const sequencePattern = pattern;
|
|
1716
|
+
if (pattern.id === this.id && sequencePattern._firstIndex === this._firstIndex) {
|
|
1717
|
+
matches.push(pattern);
|
|
1718
|
+
if (matches.length > 1) {
|
|
1719
|
+
return true;
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
pattern = pattern.parent;
|
|
1723
|
+
}
|
|
1724
|
+
return false;
|
|
1725
|
+
}
|
|
1751
1726
|
getLastValidNode() {
|
|
1752
1727
|
const nodes = filterOutNull(this._nodes);
|
|
1753
1728
|
if (nodes.length === 0) {
|
|
@@ -1930,17 +1905,14 @@ class Optional {
|
|
|
1930
1905
|
};
|
|
1931
1906
|
}
|
|
1932
1907
|
parse(cursor) {
|
|
1933
|
-
cursor.startParseWith(this);
|
|
1934
1908
|
const firstIndex = cursor.index;
|
|
1935
1909
|
const node = this._children[0].parse(cursor);
|
|
1936
1910
|
if (cursor.hasError) {
|
|
1937
1911
|
cursor.resolveError();
|
|
1938
1912
|
cursor.moveTo(firstIndex);
|
|
1939
|
-
cursor.endParse();
|
|
1940
1913
|
return null;
|
|
1941
1914
|
}
|
|
1942
1915
|
else {
|
|
1943
|
-
cursor.endParse();
|
|
1944
1916
|
return node;
|
|
1945
1917
|
}
|
|
1946
1918
|
}
|
|
@@ -2245,7 +2217,6 @@ class Not {
|
|
|
2245
2217
|
};
|
|
2246
2218
|
}
|
|
2247
2219
|
parse(cursor) {
|
|
2248
|
-
cursor.startParseWith(this);
|
|
2249
2220
|
const firstIndex = cursor.index;
|
|
2250
2221
|
this._children[0].parse(cursor);
|
|
2251
2222
|
if (cursor.hasError) {
|
|
@@ -2257,7 +2228,6 @@ class Not {
|
|
|
2257
2228
|
cursor.resolveError();
|
|
2258
2229
|
cursor.recordErrorAt(firstIndex, firstIndex, this);
|
|
2259
2230
|
}
|
|
2260
|
-
cursor.endParse();
|
|
2261
2231
|
return null;
|
|
2262
2232
|
}
|
|
2263
2233
|
clone(name = this._name) {
|