clarity-pattern-parser 10.0.6 → 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.esm.js CHANGED
@@ -876,6 +876,33 @@ function clonePatterns(patterns) {
876
876
  return patterns.map(p => p.clone());
877
877
  }
878
878
 
879
+ class DepthCache {
880
+ constructor() {
881
+ this._depthMap = {};
882
+ }
883
+ getDepth(name, cursorIndex) {
884
+ if (this._depthMap[name] == null) {
885
+ this._depthMap[name] = {};
886
+ }
887
+ if (this._depthMap[name][cursorIndex] == null) {
888
+ this._depthMap[name][cursorIndex] = 0;
889
+ }
890
+ return this._depthMap[name][cursorIndex];
891
+ }
892
+ incrementDepth(name, cursorIndex) {
893
+ const depth = this.getDepth(name, cursorIndex);
894
+ this._depthMap[name][cursorIndex] = depth + 1;
895
+ }
896
+ decrementDepth(name, cursorIndex) {
897
+ const depth = this.getDepth(name, cursorIndex);
898
+ this._depthMap[name][cursorIndex] = depth - 1;
899
+ }
900
+ }
901
+
902
+ /*
903
+ The following is created to reduce the overhead of recursion check.
904
+ */
905
+ const depthCache$1 = new DepthCache();
879
906
  let idIndex$6 = 0;
880
907
  class Options {
881
908
  get id() {
@@ -930,8 +957,12 @@ class Options {
930
957
  };
931
958
  }
932
959
  parse(cursor) {
960
+ // This is a cache to help with speed
961
+ this._firstIndex = cursor.index;
962
+ depthCache$1.incrementDepth(this._id, this._firstIndex);
933
963
  this._firstIndex = cursor.index;
934
964
  const node = this._tryToParse(cursor);
965
+ depthCache$1.decrementDepth(this._id, this._firstIndex);
935
966
  if (node != null) {
936
967
  cursor.moveTo(node.lastIndex);
937
968
  cursor.resolveError();
@@ -941,8 +972,8 @@ class Options {
941
972
  return null;
942
973
  }
943
974
  _tryToParse(cursor) {
944
- if (this._isBeyondRecursiveLimit()) {
945
- cursor.recordErrorAt(cursor.index, cursor.index, this);
975
+ if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
976
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
946
977
  return null;
947
978
  }
948
979
  const results = [];
@@ -962,25 +993,6 @@ class Options {
962
993
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
963
994
  return nonNullResults[0] || null;
964
995
  }
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
- }
984
996
  getTokens() {
985
997
  const tokens = [];
986
998
  for (const child of this._children) {
@@ -1573,6 +1585,7 @@ function filterOutNull(nodes) {
1573
1585
  return filteredNodes;
1574
1586
  }
1575
1587
 
1588
+ const depthCache = new DepthCache();
1576
1589
  let idIndex$2 = 0;
1577
1590
  class Sequence {
1578
1591
  get id() {
@@ -1627,13 +1640,12 @@ class Sequence {
1627
1640
  };
1628
1641
  }
1629
1642
  parse(cursor) {
1643
+ // This is a cache to help with speed
1630
1644
  this._firstIndex = cursor.index;
1645
+ depthCache.incrementDepth(this._id, this._firstIndex);
1631
1646
  this._nodes = [];
1632
- if (this._isBeyondRecursiveLimit()) {
1633
- cursor.recordErrorAt(cursor.index, cursor.index, this);
1634
- return null;
1635
- }
1636
1647
  const passed = this.tryToParse(cursor);
1648
+ depthCache.decrementDepth(this._id, this._firstIndex);
1637
1649
  if (passed) {
1638
1650
  const node = this.createNode(cursor);
1639
1651
  if (node !== null) {
@@ -1644,6 +1656,10 @@ class Sequence {
1644
1656
  return null;
1645
1657
  }
1646
1658
  tryToParse(cursor) {
1659
+ if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1660
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1661
+ return false;
1662
+ }
1647
1663
  let passed = false;
1648
1664
  for (let i = 0; i < this._children.length; i++) {
1649
1665
  const runningCursorIndex = cursor.index;
@@ -1700,25 +1716,6 @@ class Sequence {
1700
1716
  }
1701
1717
  return passed;
1702
1718
  }
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
- }
1722
1719
  getLastValidNode() {
1723
1720
  const nodes = filterOutNull(this._nodes);
1724
1721
  if (nodes.length === 0) {