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.
@@ -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;
@@ -205,16 +205,26 @@
205
205
  }
206
206
  normalize(startIndex = this._firstIndex) {
207
207
  let length = 0;
208
+ let runningOffset = startIndex;
208
209
  if (this.children.length === 0) {
209
210
  length = this._value.length;
210
211
  }
211
212
  else {
212
- length = this.children.reduce((acc, c) => acc + c.normalize(acc + startIndex), startIndex) - startIndex;
213
+ for (let x = 0; x < this.children.length; x++) {
214
+ const child = this.children[x];
215
+ const childLength = child.normalize(runningOffset);
216
+ runningOffset += childLength;
217
+ length += childLength;
218
+ }
213
219
  }
214
220
  this._firstIndex = startIndex;
215
221
  this._lastIndex = Math.max(startIndex + length - 1, 0);
216
222
  return length;
217
223
  }
224
+ compact() {
225
+ this._value = this.toString();
226
+ this._children.length = 0;
227
+ }
218
228
  toString() {
219
229
  if (this._children.length === 0) {
220
230
  return this._value;
@@ -526,6 +536,7 @@
526
536
  return [];
527
537
  }
528
538
  constructor(name, value) {
539
+ this.shouldCompactAst = false;
529
540
  if (value.length === 0) {
530
541
  throw new Error("Value Cannot be empty.");
531
542
  }
@@ -595,6 +606,7 @@
595
606
  clone(name = this._name) {
596
607
  const clone = new Literal(name, this._token);
597
608
  clone._id = this._id;
609
+ clone.shouldCompactAst = this.shouldCompactAst;
598
610
  return clone;
599
611
  }
600
612
  getTokens() {
@@ -658,6 +670,7 @@
658
670
  this._firstIndex = -1;
659
671
  this._substring = "";
660
672
  this._tokens = [];
673
+ this.shouldCompactAst = false;
661
674
  this._id = `regex-${idIndex$8++}`;
662
675
  this._type = "regex";
663
676
  this._name = name;
@@ -727,6 +740,7 @@
727
740
  const clone = new Regex(name, this._originalRegexString);
728
741
  clone._tokens = this._tokens.slice();
729
742
  clone._id = this._id;
743
+ clone.shouldCompactAst = this.shouldCompactAst;
730
744
  return clone;
731
745
  }
732
746
  getTokens() {
@@ -807,6 +821,7 @@
807
821
  return this._children;
808
822
  }
809
823
  constructor(name) {
824
+ this.shouldCompactAst = false;
810
825
  this._id = `reference-${idIndex$7++}`;
811
826
  this._type = "reference";
812
827
  this._name = name;
@@ -927,6 +942,7 @@
927
942
  clone(name = this._name) {
928
943
  const clone = new Reference(name);
929
944
  clone._id = this._id;
945
+ clone.shouldCompactAst = this.shouldCompactAst;
930
946
  // Optimize future clones, by caching the pattern we already found.
931
947
  if (this._pattern != null) {
932
948
  clone._cachedPattern = this._pattern;
@@ -1005,6 +1021,7 @@
1005
1021
  return this._children;
1006
1022
  }
1007
1023
  constructor(name, options, isGreedy = false) {
1024
+ this.shouldCompactAst = false;
1008
1025
  if (options.length === 0) {
1009
1026
  throw new Error("Need at least one pattern with an 'options' pattern.");
1010
1027
  }
@@ -1047,6 +1064,9 @@
1047
1064
  if (node != null) {
1048
1065
  cursor.moveTo(node.lastIndex);
1049
1066
  cursor.resolveError();
1067
+ if (this.shouldCompactAst) {
1068
+ node.compact();
1069
+ }
1050
1070
  return node;
1051
1071
  }
1052
1072
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -1121,9 +1141,10 @@
1121
1141
  return findPattern(this, predicate);
1122
1142
  }
1123
1143
  clone(name = this._name) {
1124
- const or = new Options(name, this._children, this._isGreedy);
1125
- or._id = this._id;
1126
- 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;
1127
1148
  }
1128
1149
  isEqual(pattern) {
1129
1150
  return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
@@ -1157,6 +1178,7 @@
1157
1178
  return this._max;
1158
1179
  }
1159
1180
  constructor(name, pattern, options = {}) {
1181
+ this.shouldCompactAst = false;
1160
1182
  this._id = `finite-repeat-${idIndex$5++}`;
1161
1183
  this._type = "finite-repeat";
1162
1184
  this._name = name;
@@ -1226,7 +1248,11 @@
1226
1248
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1227
1249
  cursor.resolveError();
1228
1250
  cursor.moveTo(lastIndex);
1229
- 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;
1230
1256
  }
1231
1257
  test(text) {
1232
1258
  const cursor = new Cursor(text);
@@ -1252,6 +1278,7 @@
1252
1278
  trimDivider: this._trimDivider
1253
1279
  });
1254
1280
  clone._id = this._id;
1281
+ clone.shouldCompactAst = this.shouldCompactAst;
1255
1282
  return clone;
1256
1283
  }
1257
1284
  getTokens() {
@@ -1329,6 +1356,7 @@
1329
1356
  return this._min;
1330
1357
  }
1331
1358
  constructor(name, pattern, options = {}) {
1359
+ this.shouldCompactAst = false;
1332
1360
  const min = options.min != null ? Math.max(options.min, 1) : 1;
1333
1361
  const divider = options.divider;
1334
1362
  let children;
@@ -1380,6 +1408,9 @@
1380
1408
  if (node != null) {
1381
1409
  cursor.moveTo(node.lastIndex);
1382
1410
  cursor.recordMatch(this, node);
1411
+ if (this.shouldCompactAst) {
1412
+ node.compact();
1413
+ }
1383
1414
  }
1384
1415
  return node;
1385
1416
  }
@@ -1558,6 +1589,7 @@
1558
1589
  trimDivider: this._trimDivider
1559
1590
  });
