clarity-pattern-parser 11.1.4 → 11.2.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/grammar/Grammar.d.ts +2 -0
- package/dist/index.browser.js +98 -49
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +98 -49
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +98 -49
- package/dist/index.js.map +1 -1
- package/dist/patterns/Context.d.ts +1 -0
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +82 -0
- package/src/grammar/Grammar.ts +113 -48
- package/src/grammar/patterns/import.ts +13 -1
- package/src/grammar/patterns/literals.ts +1 -0
- package/src/grammar/patterns/pattern.ts +3 -1
- package/src/grammar/patterns/takeUtilLiteral.ts +21 -0
- package/src/grammar/patterns.test.ts +6 -6
- package/src/grammar/patterns.ts +1 -1
- package/src/patterns/Context.ts +7 -0
- package/src/patterns/TakeUntil.test.ts +62 -0
- package/src/patterns/TakeUntil.ts +166 -0
package/dist/index.esm.js
CHANGED
|
@@ -2276,11 +2276,22 @@ const asKeyword = new Literal("as", "as");
|
|
|
2276
2276
|
const fromKeyword = new Literal("from", "from");
|
|
2277
2277
|
const openBracket = new Literal("open-bracket", "{");
|
|
2278
2278
|
const closeBracket = new Literal("close-bracket", "}");
|
|
2279
|
+
const equal = new Literal("equal", "=");
|
|
2279
2280
|
const importNameAlias = name.clone("import-name-alias");
|
|
2280
2281
|
const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
|
|
2281
2282
|
const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
|
|
2282
2283
|
const paramName = name.clone("param-name");
|
|
2283
|
-
const
|
|
2284
|
+
const defaultParamName = name.clone("default-param-name");
|
|
2285
|
+
const paramNameWithDefault = new Sequence("param-name-with-default-value", [
|
|
2286
|
+
paramName,
|
|
2287
|
+
new Optional("optional-param-default", new Sequence("param-default", [
|
|
2288
|
+
optionalLineSpaces$1,
|
|
2289
|
+
equal,
|
|
2290
|
+
optionalLineSpaces$1,
|
|
2291
|
+
defaultParamName,
|
|
2292
|
+
])),
|
|
2293
|
+
]);
|
|
2294
|
+
const paramNames = new Repeat("param-names", paramNameWithDefault, { divider: importNameDivider });
|
|
2284
2295
|
const resource = literal$1.clone("resource");
|
|
2285
2296
|
const useParams = new Sequence("import-params", [
|
|
2286
2297
|
useParamsKeyword,
|
|
@@ -2472,6 +2483,9 @@ class Context {
|
|
|
2472
2483
|
return this.children[0].startedOnIndex;
|
|
2473
2484
|
}
|
|
2474
2485
|
getPatternWithinContext(name) {
|
|
2486
|
+
if (this._name === name || this._referencePatternName === name) {
|
|
2487
|
+
return this;
|
|
2488
|
+
}
|
|
2475
2489
|
return this._patterns[name] || null;
|
|
2476
2490
|
}
|
|
2477
2491
|
getPatternsWithinContext() {
|
|
@@ -2483,6 +2497,7 @@ class Context {
|
|
|
2483
2497
|
this._name = name;
|
|
2484
2498
|
this._parent = null;
|
|
2485
2499
|
this._patterns = {};
|
|
2500
|
+
this._referencePatternName = name;
|
|
2486
2501
|
const clonedPattern = pattern.clone();
|
|
2487
2502
|
context.forEach(p => this._patterns[p.name] = p);
|
|
2488
2503
|
clonedPattern.parent = this;
|
|
@@ -2500,6 +2515,7 @@ class Context {
|
|
|
2500
2515
|
}
|
|
2501
2516
|
clone(name = this._name) {
|
|
2502
2517
|
const clone = new Context(name, this._pattern.clone(name), Object.values(this._patterns));
|
|
2518
|
+
clone._referencePatternName = this._referencePatternName;
|
|
2503
2519
|
clone._id = this._id;
|
|
2504
2520
|
return clone;
|
|
2505
2521
|
}
|
|
@@ -3592,61 +3608,94 @@ class Grammar {
|
|
|
3592
3608
|
return this._buildPattern(wrappedNode);
|
|
3593
3609
|
}
|
|
3594
3610
|
_resolveImports(ast) {
|
|
3611
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3612
|
+
const importStatements = ast.findAll(n => {
|
|
3613
|
+
return n.name === "import-from" || n.name === "param-name-with-default-value";
|
|
3614
|
+
});
|
|
3615
|
+
for (const statement of importStatements) {
|
|
3616
|
+
if (statement.name === "import-from") {
|
|
3617
|
+
yield this.processImport(statement);
|
|
3618
|
+
}
|
|
3619
|
+
else {
|
|
3620
|
+
this.processUseParams(statement);
|
|
3621
|
+
}
|
|
3622
|
+
}
|
|
3623
|
+
});
|
|
3624
|
+
}
|
|
3625
|
+
processImport(importStatement) {
|
|
3595
3626
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3596
3627
|
const parseContext = this._parseContext;
|
|
3597
|
-
const
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3628
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
3629
|
+
const params = this._getParams(importStatement);
|
|
3630
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
3631
|
+
const grammarFile = yield this._resolveImport(resource, this._originResource || null);
|
|
3632
|
+
const grammar = new Grammar({
|
|
3633
|
+
resolveImport: this._resolveImport,
|
|
3634
|
+
originResource: grammarFile.resource,
|
|
3635
|
+
params,
|
|
3636
|
+
decorators: this._parseContext.decorators
|
|
3637
|
+
});
|
|
3638
|
+
try {
|
|
3639
|
+
const patterns = yield grammar.parse(grammarFile.expression);
|
|
3640
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
3641
|
+
importStatements.forEach((node) => {
|
|
3642
|
+
var _a, _b;
|
|
3643
|
+
if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
|
|
3644
|
+
return;
|
|
3645
|
+
}
|
|
3646
|
+
if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
|
|
3647
|
+
const importName = node.value;
|
|
3648
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
3649
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
3616
3650
|
}
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
throw new Error(`'${importName}' was already used within another import.`);
|
|
3621
|
-
}
|
|
3622
|
-
const pattern = patterns[importName];
|
|
3623
|
-
if (pattern == null) {
|
|
3624
|
-
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3625
|
-
}
|
|
3626
|
-
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3651
|
+
const pattern = patterns[importName];
|
|
3652
|
+
if (pattern == null) {
|
|
3653
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3627
3654
|
}
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
if (pattern == null) {
|
|
3638
|
-
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3639
|
-
}
|
|
3640
|
-
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3655
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3656
|
+
}
|
|
3657
|
+
else {
|
|
3658
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
3659
|
+
const importName = importNameNode.value;
|
|
3660
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
3661
|
+
const alias = aliasNode.value;
|
|
3662
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
3663
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
3641
3664
|
}
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3665
|
+
const pattern = patterns[importName];
|
|
3666
|
+
if (pattern == null) {
|
|
3667
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3668
|
+
}
|
|
3669
|
+
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3670
|
+
}
|
|
3671
|
+
});
|
|
3672
|
+
}
|
|
3673
|
+
catch (e) {
|
|
3674
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
3647
3675
|
}
|
|
3648
3676
|
});
|
|
3649
3677
|
}
|
|
3678
|
+
processUseParams(paramName) {
|
|
3679
|
+
const defaultValueNode = paramName.find(n => n.name === "param-default");
|
|
3680
|
+
if (defaultValueNode === null) {
|
|
3681
|
+
return;
|
|
3682
|
+
}
|
|
3683
|
+
const nameNode = paramName.find(n => n.name === "param-name");
|
|
3684
|
+
const defaultNameNode = defaultValueNode.find(n => n.name === "default-param-name");
|
|
3685
|
+
if (nameNode == null || defaultNameNode == null) {
|
|
3686
|
+
return;
|
|
3687
|
+
}
|
|
3688
|
+
const name = nameNode.value;
|
|
3689
|
+
const defaultName = defaultNameNode.value;
|
|
3690
|
+
if (this._parseContext.paramsByName.has(name)) {
|
|
3691
|
+
return;
|
|
3692
|
+
}
|
|
3693
|
+
let pattern = this._parseContext.importedPatternsByName.get(defaultName);
|
|
3694
|
+
if (pattern == null) {
|
|
3695
|
+
pattern = new Reference(defaultName);
|
|
3696
|
+
}
|
|
3697
|
+
this._parseContext.importedPatternsByName.set(name, pattern);
|
|
3698
|
+
}
|
|
3650
3699
|
_applyDecorators(statementNode, pattern) {
|
|
3651
3700
|
const decorators = this._parseContext.decorators;
|
|
3652
3701
|
const bodyLine = statementNode.parent;
|
|
@@ -3731,7 +3780,7 @@ class Grammar {
|
|
|
3731
3780
|
const aliasPattern = this._getPattern(aliasName);
|
|
3732
3781
|
// This solves the problem for an alias pointing to a reference.
|
|
3733
3782
|
if (aliasPattern.type === "reference") {
|
|
3734
|
-
const reference =
|
|
3783
|
+
const reference = aliasPattern.clone(name);
|
|
3735
3784
|
this._applyDecorators(statementNode, reference);
|
|
3736
3785
|
this._parseContext.patternsByName.set(name, reference);
|
|
3737
3786
|
}
|