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.esm.js CHANGED
@@ -279,7 +279,6 @@ class CursorHistory {
279
279
  this._patterns = [];
280
280
  this._nodes = [];
281
281
  this._errors = [];
282
- this._trace = [];
283
282
  }
284
283
  get isRecording() {
285
284
  return this._isRecording;
@@ -308,9 +307,6 @@ class CursorHistory {
308
307
  get patterns() {
309
308
  return this._patterns;
310
309
  }
311
- get trace() {
312
- return this._trace;
313
- }
314
310
  recordMatch(pattern, node) {
315
311
  if (this._isRecording) {
316
312
  this._patterns.push(pattern);
@@ -365,21 +361,8 @@ class CursorHistory {
365
361
  resolveError() {
366
362
  this._currentError = null;
367
363
  }
368
- pushStackTrace(trace) {
369
- if (this._isRecording) {
370
- this._trace.push(trace);
371
- }
372
- }
373
364
  }
374
365
 
375
- class CyclicalParseError extends Error {
376
- constructor(patternId, patternName, cursorIndex) {
377
- super("Cyclical Parse Error");
378
- this.patternId = patternId;
379
- this.patternName = patternName;
380
- this.cursorIndex = cursorIndex;
381
- }
382
- }
383
366
  class Cursor {
384
367
  get text() {
385
368
  return this._text;
@@ -434,7 +417,6 @@ class Cursor {
434
417
  this._index = 0;
435
418
  this._length = [...text].length;
436
419
  this._history = new CursorHistory();
437
- this._stackTrace = [];
438
420
  }
439
421
  hasNext() {
440
422
  return this._index + 1 < this._length;
@@ -484,35 +466,6 @@ class Cursor {
484
466
  stopRecording() {
485
467
  this._history.stopRecording();
486
468
  }
487
- startParseWith(pattern) {
488
- const trace = {
489
- pattern,
490
- cursorIndex: this.index
491
- };
492
- const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
493
- if (hasCycle) {
494
- throw new CyclicalParseError(pattern.id, pattern.name, this.index);
495
- }
496
- this._history.pushStackTrace(trace);
497
- this._stackTrace.push(trace);
498
- }
499
- endParse() {
500
- this._stackTrace.pop();
501
- }
502
- audit() {
503
- return this._history.trace.map(t => {
504
- const onChar = this.getChars(t.cursorIndex, t.cursorIndex);
505
- const restChars = this.getChars(t.cursorIndex + 1, t.cursorIndex + 5);
506
- const context = `{${t.cursorIndex}}[${onChar}]${restChars}`;
507
- return `${this._buildPatternContext(t.pattern)}-->${context}`;
508
- });
509
- }
510
- _buildPatternContext(pattern) {
511
- if (pattern.parent != null) {
512
- return `${pattern.parent.name}.${pattern.name}`;
513
- }
514
- return pattern.name;
515
- }
516
469
  }
517
470
 
518
471
  let idIndex$9 = 0;
@@ -568,18 +521,15 @@ class Literal {
568
521
  };
569
522
  }
570
523
  parse(cursor) {
571
- cursor.startParseWith(this);
572
524
  this._firstIndex = cursor.index;
573
525
  const passed = this._tryToParse(cursor);
574
526
  if (passed) {
575
527
  cursor.resolveError();
576
528
  const node = this._createNode();
577
529
  cursor.recordMatch(this, node);
578
- cursor.endParse();
579
530
  return node;
580
531
  }
581
532
  cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
582
- cursor.endParse();
583
533
  return null;
584
534
  }
585
535
  _tryToParse(cursor) {
@@ -708,11 +658,9 @@ class Regex {
708
658
  };
709
659
  }
710
660
  parse(cursor) {
711
- cursor.startParseWith(this);
712
661
  this._firstIndex = cursor.index;
713
662
  this.resetState(cursor);
714
663
  this.tryToParse(cursor);
715
- cursor.endParse();
716
664
  return this._node;
717
665
  }
718
666
  resetState(cursor) {
@@ -982,30 +930,26 @@ class Options {
982
930
  };
983
931
  }
984
932
  parse(cursor) {
985
- cursor.startParseWith(this);
986
933
  this._firstIndex = cursor.index;
987
934
  const node = this._tryToParse(cursor);
988
935
  if (node != null) {
989
936
  cursor.moveTo(node.lastIndex);
990
937
  cursor.resolveError();
991
- cursor.endParse();
992
938
  return node;
993
939
  }
994
940
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
995
- cursor.endParse();
996
941
  return null;
997
942
  }
998
943
  _tryToParse(cursor) {
944
+ if (this._isBeyondRecursiveLimit()) {
945
+ cursor.recordErrorAt(cursor.index, cursor.index, this);
946
+ return null;
947
+ }
999
948
  const results = [];
1000
949
  for (const pattern of this._children) {
1001
950
  cursor.moveTo(this._firstIndex);
1002
951
  let result = null;
1003
- try {
1004
- result = pattern.parse(cursor);
1005
- }
1006
- catch (_a) {
1007
- continue;
1008
- }
952
+ result = pattern.parse(cursor);
1009
953
  if (this._isGreedy) {
1010
954
  results.push(result);
1011
955
  }
@@ -1018,6 +962,25 @@ class Options {
1018
962
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1019
963
  return nonNullResults[0] || null;
1020
964
  }
965
+ _isBeyondRecursiveLimit() {
966
+ let pattern = this;
967
+ const matches = [];
968
+ while (pattern.parent != null) {
969
+ if (pattern.type !== "options") {
970
+ pattern = pattern.parent;
971
+ continue;
972
+ }
973
+ const optionsPattern = pattern;
974
+ if (pattern.id === this.id && optionsPattern._firstIndex === this._firstIndex) {
975
+ matches.push(pattern);
976
+ if (matches.length > 2) {
977
+ return true;
978
+ }
979
+ }
980
+ pattern = pattern.parent;
981
+ }
982
+ return false;
983
+ }
1021
984
  getTokens() {
1022
985
  const tokens = [];
1023
986
  for (const child of this._children) {
@@ -1117,7 +1080,6 @@ class FiniteRepeat {
1117
1080
  }
1118
1081
  }
1119
1082
  parse(cursor) {
1120
- cursor.startParseWith(this);
1121
1083
  const startIndex = cursor.index;
1122
1084
  const nodes = [];
1123
1085
  const modulo = this._hasDivider ? 2 : 1;
@@ -1156,19 +1118,16 @@ class FiniteRepeat {
1156
1118
  const lastIndex = cursor.index;
1157
1119
  cursor.moveTo(startIndex);
1158
1120
  cursor.recordErrorAt(startIndex, lastIndex, this);
1159
- cursor.endParse();
1160
1121
  return null;
1161
1122
  }
1162
1123
  if (nodes.length === 0 && !cursor.hasError) {
1163
1124
  cursor.moveTo(startIndex);
1164
- cursor.endParse();
1165
1125
  return null;
1166
1126
  }
1167
1127
  const firstIndex = nodes[0].firstIndex;
1168
1128
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1169
1129
  cursor.resolveError();
1170
1130
  cursor.moveTo(lastIndex);
1171
- cursor.endParse();
1172
1131
  return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1173
1132
  }
1174
1133
  test(text) {
@@ -1314,7 +1273,6 @@ class InfiniteRepeat {
1314
1273
  };
1315
1274
  }
1316
1275
  parse(cursor) {
1317
- cursor.startParseWith(this);
1318
1276
  this._firstIndex = cursor.index;
1319
1277
  this._nodes = [];
1320
1278
  const passed = this._tryToParse(cursor);
@@ -1325,15 +1283,12 @@ class InfiniteRepeat {
1325
1283
  cursor.moveTo(node.lastIndex);
1326
1284
  cursor.recordMatch(this, node);
1327
1285
  }
1328
- cursor.endParse();
1329
1286
  return node;
1330
1287
  }
1331
1288
  if (this._min > 0) {
1332
- cursor.endParse();
1333
1289
  return null;
1334
1290
  }
1335
1291
  cursor.resolveError();
1336
- cursor.endParse();
1337
1292
  return null;
1338
1293
  }
1339
1294
  _meetsMin() {
@@ -1672,19 +1627,20 @@ class Sequence {
1672
1627
  };
1673
1628
  }
1674
1629
  parse(cursor) {
1675
- cursor.startParseWith(this);
1676
1630
  this._firstIndex = cursor.index;
1677
1631
  this._nodes = [];
1632
+ if (this._isBeyondRecursiveLimit()) {
1633
+ cursor.recordErrorAt(cursor.index, cursor.index, this);
1634
+ return null;
1635
+ }
1678
1636
  const passed = this.tryToParse(cursor);
1679
1637
  if (passed) {
1680
1638
  const node = this.createNode(cursor);
1681
1639
  if (node !== null) {
1682
1640
  cursor.recordMatch(this, node);
1683
1641
  }
1684
- cursor.endParse();
1685
1642
  return node;
1686
1643
  }
1687
- cursor.endParse();
1688
1644
  return null;
1689
1645
  }
1690
1646
  tryToParse(cursor) {
@@ -1744,6 +1700,25 @@ class Sequence {
1744
1700
  }
1745
1701
  return passed;
1746
1702
  }
1703
+ _isBeyondRecursiveLimit() {
1704
+ let pattern = this;
1705
+ const matches = [];
1706
+ while (pattern.parent != null) {
1707
+ if (pattern.type !== "sequence") {
1708
+ pattern = pattern.parent;
1709
+ continue;
1710
+ }
1711
+ const sequencePattern = pattern;
1712
+ if (pattern.id === this.id && sequencePattern._firstIndex === this._firstIndex) {
1713
+ matches.push(pattern);
1714
+ if (matches.length > 1) {
1715
+ return true;
1716
+ }
1717
+ }
1718
+ pattern = pattern.parent;
1719
+ }
1720
+ return false;
1721
+ }
1747
1722
  getLastValidNode() {
1748
1723
  const nodes = filterOutNull(this._nodes);
1749
1724
  if (nodes.length === 0) {
@@ -1926,17 +1901,14 @@ class Optional {
1926
1901
  };
1927
1902
  }
1928
1903
  parse(cursor) {
1929
- cursor.startParseWith(this);
1930
1904
  const firstIndex = cursor.index;
1931
1905
  const node = this._children[0].parse(cursor);
1932
1906
  if (cursor.hasError) {
1933
1907
  cursor.resolveError();
1934
1908
  cursor.moveTo(firstIndex);
1935
- cursor.endParse();
1936
1909
  return null;
1937
1910
  }
1938
1911
  else {
1939
- cursor.endParse();
1940
1912
  return node;
1941
1913
  }
1942
1914
  }
@@ -2241,7 +2213,6 @@ class Not {
2241
2213
  };
2242
2214
  }
2243
2215
  parse(cursor) {
2244
- cursor.startParseWith(this);
2245
2216
  const firstIndex = cursor.index;
2246
2217
  this._children[0].parse(cursor);
2247
2218
  if (cursor.hasError) {
@@ -2253,7 +2224,6 @@ class Not {
2253
2224
  cursor.resolveError();
2254
2225
  cursor.recordErrorAt(firstIndex, firstIndex, this);
2255
2226
  }
2256
- cursor.endParse();
2257
2227
  return null;
2258
2228
  }
2259
2229
  clone(name = this._name) {