clarity-pattern-parser 10.1.13 → 10.1.15

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.
@@ -931,33 +931,9 @@
931
931
  return patterns.map(p => p.clone());
932
932
  }
933
933
 
934
- class DepthCache {
935
- constructor() {
936
- this._depthMap = {};
937
- }
938
- getDepth(name, cursorIndex) {
939
- if (this._depthMap[name] == null) {
940
- this._depthMap[name] = {};
941
- }
942
- if (this._depthMap[name][cursorIndex] == null) {
943
- this._depthMap[name][cursorIndex] = 0;
944
- }
945
- return this._depthMap[name][cursorIndex];
946
- }
947
- incrementDepth(name, cursorIndex) {
948
- const depth = this.getDepth(name, cursorIndex);
949
- this._depthMap[name][cursorIndex] = depth + 1;
950
- }
951
- decrementDepth(name, cursorIndex) {
952
- const depth = this.getDepth(name, cursorIndex);
953
- this._depthMap[name][cursorIndex] = depth - 1;
954
- }
955
- }
956
-
957
934
  /*
958
935
  The following is created to reduce the overhead of recursion check.
959
936
  */
960
- const depthCache$1 = new DepthCache();
961
937
  let idIndex$6 = 0;
962
938
  class Options {
963
939
  get id() {
@@ -1014,10 +990,7 @@
1014
990
  parse(cursor) {
1015
991
  // This is a cache to help with speed
1016
992
  this._firstIndex = cursor.index;
1017
- depthCache$1.incrementDepth(this._id, this._firstIndex);
1018
- this._firstIndex = cursor.index;
1019
993
  const node = this._tryToParse(cursor);
1020
- depthCache$1.decrementDepth(this._id, this._firstIndex);
1021
994
  if (node != null) {
1022
995
  cursor.moveTo(node.lastIndex);
1023
996
  cursor.resolveError();
@@ -1027,14 +1000,9 @@
1027
1000
  return null;
1028
1001
  }
1029
1002
  _tryToParse(cursor) {
1030
- const shouldReverseOrder = this._shouldReverseOrder();
1031
- let children = this._children;
1032
- if (shouldReverseOrder) {
1033
- children = this._children.slice().reverse();
1034
- }
1035
- if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
1036
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1037
- return null;
1003
+ let children = this.children;
1004
+ if (this._isBeyondRecursiveDepth()) {
1005
+ children = children.slice().reverse();
1038
1006
  }
1039
1007
  const results = [];
1040
1008
  for (const pattern of children) {
@@ -1053,16 +1021,19 @@
1053
1021
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1054
1022
  return nonNullResults[0] || null;
1055
1023
  }
1056
- _shouldReverseOrder() {
1057
- let count = 0;
1058
- let pattern = this._parent;
1024
+ _isBeyondRecursiveDepth() {
1025
+ let depth = 0;
1026
+ let pattern = this;
1059
1027
  while (pattern != null) {
1060
1028
  if (pattern.id === this.id) {
1061
- count++;
1029
+ depth++;
1030
+ }
1031
+ if (depth >= this.children.length) {
1032
+ return true;
1062
1033
  }
1063
1034
  pattern = pattern.parent;
1064
1035
  }
1065
- return count % 2 === 1;
1036
+ return false;
1066
1037
  }
1067
1038
  getTokens() {
1068
1039
  const tokens = [];
@@ -1656,7 +1627,6 @@
1656
1627
  return filteredNodes;
1657
1628
  }
1658
1629
 
1659
- const depthCache = new DepthCache();
1660
1630
  let idIndex$2 = 0;
1661
1631
  class Sequence {
1662
1632
  get id() {
@@ -1713,10 +1683,8 @@
1713
1683
  parse(cursor) {
1714
1684
  // This is a cache to help with speed
1715
1685
  this._firstIndex = cursor.index;
1716
- depthCache.incrementDepth(this._id, this._firstIndex);
1717
1686
  this._nodes = [];
1718
1687
  const passed = this.tryToParse(cursor);
1719
- depthCache.decrementDepth(this._id, this._firstIndex);
1720
1688
  if (passed) {
1721
1689
  const node = this.createNode(cursor);
1722
1690
  if (node !== null) {
@@ -1727,7 +1695,7 @@
1727
1695
  return null;
1728
1696
  }
1729
1697
  tryToParse(cursor) {
1730
- if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1698
+ if (this._isBeyondRecursiveDepth()) {
1731
1699
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1732
1700
  return false;
1733
1701
  }
@@ -1787,6 +1755,20 @@
1787
1755
  }
1788
1756
  return passed;
1789
1757
  }
1758
+ _isBeyondRecursiveDepth() {
1759
+ let depth = 0;
1760
+ let pattern = this;
1761
+ while (pattern != null) {
1762
+ if (pattern.id === this.id && this._firstIndex === pattern._firstIndex) {
1763
+ depth++;
1764
+ }
1765
+ if (depth > 1) {
1766
+ return true;
1767
+ }
1768
+ pattern = pattern.parent;
1769
+ }
1770
+ return false;
1771
+ }
1790
1772
  getLastValidNode() {
1791
1773
  const nodes = filterOutNull(this._nodes);
1792
1774
  if (nodes.length === 0) {