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.
@@ -534,6 +534,9 @@
534
534
  get children() {
535
535
  return [];
536
536
  }
537
+ get startedOnIndex() {
538
+ return this._firstIndex;
539
+ }
537
540
  constructor(name, value) {
538
541
  this.shouldCompactAst = false;
539
542
  if (value.length === 0) {
@@ -663,10 +666,13 @@
663
666
  get children() {
664
667
  return [];
665
668
  }
669
+ get startedOnIndex() {
670
+ return this._firstIndex;
671
+ }
666
672
  constructor(name, regex) {
667
673
  this._node = null;
668
674
  this._cursor = null;
669
- this._firstIndex = -1;
675
+ this._firstIndex = 0;
670
676
  this._substring = "";
671
677
  this._tokens = [];
672
678
  this.shouldCompactAst = false;
@@ -819,6 +825,9 @@
819
825
  get children() {
820
826
  return this._children;
821
827
  }
828
+ get startedOnIndex() {
829
+ return this._firstIndex;
830
+ }
822
831
  constructor(name) {
823
832
  this.shouldCompactAst = false;
824
833
  this._id = `reference-${idIndex$7++}`;
@@ -828,6 +837,7 @@
828
837
  this._pattern = null;
829
838
  this._cachedPattern = null;
830
839
  this._children = [];
840
+ this._firstIndex = 0;
831
841
  }
832
842
  test(text) {
833
843
  const cursor = new Cursor(text);
@@ -844,6 +854,7 @@
844
854
  };
845
855
  }
846
856
  parse(cursor) {
857
+ this._firstIndex = cursor.index;
847
858
  return this.getReferencePatternSafely().parse(cursor);
848
859
  }
849
860
  getReferencePatternSafely() {
@@ -957,29 +968,6 @@
957
968
  return patterns.map(p => p.clone());
958
969
  }
959
970
 
960
- class DepthCache {
961
- constructor() {
962
- this._depthMap = {};
963
- }
964
- getDepth(name, cursorIndex) {
965
- if (this._depthMap[name] == null) {
966
- this._depthMap[name] = {};
967
- }
968
- if (this._depthMap[name][cursorIndex] == null) {
969
- this._depthMap[name][cursorIndex] = 0;
970
- }
971
- return this._depthMap[name][cursorIndex];
972
- }
973
- incrementDepth(name, cursorIndex) {
974
- const depth = this.getDepth(name, cursorIndex);
975
- this._depthMap[name][cursorIndex] = depth + 1;
976
- }
977
- decrementDepth(name, cursorIndex) {
978
- const depth = this.getDepth(name, cursorIndex);
979
- this._depthMap[name][cursorIndex] = depth - 1;
980
- }
981
- }
982
-
983
971
  function isRecursivePattern(pattern) {
984
972
  let onPattern = pattern.parent;
985
973
  let depth = 0;
@@ -995,10 +983,6 @@
995
983
  return false;
996
984
  }
997
985
 
998
- /*
999
- The following is created to reduce the overhead of recursion check.
1000
- */
1001
- const depthCache$2 = new DepthCache();
1002
986
  let idIndex$6 = 0;
1003
987
  class Options {
1004
988
  get id() {
@@ -1019,6 +1003,9 @@
1019
1003
  get children() {
1020
1004
  return this._children;
1021
1005
  }
1006
+ get startedOnIndex() {
1007
+ return this._firstIndex;
1008
+ }
1022
1009
  constructor(name, options, isGreedy = false) {
1023
1010
  this.shouldCompactAst = false;
1024
1011
  if (options.length === 0) {
@@ -1054,12 +1041,8 @@
1054
1041
  };
1055
1042
  }
1056
1043
  parse(cursor) {
1057
- // This is a cache to help with speed
1058
- this._firstIndex = cursor.index;
1059
- depthCache$2.incrementDepth(this._id, this._firstIndex);
1060
1044
  this._firstIndex = cursor.index;
1061
1045
  const node = this._tryToParse(cursor);
1062
- depthCache$2.decrementDepth(this._id, this._firstIndex);
1063
1046
  if (node != null) {
1064
1047
  cursor.moveTo(node.lastIndex);
1065
1048
  cursor.resolveError();
@@ -1072,7 +1055,7 @@
1072
1055
  return null;
1073
1056
  }
1074
1057
  _tryToParse(cursor) {
1075
- if (depthCache$2.getDepth(this._id, this._firstIndex) > 2) {
1058
+ if (this._isBeyondRecursiveAllowance()) {
1076
1059
  return null;
1077
1060
  }
1078
1061
  const results = [];
@@ -1092,6 +1075,20 @@
1092
1075
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1093
1076
  return nonNullResults[0] || null;
1094
1077
  }
1078
+ _isBeyondRecursiveAllowance() {
1079
+ let depth = 0;
1080
+ let pattern = this;
1081
+ while (pattern != null) {
1082
+ if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1083
+ depth++;
1084
+ }
1085
+ if (depth > 2) {
1086
+ return true;
1087
+ }
1088
+ pattern = pattern.parent;
1089
+ }
1090
+ return false;
1091
+ }
1095
1092
  getTokens() {
1096
1093
  const tokens = [];
1097
1094
  for (const pattern of this._children) {
@@ -1176,6 +1173,9 @@
1176
1173
  get max() {
1177
1174
  return this._max;
1178
1175
  }
1176
+ get startedOnIndex() {
1177
+ return this._firstIndex;
1178
+ }
1179
1179
  constructor(name, pattern, options = {}) {
1180
1180
  this.shouldCompactAst = false;
1181
1181
  this._id = `finite-repeat-${idIndex$5++}`;
@@ -1187,6 +1187,7 @@
1187
1187
  this._min = options.min != null ? Math.max(options.min, 1) : 1;
1188
1188
  this._max = Math.max(this.min, options.max || this.min);
1189
1189
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1190
+ this._firstIndex = 0;
1190
1191
  for (let i = 0; i < this._max; i++) {
1191
1192
  const child = pattern.clone();
1192
1193
  child.parent = this;
@@ -1199,6 +1200,7 @@
1199
1200
  }
1200
1201
  }
1201
1202
  parse(cursor) {
1203
+ this._firstIndex = cursor.index;
1202
1204
  const startIndex = cursor.index;
1203
1205
  const nodes = [];
1204
1206
  const modulo = this._hasDivider ? 2 : 1;
@@ -1354,6 +1356,9 @@
1354
1356
  get min() {
1355
1357
  return this._min;
1356
1358
  }
1359
+ get startedOnIndex() {
1360
+ return this._firstIndex;
1361
+ }
1357
1362
  constructor(name, pattern, options = {}) {
1358
1363
  this.shouldCompactAst = false;
1359
1364
  const min = options.min != null ? Math.max(options.min, 1) : 1;
@@ -1629,6 +1634,9 @@
1629
1634
  get max() {
1630
1635
  return this._options.max;
1631
1636
  }
1637
+ get startedOnIndex() {
1638
+ return this._repeatPattern.startedOnIndex;
1639
+ }
1632
1640
  constructor(name, pattern, options = {}) {
1633
1641
  this._id = `repeat-${idIndex$3++}`;
1634
1642
  this._pattern = pattern;
@@ -1711,7 +1719,6 @@
1711
1719
  return filteredNodes;
1712
1720
  }
1713
1721
 
1714
- const depthCache$1 = new DepthCache();
1715
1722
  let idIndex$2 = 0;
1716
1723
  class Sequence {
1717
1724
  get id() {
@@ -1732,6 +1739,9 @@
1732
1739
  get children() {
1733
1740
  return this._children;
1734
1741
  }
1742
+ get startedOnIndex() {
1743
+ return this._firstIndex;
1744
+ }
1735
1745
  constructor(name, sequence) {
1736
1746
  this.shouldCompactAst = false;
1737
1747
  if (sequence.length === 0) {
@@ -1767,12 +1777,9 @@
1767
1777
  };
1768
1778
  }
1769
1779
  parse(cursor) {
1770
- // This is a cache to help with speed
1771
1780
  this._firstIndex = cursor.index;
1772
- depthCache$1.incrementDepth(this._id, this._firstIndex);
1773
1781
  this._nodes = [];
1774
1782
  const passed = this.tryToParse(cursor);
1775
- depthCache$1.decrementDepth(this._id, this._firstIndex);
1776
1783
  if (passed) {
1777
1784
  const node = this.createNode(cursor);
1778
1785
  if (node !== null) {
@@ -1786,7 +1793,7 @@
1786
1793
  return null;
1787
1794
  }
1788
1795
  tryToParse(cursor) {
1789
- if (depthCache$1.getDepth(this._id, this._firstIndex) > 1) {
1796
+ if (this._isBeyondRecursiveAllowance()) {
1790
1797
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1791
1798
  return false;
1792
1799
  }
@@ -1853,6 +1860,20 @@
1853
1860
  }
1854
1861
  return nodes[nodes.length - 1];
1855
1862
  }
1863
+ _isBeyondRecursiveAllowance() {
1864
+ let depth = 0;
1865
+ let pattern = this;
1866
+ while (pattern != null) {
1867
+ if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1868
+ depth++;
1869
+ }
1870
+ if (depth > 1) {
1871
+ return true;
1872
+ }
1873
+ pattern = pattern.parent;
1874
+ }
1875
+ return false;
1876
+ }
1856
1877
  areRemainingPatternsOptional(fromIndex) {
1857
1878
  const startOnIndex = fromIndex + 1;
1858
1879
  const length = this._children.length;
@@ -2012,6 +2033,9 @@
2012
2033
  get children() {
2013
2034
  return this._children;
2014
2035
  }
2036
+ get startedOnIndex() {
2037
+ return this._children[0].startedOnIndex;
2038
+ }
2015
2039
  constructor(name, pattern) {
2016
2040
  this.shouldCompactAst = false;
2017
2041
  this._id = `optional-${idIndex$1++}`;
@@ -2333,6 +2357,9 @@
2333
2357
  get children() {
2334
2358
  return this._children;
2335
2359
  }
2360
+ get startedOnIndex() {
2361
+ return this.children[0].startedOnIndex;
2362
+ }
2336
2363
  constructor(name, pattern) {
2337
2364
  this.shouldCompactAst = false;
2338
2365
  this._id = `not-${idIndex++}`;
@@ -2672,6 +2699,9 @@
2672
2699
  get children() {
2673
2700
  return this._children;
2674
2701
  }
2702
+ get startedOnIndex() {
2703
+ return this.children[0].startedOnIndex;
2704
+ }
2675
2705
  getPatternWithinContext(name) {
2676
2706
  return this._patterns[name] || null;
2677
2707
  }
@@ -2745,7 +2775,6 @@
2745
2775
  }
2746
2776
 
2747
2777
  let indexId = 0;
2748
- const depthCache = new DepthCache();
2749
2778
  function createNode(name, children) {
2750
2779
  return new Node("expression", name, 0, 0, children, "");
2751
2780
  }
@@ -2782,6 +2811,9 @@
2782
2811
  get recursivePatterns() {
2783
2812
  return this._recursivePatterns;
2784
2813
  }
2814
+ get startedOnIndex() {
2815
+ return this._firstIndex;
2816
+ }
2785
2817
  constructor(name, patterns) {
2786
2818
  this.shouldCompactAst = false;
2787
2819
  if (patterns.length === 0) {
@@ -2899,11 +2931,8 @@
2899
2931
  lastChild.name === this.name;
2900
2932
  }
2901
2933
  parse(cursor) {
2902
- this._firstIndex = cursor.index;
2903
- depthCache.incrementDepth(this._id, this._firstIndex);
2904
2934
  this._firstIndex = cursor.index;
2905
2935
  const node = this._tryToParse(cursor);
2906
- depthCache.decrementDepth(this._id, this._firstIndex);
2907
2936
  if (node != null) {
2908
2937
  cursor.moveTo(node.lastIndex);
2909
2938
  cursor.resolveError();
@@ -2933,7 +2962,7 @@
2933
2962
  }
2934
2963
  }
2935
2964
  _tryToParse(cursor) {
2936
- if (depthCache.getDepth(this._id, this._firstIndex) > 2) {
2965
+ if (this._isBeyondRecursiveAllowance()) {
2937
2966
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
2938
2967
  return null;
2939
2968
  }
@@ -3098,6 +3127,20 @@
3098
3127
  return root;
3099
3128
  }
3100
3129
  }
3130
+ _isBeyondRecursiveAllowance() {
3131
+ let depth = 0;
3132
+ let pattern = this;
3133
+ while (pattern != null) {
3134
+ if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
3135
+ depth++;
3136
+ }
3137
+ if (depth > 2) {
3138
+ return true;
3139
+ }
3140
+ pattern = pattern.parent;
3141
+ }
3142
+ return false;
3143
+ }
3101
3144
  test(text) {
3102
3145
  const cursor = new Cursor(text);
3103
3146
  const ast = this.parse(cursor);