clarity-pattern-parser 10.3.1 → 10.3.2

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
@@ -528,6 +528,9 @@ class Literal {
528
528
  get children() {
529
529
  return [];
530
530
  }
531
+ get startedOnIndex() {
532
+ return this._firstIndex;
533
+ }
531
534
  constructor(name, value) {
532
535
  this.shouldCompactAst = false;
533
536
  if (value.length === 0) {
@@ -657,10 +660,13 @@ class Regex {
657
660
  get children() {
658
661
  return [];
659
662
  }
663
+ get startedOnIndex() {
664
+ return this._firstIndex;
665
+ }
660
666
  constructor(name, regex) {
661
667
  this._node = null;
662
668
  this._cursor = null;
663
- this._firstIndex = -1;
669
+ this._firstIndex = 0;
664
670
  this._substring = "";
665
671
  this._tokens = [];
666
672
  this.shouldCompactAst = false;
@@ -813,6 +819,9 @@ class Reference {
813
819
  get children() {
814
820
  return this._children;
815
821
  }
822
+ get startedOnIndex() {
823
+ return this._firstIndex;
824
+ }
816
825
  constructor(name) {
817
826
  this.shouldCompactAst = false;
818
827
  this._id = `reference-${idIndex$7++}`;
@@ -822,6 +831,7 @@ class Reference {
822
831
  this._pattern = null;
823
832
  this._cachedPattern = null;
824
833
  this._children = [];
834
+ this._firstIndex = 0;
825
835
  }
826
836
  test(text) {
827
837
  const cursor = new Cursor(text);
@@ -838,6 +848,7 @@ class Reference {
838
848
  };
839
849
  }
840
850
  parse(cursor) {
851
+ this._firstIndex = cursor.index;
841
852
  return this.getReferencePatternSafely().parse(cursor);
842
853
  }
843
854
  getReferencePatternSafely() {
@@ -951,29 +962,6 @@ function clonePatterns(patterns) {
951
962
  return patterns.map(p => p.clone());
952
963
  }
953
964
 
954
- class DepthCache {
955
- constructor() {
956
- this._depthMap = {};
957
- }
958
- getDepth(name, cursorIndex) {
959
- if (this._depthMap[name] == null) {
960
- this._depthMap[name] = {};
961
- }
962
- if (this._depthMap[name][cursorIndex] == null) {
963
- this._depthMap[name][cursorIndex] = 0;
964
- }
965
- return this._depthMap[name][cursorIndex];
966
- }
967
- incrementDepth(name, cursorIndex) {
968
- const depth = this.getDepth(name, cursorIndex);
969
- this._depthMap[name][cursorIndex] = depth + 1;
970
- }
971
- decrementDepth(name, cursorIndex) {
972
- const depth = this.getDepth(name, cursorIndex);
973
- this._depthMap[name][cursorIndex] = depth - 1;
974
- }
975
- }
976
-
977
965
  function isRecursivePattern(pattern) {
978
966
  let onPattern = pattern.parent;
979
967
  let depth = 0;
@@ -989,10 +977,6 @@ function isRecursivePattern(pattern) {
989
977
  return false;
990
978
  }
991
979
 
992
- /*
993
- The following is created to reduce the overhead of recursion check.
994
- */
995
- const depthCache$2 = new DepthCache();
996
980
  let idIndex$6 = 0;
997
981
  class Options {
998
982
  get id() {
@@ -1013,6 +997,9 @@ class Options {
1013
997
  get children() {
1014
998
  return this._children;
1015
999
  }
1000
+ get startedOnIndex() {
1001
+ return this._firstIndex;
1002
+ }
1016
1003
  constructor(name, options, isGreedy = false) {
1017
1004
  this.shouldCompactAst = false;
1018
1005
  if (options.length === 0) {
@@ -1048,12 +1035,8 @@ class Options {
1048
1035
  };
1049
1036
  }
1050
1037
  parse(cursor) {
1051
- // This is a cache to help with speed
1052
- this._firstIndex = cursor.index;
1053
- depthCache$2.incrementDepth(this._id, this._firstIndex);
1054
1038
  this._firstIndex = cursor.index;
1055
1039
  const node = this._tryToParse(cursor);
1056
- depthCache$2.decrementDepth(this._id, this._firstIndex);
1057
1040
  if (node != null) {
1058
1041
  cursor.moveTo(node.lastIndex);
1059
1042
  cursor.resolveError();
@@ -1066,7 +1049,7 @@ class Options {
1066
1049
  return null;
1067
1050
  }
1068
1051
  _tryToParse(cursor) {
1069
- if (depthCache$2.getDepth(this._id, this._firstIndex) > 2) {
1052
+ if (this._isBeyondRecursiveAllowance()) {
1070
1053
  return null;
1071
1054
  }
1072
1055
  const results = [];
@@ -1086,6 +1069,20 @@ class Options {
1086
1069
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1087
1070
  return nonNullResults[0] || null;
1088
1071
  }
1072
+ _isBeyondRecursiveAllowance() {
1073
+ let depth = 0;
1074
+ let pattern = this;
1075
+ while (pattern != null) {
1076
+ if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1077
+ depth++;
1078
+ }
1079
+ if (depth > 2) {
1080
+ return true;
1081
+ }
1082
+ pattern = pattern.parent;
1083
+ }
1084
+ return false;
1085
+ }
1089
1086
  getTokens() {
1090
1087
  const tokens = [];
1091
1088
  for (const pattern of this._children) {
@@ -1170,6 +1167,9 @@ class FiniteRepeat {
1170
1167
  get max() {
1171
1168
  return this._max;
1172
1169
  }
1170
+ get startedOnIndex() {
1171
+ return this._firstIndex;
1172
+ }
1173
1173
  constructor(name, pattern, options = {}) {
1174
1174
  this.shouldCompactAst = false;
1175
1175
  this._id = `finite-repeat-${idIndex$5++}`;
@@ -1181,6 +1181,7 @@ class FiniteRepeat {
1181
1181
  this._min = options.min != null ? Math.max(options.min, 1) : 1;
1182
1182
  this._max = Math.max(this.min, options.max || this.min);
1183
1183
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1184
+ this._firstIndex = 0;
1184
1185
  for (let i = 0; i < this._max; i++) {
1185
1186
  const child = pattern.clone();
1186
1187
  child.parent = this;
@@ -1193,6 +1194,7 @@ class FiniteRepeat {
1193
1194
  }
1194
1195
  }
1195
1196
  parse(cursor) {
1197
+ this._firstIndex = cursor.index;
1196
1198
  const startIndex = cursor.index;
1197
1199
  const nodes = [];
1198
1200
  const modulo = this._hasDivider ? 2 : 1;
@@ -1348,6 +1350,9 @@ class InfiniteRepeat {
1348
1350
  get min() {
1349
1351
  return this._min;
1350
1352
  }
1353
+ get startedOnIndex() {
1354
+ return this._firstIndex;
1355
+ }
1351
1356
  constructor(name, pattern, options = {}) {
1352
1357
  this.shouldCompactAst = false;
1353
1358
  const min = options.min != null ? Math.max(options.min, 1) : 1;
@@ -1623,6 +1628,9 @@ class Repeat {
1623
1628
  get max() {
1624
1629
  return this._options.max;
1625
1630
  }
1631
+ get startedOnIndex() {
1632
+ return this._repeatPattern.startedOnIndex;
1633
+ }
1626
1634
  constructor(name, pattern, options = {}) {
1627
1635
  this._id = `repeat-${idIndex$3++}`;
1628
1636
  this._pattern = pattern;
@@ -1705,7 +1713,6 @@ function filterOutNull(nodes) {
1705
1713
  return filteredNodes;
1706
1714
  }
1707
1715
 
1708
- const depthCache$1 = new DepthCache();
1709
1716
  let idIndex$2 = 0;
1710
1717
  class Sequence {
1711
1718
  get id() {
@@ -1726,6 +1733,9 @@ class Sequence {
1726
1733
  get children() {
1727
1734
  return this._children;
1728
1735
  }
1736
+ get startedOnIndex() {
1737
+ return this._firstIndex;
1738
+ }
1729
1739
  constructor(name, sequence) {
1730
1740
  this.shouldCompactAst = false;
1731
1741
  if (sequence.length === 0) {
@@ -1761,12 +1771,9 @@ class Sequence {
1761
1771
  };
1762
1772
  }
1763
1773
  parse(cursor) {
1764
- // This is a cache to help with speed
1765
1774
  this._firstIndex = cursor.index;
1766
- depthCache$1.incrementDepth(this._id, this._firstIndex);
1767
1775
  this._nodes = [];
1768
1776
  const passed = this.tryToParse(cursor);
1769
- depthCache$1.decrementDepth(this._id, this._firstIndex);
1770
1777
  if (passed) {
1771
1778
  const node = this.createNode(cursor);
1772
1779
  if (node !== null) {
@@ -1780,7 +1787,7 @@ class Sequence {
1780
1787
  return null;
1781
1788
  }
1782
1789
  tryToParse(cursor) {
1783
- if (depthCache$1.getDepth(this._id, this._firstIndex) > 1) {
1790
+ if (this._isBeyondRecursiveAllowance()) {
1784
1791
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1785
1792
  return false;
1786
1793
  }
@@ -1847,6 +1854,20 @@ class Sequence {
1847
1854
  }
1848
1855
  return nodes[nodes.length - 1];
1849
1856
  }
1857
+ _isBeyondRecursiveAllowance() {
1858
+ let depth = 0;
1859
+ let pattern = this;
1860
+ while (pattern != null) {
1861
+ if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1862
+ depth++;
1863
+ }
1864
+ if (depth > 1) {
1865
+ return true;
1866
+ }
1867
+ pattern = pattern.parent;
1868
+ }
1869
+ return false;
1870
+ }
1850
1871
  areRemainingPatternsOptional(fromIndex) {
1851
1872
  const startOnIndex = fromIndex + 1;
1852
1873
  const length = this._children.length;
@@ -2006,6 +2027,9 @@ class Optional {
2006
2027
  get children() {
2007
2028
  return this._children;
2008
2029
  }
2030
+ get startedOnIndex() {
2031
+ return this._children[0].startedOnIndex;
2032
+ }
2009
2033
  constructor(name, pattern) {
2010
2034
  this.shouldCompactAst = false;
2011
2035
  this._id = `optional-${idIndex$1++}`;
@@ -2327,6 +2351,9 @@ class Not {
2327
2351
  get children() {
2328
2352
  return this._children;
2329
2353
  }
2354
+ get startedOnIndex() {
2355
+ return this.children[0].startedOnIndex;
2356
+ }
2330
2357
  constructor(name, pattern) {
2331
2358
  this.shouldCompactAst = false;
2332
2359
  this._id = `not-${idIndex++}`;
@@ -2666,6 +2693,9 @@ class Context {
2666
2693
  get children() {
2667
2694
  return this._children;
2668
2695
  }
2696
+ get startedOnIndex() {
2697
+ return this.children[0].startedOnIndex;
2698
+ }
2669
2699
  getPatternWithinContext(name) {
2670
2700
  return this._patterns[name] || null;
2671
2701
  }
@@ -2739,7 +2769,6 @@ class Context {
2739
2769
  }
2740
2770
 
2741
2771
  let indexId = 0;
2742
- const depthCache = new DepthCache();
2743
2772
  function createNode(name, children) {
2744
2773
  return new Node("expression", name, 0, 0, children, "");
2745
2774
  }
@@ -2776,6 +2805,9 @@ class ExpressionPattern {
2776
2805
  get recursivePatterns() {
2777
2806
  return this._recursivePatterns;
2778
2807
  }
2808
+ get startedOnIndex() {
2809
+ return this._firstIndex;
2810
+ }
2779
2811
  constructor(name, patterns) {
2780
2812
  this.shouldCompactAst = false;
2781
2813
  if (patterns.length === 0) {
@@ -2893,11 +2925,8 @@ class ExpressionPattern {
2893
2925
  lastChild.name === this.name;
2894
2926
  }
2895
2927
  parse(cursor) {
2896
- this._firstIndex = cursor.index;
2897
- depthCache.incrementDepth(this._id, this._firstIndex);
2898
2928
  this._firstIndex = cursor.index;
2899
2929
  const node = this._tryToParse(cursor);
2900
- depthCache.decrementDepth(this._id, this._firstIndex);
2901
2930
  if (node != null) {
2902
2931
  cursor.moveTo(node.lastIndex);
2903
2932
  cursor.resolveError();
@@ -2927,7 +2956,7 @@ class ExpressionPattern {
2927
2956
  }
2928
2957
  }
2929
2958
  _tryToParse(cursor) {
2930
- if (depthCache.getDepth(this._id, this._firstIndex) > 2) {
2959
+ if (this._isBeyondRecursiveAllowance()) {
2931
2960
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
2932
2961
  return null;
2933
2962
  }
@@ -3092,6 +3121,20 @@ class ExpressionPattern {
3092
3121
  return root;
3093
3122
  }
3094
3123
  }
3124
+ _isBeyondRecursiveAllowance() {
3125
+ let depth = 0;
3126
+ let pattern = this;
3127
+ while (pattern != null) {
3128
+ if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
3129
+ depth++;
3130
+ }
3131
+ if (depth > 2) {
3132
+ return true;
3133
+ }
3134
+ pattern = pattern.parent;
3135
+ }
3136
+ return false;
3137
+ }
3095
3138
  test(text) {
3096
3139
  const cursor = new Cursor(text);
3097
3140
  const ast = this.parse(cursor);