clarity-pattern-parser 10.1.21 → 10.1.23

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
@@ -813,9 +813,9 @@ class Reference {
813
813
  };
814
814
  }
815
815
  parse(cursor) {
816
- return this._getPatternSafely().parse(cursor);
816
+ return this.getReferencePatternSafely().parse(cursor);
817
817
  }
818
- _getPatternSafely() {
818
+ getReferencePatternSafely() {
819
819
  if (this._pattern === null) {
820
820
  let pattern = null;
821
821
  if (this._cachedPattern == null) {
@@ -875,7 +875,7 @@ class Reference {
875
875
  return node;
876
876
  }
877
877
  getTokens() {
878
- return this._getPatternSafely().getTokens();
878
+ return this.getReferencePatternSafely().getTokens();
879
879
  }
880
880
  getTokensAfter(_lastMatched) {
881
881
  if (this._parent == null) {
@@ -890,7 +890,7 @@ class Reference {
890
890
  return this.parent.getTokensAfter(this);
891
891
  }
892
892
  getPatterns() {
893
- return this._getPatternSafely().getPatterns();
893
+ return this.getReferencePatternSafely().getPatterns();
894
894
  }
895
895
  getPatternsAfter(_childReference) {
896
896
  if (this._parent == null) {
@@ -948,6 +948,21 @@ class DepthCache {
948
948
  }
949
949
  }
950
950
 
951
+ function isRecursivePattern(pattern) {
952
+ let onPattern = pattern.parent;
953
+ let depth = 0;
954
+ while (onPattern != null) {
955
+ if (onPattern.id === pattern.id) {
956
+ depth++;
957
+ }
958
+ onPattern = onPattern.parent;
959
+ if (depth > 1) {
960
+ return true;
961
+ }
962
+ }
963
+ return false;
964
+ }
965
+
951
966
  /*
952
967
  The following is created to reduce the overhead of recursion check.
953
968
  */
@@ -1045,7 +1060,7 @@ class Options {
1045
1060
  getTokens() {
1046
1061
  const tokens = [];
1047
1062
  for (const pattern of this._children) {
1048
- if (pattern.type === "reference" && pattern.name === this.name) {
1063
+ if (isRecursivePattern(pattern)) {
1049
1064
  continue;
1050
1065
  }
1051
1066
  tokens.push(...pattern.getTokens());
@@ -1067,7 +1082,7 @@ class Options {
1067
1082
  getPatterns() {
1068
1083
  const patterns = [];
1069
1084
  for (const pattern of this._children) {
1070
- if (pattern.type === "reference" && pattern.name === this.name) {
1085
+ if (isRecursivePattern(pattern)) {
1071
1086
  continue;
1072
1087
  }
1073
1088
  patterns.push(...pattern.getPatterns());
@@ -1798,7 +1813,7 @@ class Sequence {
1798
1813
  getTokens() {
1799
1814
  const tokens = [];
1800
1815
  for (const pattern of this._children) {
1801
- if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
1816
+ if (isRecursivePattern(pattern) && pattern === this._children[0]) {
1802
1817
  return tokens;
1803
1818
  }
1804
1819
  tokens.push(...pattern.getTokens());
@@ -1823,7 +1838,7 @@ class Sequence {
1823
1838
  getPatterns() {
1824
1839
  const patterns = [];
1825
1840
  for (const pattern of this._children) {
1826
- if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
1841
+ if (isRecursivePattern(pattern) && pattern === this._children[0]) {
1827
1842
  return patterns;
1828
1843
  }
1829
1844
  patterns.push(...pattern.getPatterns());
@@ -2608,6 +2623,9 @@ class Context {
2608
2623
  return this._pattern.getTokens();
2609
2624
  }
2610
2625
  getTokensAfter(childReference) {
2626
+ if (this.parent == null) {
2627
+ return [];
2628
+ }
2611
2629
  return this._pattern.getTokensAfter(childReference);
2612
2630
  }
2613
2631
  getNextTokens() {
@@ -2617,7 +2635,10 @@ class Context {
2617
2635
  return this._pattern.getPatterns();
2618
2636
  }
2619
2637
  getPatternsAfter(childReference) {
2620
- return this._pattern.getPatternsAfter(childReference);
2638
+ if (this.parent == null) {
2639
+ return [];
2640
+ }
2641
+ return this.parent.getPatternsAfter(childReference);
2621
2642
  }
2622
2643
  getNextPatterns() {
2623
2644
  return this._pattern.getNextPatterns();