clarity-pattern-parser 10.0.4 → 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 -82
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +47 -82
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +47 -82
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +0 -10
- 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 -49
- 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 +18 -2
- package/src/patterns/Options.ts +34 -15
- package/src/patterns/Regex.ts +0 -3
- package/src/patterns/Sequence.ts +33 -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,20 +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) {
|
|
383
|
-
super("Cyclical Parse Error");
|
|
384
|
-
this.patternId = patternId;
|
|
385
|
-
this.patternName = patternName;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
372
|
class Cursor {
|
|
389
373
|
get text() {
|
|
390
374
|
return this._text;
|
|
@@ -439,7 +423,6 @@
|
|
|
439
423
|
this._index = 0;
|
|
440
424
|
this._length = [...text].length;
|
|
441
425
|
this._history = new CursorHistory();
|
|
442
|
-
this._stackTrace = [];
|
|
443
426
|
}
|
|
444
427
|
hasNext() {
|
|
445
428
|
return this._index + 1 < this._length;
|
|
@@ -489,35 +472,6 @@
|
|
|
489
472
|
stopRecording() {
|
|
490
473
|
this._history.stopRecording();
|
|
491
474
|
}
|
|
492
|
-
startParseWith(pattern) {
|
|
493
|
-
const trace = {
|
|
494
|
-
pattern,
|
|
495
|
-
cursorIndex: this.index
|
|
496
|
-
};
|
|
497
|
-
const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
|
|
498
|
-
if (hasCycle) {
|
|
499
|
-
throw new CyclicalParseError(pattern.id, pattern.name);
|
|
500
|
-
}
|
|
501
|
-
this._history.pushStackTrace(trace);
|
|
502
|
-
this._stackTrace.push(trace);
|
|
503
|
-
}
|
|
504
|
-
endParse() {
|
|
505
|
-
this._stackTrace.pop();
|
|
506
|
-
}
|
|
507
|
-
audit() {
|
|
508
|
-
return this._history.trace.map(t => {
|
|
509
|
-
const onChar = this.getChars(t.cursorIndex, t.cursorIndex);
|
|
510
|
-
const restChars = this.getChars(t.cursorIndex + 1, t.cursorIndex + 5);
|
|
511
|
-
const context = `{${t.cursorIndex}}[${onChar}]${restChars}`;
|
|
512
|
-
return `${this._buildPatternContext(t.pattern)}-->${context}`;
|
|
513
|
-
});
|
|
514
|
-
}
|
|
515
|
-
_buildPatternContext(pattern) {
|
|
516
|
-
if (pattern.parent != null) {
|
|
517
|
-
return `${pattern.parent.name}.${pattern.name}`;
|
|
518
|
-
}
|
|
519
|
-
return pattern.name;
|
|
520
|
-
}
|
|
521
475
|
}
|
|
522
476
|
|
|
523
477
|
let idIndex$9 = 0;
|
|
@@ -573,18 +527,15 @@
|
|
|
573
527
|
};
|
|
574
528
|
}
|
|
575
529
|
parse(cursor) {
|
|
576
|
-
cursor.startParseWith(this);
|
|
577
530
|
this._firstIndex = cursor.index;
|
|
578
531
|
const passed = this._tryToParse(cursor);
|
|
579
532
|
if (passed) {
|
|
580
533
|
cursor.resolveError();
|
|
581
534
|
const node = this._createNode();
|
|
582
535
|
cursor.recordMatch(this, node);
|
|
583
|
-
cursor.endParse();
|
|
584
536
|
return node;
|
|
585
537
|
}
|
|
586
538
|
cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
|
|
587
|
-
cursor.endParse();
|
|
588
539
|
return null;
|
|
589
540
|
}
|
|
590
541
|
_tryToParse(cursor) {
|
|
@@ -713,11 +664,9 @@
|
|
|
713
664
|
};
|
|
714
665
|
}
|
|
715
666
|
parse(cursor) {
|
|
716
|
-
cursor.startParseWith(this);
|
|
717
667
|
this._firstIndex = cursor.index;
|
|
718
668
|
this.resetState(cursor);
|
|
719
669
|
this.tryToParse(cursor);
|
|
720
|
-
cursor.endParse();
|
|
721
670
|
return this._node;
|
|
722
671
|
}
|
|
723
672
|
resetState(cursor) {
|
|
@@ -987,36 +936,26 @@
|
|
|
987
936
|
};
|
|
988
937
|
}
|
|
989
938
|
parse(cursor) {
|
|
990
|
-
cursor.startParseWith(this);
|
|
991
939
|
this._firstIndex = cursor.index;
|
|
992
940
|
const node = this._tryToParse(cursor);
|
|
993
941
|
if (node != null) {
|
|
994
942
|
cursor.moveTo(node.lastIndex);
|
|
995
943
|
cursor.resolveError();
|
|
996
|
-
cursor.endParse();
|
|
997
944
|
return node;
|
|
998
945
|
}
|
|
999
946
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
1000
|
-
cursor.endParse();
|
|
1001
947
|
return null;
|
|
1002
948
|
}
|
|
1003
949
|
_tryToParse(cursor) {
|
|
950
|
+
if (this._isBeyondRecursiveLimit()) {
|
|
951
|
+
cursor.recordErrorAt(cursor.index, cursor.index, this);
|
|
952
|
+
return null;
|
|
953
|
+
}
|
|
1004
954
|
const results = [];
|
|
1005
955
|
for (const pattern of this._children) {
|
|
1006
956
|
cursor.moveTo(this._firstIndex);
|
|
1007
957
|
let result = null;
|
|
1008
|
-
|
|
1009
|
-
result = pattern.parse(cursor);
|
|
1010
|
-
}
|
|
1011
|
-
catch (error) {
|
|
1012
|
-
if (error.patternId === this._id) {
|
|
1013
|
-
continue;
|
|
1014
|
-
}
|
|
1015
|
-
else {
|
|
1016
|
-
cursor.endParse();
|
|
1017
|
-
throw error;
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
958
|
+
result = pattern.parse(cursor);
|
|
1020
959
|
if (this._isGreedy) {
|
|
1021
960
|
results.push(result);
|
|
1022
961
|
}
|
|
@@ -1029,6 +968,25 @@
|
|
|
1029
968
|
nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
|
|
1030
969
|
return nonNullResults[0] || null;
|
|
1031
970
|
}
|
|
971
|
+
_isBeyondRecursiveLimit() {
|
|
972
|
+
let pattern = this;
|
|
973
|
+
const matches = [];
|
|
974
|
+
while (pattern.parent != null) {
|
|
975
|
+
if (pattern.type !== "options") {
|
|
976
|
+
pattern = pattern.parent;
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
const optionsPattern = pattern;
|
|
980
|
+
if (pattern.id === this.id && optionsPattern._firstIndex === this._firstIndex) {
|
|
981
|
+
matches.push(pattern);
|
|
982
|
+
if (matches.length > 2) {
|
|
983
|
+
return true;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
pattern = pattern.parent;
|
|
987
|
+
}
|
|
988
|
+
return false;
|
|
989
|
+
}
|
|
1032
990
|
getTokens() {
|
|
1033
991
|
const tokens = [];
|
|
1034
992
|
for (const child of this._children) {
|
|
@@ -1128,7 +1086,6 @@
|
|
|
1128
1086
|
}
|
|
1129
1087
|
}
|
|
1130
1088
|
parse(cursor) {
|
|
1131
|
-
cursor.startParseWith(this);
|
|
1132
1089
|
const startIndex = cursor.index;
|
|
1133
1090
|
const nodes = [];
|
|
1134
1091
|
const modulo = this._hasDivider ? 2 : 1;
|
|
@@ -1167,19 +1124,16 @@
|
|
|
1167
1124
|
const lastIndex = cursor.index;
|
|
1168
1125
|
cursor.moveTo(startIndex);
|
|
1169
1126
|
cursor.recordErrorAt(startIndex, lastIndex, this);
|
|
1170
|
-
cursor.endParse();
|
|
1171
1127
|
return null;
|
|
1172
1128
|
}
|
|
1173
1129
|
if (nodes.length === 0 && !cursor.hasError) {
|
|
1174
1130
|
cursor.moveTo(startIndex);
|
|
1175
|
-
cursor.endParse();
|
|
1176
1131
|
return null;
|
|
1177
1132
|
}
|
|
1178
1133
|
const firstIndex = nodes[0].firstIndex;
|
|
1179
1134
|
const lastIndex = nodes[nodes.length - 1].lastIndex;
|
|
1180
1135
|
cursor.resolveError();
|
|
1181
1136
|
cursor.moveTo(lastIndex);
|
|
1182
|
-
cursor.endParse();
|
|
1183
1137
|
return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
|
|
1184
1138
|
}
|
|
1185
1139
|
test(text) {
|
|
@@ -1325,7 +1279,6 @@
|
|
|
1325
1279
|
};
|
|
1326
1280
|
}
|
|
1327
1281
|
parse(cursor) {
|
|
1328
|
-
cursor.startParseWith(this);
|
|
1329
1282
|
this._firstIndex = cursor.index;
|
|
1330
1283
|
this._nodes = [];
|
|
1331
1284
|
const passed = this._tryToParse(cursor);
|
|
@@ -1336,15 +1289,12 @@
|
|
|
1336
1289
|
cursor.moveTo(node.lastIndex);
|
|
1337
1290
|
cursor.recordMatch(this, node);
|
|
1338
1291
|
}
|
|
1339
|
-
cursor.endParse();
|
|
1340
1292
|
return node;
|
|
1341
1293
|
}
|
|
1342
1294
|
if (this._min > 0) {
|
|
1343
|
-
cursor.endParse();
|
|
1344
1295
|
return null;
|
|
1345
1296
|
}
|
|
1346
1297
|
cursor.resolveError();
|
|
1347
|
-
cursor.endParse();
|
|
1348
1298
|
return null;
|
|
1349
1299
|
}
|
|
1350
1300
|
_meetsMin() {
|
|
@@ -1683,19 +1633,20 @@
|
|
|
1683
1633
|
};
|
|
1684
1634
|
}
|
|
1685
1635
|
parse(cursor) {
|
|
1686
|
-
cursor.startParseWith(this);
|
|
1687
1636
|
this._firstIndex = cursor.index;
|
|
1688
1637
|
this._nodes = [];
|
|
1638
|
+
if (this._isBeyondRecursiveLimit()) {
|
|
1639
|
+
cursor.recordErrorAt(cursor.index, cursor.index, this);
|
|
1640
|
+
return null;
|
|
1641
|
+
}
|
|
1689
1642
|
const passed = this.tryToParse(cursor);
|
|
1690
1643
|
if (passed) {
|
|
1691
1644
|
const node = this.createNode(cursor);
|
|
1692
1645
|
if (node !== null) {
|
|
1693
1646
|
cursor.recordMatch(this, node);
|
|
1694
1647
|
}
|
|
1695
|
-
cursor.endParse();
|
|
1696
1648
|
return node;
|
|
1697
1649
|
}
|
|
1698
|
-
cursor.endParse();
|
|
1699
1650
|
return null;
|
|
1700
1651
|
}
|
|
1701
1652
|
tryToParse(cursor) {
|
|
@@ -1755,6 +1706,25 @@
|
|
|
1755
1706
|
}
|
|
1756
1707
|
return passed;
|
|
1757
1708
|
}
|
|
1709
|
+
_isBeyondRecursiveLimit() {
|
|
1710
|
+
let pattern = this;
|
|
1711
|
+
const matches = [];
|
|
1712
|
+
while (pattern.parent != null) {
|
|
1713
|
+
if (pattern.type !== "sequence") {
|
|
1714
|
+
pattern = pattern.parent;
|
|
1715
|
+
continue;
|
|
1716
|
+
}
|
|
1717
|
+
const sequencePattern = pattern;
|
|
1718
|
+
if (pattern.id === this.id && sequencePattern._firstIndex === this._firstIndex) {
|
|
1719
|
+
matches.push(pattern);
|
|
1720
|
+
if (matches.length > 1) {
|
|
1721
|
+
return true;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
pattern = pattern.parent;
|
|
1725
|
+
}
|
|
1726
|
+
return false;
|
|
1727
|
+
}
|
|
1758
1728
|
getLastValidNode() {
|
|
1759
1729
|
const nodes = filterOutNull(this._nodes);
|
|
1760
1730
|
if (nodes.length === 0) {
|
|
@@ -1937,17 +1907,14 @@
|
|
|
1937
1907
|
};
|
|
1938
1908
|
}
|
|
1939
1909
|
parse(cursor) {
|
|
1940
|
-
cursor.startParseWith(this);
|
|
1941
1910
|
const firstIndex = cursor.index;
|
|
1942
1911
|
const node = this._children[0].parse(cursor);
|
|
1943
1912
|
if (cursor.hasError) {
|
|
1944
1913
|
cursor.resolveError();
|
|
1945
1914
|
cursor.moveTo(firstIndex);
|
|
1946
|
-
cursor.endParse();
|
|
1947
1915
|
return null;
|
|
1948
1916
|
}
|
|
1949
1917
|
else {
|
|
1950
|
-
cursor.endParse();
|
|
1951
1918
|
return node;
|
|
1952
1919
|
}
|
|
1953
1920
|
}
|
|
@@ -2252,7 +2219,6 @@
|
|
|
2252
2219
|
};
|
|
2253
2220
|
}
|
|
2254
2221
|
parse(cursor) {
|
|
2255
|
-
cursor.startParseWith(this);
|
|
2256
2222
|
const firstIndex = cursor.index;
|
|
2257
2223
|
this._children[0].parse(cursor);
|
|
2258
2224
|
if (cursor.hasError) {
|
|
@@ -2264,7 +2230,6 @@
|
|
|
2264
2230
|
cursor.resolveError();
|
|
2265
2231
|
cursor.recordErrorAt(firstIndex, firstIndex, this);
|
|
2266
2232
|
}
|
|
2267
|
-
cursor.endParse();
|
|
2268
2233
|
return null;
|
|
2269
2234
|
}
|
|
2270
2235
|
clone(name = this._name) {
|