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.js CHANGED
@@ -203,16 +203,26 @@ class Node {
203
203
  }
204
204
  normalize(startIndex = this._firstIndex) {
205
205
  let length = 0;
206
+ let runningOffset = startIndex;
206
207
  if (this.children.length === 0) {
207
208
  length = this._value.length;
208
209
  }
209
210
  else {
210
- length = this.children.reduce((acc, c) => acc + c.normalize(acc + startIndex), startIndex) - startIndex;
211
+ for (let x = 0; x < this.children.length; x++) {
212
+ const child = this.children[x];
213
+ const childLength = child.normalize(runningOffset);
214
+ runningOffset += childLength;
215
+ length += childLength;
216
+ }
211
217
  }
212
218
  this._firstIndex = startIndex;
213
219
  this._lastIndex = Math.max(startIndex + length - 1, 0);
214
220
  return length;
215
221
  }
222
+ compact() {
223
+ this._value = this.toString();
224
+ this._children.length = 0;
225
+ }
216
226
  toString() {
217
227
  if (this._children.length === 0) {
218
228
  return this._value;
@@ -524,6 +534,7 @@ class Literal {
524
534
  return [];
525
535
  }
526
536
  constructor(name, value) {
537
+ this.shouldCompactAst = false;
527
538
  if (value.length === 0) {
528
539
  throw new Error("Value Cannot be empty.");
529
540
  }
@@ -593,6 +604,7 @@ class Literal {
593
604
  clone(name = this._name) {
594
605
  const clone = new Literal(name, this._token);
595
606
  clone._id = this._id;
607
+ clone.shouldCompactAst = this.shouldCompactAst;
596
608
  return clone;
597
609
  }
598
610
  getTokens() {
@@ -656,6 +668,7 @@ class Regex {
656
668
  this._firstIndex = -1;
657
669
  this._substring = "";
658
670
  this._tokens = [];
671
+ this.shouldCompactAst = false;
659
672
  this._id = `regex-${idIndex$8++}`;
660
673
  this._type = "regex";
661
674
  this._name = name;
@@ -725,6 +738,7 @@ class Regex {
725
738
  const clone = new Regex(name, this._originalRegexString);
726
739
  clone._tokens = this._tokens.slice();
727
740
  clone._id = this._id;
741
+ clone.shouldCompactAst = this.shouldCompactAst;
728
742
  return clone;
729
743
  }
730
744
  getTokens() {
@@ -805,6 +819,7 @@ class Reference {
805
819
  return this._children;
806
820
  }
807
821
  constructor(name) {
822
+ this.shouldCompactAst = false;
808
823
  this._id = `reference-${idIndex$7++}`;
809
824
  this._type = "reference";
810
825
  this._name = name;
@@ -925,6 +940,7 @@ class Reference {
925
940
  clone(name = this._name) {
926
941
  const clone = new Reference(name);
927
942
  clone._id = this._id;
943
+ clone.shouldCompactAst = this.shouldCompactAst;
928
944
  // Optimize future clones, by caching the pattern we already found.
929
945
  if (this._pattern != null) {
930
946
  clone._cachedPattern = this._pattern;
@@ -1003,6 +1019,7 @@ class Options {
1003
1019
  return this._children;
1004
1020
  }
1005
1021
  constructor(name, options, isGreedy = false) {
1022
+ this.shouldCompactAst = false;
1006
1023
  if (options.length === 0) {
1007
1024
  throw new Error("Need at least one pattern with an 'options' pattern.");
1008
1025
  }
@@ -1045,6 +1062,9 @@ class Options {
1045
1062
  if (node != null) {
1046
1063
  cursor.moveTo(node.lastIndex);
1047
1064
  cursor.resolveError();
1065
+ if (this.shouldCompactAst) {
1066
+ node.compact();
1067
+ }
1048
1068
  return node;
1049
1069
  }
1050
1070
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -1119,9 +1139,10 @@ class Options {
1119
1139
  return findPattern(this, predicate);
1120
1140
  }
1121
1141
  clone(name = this._name) {
1122
- const or = new Options(name, this._children, this._isGreedy);
1123
- or._id = this._id;
1124
- return or;
1142
+ const clone = new Options(name, this._children, this._isGreedy);
1143
+ clone._id = this._id;
1144
+ clone.shouldCompactAst = this.shouldCompactAst;
1145
+ return clone;
1125
1146
  }
1126
1147
  isEqual(pattern) {
1127
1148
  return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
@@ -1155,6 +1176,7 @@ class FiniteRepeat {
1155
1176
  return this._max;
1156
1177
  }
1157
1178
  constructor(name, pattern, options = {}) {
1179
+ this.shouldCompactAst = false;
1158
1180
  this._id = `finite-repeat-${idIndex$5++}`;
1159
1181
  this._type = "finite-repeat";
1160
1182
  this._name = name;
@@ -1224,7 +1246,11 @@ class FiniteRepeat {
1224
1246
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1225
1247
  cursor.resolveError();
1226
1248
  cursor.moveTo(lastIndex);
1227
- return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1249
+ const node = new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1250
+ if (this.shouldCompactAst) {
1251
+ node.compact();
1252
+ }
1253
+ return node;
1228
1254
  }
1229
1255
  test(text) {
1230
1256
  const cursor = new Cursor(text);
@@ -1250,6 +1276,7 @@ class FiniteRepeat {
1250
1276
  trimDivider: this._trimDivider
1251
1277
  });
1252
1278
  clone._id = this._id;
1279
+ clone.shouldCompactAst = this.shouldCompactAst;
1253
1280
  return clone;
1254
1281
  }
1255
1282
  getTokens() {
@@ -1327,6 +1354,7 @@ class InfiniteRepeat {
1327
1354
  return this._min;
1328
1355
  }
1329
1356
  constructor(name, pattern, options = {}) {
1357
+ this.shouldCompactAst = false;
1330
1358
  const min = options.min != null ? Math.max(options.min, 1) : 1;
1331
1359
  const divider = options.divider;
1332
1360
  let children;
@@ -1378,6 +1406,9 @@ class InfiniteRepeat {
1378
1406
  if (node != null) {
1379
1407
  cursor.moveTo(node.lastIndex);
1380
1408
  cursor.recordMatch(this, node);
1409
+ if (this.shouldCompactAst) {
1410
+ node.compact();
1411
+ }
1381
1412
  }
1382
1413
  return node;
1383
1414
  }
@@ -1556,6 +1587,7 @@ class InfiniteRepeat {
1556
1587
  trimDivider: this._trimDivider
1557
1588
  });
1558
1589
  clone._id = this._id;
1590
+ clone.shouldCompactAst = this.shouldCompactAst;
1559
1591
  return clone;
1560
1592
  }
1561
1593
  isEqual(pattern) {
@@ -1590,6 +1622,7 @@ class Repeat {
1590
1622
  return this._options.max;
1591
1623
  }
1592
1624
  constructor(name, pattern, options = {}) {
1625
+ this.shouldCompactAst = false;
1593
1626
  this._id = `repeat-${idIndex$3++}`;
1594
1627
  this._pattern = pattern;
1595
1628
  this._parent = null;
@@ -1600,6 +1633,7 @@ class Repeat {
1600
1633
  else {
1601
1634
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1602
1635
  }
1636
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1603
1637
  this._children = [this._repeatPattern];
1604
1638
  this._repeatPattern.parent = this;
1605
1639
  }
@@ -1616,6 +1650,7 @@ class Repeat {
1616
1650
  let min = this._options.min;
1617
1651
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1618
1652
  clone._id = this._id;
1653
+ clone.shouldCompactAst = this.shouldCompactAst;
1619
1654
  return clone;
1620
1655
  }
1621
1656
  getTokens() {
@@ -1691,6 +1726,7 @@ class Sequence {
1691
1726
  return this._children;
1692
1727
  }
1693
1728
  constructor(name, sequence) {
1729
+ this.shouldCompactAst = false;
1694
1730
  if (sequence.length === 0) {
1695
1731
  throw new Error("Need at least one pattern with a 'sequence' pattern.");
1696
1732
  }
@@ -1734,6 +1770,9 @@ class Sequence {
1734
1770
  const node = this.createNode(cursor);
1735
1771
  if (node !== null) {
1736
1772
  cursor.recordMatch(this, node);
1773
+ if (this.shouldCompactAst) {
1774
+ node.compact();
1775
+ }
1737
1776
  }
1738
1777
  return node;
1739
1778
  }
@@ -1907,6 +1946,7 @@ class Sequence {
1907
1946
  clone(name = this._name) {
1908
1947
  const clone = new Sequence(name, this._children);
1909
1948
  clone._id = this._id;
1949
+ clone.shouldCompactAst = this.shouldCompactAst;
1910
1950
  return clone;
1911
1951
  }
1912
1952
  isEqual(pattern) {
@@ -1966,6 +2006,7 @@ class Optional {
1966
2006
  return this._children;
1967
2007
  }
1968
2008
  constructor(name, pattern) {
2009
+ this.shouldCompactAst = false;
1969
2010
  this._id = `optional-${idIndex$1++}`;
1970
2011
  this._type = "optional";
1971
2012
  this._name = name;
@@ -1996,13 +2037,17 @@ class Optional {
1996
2037
  return null;
1997
2038
  }
1998
2039
  else {
2040
+ if (node != null && this.shouldCompactAst) {
2041
+ node.compact();
2042
+ }
1999
2043
  return node;
2000
2044
  }
2001
2045
  }
2002
2046
  clone(name = this._name) {
2003
- const optional = new Optional(name, this._children[0]);
2004
- optional._id = this._id;
2005
- return optional;
2047
+ const clone = new Optional(name, this._children[0]);
2048
+ clone._id = this._id;
2049
+ clone.shouldCompactAst = this.shouldCompactAst;
2050
+ return clone;
2006
2051
  }
2007
2052
  getTokens() {
2008
2053
  return this._children[0].getTokens();
@@ -2278,6 +2323,7 @@ class Not {
2278
2323
  return this._children;
2279
2324
  }
2280
2325
  constructor(name, pattern) {
2326
+ this.shouldCompactAst = false;
2281
2327
  this._id = `not-${idIndex++}`;
2282
2328
  this._type = "not";
2283
2329
  this._name = name;
@@ -2622,6 +2668,7 @@ class Context {
2622
2668
  return Object.assign({}, this._patterns);
2623
2669
  }
2624
2670
  constructor(name, pattern, context = []) {
2671
+ this.shouldCompactAst = false;
2625
2672
  this._id = `context-${contextId++}`;
2626
2673
  this._type = "context";
2627
2674
  this._name = name;
@@ -2645,6 +2692,7 @@ class Context {
2645
2692
  clone(name = this._name) {
2646
2693
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2647
2694
  clone._id = this._id;
2695
+ clone.shouldCompactAst = this.shouldCompactAst;
2648
2696
  return clone;
2649
2697
  }
2650
2698
  getTokens() {
@@ -2724,6 +2772,7 @@ class ExpressionPattern {
2724
2772
  return this._recursivePatterns;
2725
2773
  }
2726
2774
  constructor(name, patterns) {
2775
+ this.shouldCompactAst = false;
2727
2776
  if (patterns.length === 0) {
2728
2777
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2729
2778
  }
@@ -3091,6 +3140,7 @@ class ExpressionPattern {
3091
3140
  clone(name = this._name) {
3092
3141
  const clone = new ExpressionPattern(name, this._originalPatterns);
3093
3142
  clone._id = this._id;
3143
+ clone.shouldCompactAst = this.shouldCompactAst;
3094
3144
  return clone;
3095
3145
  }
3096
3146
  isEqual(pattern) {