clarity-pattern-parser 10.2.12 → 10.2.14

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.esm.js CHANGED
@@ -199,16 +199,26 @@ class Node {
199
199
  }
200
200
  normalize(startIndex = this._firstIndex) {
201
201
  let length = 0;
202
+ let runningOffset = startIndex;
202
203
  if (this.children.length === 0) {
203
204
  length = this._value.length;
204
205
  }
205
206
  else {
206
- length = this.children.reduce((acc, c) => acc + c.normalize(acc + startIndex), startIndex) - startIndex;
207
+ for (let x = 0; x < this.children.length; x++) {
208
+ const child = this.children[x];
209
+ const childLength = child.normalize(runningOffset);
210
+ runningOffset += childLength;
211
+ length += childLength;
212
+ }
207
213
  }
208
214
  this._firstIndex = startIndex;
209
215
  this._lastIndex = Math.max(startIndex + length - 1, 0);
210
216
  return length;
211
217
  }
218
+ compact() {
219
+ this._value = this.toString();
220
+ this._children.length = 0;
221
+ }
212
222
  toString() {
213
223
  if (this._children.length === 0) {
214
224
  return this._value;
@@ -520,6 +530,7 @@ class Literal {
520
530
  return [];
521
531
  }
522
532
  constructor(name, value) {
533
+ this.shouldCompactAst = false;
523
534
  if (value.length === 0) {
524
535
  throw new Error("Value Cannot be empty.");
525
536
  }
@@ -589,6 +600,7 @@ class Literal {
589
600
  clone(name = this._name) {
590
601
  const clone = new Literal(name, this._token);
591
602
  clone._id = this._id;
603
+ clone.shouldCompactAst = this.shouldCompactAst;
592
604
  return clone;
593
605
  }
594
606
  getTokens() {
@@ -652,6 +664,7 @@ class Regex {
652
664
  this._firstIndex = -1;
653
665
  this._substring = "";
654
666
  this._tokens = [];
667
+ this.shouldCompactAst = false;
655
668
  this._id = `regex-${idIndex$8++}`;
656
669
  this._type = "regex";
657
670
  this._name = name;
@@ -721,6 +734,7 @@ class Regex {
721
734
  const clone = new Regex(name, this._originalRegexString);
722
735
  clone._tokens = this._tokens.slice();
723
736
  clone._id = this._id;
737
+ clone.shouldCompactAst = this.shouldCompactAst;
724
738
  return clone;
725
739
  }
726
740
  getTokens() {
@@ -801,6 +815,7 @@ class Reference {
801
815
  return this._children;
802
816
  }
803
817
  constructor(name) {
818
+ this.shouldCompactAst = false;
804
819
  this._id = `reference-${idIndex$7++}`;
805
820
  this._type = "reference";
806
821
  this._name = name;
@@ -921,6 +936,7 @@ class Reference {
921
936
  clone(name = this._name) {
922
937
  const clone = new Reference(name);
923
938
  clone._id = this._id;
939
+ clone.shouldCompactAst = this.shouldCompactAst;
924
940
  // Optimize future clones, by caching the pattern we already found.
925
941
  if (this._pattern != null) {
926
942
  clone._cachedPattern = this._pattern;
@@ -999,6 +1015,7 @@ class Options {
999
1015
  return this._children;
1000
1016
  }
1001
1017
  constructor(name, options, isGreedy = false) {
1018
+ this.shouldCompactAst = false;
1002
1019
  if (options.length === 0) {
1003
1020
  throw new Error("Need at least one pattern with an 'options' pattern.");
1004
1021
  }
@@ -1041,6 +1058,9 @@ class Options {
1041
1058
  if (node != null) {
1042
1059
  cursor.moveTo(node.lastIndex);
1043
1060
  cursor.resolveError();
1061
+ if (this.shouldCompactAst) {
1062
+ node.compact();
1063
+ }
1044
1064
  return node;
1045
1065
  }
1046
1066
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -1115,9 +1135,10 @@ class Options {
1115
1135
  return findPattern(this, predicate);
1116
1136
  }
1117
1137
  clone(name = this._name) {
1118
- const or = new Options(name, this._children, this._isGreedy);
1119
- or._id = this._id;
1120
- return or;
1138
+ const clone = new Options(name, this._children, this._isGreedy);
1139
+ clone._id = this._id;
1140
+ clone.shouldCompactAst = this.shouldCompactAst;
1141
+ return clone;
1121
1142
  }
1122
1143
  isEqual(pattern) {
1123
1144
  return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
@@ -1151,6 +1172,7 @@ class FiniteRepeat {
1151
1172
  return this._max;
1152
1173
  }
1153
1174
  constructor(name, pattern, options = {}) {
1175
+ this.shouldCompactAst = false;
1154
1176
  this._id = `finite-repeat-${idIndex$5++}`;
1155
1177
  this._type = "finite-repeat";
1156
1178
  this._name = name;
@@ -1220,7 +1242,11 @@ class FiniteRepeat {
1220
1242
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1221
1243
  cursor.resolveError();
1222
1244
  cursor.moveTo(lastIndex);
1223
- return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1245
+ const node = new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1246
+ if (this.shouldCompactAst) {
1247
+ node.compact();
1248
+ }
1249
+ return node;
1224
1250
  }
1225
1251
  test(text) {
1226
1252
  const cursor = new Cursor(text);
@@ -1246,6 +1272,7 @@ class FiniteRepeat {
1246
1272
  trimDivider: this._trimDivider
1247
1273
  });
1248
1274
  clone._id = this._id;
1275
+ clone.shouldCompactAst = this.shouldCompactAst;
1249
1276
  return clone;
1250
1277
  }
1251
1278
  getTokens() {
@@ -1323,6 +1350,7 @@ class InfiniteRepeat {
1323
1350
  return this._min;
1324
1351
  }
1325
1352
  constructor(name, pattern, options = {}) {
1353
+ this.shouldCompactAst = false;
1326
1354
  const min = options.min != null ? Math.max(options.min, 1) : 1;
1327
1355
  const divider = options.divider;
1328
1356
  let children;
@@ -1374,6 +1402,9 @@ class InfiniteRepeat {
1374
1402
  if (node != null) {
1375
1403
  cursor.moveTo(node.lastIndex);
1376
1404
  cursor.recordMatch(this, node);
1405
+ if (this.shouldCompactAst) {
1406
+ node.compact();
1407
+ }
1377
1408
  }
1378
1409
  return node;
1379
1410
  }
@@ -1552,6 +1583,7 @@ class InfiniteRepeat {
1552
1583
  trimDivider: this._trimDivider
1553
1584
  });
1554
1585
  clone._id = this._id;
1586
+ clone.shouldCompactAst = this.shouldCompactAst;
1555
1587
  return clone;
1556
1588
  }
1557
1589
  isEqual(pattern) {
@@ -1586,6 +1618,7 @@ class Repeat {
1586
1618
  return this._options.max;
1587
1619
  }
1588
1620
  constructor(name, pattern, options = {}) {
1621
+ this.shouldCompactAst = false;
1589
1622
  this._id = `repeat-${idIndex$3++}`;
1590
1623
  this._pattern = pattern;
1591
1624
  this._parent = null;
@@ -1596,6 +1629,7 @@ class Repeat {
1596
1629
  else {
1597
1630
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1598
1631
  }
1632
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1599
1633
  this._children = [this._repeatPattern];
1600
1634
  this._repeatPattern.parent = this;
1601
1635
  }
@@ -1612,6 +1646,7 @@ class Repeat {
1612
1646
  let min = this._options.min;
1613
1647
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1614
1648
  clone._id = this._id;
1649
+ clone.shouldCompactAst = this.shouldCompactAst;
1615
1650
  return clone;
1616
1651
  }
1617
1652
  getTokens() {
@@ -1687,6 +1722,7 @@ class Sequence {
1687
1722
  return this._children;
1688
1723
  }
1689
1724
  constructor(name, sequence) {
1725
+ this.shouldCompactAst = false;
1690
1726
  if (sequence.length === 0) {
1691
1727
  throw new Error("Need at least one pattern with a 'sequence' pattern.");
1692
1728
  }
@@ -1730,6 +1766,9 @@ class Sequence {
1730
1766
  const node = this.createNode(cursor);
1731
1767
  if (node !== null) {
1732
1768
  cursor.recordMatch(this, node);
1769
+ if (this.shouldCompactAst) {
1770
+ node.compact();
1771
+ }
1733
1772
  }
1734
1773
  return node;
1735
1774
  }
@@ -1903,6 +1942,7 @@ class Sequence {
1903
1942
  clone(name = this._name) {
1904
1943
  const clone = new Sequence(name, this._children);
1905
1944
  clone._id = this._id;
1945
+ clone.shouldCompactAst = this.shouldCompactAst;
1906
1946
  return clone;
1907
1947
  }
1908
1948
  isEqual(pattern) {
@@ -1962,6 +2002,7 @@ class Optional {
1962
2002
  return this._children;
1963
2003
  }
1964
2004
  constructor(name, pattern) {
2005
+ this.shouldCompactAst = false;
1965
2006
  this._id = `optional-${idIndex$1++}`;
1966
2007
  this._type = "optional";
1967
2008
  this._name = name;
@@ -1992,13 +2033,17 @@ class Optional {
1992
2033
  return null;
1993
2034
  }
1994
2035
  else {
2036
+ if (node != null && this.shouldCompactAst) {
2037
+ node.compact();
2038
+ }
1995
2039
  return node;
1996
2040
  }
1997
2041
  }
1998
2042
  clone(name = this._name) {
1999
- const optional = new Optional(name, this._children[0]);
2000
- optional._id = this._id;
2001
- return optional;
2043
+ const clone = new Optional(name, this._children[0]);
2044
+ clone._id = this._id;
2045
+ clone.shouldCompactAst = this.shouldCompactAst;
2046
+ return clone;
2002
2047
  }
2003
2048
  getTokens() {
2004
2049
  return this._children[0].getTokens();
@@ -2274,6 +2319,7 @@ class Not {
2274
2319
  return this._children;
2275
2320
  }
2276
2321
  constructor(name, pattern) {
2322
+ this.shouldCompactAst = false;
2277
2323
  this._id = `not-${idIndex++}`;
2278
2324
  this._type = "not";
2279
2325
  this._name = name;
@@ -2618,6 +2664,7 @@ class Context {
2618
2664
  return Object.assign({}, this._patterns);
2619
2665
  }
2620
2666
  constructor(name, pattern, context = []) {
2667
+ this.shouldCompactAst = false;
2621
2668
  this._id = `context-${contextId++}`;
2622
2669
  this._type = "context";
2623
2670
  this._name = name;
@@ -2641,6 +2688,7 @@ class Context {
2641
2688
  clone(name = this._name) {
2642
2689
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2643
2690
  clone._id = this._id;
2691
+ clone.shouldCompactAst = this.shouldCompactAst;
2644
2692
  return clone;
2645
2693
  }
2646
2694
  getTokens() {
@@ -2720,6 +2768,7 @@ class ExpressionPattern {
2720
2768
  return this._recursivePatterns;
2721
2769
  }
2722
2770
  constructor(name, patterns) {
2771
+ this.shouldCompactAst = false;
2723
2772
  if (patterns.length === 0) {
2724
2773
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2725
2774
  }
@@ -3087,6 +3136,7 @@ class ExpressionPattern {
3087
3136
  clone(name = this._name) {
3088
3137
  const clone = new ExpressionPattern(name, this._originalPatterns);
3089
3138
  clone._id = this._id;
3139
+ clone.shouldCompactAst = this.shouldCompactAst;
3090
3140
  return clone;
3091
3141
  }
3092
3142
  isEqual(pattern) {