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.browser.js +51 -4
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +51 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +51 -4
- 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.ts +32 -0
- 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();
|
|
@@ -2891,11 +2902,31 @@ class ExpressionPattern {
|
|
|
2891
2902
|
if (node != null) {
|
|
2892
2903
|
cursor.moveTo(node.lastIndex);
|
|
2893
2904
|
cursor.resolveError();
|
|
2905
|
+
this._compactResult(node);
|
|
2894
2906
|
return node;
|
|
2895
2907
|
}
|
|
2896
2908
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
2897
2909
|
return null;
|
|
2898
2910
|
}
|
|
2911
|
+
_compactResult(node) {
|
|
2912
|
+
if (node == null) {
|
|
2913
|
+
return;
|
|
2914
|
+
}
|
|
2915
|
+
if (this.shouldCompactAst) {
|
|
2916
|
+
node.compact();
|
|
2917
|
+
return;
|
|
2918
|
+
}
|
|
2919
|
+
// This could be really expensive with large trees. So we optimize with these checks,
|
|
2920
|
+
// as well as use breadth first as to not recompact nodes over and over again.
|
|
2921
|
+
const isCompactingNeeded = Object.values(this._shouldCompactPatternsMap).some(p => p);
|
|
2922
|
+
if (isCompactingNeeded) {
|
|
2923
|
+
node.walkBreadthFirst(n => {
|
|
2924
|
+
if (this._shouldCompactPatternsMap[n.name]) {
|
|
2925
|
+
n.compact();
|
|
2926
|
+
}
|
|
2927
|
+
});
|
|
2928
|
+
}
|
|
2929
|
+
}
|
|
2899
2930
|
_tryToParse(cursor) {
|
|
2900
2931
|
if (depthCache.getDepth(this._id, this._firstIndex) > 2) {
|
|
2901
2932
|
cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
|
|
@@ -3317,9 +3348,13 @@ class Grammar {
|
|
|
3317
3348
|
}
|
|
3318
3349
|
_saveOptions(statementNode) {
|
|
3319
3350
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3351
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3320
3352
|
const name = nameNode.value;
|
|
3321
3353
|
const optionsNode = statementNode.find(n => n.name === "options-literal");
|
|
3322
3354
|
const options = this._buildOptions(name, optionsNode);
|
|
3355
|
+
if (shouldCompactAst != null) {
|
|
3356
|
+
options.shouldCompactAst = true;
|
|
3357
|
+
}
|
|
3323
3358
|
this._parseContext.patternsByName.set(name, options);
|
|
3324
3359
|
}
|
|
3325
3360
|
_buildOptions(name, node) {
|
|
@@ -3379,9 +3414,13 @@ class Grammar {
|
|
|
3379
3414
|
}
|
|
3380
3415
|
_saveSequence(statementNode) {
|
|
3381
3416
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3417
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3382
3418
|
const name = nameNode.value;
|
|
3383
3419
|
const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
|
|
3384
3420
|
const sequence = this._buildSequence(name, sequenceNode);
|
|
3421
|
+
if (shouldCompactAst != null) {
|
|
3422
|
+
sequence.shouldCompactAst = true;
|
|
3423
|
+
}
|
|
3385
3424
|
this._parseContext.patternsByName.set(name, sequence);
|
|
3386
3425
|
}
|
|
3387
3426
|
_buildSequence(name, node) {
|
|
@@ -3401,9 +3440,13 @@ class Grammar {
|
|
|
3401
3440
|
}
|
|
3402
3441
|
_saveRepeat(statementNode) {
|
|
3403
3442
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3443
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3404
3444
|
const name = nameNode.value;
|
|
3405
3445
|
const repeatNode = statementNode.find(n => n.name === "repeat-literal");
|
|
3406
3446
|
const repeat = this._buildRepeat(name, repeatNode);
|
|
3447
|
+
if (shouldCompactAst != null) {
|
|
3448
|
+
repeat.shouldCompactAst = true;
|
|
3449
|
+
}
|
|
3407
3450
|
this._parseContext.patternsByName.set(name, repeat);
|
|
3408
3451
|
}
|
|
3409
3452
|
_buildRepeat(name, repeatNode) {
|
|
@@ -3556,10 +3599,14 @@ class Grammar {
|
|
|
3556
3599
|
}
|
|
3557
3600
|
_saveAlias(statementNode) {
|
|
3558
3601
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
3602
|
+
const shouldCompactAst = statementNode.find(n => n.name === "compact");
|
|
3559
3603
|
const aliasNode = statementNode.find(n => n.name === "alias-literal");
|
|
3560
3604
|
const aliasName = aliasNode.value;
|
|
3561
3605
|
const name = nameNode.value;
|
|
3562
3606
|
const alias = this._getPattern(aliasName).clone(name);
|
|
3607
|
+
if (shouldCompactAst != null) {
|
|
3608
|
+
alias.shouldCompactAst = true;
|
|
3609
|
+
}
|
|
3563
3610
|
this._parseContext.patternsByName.set(name, alias);
|
|
3564
3611
|
}
|
|
3565
3612
|
static parse(expression, options) {
|