clarity-pattern-parser 10.0.6 → 10.0.7
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.browser.js +41 -44
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +41 -44
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +41 -44
- package/dist/index.js.map +1 -1
- package/dist/patterns/DepthCache.d.ts +6 -0
- package/dist/patterns/Options.d.ts +0 -1
- package/dist/patterns/Sequence.d.ts +0 -1
- package/package.json +1 -1
- package/src/patterns/DepthCache.ts +26 -0
- package/src/patterns/Options.ts +14 -29
- package/src/patterns/Sequence.ts +10 -32
package/dist/index.js
CHANGED
|
@@ -880,6 +880,33 @@ function clonePatterns(patterns) {
|
|
|
880
880
|
return patterns.map(p => p.clone());
|
|
881
881
|
}
|
|
882
882
|
|
|
883
|
+
class DepthCache {
|
|
884
|
+
constructor() {
|
|
885
|
+
this._depthMap = {};
|
|
886
|
+
}
|
|
887
|
+
getDepth(name, cursorIndex) {
|
|
888
|
+
if (this._depthMap[name] == null) {
|
|
889
|
+
this._depthMap[name] = {};
|
|
890
|
+
}
|
|
891
|
+
if (this._depthMap[name][cursorIndex] == null) {
|
|
892
|
+
this._depthMap[name][cursorIndex] = 0;
|
|
893
|
+
}
|
|
894
|
+
return this._depthMap[name][cursorIndex];
|
|
895
|
+
}
|
|
896
|
+
incrementDepth(name, cursorIndex) {
|
|
897
|
+
const depth = this.getDepth(name, cursorIndex);
|
|
898
|
+
this._depthMap[name][cursorIndex] = depth + 1;
|
|
899
|
+
}
|
|
900
|
+
decrementDepth(name, cursorIndex) {
|
|
901
|
+
const depth = this.getDepth(name, cursorIndex);
|
|
902
|
+
this._depthMap[name][cursorIndex] = depth - 1;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/*
|
|
907
|
+
The following is created to reduce the overhead of recursion check.
|
|
908
|
+
*/
|
|
909
|
+
const depthCache$1 = new DepthCache();
|
|
883
910
|
let idIndex$6 = 0;
|
|
884
911
|
class Options {
|
|
885
912
|
get id() {
|
|
@@ -934,8 +961,12 @@ class Options {
|
|
|
934
961
|
};
|
|
935
962
|
}
|
|
936
963
|
parse(cursor) {
|
|
964
|
+
// This is a cache to help with speed
|
|
965
|
+
this._firstIndex = cursor.index;
|
|
966
|
+
depthCache$1.incrementDepth(this._id, this._firstIndex);
|
|
937
967
|
this._firstIndex = cursor.index;
|
|
938
968
|
const node = this._tryToParse(cursor);
|
|
969
|
+
depthCache$1.decrementDepth(this._id, this._firstIndex);
|
|
939
970
|
if (node != null) {
|
|
940
971
|
cursor.moveTo(node.lastIndex);
|
|
941
972
|
cursor.resolveError();
|
|
@@ -945,8 +976,8 @@ class Options {
|
|
|
945
976
|
return null;
|
|
946
977
|
}
|
|
947
978
|
_tryToParse(cursor) {
|
|
948
|
-
if (this.
|
|
949
|
-
cursor.recordErrorAt(
|
|
979
|
+
if (depthCache$1.getDepth(this._id, this._firstIndex) > 2) {
|
|
980
|
+
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
950
981
|
return null;
|
|
951
982
|
}
|
|
952
983
|
const results = [];
|
|
@@ -966,25 +997,6 @@ class Options {
|
|
|
966
997
|
nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
|
|
967
998
|
return nonNullResults[0] || null;
|
|
968
999
|
}
|
|
969
|
-
_isBeyondRecursiveLimit() {
|
|
970
|
-
let pattern = this;
|
|
971
|
-
const matches = [];
|
|
972
|
-
while (pattern.parent != null) {
|
|
973
|
-
if (pattern.type !== "options") {
|
|
974
|
-
pattern = pattern.parent;
|
|
975
|
-
continue;
|
|
976
|
-
}
|
|
977
|
-
const optionsPattern = pattern;
|
|
978
|
-
if (pattern.id === this.id && optionsPattern._firstIndex === this._firstIndex) {
|
|
979
|
-
matches.push(pattern);
|
|
980
|
-
if (matches.length > 2) {
|
|
981
|
-
return true;
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
pattern = pattern.parent;
|
|
985
|
-
}
|
|
986
|
-
return false;
|
|
987
|
-
}
|
|
988
1000
|
getTokens() {
|
|
989
1001
|
const tokens = [];
|
|
990
1002
|
for (const child of this._children) {
|
|
@@ -1577,6 +1589,7 @@ function filterOutNull(nodes) {
|
|
|
1577
1589
|
return filteredNodes;
|
|
1578
1590
|
}
|
|
1579
1591
|
|
|
1592
|
+
const depthCache = new DepthCache();
|
|
1580
1593
|
let idIndex$2 = 0;
|
|
1581
1594
|
class Sequence {
|
|
1582
1595
|
get id() {
|
|
@@ -1631,13 +1644,12 @@ class Sequence {
|
|
|
1631
1644
|
};
|
|
1632
1645
|
}
|
|
1633
1646
|
parse(cursor) {
|
|
1647
|
+
// This is a cache to help with speed
|
|
1634
1648
|
this._firstIndex = cursor.index;
|
|
1649
|
+
depthCache.incrementDepth(this._id, this._firstIndex);
|
|
1635
1650
|
this._nodes = [];
|
|
1636
|
-
if (this._isBeyondRecursiveLimit()) {
|
|
1637
|
-
cursor.recordErrorAt(cursor.index, cursor.index, this);
|
|
1638
|
-
return null;
|
|
1639
|
-
}
|
|
1640
1651
|
const passed = this.tryToParse(cursor);
|
|
1652
|
+
depthCache.decrementDepth(this._id, this._firstIndex);
|
|
1641
1653
|
if (passed) {
|
|
1642
1654
|
const node = this.createNode(cursor);
|
|
1643
1655
|
if (node !== null) {
|
|
@@ -1648,6 +1660,10 @@ class Sequence {
|
|
|
1648
1660
|
return null;
|
|
1649
1661
|
}
|
|
1650
1662
|
tryToParse(cursor) {
|
|
1663
|
+
if (depthCache.getDepth(this._id, this._firstIndex) > 1) {
|
|
1664
|
+
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
1665
|
+
return false;
|
|
1666
|
+
}
|
|
1651
1667
|
let passed = false;
|
|
1652
1668
|
for (let i = 0; i < this._children.length; i++) {
|
|
1653
1669
|
const runningCursorIndex = cursor.index;
|
|
@@ -1704,25 +1720,6 @@ class Sequence {
|
|
|
1704
1720
|
}
|
|
1705
1721
|
return passed;
|
|
1706
1722
|
}
|
|
1707
|
-
_isBeyondRecursiveLimit() {
|
|
1708
|
-
let pattern = this;
|
|
1709
|
-
const matches = [];
|
|
1710
|
-
while (pattern.parent != null) {
|
|
1711
|
-
if (pattern.type !== "sequence") {
|
|
1712
|
-
pattern = pattern.parent;
|
|
1713
|
-
continue;
|
|
1714
|
-
}
|
|
1715
|
-
const sequencePattern = pattern;
|
|
1716
|
-
if (pattern.id === this.id && sequencePattern._firstIndex === this._firstIndex) {
|
|
1717
|
-
matches.push(pattern);
|
|
1718
|
-
if (matches.length > 1) {
|
|
1719
|
-
return true;
|
|
1720
|
-
}
|
|
1721
|
-
}
|
|
1722
|
-
pattern = pattern.parent;
|
|
1723
|
-
}
|
|
1724
|
-
return false;
|
|
1725
|
-
}
|
|
1726
1723
|
getLastValidNode() {
|
|
1727
1724
|
const nodes = filterOutNull(this._nodes);
|
|
1728
1725
|
if (nodes.length === 0) {
|