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.js CHANGED
@@ -219,6 +219,10 @@ class Node {
219
219
  this._lastIndex = Math.max(startIndex + length - 1, 0);
220
220
  return length;
221
221
  }
222
+ compact() {
223
+ this._value = this.toString();
224
+ this._children.length = 0;
225
+ }
222
226
  toString() {
223
227
  if (this._children.length === 0) {
224
228
  return this._value;
@@ -530,6 +534,7 @@ class Literal {
530
534
  return [];
531
535
  }
532
536
  constructor(name, value) {
537
+ this.shouldCompactAst = false;
533
538
  if (value.length === 0) {
534
539
  throw new Error("Value Cannot be empty.");
535
540
  }
@@ -599,6 +604,7 @@ class Literal {
599
604
  clone(name = this._name) {
600
605
  const clone = new Literal(name, this._token);
601
606
  clone._id = this._id;
607
+ clone.shouldCompactAst = this.shouldCompactAst;
602
608
  return clone;
603
609
  }
604
610
  getTokens() {
@@ -662,6 +668,7 @@ class Regex {
662
668
  this._firstIndex = -1;
663
669
  this._substring = "";
664
670
  this._tokens = [];
671
+ this.shouldCompactAst = false;
665
672
  this._id = `regex-${idIndex$8++}`;
666
673
  this._type = "regex";
667
674
  this._name = name;
@@ -731,6 +738,7 @@ class Regex {
731
738
  const clone = new Regex(name, this._originalRegexString);
732
739
  clone._tokens = this._tokens.slice();
733
740
  clone._id = this._id;
741
+ clone.shouldCompactAst = this.shouldCompactAst;
734
742
  return clone;
735
743
  }
736
744
  getTokens() {
@@ -811,6 +819,7 @@ class Reference {
811
819
  return this._children;
812
820
  }
813
821
  constructor(name) {
822
+ this.shouldCompactAst = false;
814
823
  this._id = `reference-${idIndex$7++}`;
815
824
  this._type = "reference";
816
825
  this._name = name;
@@ -931,6 +940,7 @@ class Reference {
931
940
  clone(name = this._name) {
932
941
  const clone = new Reference(name);
933
942
  clone._id = this._id;
943
+ clone.shouldCompactAst = this.shouldCompactAst;
934
944
  // Optimize future clones, by caching the pattern we already found.
935
945
  if (this._pattern != null) {
936
946
  clone._cachedPattern = this._pattern;
@@ -1009,6 +1019,7 @@ class Options {
1009
1019
  return this._children;
1010
1020
  }
1011
1021
  constructor(name, options, isGreedy = false) {
1022
+ this.shouldCompactAst = false;
1012
1023
  if (options.length === 0) {
1013
1024
  throw new Error("Need at least one pattern with an 'options' pattern.");
1014
1025
  }
@@ -1051,6 +1062,9 @@ class Options {
1051
1062
  if (node != null) {
1052
1063
  cursor.moveTo(node.lastIndex);
1053
1064
  cursor.resolveError();
1065
+ if (this.shouldCompactAst) {
1066
+ node.compact();
1067
+ }
1054
1068
  return node;
1055
1069
  }
1056
1070
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -1125,9 +1139,10 @@ class Options {
1125
1139
  return findPattern(this, predicate);
1126
1140
  }
1127
1141
  clone(name = this._name) {
1128
- const or = new Options(name, this._children, this._isGreedy);
1129
- or._id = this._id;
1130
- 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;
1131
1146
  }
1132
1147
  isEqual(pattern) {
1133
1148
  return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
@@ -1161,6 +1176,7 @@ class FiniteRepeat {
1161
1176
  return this._max;
1162
1177
  }
1163
1178
  constructor(name, pattern, options = {}) {
1179
+ this.shouldCompactAst = false;
1164
1180
  this._id = `finite-repeat-${idIndex$5++}`;
1165
1181
  this._type = "finite-repeat";
1166
1182
  this._name = name;
@@ -1230,7 +1246,11 @@ class FiniteRepeat {
1230
1246
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1231
1247
  cursor.resolveError();
1232
1248
  cursor.moveTo(lastIndex);
1233
- 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;
1234
1254
  }
1235
1255
  test(text) {
1236
1256
  const cursor = new Cursor(text);
@@ -1256,6 +1276,7 @@ class FiniteRepeat {
1256
1276
  trimDivider: this._trimDivider
1257
1277
  });
1258
1278
  clone._id = this._id;
1279
+ clone.shouldCompactAst = this.shouldCompactAst;
1259
1280
  return clone;
1260
1281
  }
1261
1282
  getTokens() {
@@ -1333,6 +1354,7 @@ class InfiniteRepeat {
1333
1354
  return this._min;
1334
1355
  }
1335
1356
  constructor(name, pattern, options = {}) {
1357
+ this.shouldCompactAst = false;
1336
1358
  const min = options.min != null ? Math.max(options.min, 1) : 1;
1337
1359
  const divider = options.divider;
1338
1360
  let children;
@@ -1384,6 +1406,9 @@ class InfiniteRepeat {
1384
1406
  if (node != null) {
1385
1407
  cursor.moveTo(node.lastIndex);
1386
1408
  cursor.recordMatch(this, node);
1409
+ if (this.shouldCompactAst) {
1410
+ node.compact();
1411
+ }
1387
1412
  }
1388
1413
  return node;
1389
1414
  }
@@ -1562,6 +1587,7 @@ class InfiniteRepeat {
1562
1587
  trimDivider: this._trimDivider
1563
1588
  });
1564
1589
  clone._id = this._id;
1590
+ clone.shouldCompactAst = this.shouldCompactAst;
1565
1591
  return clone;
1566
1592
  }
1567
1593
  isEqual(pattern) {
@@ -1596,6 +1622,7 @@ class Repeat {
1596
1622
  return this._options.max;
1597
1623
  }
1598
1624
  constructor(name, pattern, options = {}) {
1625
+ this.shouldCompactAst = false;
1599
1626
  this._id = `repeat-${idIndex$3++}`;
1600
1627
  this._pattern = pattern;
1601
1628
  this._parent = null;
@@ -1606,6 +1633,7 @@ class Repeat {
1606
1633
  else {
1607
1634
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1608
1635
  }
1636
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1609
1637
  this._children = [this._repeatPattern];
1610
1638
  this._repeatPattern.parent = this;
1611
1639
  }
@@ -1622,6 +1650,7 @@ class Repeat {
1622
1650
  let min = this._options.min;
1623
1651
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1624
1652
  clone._id = this._id;
1653
+ clone.shouldCompactAst = this.shouldCompactAst;
1625
1654
  return clone;
1626
1655
  }
1627
1656
  getTokens() {
@@ -1697,6 +1726,7 @@ class Sequence {
1697
1726
  return this._children;
1698
1727
  }
1699
1728
  constructor(name, sequence) {
1729
+ this.shouldCompactAst = false;
1700
1730
  if (sequence.length === 0) {
1701
1731
  throw new Error("Need at least one pattern with a 'sequence' pattern.");
1702
1732
  }
@@ -1740,6 +1770,9 @@ class Sequence {
1740
1770
  const node = this.createNode(cursor);
1741
1771
  if (node !== null) {
1742
1772
  cursor.recordMatch(this, node);
1773
+ if (this.shouldCompactAst) {
1774
+ node.compact();
1775
+ }
1743
1776
  }
1744
1777
  return node;
1745
1778
  }
@@ -1913,6 +1946,7 @@ class Sequence {
1913
1946
  clone(name = this._name) {
1914
1947
  const clone = new Sequence(name, this._children);
1915
1948
  clone._id = this._id;
1949
+ clone.shouldCompactAst = this.shouldCompactAst;
1916
1950
  return clone;
1917
1951
  }
1918
1952
  isEqual(pattern) {
@@ -1972,6 +2006,7 @@ class Optional {
1972
2006
  return this._children;
1973
2007
  }
1974
2008
  constructor(name, pattern) {
2009
+ this.shouldCompactAst = false;
1975
2010
  this._id = `optional-${idIndex$1++}`;
1976
2011
  this._type = "optional";
1977
2012
  this._name = name;
@@ -2002,13 +2037,17 @@ class Optional {
2002
2037
  return null;
2003
2038
  }
2004
2039
  else {
2040
+ if (node != null && this.shouldCompactAst) {
2041
+ node.compact();
2042
+ }
2005
2043
  return node;
2006
2044
  }
2007
2045
  }
2008
2046
  clone(name = this._name) {
2009
- const optional = new Optional(name, this._children[0]);
2010
- optional._id = this._id;
2011
- return optional;
2047
+ const clone = new Optional(name, this._children[0]);
2048
+ clone._id = this._id;
2049
+ clone.shouldCompactAst = this.shouldCompactAst;
2050
+ return clone;
2012
2051
  }
2013
2052
  getTokens() {
2014
2053
  return this._children[0].getTokens();
@@ -2284,6 +2323,7 @@ class Not {
2284
2323
  return this._children;
2285
2324
  }
2286
2325
  constructor(name, pattern) {
2326
+ this.shouldCompactAst = false;
2287
2327
  this._id = `not-${idIndex++}`;
2288
2328
  this._type = "not";
2289
2329
  this._name = name;
@@ -2628,6 +2668,7 @@ class Context {
2628
2668
  return Object.assign({}, this._patterns);
2629
2669
  }
2630
2670
  constructor(name, pattern, context = []) {
2671
+ this.shouldCompactAst = false;
2631
2672
  this._id = `context-${contextId++}`;
2632
2673
  this._type = "context";
2633
2674
  this._name = name;
@@ -2651,6 +2692,7 @@ class Context {
2651
2692
  clone(name = this._name) {
2652
2693
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2653
2694
  clone._id = this._id;
2695
+ clone.shouldCompactAst = this.shouldCompactAst;
2654
2696
  return clone;
2655
2697
  }
2656
2698
  getTokens() {
@@ -2730,6 +2772,7 @@ class ExpressionPattern {
2730
2772
  return this._recursivePatterns;
2731
2773
  }
2732
2774
  constructor(name, patterns) {
2775
+ this.shouldCompactAst = false;
2733
2776
  if (patterns.length === 0) {
2734
2777
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2735
2778
  }
@@ -3097,6 +3140,7 @@ class ExpressionPattern {
3097
3140
  clone(name = this._name) {
3098
3141
  const clone = new ExpressionPattern(name, this._originalPatterns);
3099
3142
  clone._id = this._id;
3143
+ clone.shouldCompactAst = this.shouldCompactAst;
3100
3144
  return clone;
3101
3145
  }
3102
3146
  isEqual(pattern) {