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.
@@ -882,6 +882,33 @@
882
882
  return patterns.map(p => p.clone());
883
883
  }
884
884
 
885
+ class DepthCache {
886
+ constructor() {
887
+ this._depthMap = {};
888
+ }
889
+ getDepth(name, cursorIndex) {
890
+ if (this._depthMap[name] == null) {
891
+ this._depthMap[name] = {};
892
+ }
893
+ if (this._depthMap[name][cursorIndex] == null) {
894
+ this._depthMap[name][cursorIndex] = 0;
895
+ }
896
+ return this._depthMap[name][cursorIndex];
897
+ }
898
+ incrementDepth(name, cursorIndex) {
899
+ const depth = this.getDepth(name, cursorIndex);
900
+ this._depthMap[name][cursorIndex] = depth + 1;
901
+ }
902
+ decrementDepth(name, cursorIndex) {
903
+ const depth = this.getDepth(name, cursorIndex);
904
+ this._depthMap[name][cursorIndex] = depth - 1;
905
+ }
906
+ }
907
+
908
+ /*
909
+ The following is created to reduce the overhead of recursion check.
910
+ */
911
+ const depthCache$1 = new DepthCache();
885
912
  let idIndex$6 = 0;
886
913
  class Options {
887
914
  get id() {
@@ -936,8 +963,12 @@
936
963
  };
937
964
  }
938
965
  parse(cursor) {
966
+ // This is a cache to help with speed
967
+ this._firstIndex = cursor.index;
968
+ depthCache$1.incrementDepth(this._id, this._firstIndex);
939
969
  this._firstIndex = cursor.index;
940
970
  const node = this._tryToParse(cursor);
971
+ depthCache$1.decrementDepth(this._id, this._firstIndex);
941
972
  if (node != null) {
942
973
  cursor.moveTo(node.lastIndex);
943
974
  cursor.resolveError();
@@ -947,8 +978,8 @@
947
978
  return null;
948
979
  }
949
980
  _tryToParse(cursor) {
950
- if (this._isBeyondRecursiveLimit()) {
951
- cursor.recordErrorAt(cursor.index, cursor.index, this);
981
+ if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
982
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
952
983
  return null;
953
984
  }
954
985
  const results = [];
@@ -968,25 +999,6 @@
968
999
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
969
1000
  return nonNullResults[0] || null;
970
1001
  }
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
- }
990
1002
  getTokens() {
991
1003
  const tokens = [];
992
1004
  for (const child of this._children) {
@@ -1579,6 +1591,7 @@
1579
1591
  return filteredNodes;
1580
1592
  }
1581
1593
 
1594
+ const depthCache = new DepthCache();
1582
1595
  let idIndex$2 = 0;
1583
1596
  class Sequence {
1584
1597
  get id() {
@@ -1633,13 +1646,12 @@
1633
1646
  };
1634
1647
  }
1635
1648
  parse(cursor) {
1649
+ // This is a cache to help with speed
1636
1650
  this._firstIndex = cursor.index;
1651
+ depthCache.incrementDepth(this._id, this._firstIndex);
1637
1652
  this._nodes = [];
1638
- if (this._isBeyondRecursiveLimit()) {
1639
- cursor.recordErrorAt(cursor.index, cursor.index, this);
1640
- return null;
1641
- }
1642
1653
  const passed = this.tryToParse(cursor);
1654
+ depthCache.decrementDepth(this._id, this._firstIndex);
1643
1655
  if (passed) {
1644
1656
  const node = this.createNode(cursor);
1645
1657
  if (node !== null) {
@@ -1650,6 +1662,10 @@
1650
1662
  return null;
1651
1663
  }
1652
1664
  tryToParse(cursor) {
1665
+ if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1666
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1667
+ return false;
1668
+ }
1653
1669
  let passed = false;
1654
1670
  for (let i = 0; i < this._children.length; i++) {
1655
1671
  const runningCursorIndex = cursor.index;
@@ -1706,25 +1722,6 @@
1706
1722
  }
1707
1723
  return passed;
1708
1724
  }
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
- }
1728
1725
  getLastValidNode() {
1729
1726
  const nodes = filterOutNull(this._nodes);
1730
1727
  if (nodes.length === 0) {