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.
@@ -50,6 +50,7 @@ export declare class Node {
50
50
  remove(): void;
51
51
  clone(): Node;
52
52
  normalize(startIndex?: number): number;
53
+ compact(): void;
53
54
  toString(): string;
54
55
  toCycleFreeObject(): CycleFreeNode;
55
56
  toJson(space?: number): string;
@@ -221,6 +221,10 @@
221
221
  this._lastIndex = Math.max(startIndex + length - 1, 0);
222
222
  return length;
223
223
  }
224
+ compact() {
225
+ this._value = this.toString();
226
+ this._children.length = 0;
227
+ }
224
228
  toString() {
225
229
  if (this._children.length === 0) {
226
230
  return this._value;
@@ -532,6 +536,7 @@
532
536
  return [];
533
537
  }
534
538
  constructor(name, value) {
539
+ this.shouldCompactAst = false;
535
540
  if (value.length === 0) {
536
541
  throw new Error("Value Cannot be empty.");
537
542
  }
@@ -601,6 +606,7 @@
601
606
  clone(name = this._name) {
602
607
  const clone = new Literal(name, this._token);
603
608
  clone._id = this._id;
609
+ clone.shouldCompactAst = this.shouldCompactAst;
604
610
  return clone;
605
611
  }
606
612
  getTokens() {
@@ -664,6 +670,7 @@
664
670
  this._firstIndex = -1;
665
671
  this._substring = "";
666
672
  this._tokens = [];
673
+ this.shouldCompactAst = false;
667
674
  this._id = `regex-${idIndex$8++}`;
668
675
  this._type = "regex";
669
676
  this._name = name;
@@ -733,6 +740,7 @@
733
740
  const clone = new Regex(name, this._originalRegexString);
734
741
  clone._tokens = this._tokens.slice();
735
742
  clone._id = this._id;
743
+ clone.shouldCompactAst = this.shouldCompactAst;
736
744
  return clone;
737
745
  }
738
746
  getTokens() {
@@ -813,6 +821,7 @@
813
821
  return this._children;
814
822
  }
815
823
  constructor(name) {
824
+ this.shouldCompactAst = false;
816
825
  this._id = `reference-${idIndex$7++}`;
817
826
  this._type = "reference";
818
827
  this._name = name;
@@ -933,6 +942,7 @@
933
942
  clone(name = this._name) {
934
943
  const clone = new Reference(name);
935
944
  clone._id = this._id;
945
+ clone.shouldCompactAst = this.shouldCompactAst;
936
946
  // Optimize future clones, by caching the pattern we already found.
937
947
  if (this._pattern != null) {
938
948
  clone._cachedPattern = this._pattern;
@@ -1011,6 +1021,7 @@
1011
1021
  return this._children;
1012
1022
  }
1013
1023
  constructor(name, options, isGreedy = false) {
1024
+ this.shouldCompactAst = false;
1014
1025
  if (options.length === 0) {
1015
1026
  throw new Error("Need at least one pattern with an 'options' pattern.");
1016
1027
  }
@@ -1053,6 +1064,9 @@
1053
1064
  if (node != null) {
1054
1065
  cursor.moveTo(node.lastIndex);
1055
1066
  cursor.resolveError();
1067
+ if (this.shouldCompactAst) {
1068
+ node.compact();
1069
+ }
1056
1070
  return node;
1057
1071
  }
1058
1072
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -1127,9 +1141,10 @@
1127
1141
  return findPattern(this, predicate);
1128
1142
  }
1129
1143
  clone(name = this._name) {
1130
- const or = new Options(name, this._children, this._isGreedy);
1131
- or._id = this._id;
1132
- return or;
1144
+ const clone = new Options(name, this._children, this._isGreedy);
1145
+ clone._id = this._id;
1146
+ clone.shouldCompactAst = this.shouldCompactAst;
1147
+ return clone;
1133
1148
  }
1134
1149
  isEqual(pattern) {
1135
1150
  return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
@@ -1163,6 +1178,7 @@
1163
1178
  return this._max;
1164
1179
  }
1165
1180
  constructor(name, pattern, options = {}) {
1181
+ this.shouldCompactAst = false;
1166
1182
  this._id = `finite-repeat-${idIndex$5++}`;
1167
1183
  this._type = "finite-repeat";
1168
1184
  this._name = name;
@@ -1232,7 +1248,11 @@
1232
1248
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1233
1249
  cursor.resolveError();
1234
1250
  cursor.moveTo(lastIndex);
1235
- return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1251
+ const node = new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1252
+ if (this.shouldCompactAst) {
1253
+ node.compact();
1254
+ }
1255
+ return node;
1236
1256
  }
1237
1257
  test(text) {
1238
1258
  const cursor = new Cursor(text);
@@ -1258,6 +1278,7 @@
1258
1278
  trimDivider: this._trimDivider
1259
1279
  });
1260
1280
  clone._id = this._id;
1281
+ clone.shouldCompactAst = this.shouldCompactAst;
1261
1282
  return clone;
1262
1283
  }
1263
1284
  getTokens() {
@@ -1335,6 +1356,7 @@
1335
1356
  return this._min;
1336
1357
  }
1337
1358
  constructor(name, pattern, options = {}) {
1359
+ this.shouldCompactAst = false;
1338
1360
  const min = options.min != null ? Math.max(options.min, 1) : 1;
1339
1361
  const divider = options.divider;
1340
1362
  let children;
@@ -1386,6 +1408,9 @@
1386
1408
  if (node != null) {
1387
1409
  cursor.moveTo(node.lastIndex);
1388
1410
  cursor.recordMatch(this, node);
1411
+ if (this.shouldCompactAst) {
1412
+ node.compact();
1413
+ }
1389
1414
  }
1390
1415
  return node;
1391
1416
  }
@@ -1564,6 +1589,7 @@
1564
1589
  trimDivider: this._trimDivider
1565
1590
  });
