clarity-pattern-parser 10.1.14 → 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,12 +1000,12 @@
1027
1000
  return null;
1028
1001
  }
1029
1002
  _tryToParse(cursor) {
1030
- if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
1031
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1032
- return null;
1003
+ let children = this.children;
1004
+ if (this._isBeyondRecursiveDepth()) {
1005
+ children = children.slice().reverse();
1033
1006
  }
1034
1007
  const results = [];
1035
- for (const pattern of this._children) {
1008
+ for (const pattern of children) {
1036
1009
  cursor.moveTo(this._firstIndex);
1037
1010
  let result = null;
1038
1011
  result = pattern.parse(cursor);
@@ -1048,6 +1021,20 @@
1048
1021
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1049
1022
  return nonNullResults[0] || null;
1050
1023
  }
1024
+ _isBeyondRecursiveDepth() {
1025
+ let depth = 0;
1026
+ let pattern = this;
1027
+ while (pattern != null) {
1028
+ if (pattern.id === this.id) {
1029
+ depth++;
1030
+ }
1031
+ if (depth >= this.children.length) {
1032
+ return true;
1033
+ }
1034
+ pattern = pattern.parent;
1035
+ }
1036
+ return false;
1037
+ }
1051
1038
  getTokens() {
1052
1039
  const tokens = [];
1053
1040
  for (const child of this._children) {
@@ -1640,7 +1627,6 @@
1640
1627
  return filteredNodes;
1641
1628
  }
1642
1629
 
1643
- const depthCache = new DepthCache();
1644
1630
  let idIndex$2 = 0;
1645
1631
  class Sequence {
1646
1632
  get id() {
@@ -1697,10 +1683,8 @@
1697
1683
  parse(cursor) {
1698
1684
  // This is a cache to help with speed
1699
1685
  this._firstIndex = cursor.index;
1700
- depthCache.incrementDepth(this._id, this._firstIndex);
1701
1686
  this._nodes = [];
1702
1687
  const passed = this.tryToParse(cursor);
1703
- depthCache.decrementDepth(this._id, this._firstIndex);
1704
1688
  if (passed) {
1705
1689
  const node = this.createNode(cursor);
1706
1690
  if (node !== null) {
@@ -1711,7 +1695,7 @@
1711
1695
  return null;
1712
1696
  }
1713
1697
  tryToParse(cursor) {
1714
- if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1698
+ if (this._isBeyondRecursiveDepth()) {
1715
1699
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1716
1700
  return false;
1717
1701
  }
@@ -1771,6 +1755,20 @@
1771
1755
  }
1772
1756
  return passed;
1773
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
+ }
1774
1772
  getLastValidNode() {
1775
1773
  const nodes = filterOutNull(this._nodes);
1776
1774
  if (nodes.length === 0) {