clarity-pattern-parser 11.3.13 → 11.4.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/grammar/Grammar.d.ts +6 -2
- package/dist/grammar_v2/patterns/Grammar.d.ts +23 -0
- package/dist/grammar_v2/patterns/patterns/grammar.d.ts +87 -0
- package/dist/grammar_v2/patterns/patterns/grammar.test.d.ts +1 -0
- package/dist/index.browser.js +82 -13
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +82 -13
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +82 -13
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +1 -1
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +38 -0
- package/src/grammar/Grammar.ts +87 -8
- package/src/grammar_v2/patterns/Grammar.ts +170 -0
- package/src/grammar_v2/patterns/patterns/cpat.cpat +91 -0
- package/src/grammar_v2/patterns/patterns/grammar.test.ts +218 -0
- package/src/grammar_v2/patterns/patterns/grammar.ts +103 -0
- package/src/patterns/Cursor.ts +6 -6
- package/src/patterns/Regex.ts +4 -2
- package/src/patterns/Sequence.test.ts +11 -0
package/dist/index.esm.js
CHANGED
|
@@ -429,7 +429,7 @@ class CursorHistory {
|
|
|
429
429
|
|
|
430
430
|
class Cursor {
|
|
431
431
|
get text() {
|
|
432
|
-
return this.
|
|
432
|
+
return this._chars.join("");
|
|
433
433
|
}
|
|
434
434
|
get isOnFirst() {
|
|
435
435
|
return this._index === 0;
|
|
@@ -477,12 +477,12 @@ class Cursor {
|
|
|
477
477
|
return this._history.error != null;
|
|
478
478
|
}
|
|
479
479
|
get currentChar() {
|
|
480
|
-
return this.
|
|
480
|
+
return this._chars[this._index];
|
|
481
481
|
}
|
|
482
482
|
constructor(text) {
|
|
483
|
-
this.
|
|
483
|
+
this._chars = [...text];
|
|
484
484
|
this._index = 0;
|
|
485
|
-
this._length =
|
|
485
|
+
this._length = this._chars.length;
|
|
486
486
|
this._history = new CursorHistory();
|
|
487
487
|
}
|
|
488
488
|
hasNext() {
|
|
@@ -516,7 +516,7 @@ class Cursor {
|
|
|
516
516
|
return this._length - 1;
|
|
517
517
|
}
|
|
518
518
|
getChars(first, last) {
|
|
519
|
-
return this.
|
|
519
|
+
return this._chars.slice(first, last + 1).join("");
|
|
520
520
|
}
|
|
521
521
|
recordMatch(pattern, node) {
|
|
522
522
|
this._history.recordMatch(pattern, node);
|
|
@@ -765,7 +765,9 @@ class Regex {
|
|
|
765
765
|
}
|
|
766
766
|
processResult(cursor, result) {
|
|
767
767
|
const currentIndex = cursor.index;
|
|
768
|
-
const
|
|
768
|
+
const match = result[0];
|
|
769
|
+
const matchLength = [...match].length;
|
|
770
|
+
const newIndex = currentIndex + matchLength - 1;
|
|
769
771
|
this._node = new Node("regex", this._name, currentIndex, newIndex, undefined, result[0]);
|
|
770
772
|
cursor.moveTo(newIndex);
|
|
771
773
|
cursor.recordMatch(this, this._node);
|
|
@@ -3497,11 +3499,15 @@ class ParseContext {
|
|
|
3497
3499
|
function defaultImportResolver(_path, _basePath) {
|
|
3498
3500
|
throw new Error("No import resolver supplied.");
|
|
3499
3501
|
}
|
|
3502
|
+
function defaultImportResolverSync(_path, _basePath) {
|
|
3503
|
+
throw new Error("No import resolver supplied.");
|
|
3504
|
+
}
|
|
3500
3505
|
class Grammar {
|
|
3501
3506
|
constructor(options = {}) {
|
|
3502
3507
|
this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
|
|
3503
3508
|
this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
|
|
3504
3509
|
this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
|
|
3510
|
+
this._resolveImportSync = options.resolveImportSync == null ? defaultImportResolverSync : options.resolveImportSync;
|
|
3505
3511
|
this._parseContext = new ParseContext(this._params, options.decorators || {});
|
|
3506
3512
|
}
|
|
3507
3513
|
import(path) {
|
|
@@ -3528,9 +3534,7 @@ class Grammar {
|
|
|
3528
3534
|
parseString(expression) {
|
|
3529
3535
|
this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
|
|
3530
3536
|
const ast = this._tryToParse(expression);
|
|
3531
|
-
|
|
3532
|
-
throw new Error("Cannot use imports on parseString, use parse instead.");
|
|
3533
|
-
}
|
|
3537
|
+
this._resolveImportsSync(ast);
|
|
3534
3538
|
this._buildPatterns(ast);
|
|
3535
3539
|
return this._buildPatternRecord();
|
|
3536
3540
|
}
|
|
@@ -3837,15 +3841,80 @@ class Grammar {
|
|
|
3837
3841
|
});
|
|
3838
3842
|
for (const statement of importStatements) {
|
|
3839
3843
|
if (statement.name === "import-from") {
|
|
3840
|
-
yield this.
|
|
3844
|
+
yield this._processImport(statement);
|
|
3841
3845
|
}
|
|
3842
3846
|
else {
|
|
3843
|
-
this.
|
|
3847
|
+
this._processUseParams(statement);
|
|
3844
3848
|
}
|
|
3845
3849
|
}
|
|
3846
3850
|
});
|
|
3847
3851
|
}
|
|
3848
|
-
|
|
3852
|
+
_resolveImportsSync(ast) {
|
|
3853
|
+
const importStatements = ast.findAll(n => {
|
|
3854
|
+
return n.name === "import-from" || n.name === "param-name-with-default-value";
|
|
3855
|
+
});
|
|
3856
|
+
for (const statement of importStatements) {
|
|
3857
|
+
if (statement.name === "import-from") {
|
|
3858
|
+
this._processImportSync(statement);
|
|
3859
|
+
}
|
|
3860
|
+
else {
|
|
3861
|
+
this._processUseParams(statement);
|
|
3862
|
+
}
|
|
3863
|
+
}
|
|
3864
|
+
}
|
|
3865
|
+
_processImportSync(importStatement) {
|
|
3866
|
+
const parseContext = this._parseContext;
|
|
3867
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
3868
|
+
const params = this._getParams(importStatement);
|
|
3869
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
3870
|
+
const grammarFile = this._resolveImportSync(resource, this._originResource || null);
|
|
3871
|
+
const grammar = new Grammar({
|
|
3872
|
+
resolveImport: this._resolveImport,
|
|
3873
|
+
resolveImportSync: this._resolveImportSync,
|
|
3874
|
+
originResource: grammarFile.resource,
|
|
3875
|
+
params,
|
|
3876
|
+
decorators: this._parseContext.decorators
|
|
3877
|
+
});
|
|
3878
|
+
try {
|
|
3879
|
+
const patterns = grammar.parseString(grammarFile.expression);
|
|
3880
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
3881
|
+
importStatements.forEach((node) => {
|
|
3882
|
+
var _a, _b;
|
|
3883
|
+
if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
|
|
3884
|
+
return;
|
|
3885
|
+
}
|
|
3886
|
+
if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
|
|
3887
|
+
const importName = node.value;
|
|
3888
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
3889
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
3890
|
+
}
|
|
3891
|
+
const pattern = patterns[importName];
|
|
3892
|
+
if (pattern == null) {
|
|
3893
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3894
|
+
}
|
|
3895
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3896
|
+
}
|
|
3897
|
+
else {
|
|
3898
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
3899
|
+
const importName = importNameNode.value;
|
|
3900
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
3901
|
+
const alias = aliasNode.value;
|
|
3902
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
3903
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
3904
|
+
}
|
|
3905
|
+
const pattern = patterns[importName];
|
|
3906
|
+
if (pattern == null) {
|
|
3907
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3908
|
+
}
|
|
3909
|
+
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3910
|
+
}
|
|
3911
|
+
});
|
|
3912
|
+
}
|
|
3913
|
+
catch (e) {
|
|
3914
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
3915
|
+
}
|
|
3916
|
+
}
|
|
3917
|
+
_processImport(importStatement) {
|
|
3849
3918
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3850
3919
|
const parseContext = this._parseContext;
|
|
3851
3920
|
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
@@ -3898,7 +3967,7 @@ class Grammar {
|
|
|
3898
3967
|
}
|
|
3899
3968
|
});
|
|
3900
3969
|
}
|
|
3901
|
-
|
|
3970
|
+
_processUseParams(paramName) {
|
|
3902
3971
|
const defaultValueNode = paramName.find(n => n.name === "param-default");
|
|
3903
3972
|
if (defaultValueNode === null) {
|
|
3904
3973
|
return;
|