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.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,20 +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) {
377
- super("Cyclical Parse Error");
378
- this.patternId = patternId;
379
- this.patternName = patternName;
380
- }
381
- }
382
366
  class Cursor {
383
367
  get text() {
384
368
  return this._text;
@@ -433,7 +417,6 @@ class Cursor {
433
417
  this._index = 0;
434
418
  this._length = [...text].length;
435
419
  this._history = new CursorHistory();
436
- this._stackTrace = [];
437
420
  }
438
421
  hasNext() {
439
422
  return this._index + 1 < this._length;
@@ -483,35 +466,6 @@ class Cursor {
483
466
  stopRecording() {
484
467
  this._history.stopRecording();
485
468
  }
486
- startParseWith(pattern) {
487
- const trace = {
488
- pattern,
489
- cursorIndex: this.index
490
- };
491
- const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
492
- if (hasCycle) {
493
- throw new CyclicalParseError(pattern.id, pattern.name);
494
- }
495
- this._history.pushStackTrace(trace);
496
- this._stackTrace.push(trace);
497
- }
498
- endParse() {
499
- this._stackTrace.pop();
500
- }
501
- audit() {
502
- return this._history.trace.map(t => {
503
- const onChar = this.getChars(t.cursorIndex, t.cursorIndex);
504
- const restChars = this.getChars(t.cursorIndex + 1, t.cursorIndex + 5);
505
- const context = `{${t.cursorIndex}}[${onChar}]${restChars}`;
506
- return `${this._buildPatternContext(t.pattern)}-->${context}`;
507
- });
508
- }
509
- _buildPatternContext(pattern) {
510
- if (pattern.parent != null) {
511
- return `${pattern.parent.name}.${pattern.name}`;
512
- }
513
- return pattern.name;
514
- }
515
469
  }
516
470
 
517
471
  let idIndex$9 = 0;
@@ -567,18 +521,15 @@ class Literal {
567
521
  };
568
522
  }
569
523
  parse(cursor) {
570
- cursor.startParseWith(this);
571
524
  this._firstIndex = cursor.index;
572
525
  const passed = this._tryToParse(cursor);
573
526
  if (passed) {
574
527
  cursor.resolveError();
575
528
  const node = this._createNode();
576
529
  cursor.recordMatch(this, node);
577
- cursor.endParse();
578
530
  return node;
579
531
  }
580
532
  cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
581
- cursor.endParse();
582
533
  return null;
583
534
  }
584
535
  _tryToParse(cursor) {
@@ -707,11 +658,9 @@ class Regex {
707
658
  };
708
659
  }
709
660
  parse(cursor) {
710
- cursor.startParseWith(this);
711
661
  this._firstIndex = cursor.index;
712
662
  this.resetState(cursor);
713
663
  this.tryToParse(cursor);
714
- cursor.endParse();
715
664
  return this._node;
716
665
  }
717
666
  resetState(cursor) {
@@ -981,36 +930,26 @@ class Options {
981
930
  };
982
931
  }
983
932
  parse(cursor) {
984
- cursor.startParseWith(this);
985
933
  this._firstIndex = cursor.index;
986
934
  const node = this._tryToParse(cursor);
987
935
  if (node != null) {
988
936
  cursor.moveTo(node.lastIndex);
989
937
  cursor.resolveError();
990
- cursor.endParse();
991
938
  return node;
992
939
  }
993
940
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
994
- cursor.endParse();
995
941
  return null;
996
942
  }
997
943
  _tryToParse(cursor) {
944
+ if (this._isBeyondRecursiveLimit()) {
945
+ cursor.recordErrorAt(cursor.index, cursor.index, this);
946
+ return null;
947
+ }
998
948
  const results = [];
999
949
  for (const pattern of this._children) {
1000
950
  cursor.moveTo(this._firstIndex);
1001
951
  let result = null;
1002
- try {
1003
- result = pattern.parse(cursor);
1004
- }
1005
- catch (error) {
1006
- if (error.patternId === this._id) {
1007
- continue;
1008
- }
1009
- else {
1010
- cursor.endParse();
1011
- throw error;
1012
- }
1013
- }
952
+ result = pattern.parse(cursor);
1014
953
  if (this._isGreedy) {
1015
954
  results.push(result);
1016
955
  }
@@ -1023,6 +962,25 @@ class Options {
1023
962
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1024
963
  return nonNullResults[0] || null;
1025
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
+ }
1026
984
  getTokens() {
1027
985
  const tokens = [];
1028
986
  for (const child of this._children) {
@@ -1122,7 +1080,6 @@ class FiniteRepeat {
1122
1080
  }
1123
1081
  }
1124
1082
  parse(cursor) {
1125
- cursor.startParseWith(this);
1126
1083
  const startIndex = cursor.index;
1127
1084
  const nodes = [];
1128
1085
  const modulo = this._hasDivider ? 2 : 1;
@@ -1161,19 +1118,16 @@ class FiniteRepeat {
1161
1118
  const lastIndex = cursor.index;
1162
1119
  cursor.moveTo(startIndex);
1163
1120
  cursor.recordErrorAt(startIndex, lastIndex, this);
1164
- cursor.endParse();
1165
1121
  return null;
1166
1122
  }
1167
1123
  if (nodes.length === 0 && !cursor.hasError) {
1168
1124
  cursor.moveTo(startIndex);
1169
- cursor.endParse();
1170
1125
  return null;
1171
1126
  }
1172
1127
  const firstIndex = nodes[0].firstIndex;
1173
1128
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1174
1129
  cursor.resolveError();
1175
1130
  cursor.moveTo(lastIndex);
1176
- cursor.endParse();
1177
1131
  return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1178
1132
  }
1179
1133
  test(text) {
@@ -1319,7 +1273,6 @@ class InfiniteRepeat {
1319
1273
  };
1320
1274
  }
1321
1275
  parse(cursor) {
1322
- cursor.startParseWith(this);
1323
1276
  this._firstIndex = cursor.index;
1324
1277
  this._nodes = [];
1325
1278
  const passed = this._tryToParse(cursor);
@@ -1330,15 +1283,12 @@ class InfiniteRepeat {
1330
1283
  cursor.moveTo(node.lastIndex);
1331
1284
  cursor.recordMatch(this, node);
1332
1285
  }
1333
- cursor.endParse();
1334
1286
  return node;
1335
1287
  }
1336
1288
  if (this._min > 0) {
1337
- cursor.endParse();
1338
1289
  return null;
1339
1290
  }
1340
1291
  cursor.resolveError();
1341
- cursor.endParse();
1342
1292
  return null;
1343
1293
  }
1344
1294
  _meetsMin() {
@@ -1677,19 +1627,20 @@ class Sequence {
1677
1627
  };
1678
1628
  }
1679
1629
  parse(cursor) {
1680
- cursor.startParseWith(this);
1681
1630
  this._firstIndex = cursor.index;
1682
1631
  this._nodes = [];
1632
+ if (this._isBeyondRecursiveLimit()) {
1633
+ cursor.recordErrorAt(cursor.index, cursor.index, this);
1634
+ return null;
1635
+ }
1683
1636
  const passed = this.tryToParse(cursor);
1684
1637
  if (passed) {
1685
1638
  const node = this.createNode(cursor);
1686
1639
  if (node !== null) {
1687
1640
  cursor.recordMatch(this, node);
1688
1641
  }
1689
- cursor.endParse();
1690
1642
  return node;
1691
1643
  }
1692
- cursor.endParse();
1693
1644
  return null;
1694
1645
  }
1695
1646
  tryToParse(cursor) {
@@ -1749,6 +1700,25 @@ class Sequence {
1749
1700
  }
1750
1701
  return passed;
1751
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
+ }
1752
1722
  getLastValidNode() {
1753
1723
  const nodes = filterOutNull(this._nodes);
1754
1724
  if (nodes.length === 0) {
@@ -1931,17 +1901,14 @@ class Optional {
1931
1901
  };
1932
1902
  }
1933
1903
  parse(cursor) {
1934
- cursor.startParseWith(this);
1935
1904
  const firstIndex = cursor.index;
1936
1905
  const node = this._children[0].parse(cursor);
1937
1906
  if (cursor.hasError) {
1938
1907
  cursor.resolveError();
1939
1908
  cursor.moveTo(firstIndex);
1940
- cursor.endParse();
1941
1909
  return null;
1942
1910
  }
1943
1911
  else {
1944
- cursor.endParse();
1945
1912
  return node;
1946
1913
  }
1947
1914
  }
@@ -2246,7 +2213,6 @@ class Not {
2246
2213
  };
2247
2214
  }
2248
2215
  parse(cursor) {
2249
- cursor.startParseWith(this);
2250
2216
  const firstIndex = cursor.index;
2251
2217
  this._children[0].parse(cursor);
2252
2218
  if (cursor.hasError) {
@@ -2258,7 +2224,6 @@ class Not {
2258
2224
  cursor.resolveError();
2259
2225
  cursor.recordErrorAt(firstIndex, firstIndex, this);
2260
2226
  }
2261
- cursor.endParse();
2262
2227
  return null;
2263
2228
  }
2264
2229
  clone(name = this._name) {