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