clarity-pattern-parser 10.2.13 → 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
@@ -215,6 +215,10 @@ class Node {
215
215
  this._lastIndex = Math.max(startIndex + length - 1, 0);
216
216
  return length;
217
217
  }
218
+ compact() {
219
+ this._value = this.toString();
220
+ this._children.length = 0;
221
+ }
218
222
  toString() {
219
223
  if (this._children.length === 0) {
220
224
  return this._value;
@@ -526,6 +530,7 @@ class Literal {
526
530
  return [];
527
531
  }
528
532
  constructor(name, value) {
533
+ this.shouldCompactAst = false;
529
534
  if (value.length === 0) {
530
535
  throw new Error("Value Cannot be empty.");
531
536
  }
@@ -595,6 +600,7 @@ class Literal {
595
600
  clone(name = this._name) {
596
601
  const clone = new Literal(name, this._token);
597
602
  clone._id = this._id;
603
+ clone.shouldCompactAst = this.shouldCompactAst;
598
604
  return clone;
599
605
  }
600
606
  getTokens() {
@@ -658,6 +664,7 @@ class Regex {
658
664
  this._firstIndex = -1;
659
665
  this._substring = "";
660
666
  this._tokens = [];
667
+ this.shouldCompactAst = false;
661
668
  this._id = `regex-${idIndex$8++}`;
662
669
  this._type = "regex";
663
670
  this._name = name;
@@ -727,6 +734,7 @@ class Regex {
727
734
  const clone = new Regex(name, this._originalRegexString);
728
735
  clone._tokens = this._tokens.slice();
729
736
  clone._id = this._id;
737
+ clone.shouldCompactAst = this.shouldCompactAst;
730
738
  return clone;
731
739
  }
732
740
  getTokens() {
@@ -807,6 +815,7 @@ class Reference {
807
815
  return this._children;
808
816
  }
809
817
  constructor(name) {
818
+ this.shouldCompactAst = false;
810
819
  this._id = `reference-${idIndex$7++}`;
811
820
  this._type = "reference";
812
821
  this._name = name;
@@ -927,6 +936,7 @@ class Reference {
927
936
  clone(name = this._name) {
928
937
  const clone = new Reference(name);
929
938
  clone._id = this._id;
939
+ clone.shouldCompactAst = this.shouldCompactAst;
930
940
  // Optimize future clones, by caching the pattern we already found.
931
941
  if (this._pattern != null) {
932
942
  clone._cachedPattern = this._pattern;
@@ -1005,6 +1015,7 @@ class Options {
1005
1015
  return this._children;
1006
1016
  }
1007
1017
  constructor(name, options, isGreedy = false) {
1018
+ this.shouldCompactAst = false;
1008
1019
  if (options.length === 0) {
1009
1020
  throw new Error("Need at least one pattern with an 'options' pattern.");
1010
1021
  }
@@ -1047,6 +1058,9 @@ class Options {
1047
1058
  if (node != null) {
1048
1059
  cursor.moveTo(node.lastIndex);
1049
1060
  cursor.resolveError();
1061
+ if (this.shouldCompactAst) {
1062
+ node.compact();
1063
+ }
1050
1064
  return node;
1051
1065
  }
1052
1066
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -1121,9 +1135,10 @@ class Options {
1121
1135
  return findPattern(this, predicate);
1122
1136
  }
1123
1137
  clone(name = this._name) {
1124
- const or = new Options(name, this._children, this._isGreedy);
1125
- or._id = this._id;
1126
- 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;
1127
1142
  }
1128
1143
  isEqual(pattern) {
1129
1144
  return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
@@ -1157,6 +1172,7 @@ class FiniteRepeat {
1157
1172
  return this._max;
1158
1173
  }
1159
1174
  constructor(name, pattern, options = {}) {
1175
+ this.shouldCompactAst = false;
1160
1176
  this._id = `finite-repeat-${idIndex$5++}`;
1161
1177
  this._type = "finite-repeat";
1162
1178
  this._name = name;
@@ -1226,7 +1242,11 @@ class FiniteRepeat {
1226
1242
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1227
1243
  cursor.resolveError();
1228
1244
  cursor.moveTo(lastIndex);
1229
- 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;
1230
1250
  }
1231
1251
  test(text) {
1232
1252
  const cursor = new Cursor(text);
@@ -1252,6 +1272,7 @@ class FiniteRepeat {
1252
1272
  trimDivider: this._trimDivider
1253
1273
  });
1254
1274
  clone._id = this._id;
1275
+ clone.shouldCompactAst = this.shouldCompactAst;
1255
1276
  return clone;
1256
1277
  }
1257
1278
  getTokens() {
@@ -1329,6 +1350,7 @@ class InfiniteRepeat {
1329
1350
  return this._min;
1330
1351
  }
1331
1352
  constructor(name, pattern, options = {}) {
1353
+ this.shouldCompactAst = false;
1332
1354
  const min = options.min != null ? Math.max(options.min, 1) : 1;
1333
1355
  const divider = options.divider;
1334
1356
  let children;
@@ -1380,6 +1402,9 @@ class InfiniteRepeat {
1380
1402
  if (node != null) {
1381
1403
  cursor.moveTo(node.lastIndex);
1382
1404
  cursor.recordMatch(this, node);
1405
+ if (this.shouldCompactAst) {
1406
+ node.compact();
1407
+ }
1383
1408
  }
1384
1409
  return node;
1385
1410
  }
@@ -1558,6 +1583,7 @@ class InfiniteRepeat {
1558
1583
  trimDivider: this._trimDivider
1559
1584
  });
1560
1585
  clone._id = this._id;
1586
+ clone.shouldCompactAst = this.shouldCompactAst;
1561
1587
  return clone;
1562
1588
  }
1563
1589
  isEqual(pattern) {
@@ -1592,6 +1618,7 @@ class Repeat {
1592
1618
  return this._options.max;
1593
1619
  }
1594
1620
  constructor(name, pattern, options = {}) {
1621
+ this.shouldCompactAst = false;
1595
1622
  this._id = `repeat-${idIndex$3++}`;
1596
1623
  this._pattern = pattern;
1597
1624
  this._parent = null;
@@ -1602,6 +1629,7 @@ class Repeat {
1602
1629
  else {
1603
1630
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1604
1631
  }
1632
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1605
1633
  this._children = [this._repeatPattern];
1606
1634
  this._repeatPattern.parent = this;
1607
1635
  }
@@ -1618,6 +1646,7 @@ class Repeat {
1618
1646
  let min = this._options.min;
1619
1647
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1620
1648
  clone._id = this._id;
1649
+ clone.shouldCompactAst = this.shouldCompactAst;
1621
1650
  return clone;
1622
1651
  }
1623
1652
  getTokens() {
@@ -1693,6 +1722,7 @@ class Sequence {
1693
1722
  return this._children;
1694
1723
  }
1695
1724
  constructor(name, sequence) {
1725
+ this.shouldCompactAst = false;
1696
1726
  if (sequence.length === 0) {
1697
1727
  throw new Error("Need at least one pattern with a 'sequence' pattern.");
1698
1728
  }
@@ -1736,6 +1766,9 @@ class Sequence {
1736
1766
  const node = this.createNode(cursor);
1737
1767
  if (node !== null) {
1738
1768
  cursor.recordMatch(this, node);
1769
+ if (this.shouldCompactAst) {
1770
+ node.compact();
1771
+ }
1739
1772
  }
1740
1773
  return node;
1741
1774
  }
@@ -1909,6 +1942,7 @@ class Sequence {
1909
1942
  clone(name = this._name) {
1910
1943
  const clone = new Sequence(name, this._children);
1911
1944
  clone._id = this._id;
1945
+ clone.shouldCompactAst = this.shouldCompactAst;
1912
1946
  return clone;
1913
1947
  }
1914
1948
  isEqual(pattern) {
@@ -1968,6 +2002,7 @@ class Optional {
1968
2002
  return this._children;
1969
2003
  }
1970
2004
  constructor(name, pattern) {
2005
+ this.shouldCompactAst = false;
1971
2006
  this._id = `optional-${idIndex$1++}`;
1972
2007
  this._type = "optional";
1973
2008
  this._name = name;
@@ -1998,13 +2033,17 @@ class Optional {
1998
2033
  return null;
1999
2034
  }
2000
2035
  else {
2036
+ if (node != null && this.shouldCompactAst) {
2037
+ node.compact();
2038
+ }
2001
2039
  return node;
2002
2040
  }
2003
2041
  }
2004
2042
  clone(name = this._name) {
2005
- const optional = new Optional(name, this._children[0]);
2006
- optional._id = this._id;
2007
- return optional;
2043
+ const clone = new Optional(name, this._children[0]);
2044
+ clone._id = this._id;
2045
+ clone.shouldCompactAst = this.shouldCompactAst;
2046
+ return clone;
2008
2047
  }
2009
2048
  getTokens() {
2010
2049
  return this._children[0].getTokens();
@@ -2280,6 +2319,7 @@ class Not {
2280
2319
  return this._children;
2281
2320
  }
2282
2321
  constructor(name, pattern) {
2322
+ this.shouldCompactAst = false;
2283
2323
  this._id = `not-${idIndex++}`;
2284
2324
  this._type = "not";
2285
2325
  this._name = name;
@@ -2624,6 +2664,7 @@ class Context {
2624
2664
  return Object.assign({}, this._patterns);
2625
2665
  }
2626
2666
  constructor(name, pattern, context = []) {
2667
+ this.shouldCompactAst = false;
2627
2668
  this._id = `context-${contextId++}`;
2628
2669
  this._type = "context";
2629
2670
  this._name = name;
@@ -2647,6 +2688,7 @@ class Context {
2647
2688
  clone(name = this._name) {
2648
2689
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2649
2690
  clone._id = this._id;
2691
+ clone.shouldCompactAst = this.shouldCompactAst;
2650
2692
  return clone;
2651
2693
  }
2652
2694
  getTokens() {
@@ -2726,6 +2768,7 @@ class ExpressionPattern {
2726
2768
  return this._recursivePatterns;
2727
2769
  }
2728
2770
  constructor(name, patterns) {
2771
+ this.shouldCompactAst = false;
2729
2772
  if (patterns.length === 0) {
2730
2773
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2731
2774
  }
@@ -3093,6 +3136,7 @@ class ExpressionPattern {
3093
3136
  clone(name = this._name) {
3094
3137
  const clone = new ExpressionPattern(name, this._originalPatterns);
3095
3138
  clone._id = this._id;
3139
+ clone.shouldCompactAst = this.shouldCompactAst;
3096
3140
  return clone;
3097
3141
  }
3098
3142
  isEqual(pattern) {