clarity-pattern-parser 11.3.13 → 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.
@@ -6,6 +6,7 @@ export interface GrammarFile {
6
6
  }
7
7
  export interface GrammarOptions {
8
8
  resolveImport?: (resource: string, originResource: string | null) => Promise<GrammarFile>;
9
+ resolveImportSync?: (resource: string, originResource: string | null) => GrammarFile;
9
10
  originResource?: string | null;
10
11
  params?: Pattern[];
11
12
  decorators?: Record<string, Decorator>;
@@ -14,6 +15,7 @@ export declare class Grammar {
14
15
  private _params;
15
16
  private _originResource?;
16
17
  private _resolveImport;
18
+ private _resolveImportSync;
17
19
  private _parseContext;
18
20
  constructor(options?: GrammarOptions);
19
21
  import(path: string): Promise<Record<string, Pattern>>;
@@ -42,8 +44,10 @@ export declare class Grammar {
42
44
  private _saveConfigurableAnonymous;
43
45
  private _buildComplexAnonymousPattern;
44
46
  private _resolveImports;
45
- private processImport;
46
- private processUseParams;
47
+ private _resolveImportsSync;
48
+ private _processImportSync;
49
+ private _processImport;
50
+ private _processUseParams;
47
51
  private _applyDecorators;
48
52
  private _getParams;
49
53
  private _getPattern;
@@ -3503,11 +3503,15 @@
3503
3503
  function defaultImportResolver(_path, _basePath) {
3504
3504
  throw new Error("No import resolver supplied.");
3505
3505
  }
3506
+ function defaultImportResolverSync(_path, _basePath) {
3507
+ throw new Error("No import resolver supplied.");
3508
+ }
3506
3509
  class Grammar {
3507
3510
  constructor(options = {}) {
3508
3511
  this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
3509
3512
  this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
3510
3513
  this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
3514
+ this._resolveImportSync = options.resolveImportSync == null ? defaultImportResolverSync : options.resolveImportSync;
3511
3515
  this._parseContext = new ParseContext(this._params, options.decorators || {});
3512
3516
  }
3513
3517
  import(path) {
@@ -3534,9 +3538,7 @@
3534
3538
  parseString(expression) {
3535
3539
  this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
3536
3540
  const ast = this._tryToParse(expression);
3537
- if (this._hasImports(ast)) {
3538
- throw new Error("Cannot use imports on parseString, use parse instead.");
3539
- }
3541
+ this._resolveImportsSync(ast);
3540
3542
  this._buildPatterns(ast);
3541
3543
  return this._buildPatternRecord();
3542
3544
  }
@@ -3843,15 +3845,80 @@
3843
3845
  });
3844
3846
  for (const statement of importStatements) {
3845
3847
  if (statement.name === "import-from") {
3846
- yield this.processImport(statement);
3848
+ yield this._processImport(statement);
3847
3849
  }
3848
3850
  else {
3849
- this.processUseParams(statement);
3851
+ this._processUseParams(statement);
3850
3852
  }
3851
3853
  }
3852
3854
  });
3853
3855
  }
3854
- processImport(importStatement) {
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) {
3855
3922
  return __awaiter(this, void 0, void 0, function* () {
3856
3923
  const parseContext = this._parseContext;
3857
3924
  const resourceNode = importStatement.find(n => n.name === "resource");
@@ -3904,7 +3971,7 @@
3904
3971
  }
3905
3972
  });
3906
3973
  }
3907
- processUseParams(paramName) {
3974
+ _processUseParams(paramName) {
3908
3975
  const defaultValueNode = paramName.find(n => n.name === "param-default");
3909
3976
  if (defaultValueNode === null) {
3910
3977
  return;