clarity-pattern-parser 11.3.12 → 11.4.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/index.esm.js CHANGED
@@ -1653,11 +1653,11 @@ class Repeat {
1653
1653
  parse(cursor) {
1654
1654
  return this._repeatPattern.parse(cursor);
1655
1655
  }
1656
- exec(text) {
1657
- return this._repeatPattern.exec(text);
1656
+ exec(text, record = false) {
1657
+ return this._repeatPattern.exec(text, record);
1658
1658
  }
1659
- test(text) {
1660
- return this._repeatPattern.test(text);
1659
+ test(text, record = false) {
1660
+ return this._repeatPattern.test(text, record);
1661
1661
  }
1662
1662
  clone(name = this.name) {
1663
1663
  let min = this._options.min;
@@ -3497,11 +3497,15 @@ class ParseContext {
3497
3497
  function defaultImportResolver(_path, _basePath) {
3498
3498
  throw new Error("No import resolver supplied.");
3499
3499
  }
3500
+ function defaultImportResolverSync(_path, _basePath) {
3501
+ throw new Error("No import resolver supplied.");
3502
+ }
3500
3503
  class Grammar {
3501
3504
  constructor(options = {}) {
3502
3505
  this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
3503
3506
  this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
3504
3507
  this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
3508
+ this._resolveImportSync = options.resolveImportSync == null ? defaultImportResolverSync : options.resolveImportSync;
3505
3509
  this._parseContext = new ParseContext(this._params, options.decorators || {});
3506
3510
  }
3507
3511
  import(path) {
@@ -3528,9 +3532,7 @@ class Grammar {
3528
3532
  parseString(expression) {
3529
3533
  this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
3530
3534
  const ast = this._tryToParse(expression);
3531
- if (this._hasImports(ast)) {
3532
- throw new Error("Cannot use imports on parseString, use parse instead.");
3533
- }
3535
+ this._resolveImportsSync(ast);
3534
3536
  this._buildPatterns(ast);
3535
3537
  return this._buildPatternRecord();
3536
3538
  }
@@ -3837,15 +3839,80 @@ class Grammar {
3837
3839
  });
3838
3840
  for (const statement of importStatements) {
3839
3841
  if (statement.name === "import-from") {
3840
- yield this.processImport(statement);
3842
+ yield this._processImport(statement);
3841
3843
  }
3842
3844
  else {
3843
- this.processUseParams(statement);
3845
+ this._processUseParams(statement);
3844
3846
  }
3845
3847
  }
3846
3848
  });
3847
3849
  }
3848
- processImport(importStatement) {
3850
+ _resolveImportsSync(ast) {
3851
+ const importStatements = ast.findAll(n => {
3852
+ return n.name === "import-from" || n.name === "param-name-with-default-value";
3853
+ });
3854
+ for (const statement of importStatements) {
3855
+ if (statement.name === "import-from") {
3856
+ this._processImportSync(statement);
3857
+ }
3858
+ else {
3859
+ this._processUseParams(statement);
3860
+ }
3861
+ }
3862
+ }
3863
+ _processImportSync(importStatement) {
3864
+ const parseContext = this._parseContext;
3865
+ const resourceNode = importStatement.find(n => n.name === "resource");
3866
+ const params = this._getParams(importStatement);
3867
+ const resource = resourceNode.value.slice(1, -1);
3868
+ const grammarFile = this._resolveImportSync(resource, this._originResource || null);
3869
+ const grammar = new Grammar({
3870
+ resolveImport: this._resolveImport,
3871
+ resolveImportSync: this._resolveImportSync,
3872
+ originResource: grammarFile.resource,
3873
+ params,
3874
+ decorators: this._parseContext.decorators
3875
+ });
3876
+ try {
3877
+ const patterns = grammar.parseString(grammarFile.expression);
3878
+ const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
3879
+ importStatements.forEach((node) => {
3880
+ var _a, _b;
3881
+ if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
3882
+ return;
3883
+ }
3884
+ if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
3885
+ const importName = node.value;
3886
+ if (parseContext.importedPatternsByName.has(importName)) {
3887
+ throw new Error(`'${importName}' was already used within another import.`);
3888
+ }
3889
+ const pattern = patterns[importName];
3890
+ if (pattern == null) {
3891
+ throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
3892
+ }
3893
+ parseContext.importedPatternsByName.set(importName, pattern);
3894
+ }
3895
+ else {
3896
+ const importNameNode = node.find(n => n.name === "import-name");
3897
+ const importName = importNameNode.value;
3898
+ const aliasNode = node.find(n => n.name === "import-name-alias");
3899
+ const alias = aliasNode.value;
3900
+ if (parseContext.importedPatternsByName.has(alias)) {
3901
+ throw new Error(`'${alias}' was already used within another import.`);
3902
+ }
3903
+ const pattern = patterns[importName];
3904
+ if (pattern == null) {
3905
+ throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
3906
+ }
3907
+ parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
3908
+ }
3909
+ });
3910
+ }
3911
+ catch (e) {
3912
+ throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
3913
+ }
3914
+ }
3915
+ _processImport(importStatement) {
3849
3916
  return __awaiter(this, void 0, void 0, function* () {
3850
3917
  const parseContext = this._parseContext;
3851
3918
  const resourceNode = importStatement.find(n => n.name === "resource");
@@ -3898,7 +3965,7 @@ class Grammar {
3898
3965
  }
3899
3966
  });
3900
3967
  }
3901
- processUseParams(paramName) {
3968
+ _processUseParams(paramName) {
3902
3969
  const defaultValueNode = paramName.find(n => n.name === "param-default");
3903
3970
  if (defaultValueNode === null) {
3904
3971
  return;