1560
1591
  clone._id = this._id;
1592
+ clone.shouldCompactAst = this.shouldCompactAst;
1561
1593
  return clone;
1562
1594
  }
1563
1595
  isEqual(pattern) {
@@ -1592,6 +1624,7 @@
1592
1624
  return this._options.max;
1593
1625
  }
1594
1626
  constructor(name, pattern, options = {}) {
1627
+ this.shouldCompactAst = false;
1595
1628
  this._id = `repeat-${idIndex$3++}`;
1596
1629
  this._pattern = pattern;
1597
1630
  this._parent = null;
@@ -1602,6 +1635,7 @@
1602
1635
  else {
1603
1636
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1604
1637
  }
1638
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1605
1639
  this._children = [this._repeatPattern];
1606
1640
  this._repeatPattern.parent = this;
1607
1641
  }
@@ -1618,6 +1652,7 @@
1618
1652
  let min = this._options.min;
1619
1653
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1620
1654
  clone._id = this._id;
1655
+ clone.shouldCompactAst = this.shouldCompactAst;
1621
1656
  return clone;
1622
1657
  }
1623
1658
  getTokens() {
@@ -1693,6 +1728,7 @@
1693
1728
  return this._children;
1694
1729
  }
1695
1730
  constructor(name, sequence) {
1731
+ this.shouldCompactAst = false;
1696
1732
  if (sequence.length === 0) {
1697
1733
  throw new Error("Need at least one pattern with a 'sequence' pattern.");
1698
1734
  }
@@ -1736,6 +1772,9 @@
1736
1772
  const node = this.createNode(cursor);
1737
1773
  if (node !== null) {
1738
1774
  cursor.recordMatch(this, node);
1775
+ if (this.shouldCompactAst) {
1776
+ node.compact();
1777
+ }
1739
1778
  }
1740
1779
  return node;
1741
1780
  }
@@ -1909,6 +1948,7 @@
1909
1948
  clone(name = this._name) {
1910
1949
  const clone = new Sequence(name, this._children);
1911
1950
  clone._id = this._id;
1951
+ clone.shouldCompactAst = this.shouldCompactAst;
1912
1952
  return clone;
1913
1953
  }
1914
1954
  isEqual(pattern) {
@@ -1968,6 +2008,7 @@
1968
2008
  return this._children;
1969
2009
  }
1970
2010
  constructor(name, pattern) {
2011
+ this.shouldCompactAst = false;
1971
2012
  this._id = `optional-${idIndex$1++}`;
1972
2013
  this._type = "optional";
1973
2014
  this._name = name;
@@ -1998,13 +2039,17 @@
1998
2039
  return null;
1999
2040
  }
2000
2041
  else {
2042
+ if (node != null && this.shouldCompactAst) {
2043
+ node.compact();
2044
+ }
2001
2045
  return node;
2002
2046
  }
2003
2047
  }
2004
2048
  clone(name = this._name) {
2005
- const optional = new Optional(name, this._children[0]);
2006
- optional._id = this._id;
2007
- return optional;
2049
+ const clone = new Optional(name, this._children[0]);
2050
+ clone._id = this._id;
2051
+ clone.shouldCompactAst = this.shouldCompactAst;
2052
+ return clone;
2008
2053
  }
2009
2054
  getTokens() {
2010
2055
  return this._children[0].getTokens();
@@ -2280,6 +2325,7 @@
2280
2325
  return this._children;
2281
2326
  }
2282
2327
  constructor(name, pattern) {
2328
+ this.shouldCompactAst = false;
2283
2329
  this._id = `not-${idIndex++}`;
2284
2330
  this._type = "not";
2285
2331
  this._name = name;
@@ -2624,6 +2670,7 @@
2624
2670
  return Object.assign({}, this._patterns);
2625
2671
  }
2626
2672
  constructor(name, pattern, context = []) {
2673
+ this.shouldCompactAst = false;
2627
2674
  this._id = `context-${contextId++}`;
2628
2675
  this._type = "context";
2629
2676
  this._name = name;
@@ -2647,6 +2694,7 @@
2647
2694
  clone(name = this._name) {
2648
2695
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2649
2696
  clone._id = this._id;
2697
+ clone.shouldCompactAst = this.shouldCompactAst;
2650
2698
  return clone;
2651
2699
  }
2652
2700
  getTokens() {
@@ -2726,6 +2774,7 @@
2726
2774
  return this._recursivePatterns;
2727
2775
  }
2728
2776
  constructor(name, patterns) {
2777
+ this.shouldCompactAst = false;
2729
2778
  if (patterns.length === 0) {
2730
2779
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2731
2780
  }
@@ -3093,6 +3142,7 @@
3093
3142
  clone(name = this._name) {
3094
3143
  const clone = new ExpressionPattern(name, this._originalPatterns);
3095
3144
  clone._id = this._id;
3145
+ clone.shouldCompactAst = this.shouldCompactAst;
3096
3146
  return clone;
3097
3147
  }
3098
3148
  isEqual(pattern) {