clarity-pattern-parser 10.2.14 → 10.3.1

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();
@@ -2886,7 +2897,6 @@ class ExpressionPattern {
2886
2897
  lastChild.name === this.name;
2887
2898
  }
2888
2899
  parse(cursor) {
2889
- // This is a cache to help with speed
2890
2900
  this._firstIndex = cursor.index;
2891
2901
  depthCache.incrementDepth(this._id, this._firstIndex);
2892
2902
  this._firstIndex = cursor.index;
@@ -2895,11 +2905,31 @@ class ExpressionPattern {
2895
2905
  if (node != null) {
2896
2906
  cursor.moveTo(node.lastIndex);
2897
2907
  cursor.resolveError();
2908
+ this._compactResult(node);
2898
2909
  return node;
2899
2910
  }
2900
2911
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
2901
2912
  return null;
2902
2913
  }
2914
+ _compactResult(node) {
2915
+ if (node == null) {
2916
+ return;
2917
+ }
2918
+ if (this.shouldCompactAst) {
2919
+ node.compact();
2920
+ return;
2921
+ }
2922
+ // This could be really expensive with large trees. So we optimize with these checks,
2923
+ // as well as use breadth first as to not recompact nodes over and over again.
2924
+ const isCompactingNeeded = Object.values(this._shouldCompactPatternsMap).some(p => p);
2925
+ if (isCompactingNeeded) {
2926
+ node.walkBreadthFirst(n => {
2927
+ if (this._shouldCompactPatternsMap[n.name]) {
2928
+ n.compact();
2929
+ }
2930
+ });
2931
+ }
2932
+ }
2903
2933
  _tryToParse(cursor) {
2904
2934
  if (depthCache.getDepth(this._id, this._firstIndex) > 2) {
2905
2935
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
@@ -2964,7 +2994,9 @@ class ExpressionPattern {
2964
2994
  }
2965
2995
  break outer;
2966
2996
  }
2967
- break;
2997
+ onIndex = cursor.index;
2998
+ i = -1;
2999
+ continue;
2968
3000
  }
2969
3001
  }
2970
3002
  cursor.resolveError();
@@ -3321,9 +3353,13 @@ class Grammar {
3321
3353
  }
3322
3354
  _saveOptions(statementNode) {
3323
3355
  const nameNode = statementNode.find(n => n.name === "name");
3356
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3324
3357
  const name = nameNode.value;
3325
3358
  const optionsNode = statementNode.find(n => n.name === "options-literal");
3326
3359
  const options = this._buildOptions(name, optionsNode);
3360
+ if (shouldCompactAst != null) {
3361
+ options.shouldCompactAst = true;
3362
+ }
3327
3363
  this._parseContext.patternsByName.set(name, options);
3328
3364
  }
3329
3365
  _buildOptions(name, node) {
@@ -3383,9 +3419,13 @@ class Grammar {
3383
3419
  }
3384
3420
  _saveSequence(statementNode) {
3385
3421
  const nameNode = statementNode.find(n => n.name === "name");
3422
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3386
3423
  const name = nameNode.value;
3387
3424
  const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
3388
3425
  const sequence = this._buildSequence(name, sequenceNode);
3426
+ if (shouldCompactAst != null) {
3427
+ sequence.shouldCompactAst = true;
3428
+ }
3389
3429
  this._parseContext.patternsByName.set(name, sequence);
3390
3430
  }
3391
3431
  _buildSequence(name, node) {
@@ -3405,9 +3445,13 @@ class Grammar {
3405
3445
  }
3406
3446
  _saveRepeat(statementNode) {
3407
3447
  const nameNode = statementNode.find(n => n.name === "name");
3448
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3408
3449
  const name = nameNode.value;
3409
3450
  const repeatNode = statementNode.find(n => n.name === "repeat-literal");
3410
3451
  const repeat = this._buildRepeat(name, repeatNode);
3452
+ if (shouldCompactAst != null) {
3453
+ repeat.shouldCompactAst = true;
3454
+ }
3411
3455
  this._parseContext.patternsByName.set(name, repeat);
3412
3456
  }
3413
3457
  _buildRepeat(name, repeatNode) {
@@ -3560,10 +3604,14 @@ class Grammar {
3560
3604
  }
3561
3605
  _saveAlias(statementNode) {
3562
3606
  const nameNode = statementNode.find(n => n.name === "name");
3607
+ const shouldCompactAst = statementNode.find(n => n.name === "compact");
3563
3608
  const aliasNode = statementNode.find(n => n.name === "alias-literal");
3564
3609
  const aliasName = aliasNode.value;
3565
3610
  const name = nameNode.value;
3566
3611
  const alias = this._getPattern(aliasName).clone(name);
3612
+ if (shouldCompactAst != null) {
3613
+ alias.shouldCompactAst = true;
3614
+ }
3567
3615
  this._parseContext.patternsByName.set(name, alias);
3568
3616
  }
3569
3617
  static parse(expression, options) {