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.js
CHANGED
|
@@ -433,7 +433,7 @@ class CursorHistory {
|
|
|
433
433
|
|
|
434
434
|
class Cursor {
|
|
435
435
|
get text() {
|
|
436
|
-
return this.
|
|
436
|
+
return this._chars.join("");
|
|
437
437
|
}
|
|
438
438
|
get isOnFirst() {
|
|
439
439
|
return this._index === 0;
|
|
@@ -481,12 +481,12 @@ class Cursor {
|
|
|
481
481
|
return this._history.error != null;
|
|
482
482
|
}
|
|
483
483
|
get currentChar() {
|
|
484
|
-
return this.
|
|
484
|
+
return this._chars[this._index];
|
|
485
485
|
}
|
|
486
486
|
constructor(text) {
|
|
487
|
-
this.
|
|
487
|
+
this._chars = [...text];
|
|
488
488
|
this._index = 0;
|
|
489
|
-
this._length =
|
|
489
|
+
this._length = this._chars.length;
|
|
490
490
|
this._history = new CursorHistory();
|
|
491
491
|
}
|
|
492
492
|
hasNext() {
|
|
@@ -520,7 +520,7 @@ class Cursor {
|
|
|
520
520
|
return this._length - 1;
|
|
521
521
|
}
|
|
522
522
|
getChars(first, last) {
|
|
523
|
-
return this.
|
|
523
|
+
return this._chars.slice(first, last + 1).join("");
|
|
524
524
|
}
|
|
525
525
|
recordMatch(pattern, node) {
|
|
526
526
|
this._history.recordMatch(pattern, node);
|
|
@@ -769,7 +769,9 @@ class Regex {
|
|
|
769
769
|
}
|
|
770
770
|
processResult(cursor, result) {
|
|
771
771
|
const currentIndex = cursor.index;
|
|
772
|
-
const
|
|
772
|
+
const match = result[0];
|
|
773
|
+
const matchLength = [...match].length;
|
|
774
|
+
const newIndex = currentIndex + matchLength - 1;
|
|
773
775
|
this._node = new Node("regex", this._name, currentIndex, newIndex, undefined, result[0]);
|
|
774
776
|
cursor.moveTo(newIndex);
|
|
775
777
|
cursor.recordMatch(this, this._node);
|
|
@@ -3501,11 +3503,15 @@ class ParseContext {
|
|
|
3501
3503
|
function defaultImportResolver(_path, _basePath) {
|
|
3502
3504
|
throw new Error("No import resolver supplied.");
|
|
3503
3505
|
}
|
|
3506
|
+
function defaultImportResolverSync(_path, _basePath) {
|
|
3507
|
+
throw new Error("No import resolver supplied.");
|
|
3508
|
+
}
|
|
3504
3509
|
class Grammar {
|
|
3505
3510
|
constructor(options = {}) {
|
|
3506
3511
|
this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
|
|
3507
3512
|
this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
|
|
3508
3513
|
this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
|
|
3514
|
+
this._resolveImportSync = options.resolveImportSync == null ? defaultImportResolverSync : options.resolveImportSync;
|
|
3509
3515
|
this._parseContext = new ParseContext(this._params, options.decorators || {});
|
|
3510
3516
|
}
|
|
3511
3517
|
import(path) {
|
|
@@ -3532,9 +3538,7 @@ class Grammar {
|
|
|
3532
3538
|
parseString(expression) {
|
|
3533
3539
|
this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
|
|
3534
3540
|
const ast = this._tryToParse(expression);
|
|
3535
|
-
|
|
3536
|
-
throw new Error("Cannot use imports on parseString, use parse instead.");
|
|
3537
|
-
}
|
|
3541
|
+
this._resolveImportsSync(ast);
|
|
3538
3542
|
this._buildPatterns(ast);
|
|
3539
3543
|
return this._buildPatternRecord();
|
|
3540
3544
|
}
|
|
@@ -3841,15 +3845,80 @@ class Grammar {
|
|
|
3841
3845
|
});
|
|
3842
3846
|
for (const statement of importStatements) {
|
|
3843
3847
|
if (statement.name === "import-from") {
|
|
3844
|
-
yield this.
|
|
3848
|
+
yield this._processImport(statement);
|
|
3845
3849
|
}
|
|
3846
3850
|
else {
|
|
3847
|
-
this.
|
|
3851
|
+
this._processUseParams(statement);
|
|
3848
3852
|
}
|
|
3849
3853
|
}
|
|
3850
3854
|
});
|
|
3851
3855
|
}
|
|
3852
|
-
|
|
3856
|
+
_resolveImportsSync(ast) {
|
|
3857
|
+
const importStatements = ast.findAll(n => {
|
|
3858
|
+
return n.name === "import-from" || n.name === "param-name-with-default-value";
|
|
3859
|
+
});
|
|
3860
|
+
for (const statement of importStatements) {
|
|
3861
|
+
if (statement.name === "import-from") {
|
|
3862
|
+
this._processImportSync(statement);
|
|
3863
|
+
}
|
|
3864
|
+
else {
|
|
3865
|
+
this._processUseParams(statement);
|
|
3866
|
+
}
|
|
3867
|
+
}
|
|
3868
|
+
}
|
|
3869
|
+
_processImportSync(importStatement) {
|
|
3870
|
+
const parseContext = this._parseContext;
|
|
3871
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
3872
|
+
const params = this._getParams(importStatement);
|
|
3873
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
3874
|
+
const grammarFile = this._resolveImportSync(resource, this._originResource || null);
|
|
3875
|
+
const grammar = new Grammar({
|
|
3876
|
+
resolveImport: this._resolveImport,
|
|
3877
|
+
resolveImportSync: this._resolveImportSync,
|
|
3878
|
+
originResource: grammarFile.resource,
|
|
3879
|
+
params,
|
|
3880
|
+
decorators: this._parseContext.decorators
|
|
3881
|
+
});
|
|
3882
|
+
try {
|
|
3883
|
+
const patterns = grammar.parseString(grammarFile.expression);
|
|
3884
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
3885
|
+
importStatements.forEach((node) => {
|
|
3886
|
+
var _a, _b;
|
|
3887
|
+
if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
|
|
3888
|
+
return;
|
|
3889
|
+
}
|
|
3890
|
+
if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
|
|
3891
|
+
const importName = node.value;
|
|
3892
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
3893
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
3894
|
+
}
|
|
3895
|
+
const pattern = patterns[importName];
|
|
3896
|
+
if (pattern == null) {
|
|
3897
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3898
|
+
}
|
|
3899
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3900
|
+
}
|
|
3901
|
+
else {
|
|
3902
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
3903
|
+
const importName = importNameNode.value;
|
|
3904
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
3905
|
+
const alias = aliasNode.value;
|
|
3906
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
3907
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
3908
|
+
}
|
|
3909
|
+
const pattern = patterns[importName];
|
|
3910
|
+
if (pattern == null) {
|
|
3911
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3912
|
+
}
|
|
3913
|
+
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3914
|
+
}
|
|
3915
|
+
});
|
|
3916
|
+
}
|
|
3917
|
+
catch (e) {
|
|
3918
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
3919
|
+
}
|
|
3920
|
+
}
|
|
3921
|
+
_processImport(importStatement) {
|
|
3853
3922
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3854
3923
|
const parseContext = this._parseContext;
|
|
3855
3924
|
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
@@ -3902,7 +3971,7 @@ class Grammar {
|
|
|
3902
3971
|
}
|
|
3903
3972
|
});
|
|
3904
3973
|
}
|
|
3905
|
-
|
|
3974
|
+
_processUseParams(paramName) {
|
|
3906
3975
|
const defaultValueNode = paramName.find(n => n.name === "param-default");
|
|
3907
3976
|
if (defaultValueNode === null) {
|
|
3908
3977
|
return;
|