clarity-pattern-parser 10.1.15 → 10.1.17

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,9 +931,33 @@
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
+
934
957
  /*
935
958
  The following is created to reduce the overhead of recursion check.
936
959
  */
960
+ const depthCache$1 = new DepthCache();
937
961
  let idIndex$6 = 0;
938
962
  class Options {
939
963
  get id() {
@@ -990,7 +1014,10 @@
990
1014
  parse(cursor) {
991
1015
  // This is a cache to help with speed
992
1016
  this._firstIndex = cursor.index;
1017
+ depthCache$1.incrementDepth(this._id, this._firstIndex);
1018
+ this._firstIndex = cursor.index;
993
1019
  const node = this._tryToParse(cursor);
1020
+ depthCache$1.decrementDepth(this._id, this._firstIndex);
994
1021
  if (node != null) {
995
1022
  cursor.moveTo(node.lastIndex);
996
1023
  cursor.resolveError();
@@ -1000,12 +1027,12 @@
1000
1027
  return null;
1001
1028
  }
1002
1029
  _tryToParse(cursor) {
1003
- let children = this.children;
1004
- if (this._isBeyondRecursiveDepth()) {
1005
- children = children.slice().reverse();
1030
+ if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
1031
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1032
+ return null;
1006
1033
  }
1007
1034
  const results = [];
1008
- for (const pattern of children) {
1035
+ for (const pattern of this._children) {
1009
1036
  cursor.moveTo(this._firstIndex);
1010
1037
  let result = null;
1011
1038
  result = pattern.parse(cursor);
@@ -1021,20 +1048,6 @@
1021
1048
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1022
1049
  return nonNullResults[0] || null;
1023
1050
  }
1024
- _isBeyondRecursiveDepth() {
1025
- let depth = 0;
1026
- let pattern = this;
1027
- while (pattern != null) {
1028
- if (pattern.id === this.id) {
1029
- depth++;
1030
- }
1031
- if (depth >= this.children.length) {
1032
- return true;
1033
- }
1034
- pattern = pattern.parent;
1035
- }
1036
- return false;
1037
- }
1038
1051
  getTokens() {
1039
1052
  const tokens = [];
1040
1053
  for (const child of this._children) {
@@ -1627,6 +1640,7 @@
1627
1640
  return filteredNodes;
1628
1641
  }
1629
1642
 
1643
+ const depthCache = new DepthCache();
1630
1644
  let idIndex$2 = 0;
1631
1645
  class Sequence {
1632
1646
  get id() {
@@ -1683,8 +1697,10 @@
1683
1697
  parse(cursor) {
1684
1698
  // This is a cache to help with speed
1685
1699
  this._firstIndex = cursor.index;
1700
+ depthCache.incrementDepth(this._id, this._firstIndex);
1686
1701
  this._nodes = [];
1687
1702
  const passed = this.tryToParse(cursor);
1703
+ depthCache.decrementDepth(this._id, this._firstIndex);
1688
1704
  if (passed) {
1689
1705
  const node = this.createNode(cursor);
1690
1706
  if (node !== null) {
@@ -1695,7 +1711,7 @@
1695
1711
  return null;
1696
1712
  }
1697
1713
  tryToParse(cursor) {
1698
- if (this._isBeyondRecursiveDepth()) {
1714
+ if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
1699
1715
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1700
1716
  return false;
1701
1717
  }
@@ -1755,20 +1771,6 @@
1755
1771
  }
1756
1772
  return passed;
1757
1773
  }
1758
- _isBeyondRecursiveDepth() {
1759
- let depth = 0;
1760
- let pattern = this;
1761
- while (pattern != null) {
1762
- if (pattern.id === this.id && this._firstIndex === pattern._firstIndex) {
1763
- depth++;
1764
- }
1765
- if (depth > 1) {
1766
- return true;
1767
- }
1768
- pattern = pattern.parent;
1769
- }
1770
- return false;
1771
- }
1772
1774
  getLastValidNode() {
1773
1775
  const nodes = filterOutNull(this._nodes);
1774
1776
  if (nodes.length === 0) {