1566
1591
  clone._id = this._id;
1592
+ clone.shouldCompactAst = this.shouldCompactAst;
1567
1593
  return clone;
1568
1594
  }
1569
1595
  isEqual(pattern) {
@@ -1598,6 +1624,7 @@
1598
1624
  return this._options.max;
1599
1625
  }
1600
1626
  constructor(name, pattern, options = {}) {
1627
+ this.shouldCompactAst = false;
1601
1628
  this._id = `repeat-${idIndex$3++}`;
1602
1629
  this._pattern = pattern;
1603
1630
  this._parent = null;
@@ -1608,6 +1635,7 @@
1608
1635
  else {
1609
1636
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1610
1637
  }
1638
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1611
1639
  this._children = [this._repeatPattern];
1612
1640
  this._repeatPattern.parent = this;
1613
1641
  }
@@ -1624,6 +1652,7 @@
1624
1652
  let min = this._options.min;
1625
1653
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1626
1654
  clone._id = this._id;
1655
+ clone.shouldCompactAst = this.shouldCompactAst;
1627
1656
  return clone;
1628
1657
  }
1629
1658
  getTokens() {
@@ -1699,6 +1728,7 @@
1699
1728
  return this._children;
1700
1729
  }
1701
1730
  constructor(name, sequence) {
1731
+ this.shouldCompactAst = false;
1702
1732
  if (sequence.length === 0) {
1703
1733
  throw new Error("Need at least one pattern with a 'sequence' pattern.");
1704
1734
  }
@@ -1742,6 +1772,9 @@
1742
1772
  const node = this.createNode(cursor);
1743
1773
  if (node !== null) {
1744
1774
  cursor.recordMatch(this, node);
1775
+ if (this.shouldCompactAst) {
1776
+ node.compact();
1777
+ }
1745
1778
  }
1746
1779
  return node;
1747
1780
  }
@@ -1915,6 +1948,7 @@
1915
1948
  clone(name = this._name) {
1916
1949
  const clone = new Sequence(name, this._children);
1917
1950
  clone._id = this._id;
1951
+ clone.shouldCompactAst = this.shouldCompactAst;
1918
1952
  return clone;
1919
1953
  }
1920
1954
  isEqual(pattern) {
@@ -1974,6 +2008,7 @@
1974
2008
  return this._children;
1975
2009
  }
1976
2010
  constructor(name, pattern) {
2011
+ this.shouldCompactAst = false;
1977
2012
  this._id = `optional-${idIndex$1++}`;
1978
2013
  this._type = "optional";
1979
2014
  this._name = name;
@@ -2004,13 +2039,17 @@
2004
2039
  return null;
2005
2040
  }
2006
2041
  else {
2042
+ if (node != null && this.shouldCompactAst) {
2043
+ node.compact();
2044
+ }
2007
2045
  return node;
2008
2046
  }
2009
2047
  }
2010
2048
  clone(name = this._name) {
2011
- const optional = new Optional(name, this._children[0]);
2012
- optional._id = this._id;
2013
- return optional;
2049
+ const clone = new Optional(name, this._children[0]);
2050
+ clone._id = this._id;
2051
+ clone.shouldCompactAst = this.shouldCompactAst;
2052
+ return clone;
2014
2053
  }
2015
2054
  getTokens() {
2016
2055
  return this._children[0].getTokens();
@@ -2286,6 +2325,7 @@
2286
2325
  return this._children;
2287
2326
  }
2288
2327
  constructor(name, pattern) {
2328
+ this.shouldCompactAst = false;
2289
2329
  this._id = `not-${idIndex++}`;
2290
2330
  this._type = "not";
2291
2331
  this._name = name;
@@ -2630,6 +2670,7 @@
2630
2670
  return Object.assign({}, this._patterns);
2631
2671
  }
2632
2672
  constructor(name, pattern, context = []) {
2673
+ this.shouldCompactAst = false;
2633
2674
  this._id = `context-${contextId++}`;
2634
2675
  this._type = "context";
2635
2676
  this._name = name;
@@ -2653,6 +2694,7 @@
2653
2694
  clone(name = this._name) {
2654
2695
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2655
2696
  clone._id = this._id;
2697
+ clone.shouldCompactAst = this.shouldCompactAst;
2656
2698
  return clone;
2657
2699
  }
2658
2700
  getTokens() {
@@ -2732,6 +2774,7 @@
2732
2774
  return this._recursivePatterns;
2733
2775
  }
2734
2776
  constructor(name, patterns) {
2777
+ this.shouldCompactAst = false;
2735
2778
  if (patterns.length === 0) {
2736
2779
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2737
2780
  }
@@ -3099,6 +3142,7 @@
3099
3142
  clone(name = this._name) {
3100
3143
  const clone = new ExpressionPattern(name, this._originalPatterns);
3101
3144
  clone._id = this._id;
3145
+ clone.shouldCompactAst = this.shouldCompactAst;
3102
3146
  return clone;
3103
3147
  }
3104
3148
  isEqual(pattern) {