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