clarity-pattern-parser 10.2.14 → 10.3.0

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.
@@ -167,7 +167,6 @@
167
167
  walkBreadthFirst(callback) {
168
168
  const queue = [this];
169
169
  while (queue.length > 0) {
170
- // biome-ignore lint/style/noNonNullAssertion: This will never be undefined.
171
170
  const current = queue.shift();
172
171
  callback(current);
173
172
  queue.push(...current.children);
@@ -1599,6 +1598,13 @@
1599
1598
 
1600
1599
  let idIndex$3 = 0;
1601
1600
  class Repeat {
1601
+ get shouldCompactAst() {
1602
+ return this._shouldCompactAst;
1603
+ }
1604
+ set shouldCompactAst(value) {
1605
+ this._shouldCompactAst = value;
1606
+ this._repeatPattern.shouldCompactAst = value;
1607
+ }
1602
1608
  get id() {
1603
1609
  return this._id;
1604
1610
  }
@@ -1624,10 +1630,10 @@
1624
1630
  return this._options.max;
1625
1631
  }
1626
1632
  constructor(name, pattern, options = {}) {
1627
- this.shouldCompactAst = false;
1628
1633
  this._id = `repeat-${idIndex$3++}`;
1629
1634
  this._pattern = pattern;
1630
1635
  this._parent = null;
1636
+ this._shouldCompactAst = false;
1631
1637
  this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1632
1638
  if (this._options.max !== Infinity) {
1633
1639
  this._repeatPattern = new FiniteRepeat(name, pattern, this._options);
@@ -1635,7 +1641,6 @@
1635
1641
  else {
1636
1642
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1637
1643
  }
1638
- this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
1639
1644
  this._children = [this._repeatPattern];
1640
1645
  this._repeatPattern.parent = this;
1641
1646
  }
@@ -2198,13 +2203,17 @@
2198
2203
 
2199
2204
  const optionalSpaces$2 = new Optional("optional-spaces", spaces$1);
2200
2205
  const assignOperator = new Literal("assign-operator", "=");
2206
+ const compact = new Literal("compact", "compact");
2207
+ const compactModifier = new Sequence("compact-modifier", [lineSpaces$1, compact]);
2208
+ const optionalCompactModifier = new Optional("optional-compact-modifier", compactModifier);
2201
2209
  const assignStatement = new Sequence("assign-statement", [
2202
2210
  optionalSpaces$2,
2203
2211
  name$1,
2204
2212
  optionalSpaces$2,
2205
2213
  assignOperator,
2206
2214
  optionalSpaces$2,
2207
- pattern
2215
+ pattern,
2216
+ optionalCompactModifier
2208
2217
  ]);
2209
2218
  const statement = new Options("statement", [assignStatement, name$1.clone("export-name")]);
2210
2219
 
@@ -2792,6 +2801,7 @@
2792
2801
  this._binaryAssociation = [];
2793
2802
  this._precedenceMap = {};
2794
2803
  this._originalPatterns = patterns;
2804
+ this._shouldCompactPatternsMap = {};
2795
2805
  this._patterns = this._organizePatterns(patterns);
2796
2806
  if (this._unaryPatterns.length === 0) {
2797
2807
  throw new Error("Need at least one operand pattern with an 'expression' pattern.");
@@ -2800,6 +2810,7 @@
2800
2810
  _organizePatterns(patterns) {
2801
2811
  const finalPatterns = [];
2802
2812
  patterns.forEach((pattern) => {
2813
+ this._shouldCompactPatternsMap[pattern.name] = pattern.shouldCompactAst;
2803
2814
  if (this._isBinary(pattern)) {
2804
2815
  const binaryName = this._extractName(pattern);
2805
2816
  const clone = this._extractDelimiter(pattern).clone();
@@ -2897,11 +2908,31 @@
2897
2908
  if (node != null) {
2898
2909
  cursor.moveTo(node.lastIndex);
2899
2910
  cursor.resolveError();
2911
+ this._compactResult(node);
2900
2912
  return node;
2901
2913
  }
2902
2914
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
2903
2915
  return null;
2904
2916
  }
2917
+ _compactResult(node) {
2918
+ if (node == null) {
2919
+ return;
2920
+ }
2921
+ if (this.shouldCompactAst) {
2922
+ node.compact();
2923
+ return;
2924
+ }
2925
+ // This could be really expensive with large trees. So we optimize with these checks,
2926
+ // as well as use breadth first as to not recompact nodes over and over again.
2927
+ const isCompactingNeeded = Object.values(this._shouldCompactPatternsMap).some(p => p);
2928
+ if (isCompactingNeeded) {
2929
+ node.walkBreadthFirst(n => {
2930
+ if (this._shouldCompactPatternsMap[n.name]) {
2931
+ n.compact();
2932
+ }
2933
+ });
2934
+ }
2935
+ }
2905
2936
  _tryToParse(cursor) {
2906
2937
  if (depthCache.getDepth(this._id, this._firstIndex) > 2) {
2907
2938
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -3323,9 +3354,13 @@
3323
3354
  }
3324
3355
  _saveOptions(statementNode) {
3325
3356
  const nameNode = statementNode.find(n => n.name === "name");
3357
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3326
3358
  const name = nameNode.value;
3327
3359
  const optionsNode = statementNode.find(n => n.name === "options-literal");
3328
3360
  const options = this._buildOptions(name, optionsNode);
3361
+ if (shouldCompactAst != null) {
3362
+ options.shouldCompactAst = true;
3363
+ }
3329
3364
  this._parseContext.patternsByName.set(name, options);
3330
3365
  }
3331
3366
  _buildOptions(name, node) {
@@ -3385,9 +3420,13 @@
3385
3420
  }
3386
3421
  _saveSequence(statementNode) {
3387
3422
  const nameNode = statementNode.find(n => n.name === "name");
3423
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3388
3424
  const name = nameNode.value;
3389
3425
  const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
3390
3426
  const sequence = this._buildSequence(name, sequenceNode);
3427
+ if (shouldCompactAst != null) {
3428
+ sequence.shouldCompactAst = true;
3429
+ }
3391
3430
  this._parseContext.patternsByName.set(name, sequence);
3392
3431
  }
3393
3432
  _buildSequence(name, node) {
@@ -3407,9 +3446,13 @@
3407
3446
  }
3408
3447
  _saveRepeat(statementNode) {
3409
3448
  const nameNode = statementNode.find(n => n.name === "name");
3449
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3410
3450
  const name = nameNode.value;
3411
3451
  const repeatNode = statementNode.find(n => n.name === "repeat-literal");
3412
3452
  const repeat = this._buildRepeat(name, repeatNode);
3453
+ if (shouldCompactAst != null) {
3454
+ repeat.shouldCompactAst = true;
3455
+ }
3413
3456
  this._parseContext.patternsByName.set(name, repeat);
3414
3457
  }
3415
3458
  _buildRepeat(name, repeatNode) {
@@ -3562,10 +3605,14 @@
3562
3605
  }
3563
3606
  _saveAlias(statementNode) {
3564
3607
  const nameNode = statementNode.find(n => n.name === "name");
3608
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3565
3609
  const aliasNode = statementNode.find(n => n.name === "alias-literal");
3566
3610
  const aliasName = aliasNode.value;
3567
3611
  const name = nameNode.value;
3568
3612
  const alias = this._getPattern(aliasName).clone(name);
3613
+ if (shouldCompactAst != null) {
3614
+ alias.shouldCompactAst = true;
3615
+ }
3569
3616
  this._parseContext.patternsByName.set(name, alias);
3570
3617
  }
3571
3618
  static parse(expression, options) {