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