clarity-pattern-parser 11.1.4 → 11.1.5
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 +72 -0
- package/src/grammar/Grammar.ts +85 -48
- package/src/grammar/patterns/import.ts +13 -1
- package/src/grammar/patterns.test.ts +6 -6
- package/src/grammar/patterns.ts +1 -1
- package/src/patterns/Context.ts +7 -0
|
@@ -40,6 +40,8 @@ export declare class Grammar {
|
|
|
40
40
|
private _saveConfigurableAnonymous;
|
|
41
41
|
private _buildComplexAnonymousPattern;
|
|
42
42
|
private _resolveImports;
|
|
43
|
+
private processImport;
|
|
44
|
+
private processUseParams;
|
|
43
45
|
private _applyDecorators;
|
|
44
46
|
private _getParams;
|
|
45
47
|
private _getPattern;
|
package/dist/index.browser.js
CHANGED
|
@@ -2282,11 +2282,22 @@
|
|
|
2282
2282
|
const fromKeyword = new Literal("from", "from");
|
|
2283
2283
|
const openBracket = new Literal("open-bracket", "{");
|
|
2284
2284
|
const closeBracket = new Literal("close-bracket", "}");
|
|
2285
|
+
const equal = new Literal("equal", "=");
|
|
2285
2286
|
const importNameAlias = name.clone("import-name-alias");
|
|
2286
2287
|
const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
|
|
2287
2288
|
const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
|
|
2288
2289
|
const paramName = name.clone("param-name");
|
|
2289
|
-
const
|
|
2290
|
+
const defaultParamName = name.clone("default-param-name");
|
|
2291
|
+
const paramNameWithDefault = new Sequence("param-name-with-default-value", [
|
|
2292
|
+
paramName,
|
|
2293
|
+
new Optional("optional-param-default", new Sequence("param-default", [
|
|
2294
|
+
optionalLineSpaces$1,
|
|
2295
|
+
equal,
|
|
2296
|
+
optionalLineSpaces$1,
|
|
2297
|
+
defaultParamName,
|
|
2298
|
+
])),
|
|
2299
|
+
]);
|
|
2300
|
+
const paramNames = new Repeat("param-names", paramNameWithDefault, { divider: importNameDivider });
|
|
2290
2301
|
const resource = literal$1.clone("resource");
|
|
2291
2302
|
const useParams = new Sequence("import-params", [
|
|
2292
2303
|
useParamsKeyword,
|
|
@@ -2478,6 +2489,9 @@
|
|
|
2478
2489
|
return this.children[0].startedOnIndex;
|
|
2479
2490
|
}
|
|
2480
2491
|
getPatternWithinContext(name) {
|
|
2492
|
+
if (this._name === name || this._referencePatternName === name) {
|
|
2493
|
+
return this;
|
|
2494
|
+
}
|
|
2481
2495
|
return this._patterns[name] || null;
|
|
2482
2496
|
}
|
|
2483
2497
|
getPatternsWithinContext() {
|
|
@@ -2489,6 +2503,7 @@
|
|
|
2489
2503
|
this._name = name;
|
|
2490
2504
|
this._parent = null;
|
|
2491
2505
|
this._patterns = {};
|
|
2506
|
+
this._referencePatternName = name;
|
|
2492
2507
|
const clonedPattern = pattern.clone();
|
|
2493
2508
|
context.forEach(p => this._patterns[p.name] = p);
|
|
2494
2509
|
clonedPattern.parent = this;
|
|
@@ -2506,6 +2521,7 @@
|
|
|
2506
2521
|
}
|
|
2507
2522
|
clone(name = this._name) {
|
|
2508
2523
|
const clone = new Context(name, this._pattern.clone(name), Object.values(this._patterns));
|
|
2524
|
+
clone._referencePatternName = this._referencePatternName;
|
|
2509
2525
|
clone._id = this._id;
|
|
2510
2526
|
return clone;
|
|
2511
2527
|
}
|
|
@@ -3598,61 +3614,94 @@
|
|
|
3598
3614
|
return this._buildPattern(wrappedNode);
|
|
3599
3615
|
}
|
|
3600
3616
|
_resolveImports(ast) {
|
|
3617
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3618
|
+
const importStatements = ast.findAll(n => {
|
|
3619
|
+
return n.name === "import-from" || n.name === "param-name-with-default-value";
|
|
3620
|
+
});
|
|
3621
|
+
for (const statement of importStatements) {
|
|
3622
|
+
if (statement.name === "import-from") {
|
|
3623
|
+
yield this.processImport(statement);
|
|
3624
|
+
}
|
|
3625
|
+
else {
|
|
3626
|
+
this.processUseParams(statement);
|
|
3627
|
+
}
|
|
3628
|
+
}
|
|
3629
|
+
});
|
|
3630
|
+
}
|
|
3631
|
+
processImport(importStatement) {
|
|
3601
3632
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3602
3633
|
const parseContext = this._parseContext;
|
|
3603
|
-
const
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3634
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
3635
|
+
const params = this._getParams(importStatement);
|
|
3636
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
3637
|
+
const grammarFile = yield this._resolveImport(resource, this._originResource || null);
|
|
3638
|
+
const grammar = new Grammar({
|
|
3639
|
+
resolveImport: this._resolveImport,
|
|
3640
|
+
originResource: grammarFile.resource,
|
|
3641
|
+
params,
|
|
3642
|
+
decorators: this._parseContext.decorators
|
|
3643
|
+
});
|
|
3644
|
+
try {
|
|
3645
|
+
const patterns = yield grammar.parse(grammarFile.expression);
|
|
3646
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
3647
|
+
importStatements.forEach((node) => {
|
|
3648
|
+
var _a, _b;
|
|
3649
|
+
if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
|
|
3650
|
+
return;
|
|
3651
|
+
}
|
|
3652
|
+
if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
|
|
3653
|
+
const importName = node.value;
|
|
3654
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
3655
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
3622
3656
|
}
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
throw new Error(`'${importName}' was already used within another import.`);
|
|
3627
|
-
}
|
|
3628
|
-
const pattern = patterns[importName];
|
|
3629
|
-
if (pattern == null) {
|
|
3630
|
-
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3631
|
-
}
|
|
3632
|
-
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3657
|
+
const pattern = patterns[importName];
|
|
3658
|
+
if (pattern == null) {
|
|
3659
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3633
3660
|
}
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
if (pattern == null) {
|
|
3644
|
-
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3645
|
-
}
|
|
3646
|
-
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3661
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3662
|
+
}
|
|
3663
|
+
else {
|
|
3664
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
3665
|
+
const importName = importNameNode.value;
|
|
3666
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
3667
|
+
const alias = aliasNode.value;
|
|
3668
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
3669
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
3647
3670
|
}
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3671
|
+
const pattern = patterns[importName];
|
|
3672
|
+
if (pattern == null) {
|
|
3673
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3674
|
+
}
|
|
3675
|
+
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3676
|
+
}
|
|
3677
|
+
});
|
|
3678
|
+
}
|
|
3679
|
+
catch (e) {
|
|
3680
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
3653
3681
|
}
|
|
3654
3682
|
});
|
|
3655
3683
|
}
|
|
3684
|
+
processUseParams(paramName) {
|
|
3685
|
+
const defaultValueNode = paramName.find(n => n.name === "param-default");
|
|
3686
|
+
if (defaultValueNode === null) {
|
|
3687
|
+
return;
|
|
3688
|
+
}
|
|
3689
|
+
const nameNode = paramName.find(n => n.name === "param-name");
|
|
3690
|
+
const defaultNameNode = defaultValueNode.find(n => n.name === "default-param-name");
|
|
3691
|
+
if (nameNode == null || defaultNameNode == null) {
|
|
3692
|
+
return;
|
|
3693
|
+
}
|
|
3694
|
+
const name = nameNode.value;
|
|
3695
|
+
const defaultName = defaultNameNode.value;
|
|
3696
|
+
if (this._parseContext.paramsByName.has(name)) {
|
|
3697
|
+
return;
|
|
3698
|
+
}
|
|
3699
|
+
let pattern = this._parseContext.importedPatternsByName.get(defaultName);
|
|
3700
|
+
if (pattern == null) {
|
|
3701
|
+
pattern = new Reference(defaultName);
|
|
3702
|
+
}
|
|
3703
|
+
this._parseContext.importedPatternsByName.set(name, pattern);
|
|
3704
|
+
}
|
|
3656
3705
|
_applyDecorators(statementNode, pattern) {
|
|
3657
3706
|
const decorators = this._parseContext.decorators;
|
|
3658
3707
|
const bodyLine = statementNode.parent;
|
|
@@ -3737,7 +3786,7 @@
|
|
|
3737
3786
|
const aliasPattern = this._getPattern(aliasName);
|
|
3738
3787
|
// This solves the problem for an alias pointing to a reference.
|
|
3739
3788
|
if (aliasPattern.type === "reference") {
|
|
3740
|
-
const reference =
|
|
3789
|
+
const reference = aliasPattern.clone(name);
|
|
3741
3790
|
this._applyDecorators(statementNode, reference);
|
|
3742
3791
|
this._parseContext.patternsByName.set(name, reference);
|
|
3743
3792
|
}
|