clarity-pattern-parser 11.0.7 → 11.0.8

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
@@ -844,6 +844,8 @@ class Reference {
844
844
  this._cachedPattern = null;
845
845
  this._children = [];
846
846
  this._firstIndex = 0;
847
+ this._cachedAncestors = false;
848
+ this._recursiveAncestors = [];
847
849
  }
848
850
  test(text, record = false) {
849
851
  return testPattern(this, text, record);
@@ -853,8 +855,36 @@ class Reference {
853
855
  }
854
856
  parse(cursor) {
855
857
  this._firstIndex = cursor.index;
858
+ this._cacheAncestors();
859
+ if (this._isBeyondRecursiveAllowance()) {
860
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
861
+ return null;
862
+ }
856
863
  return this.getReferencePatternSafely().parse(cursor);
857
864
  }
865
+ _cacheAncestors() {
866
+ if (!this._cachedAncestors) {
867
+ let pattern = this.parent;
868
+ while (pattern != null) {
869
+ if (pattern.type === this.type && pattern.id === this._id) {
870
+ this._recursiveAncestors.push(pattern);
871
+ }
872
+ pattern = pattern.parent;
873
+ }
874
+ }
875
+ }
876
+ _isBeyondRecursiveAllowance() {
877
+ let depth = 0;
878
+ for (let pattern of this._recursiveAncestors) {
879
+ if (pattern._firstIndex === this._firstIndex) {
880
+ depth++;
881
+ if (depth > 2) {
882
+ return true;
883
+ }
884
+ }
885
+ }
886
+ return false;
887
+ }
858
888
  getReferencePatternSafely() {
859
889
  if (this._pattern === null) {
860
890
  let pattern = null;
@@ -1040,9 +1070,6 @@ class Options {
1040
1070
  return null;
1041
1071
  }
1042
1072
  _tryToParse(cursor) {
1043
- if (this._isBeyondRecursiveAllowance()) {
1044
- return null;
1045
- }
1046
1073
  const results = [];
1047
1074
  for (const pattern of this._children) {
1048
1075
  cursor.moveTo(this._firstIndex);
@@ -1060,20 +1087,6 @@ class Options {
1060
1087
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1061
1088
  return nonNullResults[0] || null;
1062
1089
  }
1063
- _isBeyondRecursiveAllowance() {
1064
- let depth = 0;
1065
- let pattern = this;
1066
- while (pattern != null) {
1067
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1068
- depth++;
1069
- }
1070
- if (depth > 2) {
1071
- return true;
1072
- }
1073
- pattern = pattern.parent;
1074
- }
1075
- return false;
1076
- }
1077
1090
  getTokens() {
1078
1091
  const tokens = [];
1079
1092
  for (const pattern of this._children) {
@@ -1729,10 +1742,6 @@ class Sequence {
1729
1742
  return null;
1730
1743
  }
1731
1744
  tryToParse(cursor) {
1732
- if (this._isBeyondRecursiveAllowance()) {
1733
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1734
- return false;
1735
- }
1736
1745
  let passed = false;
1737
1746
  for (let i = 0; i < this._children.length; i++) {
1738
1747
  const runningCursorIndex = cursor.index;
@@ -1796,20 +1805,6 @@ class Sequence {
1796
1805
  }
1797
1806
  return nodes[nodes.length - 1];
1798
1807
  }
1799
- _isBeyondRecursiveAllowance() {
1800
- let depth = 0;
1801
- let pattern = this;
1802
- while (pattern != null) {
1803
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1804
- depth++;
1805
- }
1806
- if (depth > 1) {
1807
- return true;
1808
- }
1809
- pattern = pattern.parent;
1810
- }
1811
- return false;
1812
- }
1813
1808
  areRemainingPatternsOptional(fromIndex) {
1814
1809
  const startOnIndex = fromIndex + 1;
1815
1810
  const length = this._children.length;
@@ -3053,10 +3048,6 @@ class Expression {
3053
3048
  return null;
3054
3049
  }
3055
3050
  _tryToParse(cursor) {
3056
- if (this._isBeyondRecursiveAllowance()) {
3057
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
3058
- return null;
3059
- }
3060
3051
  this._shouldStopParsing = false;
3061
3052
  while (true) {
3062
3053
  cursor.resolveError();
@@ -3187,20 +3178,6 @@ class Expression {
3187
3178
  this._shouldStopParsing = true;
3188
3179
  }
3189
3180
  }
3190
- _isBeyondRecursiveAllowance() {
3191
- let depth = 0;
3192
- let pattern = this;
3193
- while (pattern != null) {
3194
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
3195
- depth++;
3196
- }
3197
- if (depth > 2) {
3198
- return true;
3199
- }
3200
- pattern = pattern.parent;
3201
- }
3202
- return false;
3203
- }
3204
3181
  test(text, record = false) {
3205
3182
  return testPattern(this, text, record);
3206
3183
  }