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