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.browser.js +54 -6
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +54 -6
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +54 -6
- package/dist/index.js.map +1 -1
- package/dist/patterns/ExpressionPattern.d.ts +2 -0
- package/dist/patterns/Repeat.d.ts +3 -1
- package/package.json +1 -1
- package/src/ast/Node.ts +1 -2
- package/src/grammar/Grammar.test.ts +53 -3
- package/src/grammar/Grammar.ts +20 -0
- package/src/grammar/patterns/statement.ts +7 -2
- package/src/patterns/ExpressionPattern.test.ts +38 -0
- package/src/patterns/ExpressionPattern.ts +35 -2
- package/src/patterns/Repeat.ts +10 -2
package/dist/index.esm.js
CHANGED
|
@@ -161,7 +161,6 @@ class Node {
|
|
|
161
161
|
walkBreadthFirst(callback) {
|
|
162
162
|
const queue = [this];
|
|
163
163
|
while (queue.length > 0) {
|
|
164
|
-
// biome-ignore lint/style/noNonNullAssertion: This will never be undefined.
|
|
165
164
|
const current = queue.shift();
|
|
166
165
|
callback(current);
|
|
167
166
|
queue.push(...current.children);
|
|
@@ -1593,6 +1592,13 @@ class InfiniteRepeat {
|
|
|
1593
1592
|
|
|
1594
1593
|
let idIndex$3 = 0;
|
|
1595
1594
|
class Repeat {
|
|
1595
|
+
get shouldCompactAst() {
|
|
1596
|
+
return this._shouldCompactAst;
|
|
1597
|
+
}
|
|
1598
|
+
set shouldCompactAst(value) {
|
|
1599
|
+
this._shouldCompactAst = value;
|
|
1600
|
+
this._repeatPattern.shouldCompactAst = value;
|
|
1601
|
+
}
|
|
1596
1602
|
get id() {
|
|
1597
1603
|
return this._id;
|
|
1598
1604
|
}
|
|
@@ -1618,10 +1624,10 @@ class Repeat {
|
|
|
1618
1624
|
return this._options.max;
|
|
1619
1625
|
}
|
|
1620
1626
|
constructor(name, pattern, options = {}) {
|
|
1621
|
-
this.shouldCompactAst = false;
|
|
1622
1627
|
this._id = `repeat-${idIndex$3++}`;
|
|
1623
1628
|
this._pattern = pattern;
|
|
1624
1629
|
this._parent = null;
|
|
1630
|
+
this._shouldCompactAst = false;
|
|
1625
1631
|
this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
|
|
1626
1632
|
if (this._options.max !== Infinity) {
|
|
1627
1633
|
this._repeatPattern = new FiniteRepeat(name, pattern, this._options);
|
|
@@ -1629,7 +1635,6 @@ class Repeat {
|
|
|
1629
1635
|
else {
|
|
1630
1636
|
this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
|
|
1631
1637
|
}
|
|
1632
|
-
this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
|
|
1633
1638
|
this._children = [this._repeatPattern];
|
|
1634
1639
|
this._repeatPattern.parent = this;
|
|
1635
1640
|
}
|
|
@@ -2192,13 +2197,17 @@ const pattern = new Options("pattern", [
|
|
|
2192
2197
|
|
|
2193
2198
|
const optionalSpaces$2 = new Optional("optional-spaces", spaces$1);
|
|
2194
2199
|
const assignOperator = new Literal("assign-operator", "=");
|
|
2200
|
+
const compact = new Literal("compact", "compact");
|
|
2201
|
+
const compactModifier = new Sequence("compact-modifier", [lineSpaces$1, compact]);
|
|
2202
|
+
const optionalCompactModifier = new Optional("optional-compact-modifier", compactModifier);
|
|
2195
2203
|
const assignStatement = new Sequence("assign-statement", [
|
|
2196
2204
|
optionalSpaces$2,
|
|
2197
2205
|
name$1,
|
|
2198
2206
|
optionalSpaces$2,
|
|
2199
2207
|
assignOperator,
|
|
2200
2208
|
optionalSpaces$2,
|
|
2201
|
-
pattern
|
|
2209
|
+
pattern,
|
|
2210
|
+
optionalCompactModifier
|
|
2202
2211
|
]);
|
|
2203
2212
|
const statement = new Options("statement", [assignStatement, name$1.clone("export-name")]);
|
|
2204
2213
|
|
|
@@ -2786,6 +2795,7 @@ class ExpressionPattern {
|
|
|
2786
2795
|
this._binaryAssociation = [];
|
|
2787
2796
|
this._precedenceMap = {};
|
|
2788
2797
|
this._originalPatterns = patterns;
|
|
2798
|
+
this._shouldCompactPatternsMap = {};
|
|
2789
2799
|
this._patterns = this._organizePatterns(patterns);
|
|
2790
2800
|
if (this._unaryPatterns.length === 0) {
|
|
2791
2801
|
throw new Error("Need at least one operand pattern with an 'expression' pattern.");
|
|
@@ -2794,6 +2804,7 @@ class ExpressionPattern {
|
|
|
2794
2804
|
_organizePatterns(patterns) {
|
|
2795
2805
|
const finalPatterns = [];
|
|
2796
2806
|
patterns.forEach((pattern) => {
|
|
2807
|
+
this._shouldCompactPatternsMap[pattern.name] = pattern.shouldCompactAst;
|
|
2797
2808
|
if (this._isBinary(pattern)) {
|
|
2798
2809
|
const binaryName = this._extractName(pattern);
|
|
2799
2810
|
const clone = this._extractDelimiter(pattern).clone();
|
|
@@ -2882,7 +2893,6 @@ class ExpressionPattern {
|
|
|
2882
2893
|
lastChild.name === this.name;
|
|
2883
2894
|
}
|
|
2884
2895
|
parse(cursor) {
|
|
2885
|
-
// This is a cache to help with speed
|
|
2886
2896
|
this._firstIndex = cursor.index;
|
|
2887
2897
|
depthCache.incrementDepth(this._id, this._firstIndex);
|
|
2888
2898
|
this._firstIndex = cursor.index;
|
|
@@ -2891,11 +2901,31 @@ class ExpressionPattern {
|
|
|
2891
2901
|
if (node != null) {
|
|
2892
2902
|
cursor.moveTo(node.lastIndex);
|
|
2893
2903
|
cursor.resolveError();
|
|
2904
|
+
this._compactResult(node);
|
|
2894
2905
|
return node;
|
|
2895
2906
|
}
|
|
2896
2907
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
2897
2908
|
return null;
|
|
2898
2909
|
}
|
|
2910
|
+
_compactResult(node) {
|
|
2911
|
+
if (node == null) {
|
|
2912
|
+
return;
|
|
2913
|
+
}
|
|
2914
|
+
if (this.shouldCompactAst) {
|
|
2915
|
+
node.compact();
|
|
2916
|
+
return;
|
|
2917
|
+
}
|
|
2918
|
+
// This could be really expensive with large trees. So we optimize with these checks,
|
|
2919
|
+
// as well as use breadth first as to not recompact nodes over and over again.
|
|
2920
|
+
const isCompactingNeeded = Object.values(this._shouldCompactPatternsMap).some(p => p);
|
|
2921
|
+
if (isCompactingNeeded) {
|
|
2922
|
+
node.walkBreadthFirst(n => {
|
|
2923
|
+
if (this._shouldCompactPatternsMap[n.name]) {
|
|
2924
|
+
n.compact();
|
|
2925
|
+
}
|
|
2926
|
+
});
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2899
2929
|
_tryToParse(cursor) {
|
|
2900
2930
|
if (depthCache.getDepth(this._id, this._firstIndex) > 2) {
|
|
2901
2931
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
@@ -2960,7 +2990,9 @@ class ExpressionPattern {
|
|
|
2960
2990
|
}
|
|
2961
2991
|
break outer;
|
|
2962
2992
|
}
|
|
2963
|
-
|
|
2993
|
+
onIndex = cursor.index;
|
|
2994
|
+
i = -1;
|
|
2995
|
+
continue;
|
|
2964
2996
|
}
|
|
2965
2997
|
}
|
|
2966
2998
|
cursor.resolveError();
|
|
@@ -3317,9 +3349,13 @@ class Grammar {
|
|
|
3317
3349
|
}
|
|
3318
3350
|
_saveOptions(statementNode) {
|
|
3319
3351
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3352
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3320
3353
|
const name = nameNode.value;
|
|
3321
3354
|
const optionsNode = statementNode.find(n => n.name === "options-literal");
|
|
3322
3355
|
const options = this._buildOptions(name, optionsNode);
|
|
3356
|
+
if (shouldCompactAst != null) {
|
|
3357
|
+
options.shouldCompactAst = true;
|
|
3358
|
+
}
|
|
3323
3359
|
this._parseContext.patternsByName.set(name, options);
|
|
3324
3360
|
}
|
|
3325
3361
|
_buildOptions(name, node) {
|
|
@@ -3379,9 +3415,13 @@ class Grammar {
|
|
|
3379
3415
|
}
|
|
3380
3416
|
_saveSequence(statementNode) {
|
|
3381
3417
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3418
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3382
3419
|
const name = nameNode.value;
|
|
3383
3420
|
const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
|
|
3384
3421
|
const sequence = this._buildSequence(name, sequenceNode);
|
|
3422
|
+
if (shouldCompactAst != null) {
|
|
3423
|
+
sequence.shouldCompactAst = true;
|
|
3424
|
+
}
|
|
3385
3425
|
this._parseContext.patternsByName.set(name, sequence);
|
|
3386
3426
|
}
|
|
3387
3427
|
_buildSequence(name, node) {
|
|
@@ -3401,9 +3441,13 @@ class Grammar {
|
|
|
3401
3441
|
}
|
|
3402
3442
|
_saveRepeat(statementNode) {
|
|
3403
3443
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3444
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3404
3445
|
const name = nameNode.value;
|
|
3405
3446
|
const repeatNode = statementNode.find(n => n.name === "repeat-literal");
|
|
3406
3447
|
const repeat = this._buildRepeat(name, repeatNode);
|
|
3448
|
+
if (shouldCompactAst != null) {
|
|
3449
|
+
repeat.shouldCompactAst = true;
|
|
3450
|
+
}
|
|
3407
3451
|
this._parseContext.patternsByName.set(name, repeat);
|
|
3408
3452
|
}
|
|
3409
3453
|
_buildRepeat(name, repeatNode) {
|
|
@@ -3556,10 +3600,14 @@ class Grammar {
|
|
|
3556
3600
|
}
|
|
3557
3601
|
_saveAlias(statementNode) {
|
|
3558
3602
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3603
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3559
3604
|
const aliasNode = statementNode.find(n => n.name === "alias-literal");
|
|
3560
3605
|
const aliasName = aliasNode.value;
|
|
3561
3606
|
const name = nameNode.value;
|
|
3562
3607
|
const alias = this._getPattern(aliasName).clone(name);
|
|
3608
|
+
if (shouldCompactAst != null) {
|
|
3609
|
+
alias.shouldCompactAst = true;
|
|
3610
|
+
}
|
|
3563
3611
|
this._parseContext.patternsByName.set(name, alias);
|
|
3564
3612
|
}
|
|
3565
3613
|
static parse(expression, options) {
|