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.esm.js CHANGED
@@ -925,33 +925,9 @@ function clonePatterns(patterns) {
925
925
  return patterns.map(p => p.clone());
926
926
  }
927
927
 
928
- class DepthCache {
929
- constructor() {
930
- this._depthMap = {};
931
- }
932
- getDepth(name, cursorIndex) {
933
- if (this._depthMap[name] == null) {
934
- this._depthMap[name] = {};
935
- }
936
- if (this._depthMap[name][cursorIndex] == null) {
937
- this._depthMap[name][cursorIndex] = 0;
938
- }
939
- return this._depthMap[name][cursorIndex];
940
- }
941
- incrementDepth(name, cursorIndex) {
942
- const depth = this.getDepth(name, cursorIndex);
943
- this._depthMap[name][cursorIndex] = depth + 1;
944
- }
945
- decrementDepth(name, cursorIndex) {
946
- const depth = this.getDepth(name, cursorIndex);
947
- this._depthMap[name][cursorIndex] = depth - 1;
948
- }
949
- }
950
-
951
928
  /*
952
929
  The following is created to reduce the overhead of recursion check.
953
930
  */
954
- const depthCache$1 = new DepthCache();
955
931
  let idIndex$6 = 0;
956
932
  class Options {
957
933
  get id() {
@@ -1008,10 +984,7 @@ class Options {
1008
984
  parse(cursor) {
1009
985
  // This is a cache to help with speed
1010
986
  this._firstIndex = cursor.index;
1011
- depthCache$1.incrementDepth(this._id, this._firstIndex);
1012
- this._firstIndex = cursor.index;
1013
987
  const node = this._tryToParse(cursor);
1014
- depthCache$1.decrementDepth(this._id, this._firstIndex);
1015
988
  if (node != null) {
1016
989
  cursor.moveTo(node.lastIndex);
1017
990
  cursor.resolveError();
@@ -1021,14 +994,9 @@ class Options {
1021
994
  return null;
1022
995
  }
1023
996
  _tryToParse(cursor) {
1024
- const shouldReverseOrder = this._shouldReverseOrder();
1025
- let children = this._children;
1026
- if (shouldReverseOrder) {
1027
- children = this._children.slice().reverse();
1028
- }
1029
- if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
1030
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1031
- return null;
997
+ let children = this.children;
998
+ if (this._isBeyondRecursiveDepth()) {
999
+ children = children.slice().reverse();
1032
1000
  }
1033
1001
  const results = [];
1034
1002
  for (const pattern of children) {
@@ -1047,16 +1015,19 @@ class Options {
1047
1015
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1048
1016
  return nonNullResults[0] || null;
1049
1017
  }
1050
- _shouldReverseOrder() {
1051
- let count = 0;
1052
- let pattern = this._parent;
1018
+ _isBeyondRecursiveDepth() {
1019
+ let depth = 0;
1020
+ let pattern = this;
1053
1021
  while (pattern != null) {
1054
1022
  if (pattern.id === this.id) {
1055
- count++;
1023
+ depth++;
1024
+ }
1025
+ if (depth >= this.children.length) {
1026
+ return true;
1056
1027
  }
1057
1028
  pattern = pattern.parent;
1058
1029
  }
1059
- return count % 2 === 1;
1030
+ return false;
1060
1031
  }
1061
1032
  getTokens() {
1062
1033
  const tokens = [];
@@ -1650,7 +1621,6 @@ function filterOutNull(nodes) {
1650
1621
  return filteredNodes;
1651
1622
  }
1652
1623
 
1653
- const depthCache = new DepthCache();
1654
1624
  let idIndex$2 = 0;
1655
1625
  class Sequence {
1656
1626
  get id() {
@@ -1707,10 +1677,8 @@ class Sequence {
1707
1677
  parse(cursor) {
1708
1678
  // This is a cache to help with speed
1709
1679
  this._firstIndex = cursor.index;
1710
- depthCache.incrementDepth(this._id, this._firstIndex);
1711
1680
  this._nodes = [];
1712
1681
  const passed = this.tryToParse(cursor);
1713
- depthCache.decrementDepth(this._id, this._firstIndex);
1714
1682
  if (passed) {
1715
1683
  const node = this.createNode(cursor);
1716
1684
  if (node !== null) {
@@ -1721,7 +1689,7 @@ class Sequence {
1721
1689
  return null;
1722
1690
  }
1723
1691
  tryToParse(cursor) {
1724
- if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1692
+ if (this._isBeyondRecursiveDepth()) {
1725
1693
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1726
1694
  return false;
1727
1695
  }
@@ -1781,6 +1749,20 @@ class Sequence {
1781
1749
  }
1782
1750
  return passed;
1783
1751
  }
1752
+ _isBeyondRecursiveDepth() {
1753
+ let depth = 0;
1754
+ let pattern = this;
1755
+ while (pattern != null) {
1756
+ if (pattern.id === this.id && this._firstIndex === pattern._firstIndex) {
1757
+ depth++;
1758
+ }
1759
+ if (depth > 1) {
1760
+ return true;
1761
+ }
1762
+ pattern = pattern.parent;
1763
+ }
1764
+ return false;
1765
+ }
1784
1766
  getLastValidNode() {
1785
1767
  const nodes = filterOutNull(this._nodes);
1786
1768
  if (nodes.length === 0) {