clarity-pattern-parser 10.1.20 → 10.1.22

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.
@@ -819,9 +819,9 @@
819
819
  };
820
820
  }
821
821
  parse(cursor) {
822
- return this._getPatternSafely().parse(cursor);
822
+ return this.getReferencePatternSafely().parse(cursor);
823
823
  }
824
- _getPatternSafely() {
824
+ getReferencePatternSafely() {
825
825
  if (this._pattern === null) {
826
826
  let pattern = null;
827
827
  if (this._cachedPattern == null) {
@@ -881,7 +881,7 @@
881
881
  return node;
882
882
  }
883
883
  getTokens() {
884
- return this._getPatternSafely().getTokens();
884
+ return this.getReferencePatternSafely().getTokens();
885
885
  }
886
886
  getTokensAfter(_lastMatched) {
887
887
  if (this._parent == null) {
@@ -896,7 +896,7 @@
896
896
  return this.parent.getTokensAfter(this);
897
897
  }
898
898
  getPatterns() {
899
- return this._getPatternSafely().getPatterns();
899
+ return this.getReferencePatternSafely().getPatterns();
900
900
  }
901
901
  getPatternsAfter(_childReference) {
902
902
  if (this._parent == null) {
@@ -954,6 +954,21 @@
954
954
  }
955
955
  }
956
956
 
957
+ function isRecursivePattern(pattern) {
958
+ let onPattern = pattern.parent;
959
+ let depth = 0;
960
+ while (onPattern != null) {
961
+ if (onPattern.id === pattern.id) {
962
+ depth++;
963
+ }
964
+ onPattern = onPattern.parent;
965
+ if (depth > 1) {
966
+ return true;
967
+ }
968
+ }
969
+ return false;
970
+ }
971
+
957
972
  /*
958
973
  The following is created to reduce the overhead of recursion check.
959
974
  */
@@ -1050,8 +1065,11 @@
1050
1065
  }
1051
1066
  getTokens() {
1052
1067
  const tokens = [];
1053
- for (const child of this._children) {
1054
- tokens.push(...child.getTokens());
1068
+ for (const pattern of this._children) {
1069
+ if (isRecursivePattern(pattern)) {
1070
+ continue;
1071
+ }
1072
+ tokens.push(...pattern.getTokens());
1055
1073
  }
1056
1074
  return tokens;
1057
1075
  }
@@ -1070,6 +1088,9 @@
1070
1088
  getPatterns() {
1071
1089
  const patterns = [];
1072
1090
  for (const pattern of this._children) {
1091
+ if (isRecursivePattern(pattern)) {
1092
+ continue;
1093
+ }
1073
1094
  patterns.push(...pattern.getPatterns());
1074
1095
  }
1075
1096
  return patterns;
@@ -1797,9 +1818,12 @@
1797
1818
  }
1798
1819
  getTokens() {
1799
1820
  const tokens = [];
1800
- for (const child of this._children) {
1801
- tokens.push(...child.getTokens());
1802
- if (child.type !== "optional" && child.type !== "not") {
1821
+ for (const pattern of this._children) {
1822
+ if (isRecursivePattern(pattern) && pattern === this._children[0]) {
1823
+ return tokens;
1824
+ }
1825
+ tokens.push(...pattern.getTokens());
1826
+ if (pattern.type !== "optional" && pattern.type !== "not") {
1803
1827
  break;
1804
1828
  }
1805
1829
  }
@@ -1819,9 +1843,12 @@
1819
1843
  }
1820
1844
  getPatterns() {
1821
1845
  const patterns = [];
1822
- for (const child of this._children) {
1823
- patterns.push(...child.getPatterns());
1824
- if (child.type !== "optional" && child.type !== "not") {
1846
+ for (const pattern of this._children) {
1847
+ if (isRecursivePattern(pattern) && pattern === this._children[0]) {
1848
+ return patterns;
1849
+ }
1850
+ patterns.push(...pattern.getPatterns());
1851
+ if (pattern.type !== "optional" && pattern.type !== "not") {
1825
1852
  break;
1826
1853
  }
1827
1854
  }