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.
@@ -850,6 +850,8 @@
850
850
  this._cachedPattern = null;
851
851
  this._children = [];
852
852
  this._firstIndex = 0;
853
+ this._cachedAncestors = false;
854
+ this._recursiveAncestors = [];
853
855
  }
854
856
  test(text, record = false) {
855
857
  return testPattern(this, text, record);
@@ -859,8 +861,36 @@
859
861
  }
860
862
  parse(cursor) {
861
863
  this._firstIndex = cursor.index;
864
+ this._cacheAncestors();
865
+ if (this._isBeyondRecursiveAllowance()) {
866
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
867
+ return null;
868
+ }
862
869
  return this.getReferencePatternSafely().parse(cursor);
863
870
  }
871
+ _cacheAncestors() {
872
+ if (!this._cachedAncestors) {
873
+ let pattern = this.parent;
874
+ while (pattern != null) {
875
+ if (pattern.type === this.type && pattern.id === this._id) {
876
+ this._recursiveAncestors.push(pattern);
877
+ }
878
+ pattern = pattern.parent;
879
+ }
880
+ }
881
+ }
882
+ _isBeyondRecursiveAllowance() {
883
+ let depth = 0;
884
+ for (let pattern of this._recursiveAncestors) {
885
+ if (pattern._firstIndex === this._firstIndex) {
886
+ depth++;
887
+ if (depth > 2) {
888
+ return true;
889
+ }
890
+ }
891
+ }
892
+ return false;
893
+ }
864
894
  getReferencePatternSafely() {
865
895
  if (this._pattern === null) {
866
896
  let pattern = null;
@@ -1046,9 +1076,6 @@
1046
1076
  return null;
1047
1077
  }
1048
1078
  _tryToParse(cursor) {
1049
- if (this._isBeyondRecursiveAllowance()) {
1050
- return null;
1051
- }
1052
1079
  const results = [];
1053
1080
  for (const pattern of this._children) {
1054
1081
  cursor.moveTo(this._firstIndex);
@@ -1066,20 +1093,6 @@
1066
1093
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1067
1094
  return nonNullResults[0] || null;
1068
1095
  }
1069
- _isBeyondRecursiveAllowance() {
1070
- let depth = 0;
1071
- let pattern = this;
1072
- while (pattern != null) {
1073
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1074
- depth++;
1075
- }
1076
- if (depth > 2) {
1077
- return true;
1078
- }
1079
- pattern = pattern.parent;
1080
- }
1081
- return false;
1082
- }
1083
1096
  getTokens() {
1084
1097
  const tokens = [];
1085
1098
  for (const pattern of this._children) {
@@ -1735,10 +1748,6 @@
1735
1748
  return null;
1736
1749
  }
1737
1750
  tryToParse(cursor) {
1738
- if (this._isBeyondRecursiveAllowance()) {
1739
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1740
- return false;
1741
- }
1742
1751
  let passed = false;
1743
1752
  for (let i = 0; i < this._children.length; i++) {
1744
1753
  const runningCursorIndex = cursor.index;
@@ -1802,20 +1811,6 @@
1802
1811
  }
1803
1812
  return nodes[nodes.length - 1];
1804
1813
  }
1805
- _isBeyondRecursiveAllowance() {
1806
- let depth = 0;
1807
- let pattern = this;
1808
- while (pattern != null) {
1809
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1810
- depth++;
1811
- }
1812
- if (depth > 1) {
1813
- return true;
1814
- }
1815
- pattern = pattern.parent;
1816
- }
1817
- return false;
1818
- }
1819
1814
  areRemainingPatternsOptional(fromIndex) {
1820
1815
  const startOnIndex = fromIndex + 1;
1821
1816
  const length = this._children.length;
@@ -3059,10 +3054,6 @@
3059
3054
  return null;
3060
3055
  }
3061
3056
  _tryToParse(cursor) {
3062
- if (this._isBeyondRecursiveAllowance()) {
3063
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
3064
- return null;
3065
- }
3066
3057
  this._shouldStopParsing = false;
3067
3058
  while (true) {
3068
3059
  cursor.resolveError();
@@ -3193,20 +3184,6 @@
3193
3184
  this._shouldStopParsing = true;
3194
3185
  }
3195
3186
  }
3196
- _isBeyondRecursiveAllowance() {
3197
- let depth = 0;
3198
- let pattern = this;
3199
- while (pattern != null) {
3200
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
3201
- depth++;
3202
- }
3203
- if (depth > 2) {
3204
- return true;
3205
- }
3206
- pattern = pattern.parent;
3207
- }
3208
- return false;
3209
- }
3210
3187
  test(text, record = false) {
3211
3188
  return testPattern(this, text, record);
3212
3189
  }