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