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.
- package/dist/index.browser.js +86 -43
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +86 -43
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +86 -43
- package/dist/index.js.map +1 -1
- package/dist/patterns/Context.d.ts +1 -0
- package/dist/patterns/ExpressionPattern.d.ts +2 -0
- package/dist/patterns/FiniteRepeat.d.ts +2 -0
- package/dist/patterns/InfiniteRepeat.d.ts +1 -0
- package/dist/patterns/Literal.d.ts +1 -0
- package/dist/patterns/Not.d.ts +1 -0
- package/dist/patterns/Optional.d.ts +1 -0
- package/dist/patterns/Options.d.ts +2 -0
- package/dist/patterns/Pattern.d.ts +1 -0
- package/dist/patterns/Reference.d.ts +2 -0
- package/dist/patterns/Regex.d.ts +1 -0
- package/dist/patterns/Repeat.d.ts +1 -0
- package/dist/patterns/Sequence.d.ts +2 -0
- package/package.json +1 -1
- package/src/patterns/Context.ts +4 -0
- package/src/patterns/ExpressionPattern.ts +23 -9
- package/src/patterns/FiniteRepeat.ts +8 -0
- package/src/patterns/InfiniteRepeat.ts +4 -0
- package/src/patterns/Literal.ts +4 -0
- package/src/patterns/Not.ts +4 -0
- package/src/patterns/Optional.ts +4 -0
- package/src/patterns/Options.ts +23 -13
- package/src/patterns/Pattern.ts +1 -0
- package/src/patterns/Reference.ts +7 -0
- package/src/patterns/Regex.ts +6 -2
- package/src/patterns/Repeat.ts +4 -0
- package/src/patterns/RightAssociatedPattern.ts +4 -0
- package/src/patterns/Sequence.ts +23 -8
- package/src/patterns/DepthCache.ts +0 -26
package/dist/index.js
CHANGED
|
@@ -532,6 +532,9 @@ class Literal {
|
|
|
532
532
|
get children() {
|
|
533
533
|
return [];
|
|
534
534
|
}
|
|
535
|
+
get startedOnIndex() {
|
|
536
|
+
return this._firstIndex;
|
|
537
|
+
}
|
|
535
538
|
constructor(name, value) {
|
|
536
539
|
this.shouldCompactAst = false;
|
|
537
540
|
if (value.length === 0) {
|
|
@@ -661,10 +664,13 @@ class Regex {
|
|
|
661
664
|
get children() {
|
|
662
665
|
return [];
|
|
663
666
|
}
|
|
667
|
+
get startedOnIndex() {
|
|
668
|
+
return this._firstIndex;
|
|
669
|
+
}
|
|
664
670
|
constructor(name, regex) {
|
|
665
671
|
this._node = null;
|
|
666
672
|
this._cursor = null;
|
|
667
|
-
this._firstIndex =
|
|
673
|
+
this._firstIndex = 0;
|
|
668
674
|
this._substring = "";
|
|
669
675
|
this._tokens = [];
|
|
670
676
|
this.shouldCompactAst = false;
|
|
@@ -817,6 +823,9 @@ class Reference {
|
|
|
817
823
|
get children() {
|
|
818
824
|
return this._children;
|
|
819
825
|
}
|
|
826
|
+
get startedOnIndex() {
|
|
827
|
+
return this._firstIndex;
|
|
828
|
+
}
|
|
820
829
|
constructor(name) {
|
|
821
830
|
this.shouldCompactAst = false;
|
|
822
831
|
this._id = `reference-${idIndex$7++}`;
|
|
@@ -826,6 +835,7 @@ class Reference {
|
|
|
826
835
|
this._pattern = null;
|
|
827
836
|
this._cachedPattern = null;
|
|
828
837
|
this._children = [];
|
|
838
|
+
this._firstIndex = 0;
|
|
829
839
|
}
|
|
830
840
|
test(text) {
|
|
831
841
|
const cursor = new Cursor(text);
|
|
@@ -842,6 +852,7 @@ class Reference {
|
|
|
842
852
|
};
|
|
843
853
|
}
|
|
844
854
|
parse(cursor) {
|
|
855
|
+
this._firstIndex = cursor.index;
|
|
845
856
|
return this.getReferencePatternSafely().parse(cursor);
|
|
846
857
|
}
|
|
847
858
|
getReferencePatternSafely() {
|
|
@@ -955,29 +966,6 @@ function clonePatterns(patterns) {
|
|
|
955
966
|
return patterns.map(p => p.clone());
|
|
956
967
|
}
|
|
957
968
|
|
|
958
|
-
class DepthCache {
|
|
959
|
-
constructor() {
|
|
960
|
-
this._depthMap = {};
|
|
961
|
-
}
|
|
962
|
-
getDepth(name, cursorIndex) {
|
|
963
|
-
if (this._depthMap[name] == null) {
|
|
964
|
-
this._depthMap[name] = {};
|
|
965
|
-
}
|
|
966
|
-
if (this._depthMap[name][cursorIndex] == null) {
|
|
967
|
-
this._depthMap[name][cursorIndex] = 0;
|
|
968
|
-
}
|
|
969
|
-
return this._depthMap[name][cursorIndex];
|
|
970
|
-
}
|
|
971
|
-
incrementDepth(name, cursorIndex) {
|
|
972
|
-
const depth = this.getDepth(name, cursorIndex);
|
|
973
|
-
this._depthMap[name][cursorIndex] = depth + 1;
|
|
974
|
-
}
|
|
975
|
-
decrementDepth(name, cursorIndex) {
|
|
976
|
-
const depth = this.getDepth(name, cursorIndex);
|
|
977
|
-
this._depthMap[name][cursorIndex] = depth - 1;
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
|
|
981
969
|
function isRecursivePattern(pattern) {
|
|
982
970
|
let onPattern = pattern.parent;
|
|
983
971
|
let depth = 0;
|
|
@@ -993,10 +981,6 @@ function isRecursivePattern(pattern) {
|
|
|
993
981
|
return false;
|
|
994
982
|
}
|
|
995
983
|
|
|
996
|
-
/*
|
|
997
|
-
The following is created to reduce the overhead of recursion check.
|
|
998
|
-
*/
|
|
999
|
-
const depthCache$2 = new DepthCache();
|
|
1000
984
|
let idIndex$6 = 0;
|
|
1001
985
|
class Options {
|
|
1002
986
|
get id() {
|
|
@@ -1017,6 +1001,9 @@ class Options {
|
|
|
1017
1001
|
get children() {
|
|
1018
1002
|
return this._children;
|
|
1019
1003
|
}
|
|
1004
|
+
get startedOnIndex() {
|
|
1005
|
+
return this._firstIndex;
|
|
1006
|
+
}
|
|
1020
1007
|
constructor(name, options, isGreedy = false) {
|
|
1021
1008
|
this.shouldCompactAst = false;
|
|
1022
1009
|
if (options.length === 0) {
|
|
@@ -1052,12 +1039,8 @@ class Options {
|
|
|
1052
1039
|
};
|
|
1053
1040
|
}
|
|
1054
1041
|
parse(cursor) {
|
|
1055
|
-
// This is a cache to help with speed
|
|
1056
|
-
this._firstIndex = cursor.index;
|
|
1057
|
-
depthCache$2.incrementDepth(this._id, this._firstIndex);
|
|
1058
1042
|
this._firstIndex = cursor.index;
|
|
1059
1043
|
const node = this._tryToParse(cursor);
|
|
1060
|
-
depthCache$2.decrementDepth(this._id, this._firstIndex);
|
|
1061
1044
|
if (node != null) {
|
|
1062
1045
|
cursor.moveTo(node.lastIndex);
|
|
1063
1046
|
cursor.resolveError();
|
|
@@ -1070,7 +1053,7 @@ class Options {
|
|
|
1070
1053
|
return null;
|
|
1071
1054
|
}
|
|
1072
1055
|
_tryToParse(cursor) {
|
|
1073
|
-
if (
|
|
1056
|
+
if (this._isBeyondRecursiveAllowance()) {
|
|
1074
1057
|
return null;
|
|
1075
1058
|
}
|
|
1076
1059
|
const results = [];
|
|
@@ -1090,6 +1073,20 @@ class Options {
|
|
|
1090
1073
|
nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
|
|
1091
1074
|
return nonNullResults[0] || null;
|
|
1092
1075
|
}
|
|
1076
|
+
_isBeyondRecursiveAllowance() {
|
|
1077
|
+
let depth = 0;
|
|
1078
|
+
let pattern = this;
|
|
1079
|
+
while (pattern != null) {
|
|
1080
|
+
if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
|
|
1081
|
+
depth++;
|
|
1082
|
+
}
|
|
1083
|
+
if (depth > 2) {
|
|
1084
|
+
return true;
|
|
1085
|
+
}
|
|
1086
|
+
pattern = pattern.parent;
|
|
1087
|
+
}
|
|
1088
|
+
return false;
|
|
1089
|
+
}
|
|
1093
1090
|
getTokens() {
|
|
1094
1091
|
const tokens = [];
|
|
1095
1092
|
for (const pattern of this._children) {
|
|
@@ -1174,6 +1171,9 @@ class FiniteRepeat {
|
|
|
1174
1171
|
get max() {
|
|
1175
1172
|
return this._max;
|
|
1176
1173
|
}
|
|
1174
|
+
get startedOnIndex() {
|
|
1175
|
+
return this._firstIndex;
|
|
1176
|
+
}
|
|
1177
1177
|
constructor(name, pattern, options = {}) {
|
|
1178
1178
|
this.shouldCompactAst = false;
|
|
1179
1179
|
this._id = `finite-repeat-${idIndex$5++}`;
|
|
@@ -1185,6 +1185,7 @@ class FiniteRepeat {
|
|
|
1185
1185
|
this._min = options.min != null ? Math.max(options.min, 1) : 1;
|
|
1186
1186
|
this._max = Math.max(this.min, options.max || this.min);
|
|
1187
1187
|
this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
|
|
1188
|
+
this._firstIndex = 0;
|
|
1188
1189
|
for (let i = 0; i < this._max; i++) {
|
|
1189
1190
|
const child = pattern.clone();
|
|
1190
1191
|
child.parent = this;
|
|
@@ -1197,6 +1198,7 @@ class FiniteRepeat {
|
|
|
1197
1198
|
}
|
|
1198
1199
|
}
|
|
1199
1200
|
parse(cursor) {
|
|
1201
|
+
this._firstIndex = cursor.index;
|
|
1200
1202
|
const startIndex = cursor.index;
|
|
1201
1203
|
const nodes = [];
|
|
1202
1204
|
const modulo = this._hasDivider ? 2 : 1;
|
|
@@ -1352,6 +1354,9 @@ class InfiniteRepeat {
|
|
|
1352
1354
|
get min() {
|
|
1353
1355
|
return this._min;
|
|
1354
1356
|
}
|
|
1357
|
+
get startedOnIndex() {
|
|
1358
|
+
return this._firstIndex;
|
|
1359
|
+
}
|
|
1355
1360
|
constructor(name, pattern, options = {}) {
|
|
1356
1361
|
this.shouldCompactAst = false;
|
|
1357
1362
|
const min = options.min != null ? Math.max(options.min, 1) : 1;
|
|
@@ -1627,6 +1632,9 @@ class Repeat {
|
|
|
1627
1632
|
get max() {
|
|
1628
1633
|
return this._options.max;
|
|
1629
1634
|
}
|
|
1635
|
+
get startedOnIndex() {
|
|
1636
|
+
return this._repeatPattern.startedOnIndex;
|
|
1637
|
+
}
|
|
1630
1638
|
constructor(name, pattern, options = {}) {
|
|
1631
1639
|
this._id = `repeat-${idIndex$3++}`;
|
|
1632
1640
|
this._pattern = pattern;
|
|
@@ -1709,7 +1717,6 @@ function filterOutNull(nodes) {
|
|
|
1709
1717
|
return filteredNodes;
|
|
1710
1718
|
}
|
|
1711
1719
|
|
|
1712
|
-
const depthCache$1 = new DepthCache();
|
|
1713
1720
|
let idIndex$2 = 0;
|
|
1714
1721
|
class Sequence {
|
|
1715
1722
|
get id() {
|
|
@@ -1730,6 +1737,9 @@ class Sequence {
|
|
|
1730
1737
|
get children() {
|
|
1731
1738
|
return this._children;
|
|
1732
1739
|
}
|
|
1740
|
+
get startedOnIndex() {
|
|
1741
|
+
return this._firstIndex;
|
|
1742
|
+
}
|
|
1733
1743
|
constructor(name, sequence) {
|
|
1734
1744
|
this.shouldCompactAst = false;
|
|
1735
1745
|
if (sequence.length === 0) {
|
|
@@ -1765,12 +1775,9 @@ class Sequence {
|
|
|
1765
1775
|
};
|
|
1766
1776
|
}
|
|
1767
1777
|
parse(cursor) {
|
|
1768
|
-
// This is a cache to help with speed
|
|
1769
1778
|
this._firstIndex = cursor.index;
|
|
1770
|
-
depthCache$1.incrementDepth(this._id, this._firstIndex);
|
|
1771
1779
|
this._nodes = [];
|
|
1772
1780
|
const passed = this.tryToParse(cursor);
|
|
1773
|
-
depthCache$1.decrementDepth(this._id, this._firstIndex);
|
|
1774
1781
|
if (passed) {
|
|
1775
1782
|
const node = this.createNode(cursor);
|
|
1776
1783
|
if (node !== null) {
|
|
@@ -1784,7 +1791,7 @@ class Sequence {
|
|
|
1784
1791
|
return null;
|
|
1785
1792
|
}
|
|
1786
1793
|
tryToParse(cursor) {
|
|
1787
|
-
if (
|
|
1794
|
+
if (this._isBeyondRecursiveAllowance()) {
|
|
1788
1795
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
1789
1796
|
return false;
|
|
1790
1797
|
}
|
|
@@ -1851,6 +1858,20 @@ class Sequence {
|
|
|
1851
1858
|
}
|
|
1852
1859
|
return nodes[nodes.length - 1];
|
|
1853
1860
|
}
|
|
1861
|
+
_isBeyondRecursiveAllowance() {
|
|
1862
|
+
let depth = 0;
|
|
1863
|
+
let pattern = this;
|
|
1864
|
+
while (pattern != null) {
|
|
1865
|
+
if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
|
|
1866
|
+
depth++;
|
|
1867
|
+
}
|
|
1868
|
+
if (depth > 1) {
|
|
1869
|
+
return true;
|
|
1870
|
+
}
|
|
1871
|
+
pattern = pattern.parent;
|
|
1872
|
+
}
|
|
1873
|
+
return false;
|
|
1874
|
+
}
|
|
1854
1875
|
areRemainingPatternsOptional(fromIndex) {
|
|
1855
1876
|
const startOnIndex = fromIndex + 1;
|
|
1856
1877
|
const length = this._children.length;
|
|
@@ -2010,6 +2031,9 @@ class Optional {
|
|
|
2010
2031
|
get children() {
|
|
2011
2032
|
return this._children;
|
|
2012
2033
|
}
|
|
2034
|
+
get startedOnIndex() {
|
|
2035
|
+
return this._children[0].startedOnIndex;
|
|
2036
|
+
}
|
|
2013
2037
|
constructor(name, pattern) {
|
|
2014
2038
|
this.shouldCompactAst = false;
|
|
2015
2039
|
this._id = `optional-${idIndex$1++}`;
|
|
@@ -2331,6 +2355,9 @@ class Not {
|
|
|
2331
2355
|
get children() {
|
|
2332
2356
|
return this._children;
|
|
2333
2357
|
}
|
|
2358
|
+
get startedOnIndex() {
|
|
2359
|
+
return this.children[0].startedOnIndex;
|
|
2360
|
+
}
|
|
2334
2361
|
constructor(name, pattern) {
|
|
2335
2362
|
this.shouldCompactAst = false;
|
|
2336
2363
|
this._id = `not-${idIndex++}`;
|
|
@@ -2670,6 +2697,9 @@ class Context {
|
|
|
2670
2697
|
get children() {
|
|
2671
2698
|
return this._children;
|
|
2672
2699
|
}
|
|
2700
|
+
get startedOnIndex() {
|
|
2701
|
+
return this.children[0].startedOnIndex;
|
|
2702
|
+
}
|
|
2673
2703
|
getPatternWithinContext(name) {
|
|
2674
2704
|
return this._patterns[name] || null;
|
|
2675
2705
|
}
|
|
@@ -2743,7 +2773,6 @@ class Context {
|
|
|
2743
2773
|
}
|
|
2744
2774
|
|
|
2745
2775
|
let indexId = 0;
|
|
2746
|
-
const depthCache = new DepthCache();
|
|
2747
2776
|
function createNode(name, children) {
|
|
2748
2777
|
return new Node("expression", name, 0, 0, children, "");
|
|
2749
2778
|
}
|
|
@@ -2780,6 +2809,9 @@ class ExpressionPattern {
|
|
|
2780
2809
|
get recursivePatterns() {
|
|
2781
2810
|
return this._recursivePatterns;
|
|
2782
2811
|
}
|
|
2812
|
+
get startedOnIndex() {
|
|
2813
|
+
return this._firstIndex;
|
|
2814
|
+
}
|
|
2783
2815
|
constructor(name, patterns) {
|
|
2784
2816
|
this.shouldCompactAst = false;
|
|
2785
2817
|
if (patterns.length === 0) {
|
|
@@ -2897,11 +2929,8 @@ class ExpressionPattern {
|
|
|
2897
2929
|
lastChild.name === this.name;
|
|
2898
2930
|
}
|
|
2899
2931
|
parse(cursor) {
|
|
2900
|
-
this._firstIndex = cursor.index;
|
|
2901
|
-
depthCache.incrementDepth(this._id, this._firstIndex);
|
|
2902
2932
|
this._firstIndex = cursor.index;
|
|
2903
2933
|
const node = this._tryToParse(cursor);
|
|
2904
|
-
depthCache.decrementDepth(this._id, this._firstIndex);
|
|
2905
2934
|
if (node != null) {
|
|
2906
2935
|
cursor.moveTo(node.lastIndex);
|
|
2907
2936
|
cursor.resolveError();
|
|
@@ -2931,7 +2960,7 @@ class ExpressionPattern {
|
|
|
2931
2960
|
}
|
|
2932
2961
|
}
|
|
2933
2962
|
_tryToParse(cursor) {
|
|
2934
|
-
if (
|
|
2963
|
+
if (this._isBeyondRecursiveAllowance()) {
|
|
2935
2964
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
2936
2965
|
return null;
|
|
2937
2966
|
}
|
|
@@ -3096,6 +3125,20 @@ class ExpressionPattern {
|
|
|
3096
3125
|
return root;
|
|
3097
3126
|
}
|
|
3098
3127
|
}
|
|
3128
|
+
_isBeyondRecursiveAllowance() {
|
|
3129
|
+
let depth = 0;
|
|
3130
|
+
let pattern = this;
|
|
3131
|
+
while (pattern != null) {
|
|
3132
|
+
if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
|
|
3133
|
+
depth++;
|
|
3134
|
+
}
|
|
3135
|
+
if (depth > 2) {
|
|
3136
|
+
return true;
|
|
3137
|
+
}
|
|
3138
|
+
pattern = pattern.parent;
|
|
3139
|
+
}
|
|
3140
|
+
return false;
|
|
3141
|
+
}
|
|
3099
3142
|
test(text) {
|
|
3100
3143
|
const cursor = new Cursor(text);
|
|
3101
3144
|
const ast = this.parse(cursor);
|