clarity-pattern-parser 10.1.14 → 10.1.16

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.
@@ -931,33 +931,9 @@
931
931
  return patterns.map(p => p.clone());
932
932
  }
933
933
 
934
- class DepthCache {
935
- constructor() {
936
- this._depthMap = {};
937
- }
938
- getDepth(name, cursorIndex) {
939
- if (this._depthMap[name] == null) {
940
- this._depthMap[name] = {};
941
- }
942
- if (this._depthMap[name][cursorIndex] == null) {
943
- this._depthMap[name][cursorIndex] = 0;
944
- }
945
- return this._depthMap[name][cursorIndex];
946
- }
947
- incrementDepth(name, cursorIndex) {
948
- const depth = this.getDepth(name, cursorIndex);
949
- this._depthMap[name][cursorIndex] = depth + 1;
950
- }
951
- decrementDepth(name, cursorIndex) {
952
- const depth = this.getDepth(name, cursorIndex);
953
- this._depthMap[name][cursorIndex] = depth - 1;
954
- }
955
- }
956
-
957
934
  /*
958
935
  The following is created to reduce the overhead of recursion check.
959
936
  */
960
- const depthCache$1 = new DepthCache();
961
937
  let idIndex$6 = 0;
962
938
  class Options {
963
939
  get id() {
@@ -991,6 +967,34 @@
991
967
  this._children = children;
992
968
  this._firstIndex = 0;
993
969
  this._isGreedy = isGreedy;
970
+ this._recursiveDepth = this._calculateRecursiveDepth();
971
+ }
972
+ _calculateRecursiveDepth() {
973
+ let depth = 0;
974
+ this._children.forEach((child) => {
975
+ let hasReference = false;
976
+ let descendant = child.children[0];
977
+ while (descendant != null) {
978
+ if (descendant.type === "reference" && descendant.name === this.name) {
979
+ hasReference = true;
980
+ break;
981
+ }
982
+ if (descendant.type === "context") {
983
+ const pattern = descendant.getPatternWithinContext(this.name);
984
+ if (pattern != null) {
985
+ break;
986
+ }
987
+ }
988
+ descendant = descendant.children[0];
989
+ }
990
+ if (hasReference) {
991
+ depth++;
992
+ }
993
+ });
994
+ if (depth === 0) {
995
+ depth = this.children.length;
996
+ }
997
+ return depth;
994
998
  }
995
999
  _assignChildrenToParent(children) {
996
1000
  for (const child of children) {
@@ -1014,10 +1018,7 @@
1014
1018
  parse(cursor) {
1015
1019
  // This is a cache to help with speed
1016
1020
  this._firstIndex = cursor.index;
1017
- depthCache$1.incrementDepth(this._id, this._firstIndex);
1018
- this._firstIndex = cursor.index;
1019
1021
  const node = this._tryToParse(cursor);
1020
- depthCache$1.decrementDepth(this._id, this._firstIndex);
1021
1022
  if (node != null) {
1022
1023
  cursor.moveTo(node.lastIndex);
1023
1024
  cursor.resolveError();
@@ -1027,12 +1028,12 @@
1027
1028
  return null;
1028
1029
  }
1029
1030
  _tryToParse(cursor) {
1030
- if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
1031
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1032
- return null;
1031
+ let children = this.children;
1032
+ if (this._isBeyondRecursiveDepth()) {
1033
+ children = children.slice().reverse();
1033
1034
  }
1034
1035
  const results = [];
1035
- for (const pattern of this._children) {
1036
+ for (const pattern of children) {
1036
1037
  cursor.moveTo(this._firstIndex);
1037
1038
  let result = null;
1038
1039
  result = pattern.parse(cursor);
@@ -1048,6 +1049,20 @@
1048
1049
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1049
1050
  return nonNullResults[0] || null;
1050
1051
  }
1052
+ _isBeyondRecursiveDepth() {
1053
+ let depth = 0;
1054
+ let pattern = this;
1055
+ while (pattern != null) {
1056
+ if (pattern.id === this.id) {
1057
+ depth++;
1058
+ }
1059
+ if (depth > this._recursiveDepth) {
1060
+ return true;
1061
+ }
1062
+ pattern = pattern.parent;
1063
+ }
1064
+ return false;
1065
+ }
1051
1066
  getTokens() {
1052
1067
  const tokens = [];
1053
1068
  for (const child of this._children) {
@@ -1640,7 +1655,6 @@
1640
1655
  return filteredNodes;
1641
1656
  }
1642
1657
 
1643
- const depthCache = new DepthCache();
1644
1658
  let idIndex$2 = 0;
1645
1659
  class Sequence {
1646
1660
  get id() {
@@ -1697,10 +1711,8 @@
1697
1711
  parse(cursor) {
1698
1712
  // This is a cache to help with speed
1699
1713
  this._firstIndex = cursor.index;
1700
- depthCache.incrementDepth(this._id, this._firstIndex);
1701
1714
  this._nodes = [];
1702
1715
  const passed = this.tryToParse(cursor);
1703
- depthCache.decrementDepth(this._id, this._firstIndex);
1704
1716
  if (passed) {
1705
1717
  const node = this.createNode(cursor);
1706
1718
  if (node !== null) {
@@ -1711,7 +1723,7 @@
1711
1723
  return null;
1712
1724
  }
1713
1725
  tryToParse(cursor) {
1714
- if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1726
+ if (this._isBeyondRecursiveDepth()) {
1715
1727
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1716
1728
  return false;
1717
1729
  }
@@ -1771,6 +1783,20 @@
1771
1783
  }
1772
1784
  return passed;
1773
1785
  }
1786
+ _isBeyondRecursiveDepth() {
1787
+ let depth = 0;
1788
+ let pattern = this;
1789
+ while (pattern != null) {
1790
+ if (pattern.id === this.id && this._firstIndex === pattern._firstIndex) {
1791
+ depth++;
1792
+ }
1793
+ if (depth > 1) {
1794
+ return true;
1795
+ }
1796
+ pattern = pattern.parent;
1797
+ }
1798
+ return false;
1799
+ }
1774
1800
  getLastValidNode() {
1775
1801
  const nodes = filterOutNull(this._nodes);
1776
1802
  if (nodes.length === 0) {
@@ -2387,15 +2413,16 @@
2387
2413
  }
2388
2414
  _getAllOptions() {
2389
2415
  const errorMatches = this._getOptionsFromErrors();
2390
- const leafMatches = this._cursor.leafMatches.map((m) => this._createSuggestionsFromMatch(m)).flat();
2391
- const finalResults = [];
2392
- [...leafMatches, ...errorMatches].forEach(m => {
2393
- const index = finalResults.findIndex(f => m.text === f.text);
2416
+ const validLeafMatches = this._cursor.leafMatches.filter(v => { var _a; return ((_a = v.node) === null || _a === void 0 ? void 0 : _a.lastIndex) === this._cursor.getLastIndex(); });
2417
+ const leafMatchSuggestions = validLeafMatches.map((m) => this._createSuggestionsFromMatch(m)).flat();
2418
+ const uniqueResults = [];
2419
+ [...leafMatchSuggestions, ...errorMatches].forEach(m => {
2420
+ const index = uniqueResults.findIndex(f => m.text === f.text);
2394
2421
  if (index === -1) {
2395
- finalResults.push(m);
2422
+ uniqueResults.push(m);
2396
2423
  }
2397
2424
  });
2398
- return finalResults;
2425
+ return uniqueResults;
2399
2426
  }
2400
2427
  _getOptionsFromErrors() {
2401
2428
  // These errored because the length of the string.
@@ -2404,8 +2431,8 @@
2404
2431
  const tokens = this._getTokensForPattern(e.pattern);
2405
2432
  const adjustedTokens = tokens.map(t => t.slice(e.endIndex - e.startIndex));
2406
2433
  return this._createSuggestions(e.endIndex, adjustedTokens);
2407
- });
2408
- return suggestions.flat();
2434
+ }).flat();
2435
+ return suggestions;
2409
2436
  }
2410
2437
  _createSuggestionsFromRoot() {
2411
2438
  const suggestions = [];