@dbml/parse 8.0.0-alpha.9 → 8.0.0-prerelease.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.
@@ -1010,6 +1010,10 @@ function baseUniq(r, j, M) {
1010
1010
  return L;
1011
1011
  }
1012
1012
  var _baseUniq_default = baseUniq;
1013
+ function uniq(r) {
1014
+ return r && r.length ? _baseUniq_default(r) : [];
1015
+ }
1016
+ var uniq_default = uniq;
1013
1017
  function uniqBy(r, j) {
1014
1018
  return r && r.length ? _baseUniq_default(r, _baseIteratee_default(j, 2)) : [];
1015
1019
  }
@@ -1231,7 +1235,7 @@ var isAbsolute = function(r) {
1231
1235
  }
1232
1236
  relativeTo(r) {
1233
1237
  let j = relative$1(r, this.path);
1234
- return j.startsWith(".") ? j : `./${j}`;
1238
+ return !j.startsWith("./") && !j.startsWith("../") && ![".", ".."].includes(j) ? `./${j}` : j;
1235
1239
  }
1236
1240
  toString() {
1237
1241
  return this.path;
@@ -1310,6 +1314,9 @@ function isValidAlias(r) {
1310
1314
  function isSimpleName(r) {
1311
1315
  return r instanceof PrimaryExpressionNode && r.expression instanceof VariableNode;
1312
1316
  }
1317
+ function hasSimpleBody(r) {
1318
+ return !!r.bodyColon;
1319
+ }
1313
1320
  function isValidPartialInjection(r) {
1314
1321
  return r instanceof PrefixExpressionNode && r.op?.value === "~" && isExpressionAVariableNode(r.expression);
1315
1322
  }
@@ -2704,7 +2711,7 @@ function markInvalidNode(r) {
2704
2711
  else if (r instanceof ProgramNode) r.body.forEach(markInvalid), markInvalid(r.eof);
2705
2712
  else if (!(r instanceof EmptyNode)) if (r instanceof UseDeclarationNode) markInvalid(r.useKeyword), markInvalid(r.specifiers), markInvalid(r.fromKeyword), markInvalid(r.importPath);
2706
2713
  else if (r instanceof UseSpecifierListNode) markInvalid(r.openBrace), r.specifiers.forEach(markInvalid), markInvalid(r.closeBrace);
2707
- else if (r instanceof UseSpecifierNode) markInvalid(r.importKind), markInvalid(r.name);
2714
+ else if (r instanceof UseSpecifierNode) markInvalid(r.importKind), markInvalid(r.name), markInvalid(r.asKeyword), markInvalid(r.alias);
2708
2715
  else if (r instanceof WildcardNode) markInvalid(r.token);
2709
2716
  else throw Error("Unreachable case in markInvalidNode");
2710
2717
  }
@@ -2733,7 +2740,7 @@ function getMemberChain(r) {
2733
2740
  if (r instanceof EmptyNode) return [];
2734
2741
  if (r instanceof UseDeclarationNode) return filterUndefined(r.useKeyword, r.specifiers, r.fromKeyword, r.importPath);
2735
2742
  if (r instanceof UseSpecifierListNode) return filterUndefined(r.openBrace, ...r.specifiers, r.closeBrace);
2736
- if (r instanceof UseSpecifierNode) return filterUndefined(r.importKind, r.name);
2743
+ if (r instanceof UseSpecifierNode) return filterUndefined(r.importKind, r.name, r.asKeyword, r.alias);
2737
2744
  if (r instanceof WildcardNode) return filterUndefined(r.token);
2738
2745
  throw r instanceof GroupExpressionNode ? Error("This case is already handled by TupleExpressionNode") : Error("Unreachable - no other possible cases");
2739
2746
  }
@@ -2743,8 +2750,7 @@ function alternateLists(r, j) {
2743
2750
  return M.push(...r.slice(N), ...j.slice(N)), M;
2744
2751
  }
2745
2752
  function shouldInterpretNode(r, j) {
2746
- let M = [...r.parseProject().values()].some((r) => r.getErrors().length > 0), N = r.validateNode(j).getErrors().length > 0, P = r.bindNode(j).getErrors().length > 0;
2747
- return !M && !N && !P;
2753
+ return r.reachableFiles(j.filepath).every((j) => r.bindFile(j).getErrors().length === 0);
2748
2754
  }
2749
2755
  function scanNonListNodeForBinding(r) {
2750
2756
  if (!r) return [];
@@ -2797,7 +2803,9 @@ var DiagramViewBinder = class {
2797
2803
  let [j] = partition_default(r.body, (r) => r instanceof FunctionApplicationNode);
2798
2804
  return j.flatMap((r) => !r.callee || isWildcardExpression(r.callee) ? [] : [r.callee, ...r.args].flatMap(scanNonListNodeForBinding).flatMap((r) => {
2799
2805
  let j = r.variables.pop();
2800
- return j ? this.compiler.nodeReferee(j).getErrors() : [];
2806
+ if (!j) return [];
2807
+ let M = r.variables.pop();
2808
+ return [...M ? this.compiler.nodeReferee(M).getErrors() : [], ...this.compiler.nodeReferee(j).getErrors()];
2801
2809
  }));
2802
2810
  }
2803
2811
  bindNoteReferences(r) {
@@ -2858,31 +2866,31 @@ var DiagramViewBinder = class {
2858
2866
  let j = r.type?.value.toLowerCase();
2859
2867
  j && M.add(j), r.body instanceof BlockExpressionNode && this.interpretSubBlock(r.body, j);
2860
2868
  }
2861
- let N = this.diagramView.visibleEntities;
2862
- return (M.has("tables") && N.tables !== null || M.has("tablegroups") && N.tableGroups !== null || M.has("schemas") && N.schemas !== null) && (M.has("tables") || (N.tables = []), M.has("tablegroups") || (N.tableGroups = []), M.has("schemas") || (N.schemas = [])), M.has("tables") && this.diagramViewExplicitlySet.add("tables"), M.has("tablegroups") && this.diagramViewExplicitlySet.add("tableGroups"), M.has("schemas") && this.diagramViewExplicitlySet.add("schemas"), M.has("notes") && this.diagramViewExplicitlySet.add("stickyNotes"), [];
2869
+ let P = this.diagramView.visibleEntities;
2870
+ return (M.has("tables") && P.tables !== null || M.has("tablegroups") && P.tableGroups !== null || M.has("schemas") && P.schemas !== null) && (M.has("tables") || (P.tables = []), M.has("tablegroups") || (P.tableGroups = []), M.has("schemas") || (P.schemas = [])), M.has(ElementKind.DiagramViewTables) && this.diagramViewExplicitlySet.add("tables"), M.has(ElementKind.DiagramViewTableGroups) && this.diagramViewExplicitlySet.add("tableGroups"), M.has(ElementKind.DiagramViewSchemas) && this.diagramViewExplicitlySet.add("schemas"), M.has(ElementKind.DiagramViewNotes) && this.diagramViewExplicitlySet.add("stickyNotes"), [];
2863
2871
  }
2864
2872
  interpretSubBlock(r, M) {
2865
2873
  if (!M) return;
2866
2874
  if (r.body.some((r) => r instanceof FunctionApplicationNode && isWildcardExpression(r.callee))) {
2867
2875
  let r = this.diagramViewWildcards;
2868
2876
  switch (M) {
2869
- case "tables":
2877
+ case ElementKind.DiagramViewTables:
2870
2878
  this.diagramView.visibleEntities.tables = [], r.add("tables");
2871
2879
  break;
2872
- case "notes":
2880
+ case ElementKind.DiagramViewNotes:
2873
2881
  this.diagramView.visibleEntities.stickyNotes = [], r.add("stickyNotes");
2874
2882
  break;
2875
- case "tablegroups":
2883
+ case ElementKind.DiagramViewTableGroups:
2876
2884
  this.diagramView.visibleEntities.tableGroups = [], r.add("tableGroups");
2877
2885
  break;
2878
- case "schemas":
2886
+ case ElementKind.DiagramViewSchemas:
2879
2887
  this.diagramView.visibleEntities.schemas = [], r.add("schemas");
2880
2888
  break;
2881
2889
  }
2882
2890
  return;
2883
2891
  }
2884
2892
  if (r.body.length === 0) return;
2885
- let N = [];
2893
+ let P = [];
2886
2894
  for (let M of r.body) {
2887
2895
  if (!(M instanceof FunctionApplicationNode)) continue;
2888
2896
  let r = M.callee && this.compiler.nodeReferee(M.callee).getFiltered(UNHANDLED);
@@ -2890,33 +2898,33 @@ var DiagramViewBinder = class {
2890
2898
  let j = destructureComplexVariable(r.declaration.name) ?? [];
2891
2899
  if (j.length > 0) {
2892
2900
  let r = j[j.length - 1], M = j.length > 1 ? j.slice(0, -1).join(".") : DEFAULT_SCHEMA_NAME;
2893
- N.push({
2901
+ P.push({
2894
2902
  name: r,
2895
2903
  schemaName: M
2896
2904
  });
2897
2905
  continue;
2898
2906
  }
2899
2907
  }
2900
- let P = destructureComplexVariable(M.callee) ?? [];
2901
- if (P.length === 0) continue;
2902
- let F = P[P.length - 1], I = P.length > 1 ? P[0] : DEFAULT_SCHEMA_NAME;
2903
- N.push({
2908
+ let N = destructureComplexVariable(M.callee) ?? [];
2909
+ if (N.length === 0) continue;
2910
+ let F = N[N.length - 1], I = N.length > 1 ? N[0] : DEFAULT_SCHEMA_NAME;
2911
+ P.push({
2904
2912
  name: F,
2905
2913
  schemaName: I
2906
2914
  });
2907
2915
  }
2908
2916
  switch (M) {
2909
- case "tables":
2910
- this.diagramView.visibleEntities.tables = N.length > 0 ? N : null;
2917
+ case ElementKind.DiagramViewTables:
2918
+ this.diagramView.visibleEntities.tables = P.length > 0 ? P : null;
2911
2919
  break;
2912
- case "notes":
2913
- this.diagramView.visibleEntities.stickyNotes = N.length > 0 ? N.map((r) => ({ name: r.name })) : null;
2920
+ case ElementKind.DiagramViewNotes:
2921
+ this.diagramView.visibleEntities.stickyNotes = P.length > 0 ? P.map((r) => ({ name: r.name })) : null;
2914
2922
  break;
2915
- case "tablegroups":
2916
- this.diagramView.visibleEntities.tableGroups = N.length > 0 ? N.map((r) => ({ name: r.name })) : null;
2923
+ case ElementKind.DiagramViewTableGroups:
2924
+ this.diagramView.visibleEntities.tableGroups = P.length > 0 ? P.map((r) => ({ name: r.name })) : null;
2917
2925
  break;
2918
- case "schemas":
2919
- this.diagramView.visibleEntities.schemas = N.length > 0 ? N.map((r) => ({ name: r.name })) : null;
2926
+ case ElementKind.DiagramViewSchemas:
2927
+ this.diagramView.visibleEntities.schemas = P.length > 0 ? P.map((r) => ({ name: r.name })) : null;
2920
2928
  break;
2921
2929
  }
2922
2930
  }
@@ -3246,7 +3254,7 @@ const indexesModule$1 = {
3246
3254
  let z = isExpressionAVariableNode(F) ? F.expression.variable?.value ?? "" : "", B = P.lookupMembers(R, SymbolKind.Column, z);
3247
3255
  if (B) return Report.create(B);
3248
3256
  let V = isElementNode(L, ElementKind.TablePartial) ? "TablePartial" : "Table";
3249
- return new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${z}' does not exist in ${V} 'public.${R.name}'`, F)]);
3257
+ return new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${z}' does not exist in ${V} 'public.${R.name}'`, F)]);
3250
3258
  },
3251
3259
  bindNode(j, P) {
3252
3260
  return isElementNode(P, ElementKind.Indexes) ? Report.create(void 0, new IndexesBinder(j, P).bind()) : Report.create(PASS_THROUGH);
@@ -7350,14 +7358,17 @@ var ProgramInterpreter = class {
7350
7358
  let P = N.getValue();
7351
7359
  P && this.pushElement(M, P);
7352
7360
  }
7353
- let r = /* @__PURE__ */ new Set(), M = this.compiler.symbolMembers(this.programSymbol).getFiltered(UNHANDLED) ?? [];
7354
- for (let N of M) {
7355
- if (!(N instanceof SchemaSymbol)) continue;
7356
- let M = this.compiler.symbolMembers(N).getFiltered(UNHANDLED) ?? [];
7357
- for (let j of M) {
7361
+ let r = /* @__PURE__ */ new Set(), M = (this.compiler.symbolMembers(this.programSymbol).getFiltered(UNHANDLED) ?? []).filter((r) => r instanceof SchemaSymbol);
7362
+ for (; M.length > 0;) {
7363
+ let N = M.shift(), P = this.compiler.symbolMembers(N).getFiltered(UNHANDLED) ?? [];
7364
+ for (let j of P) {
7365
+ if (j instanceof SchemaSymbol) {
7366
+ M.push(j);
7367
+ continue;
7368
+ }
7358
7369
  if (!(j instanceof UseSymbol)) continue;
7359
- let M = j.originalSymbol.intern();
7360
- r.has(M) || (r.add(M), this.interpretUseSymbol(j));
7370
+ let N = j.originalSymbol.intern();
7371
+ r.has(N) || (r.add(N), this.interpretUseSymbol(j));
7361
7372
  }
7362
7373
  }
7363
7374
  }
@@ -7577,7 +7588,13 @@ const programModule$1 = {
7577
7588
  return isProgramNode(N) ? new Binder(N, j).resolve() : Report.create(PASS_THROUGH);
7578
7589
  },
7579
7590
  interpretSymbol(j, N, P) {
7580
- return N instanceof ProgramSymbol ? N.declaration instanceof ProgramNode ? shouldInterpretNode(j, N.declaration) ? new ProgramInterpreter(j, N, P).interpret() : Report.create(void 0, [...j.validateNode(N.declaration).getErrors(), ...j.bindNode(N.declaration).getErrors()]) : Report.create(void 0) : Report.create(PASS_THROUGH);
7591
+ if (!(N instanceof ProgramSymbol)) return Report.create(PASS_THROUGH);
7592
+ if (!(N.declaration instanceof ProgramNode)) return Report.create(void 0);
7593
+ if (!shouldInterpretNode(j, N.declaration)) {
7594
+ let r = j.reachableFiles(P).flatMap((r) => j.bindFile(r).getErrors());
7595
+ return Report.create(void 0, r);
7596
+ }
7597
+ return new ProgramInterpreter(j, N, P).interpret();
7581
7598
  }
7582
7599
  };
7583
7600
  var ProjectBinder = class {
@@ -7626,7 +7643,7 @@ var ProjectBinder = class {
7626
7643
  interpretBody(r) {
7627
7644
  return r.body.flatMap((r) => {
7628
7645
  let j = r;
7629
- switch (j.type?.value.toLowerCase()) {
7646
+ switch (j.getElementKind()) {
7630
7647
  case ElementKind.Table: {
7631
7648
  let r = this.interpretSubElement(j);
7632
7649
  return r.getValue() && this.project.tables?.push(r.getValue()), r.getErrors();
@@ -7991,23 +8008,23 @@ function nodeRefereeOfRecordsName(r, j, N) {
7991
8008
  let P = extractVarNameFromPrimaryVariable(N) ?? "";
7992
8009
  if (!isAccessExpression(N.parentNode)) {
7993
8010
  let F = r.lookupMembers(j, [SymbolKind.Table, SymbolKind.Schema], P);
7994
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table '${P}' does not exist in Schema 'public'`, N)]);
8011
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table '${P}' does not exist in Schema 'public'`, N)]);
7995
8012
  }
7996
8013
  let F = nodeRefereeOfLeftExpression(r, N);
7997
8014
  if (!F) {
7998
8015
  let F = r.lookupMembers(j, [SymbolKind.Table, SymbolKind.Schema], P);
7999
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table or schema '${P}' does not exist`, N)]);
8016
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table or schema '${P}' does not exist`, N)]);
8000
8017
  }
8001
8018
  if (F.isKind(SymbolKind.Schema)) {
8002
8019
  let j = r.lookupMembers(F, [SymbolKind.Table, SymbolKind.Schema], P);
8003
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table or schema '${P}' does not exist`, N)]);
8020
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table or schema '${P}' does not exist`, N)]);
8004
8021
  }
8005
8022
  return new Report(void 0);
8006
8023
  }
8007
8024
  function nodeRefereeOfRecordsColumn(r, j, N) {
8008
8025
  if (!isExpressionAVariableNode(N)) return new Report(void 0);
8009
8026
  let P = extractVarNameFromPrimaryVariable(N) ?? "", F = r.lookupMembers(j, SymbolKind.Column, P);
8010
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${P}' does not exist in Table 'public.${j.name}'`, N)]);
8027
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${P}' does not exist in Table 'public.${j.name}'`, N)]);
8011
8028
  }
8012
8029
  function nodeRefereeOfEnumValue(r, j, N) {
8013
8030
  if (!isExpressionAVariableNode(N)) return new Report(void 0);
@@ -8017,7 +8034,7 @@ function nodeRefereeOfEnumValue(r, j, N) {
8017
8034
  if (F) {
8018
8035
  if (F.isKind(SymbolKind.Schema)) {
8019
8036
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
8020
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
8037
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
8021
8038
  }
8022
8039
  if (F.isKind(SymbolKind.Enum)) {
8023
8040
  let j = r.lookupMembers(F, SymbolKind.EnumField, P);
@@ -8029,7 +8046,7 @@ function nodeRefereeOfEnumValue(r, j, N) {
8029
8046
  if (I.leftExpression === N) {
8030
8047
  if (isAccessExpression(I.parentNode) && I.parentNode.leftExpression === I) {
8031
8048
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
8032
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8049
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8033
8050
  }
8034
8051
  let F = r.lookupMembers(j, SymbolKind.Enum, P);
8035
8052
  return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum '${P}' does not exist in Schema 'public'`, N)]);
@@ -8238,7 +8255,7 @@ const useUtils = { visibleName(r, M) {
8238
8255
  let r = N.nodeReferee(V).getFiltered(UNHANDLED);
8239
8256
  if (!r) return Report.create(void 0);
8240
8257
  let F = N.lookupMembers(r, I, R);
8241
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Could not find ${I} '${R}'`, P)]);
8258
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Could not find ${I} '${R}'`, P)]);
8242
8259
  }
8243
8260
  let H = lookupMemberInFilepath(N, B, R, I);
8244
8261
  return H ? Report.create(H) : Report.create(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `${I} '${R}' does not exist in file ${B.toString()}`, P)]);
@@ -8277,8 +8294,8 @@ function lookupMemberInFilepath(r, M, N, P, F = /* @__PURE__ */ new Set()) {
8277
8294
  F.add(M.intern());
8278
8295
  let I = r.usableMembers(M).getFiltered(UNHANDLED);
8279
8296
  if (!I) return;
8280
- let L = I.nonSchemaMembers.find((r) => r.name === N);
8281
- if (L?.isKind(P)) return L;
8297
+ let L = I.nonSchemaMembers.find((r) => r.name === N && r.isKind(P));
8298
+ if (L) return L;
8282
8299
  let R = I.schemaMembers.find((r) => r.name === N);
8283
8300
  if (R?.isKind(SymbolKind.Schema)) return R;
8284
8301
  for (let j of I.reuses.selective) if (destructureComplexVariable(j.alias ?? j.name)?.at(-1) === N) {
@@ -8315,7 +8332,7 @@ function usableMembers(r) {
8315
8332
  function schemaMembership(r, M, N) {
8316
8333
  let P = M.qualifiedName, F;
8317
8334
  if (F = N instanceof UseSpecifierNode ? useUtils.visibleName(r, N) : r.nodeFullname(N).getFiltered(UNHANDLED), !F) return { kind: "none" };
8318
- let I = F.length <= 1 ? [DEFAULT_SCHEMA_NAME] : F.slice(0, -1);
8335
+ let I = N instanceof UseSpecifierNode && N.getSymbolKind() === SymbolKind.Schema ? F : F.length <= 1 ? [DEFAULT_SCHEMA_NAME] : F.slice(0, -1);
8319
8336
  return I.length < P.length || !P.every((r, j) => r === I[j]) ? { kind: "none" } : I.length === P.length ? { kind: "direct" } : {
8320
8337
  kind: "child",
8321
8338
  schemaName: I[P.length]
@@ -8579,7 +8596,7 @@ var TableBinder = class {
8579
8596
  }
8580
8597
  interpretSubElements(r) {
8581
8598
  return r.flatMap((r) => {
8582
- switch (r.type?.value.toLowerCase()) {
8599
+ switch (r.getElementKind()) {
8583
8600
  case ElementKind.Note: return this.table.note = {
8584
8601
  value: normalizeNote(extractQuotedStringToken(r.body instanceof BlockExpressionNode ? r.body.body[0].callee : r.body.callee)),
8585
8602
  token: getTokenPosition(r)
@@ -8599,8 +8616,8 @@ var TableBinder = class {
8599
8616
  P.name = extractVariableFromExpression(r.expression) || "";
8600
8617
  let F = r.expression ? this.compiler.nodeReferee(r.expression).getFiltered(UNHANDLED) : void 0, I = this.compiler.parseFile(this.filepath).getValue().ast, L = this.compiler.nodeSymbol(I).getFiltered(UNHANDLED);
8601
8618
  if (F && !L.inNestedSchema(this.compiler, F)) {
8602
- let M = this.compiler.nodeSymbol(this.declarationNode).getFiltered(UNHANDLED), P = M ? (this.compiler.symbolUses(M).getFiltered(UNHANDLED) ?? []).find((r) => r.filepath.equals(this.filepath)) : void 0;
8603
- N.push(new CompileError(CompileErrorCode.UNSUPPORTED, `Import TablePartial from '${F.filepath}' first. Currently, importing a table that uses a table partial requires that table partial to also be in scope.`, P?.useSpecifierDeclaration ?? r));
8619
+ let M = this.compiler.nodeSymbol(this.declarationNode).getFiltered(UNHANDLED), I = M ? (this.compiler.symbolUses(M).getFiltered(UNHANDLED) ?? []).find((r) => r.filepath.equals(this.filepath)) : void 0;
8620
+ N.push(new CompileError(CompileErrorCode.UNSUPPORTED, `TablePartial ${P.name} from '${F.filepath}' should be imported. Currently, importing a table that uses a table partial requires that table partial to also be in scope.`, I?.useSpecifierDeclaration ?? r));
8604
8621
  }
8605
8622
  return this.table.partials.push(P), N;
8606
8623
  }
@@ -8758,7 +8775,7 @@ function nodeRefereeOfPartialInjection(r, N, P) {
8758
8775
  }
8759
8776
  }
8760
8777
  let L = r.lookupMembers(N, SymbolKind.TablePartial, F);
8761
- return L ? Report.create(L) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `TablePartial '${F}' does not exist in Schema 'public'`, P)]);
8778
+ return L ? Report.create(L) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `TablePartial '${F}' does not exist in Schema 'public'`, P)]);
8762
8779
  }
8763
8780
  function nodeRefereeOfEnumType$1(r, j, N) {
8764
8781
  if (!isExpressionAVariableNode(N)) return new Report(void 0);
@@ -8768,13 +8785,13 @@ function nodeRefereeOfEnumType$1(r, j, N) {
8768
8785
  if (F) {
8769
8786
  if (F.isKind(SymbolKind.Schema)) {
8770
8787
  let j = isTerminalAccessFragment(N), I = r.lookupMembers(F, j ? SymbolKind.Enum : SymbolKind.Schema, P);
8771
- return I ? Report.create(I) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, j ? `Enum '${P}' does not exist in Schema 'public'` : `Schema '${P}' does not exist in Schema 'public'`, N)]);
8788
+ return I ? Report.create(I) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, j ? `Enum '${P}' does not exist in Schema 'public'` : `Schema '${P}' does not exist in Schema 'public'`, N)]);
8772
8789
  }
8773
8790
  return new Report(void 0);
8774
8791
  }
8775
8792
  if (N.parentNode.leftExpression === N) {
8776
8793
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
8777
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8794
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8778
8795
  }
8779
8796
  return new Report(void 0);
8780
8797
  }
@@ -8821,11 +8838,11 @@ function nodeRefereeOfEnumDefault$1(r, j, N) {
8821
8838
  if (F) {
8822
8839
  if (F.isKind(SymbolKind.Schema)) {
8823
8840
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
8824
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
8841
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
8825
8842
  }
8826
8843
  if (F.isKind(SymbolKind.Enum)) {
8827
8844
  let j = r.lookupMembers(F, SymbolKind.EnumField, P);
8828
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
8845
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
8829
8846
  }
8830
8847
  return new Report(void 0);
8831
8848
  }
@@ -8833,10 +8850,10 @@ function nodeRefereeOfEnumDefault$1(r, j, N) {
8833
8850
  if (I.leftExpression === N) {
8834
8851
  if (isAccessExpression(I.parentNode) && I.parentNode.leftExpression === I) {
8835
8852
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
8836
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8853
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
8837
8854
  }
8838
8855
  let F = r.lookupMembers(j, SymbolKind.Enum, P);
8839
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum '${P}' does not exist in Schema 'public'`, N)]);
8856
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum '${P}' does not exist in Schema 'public'`, N)]);
8840
8857
  }
8841
8858
  return new Report(void 0);
8842
8859
  }
@@ -9002,8 +9019,8 @@ var TableGroupBinder = class {
9002
9019
  }
9003
9020
  interpretSubElements(r) {
9004
9021
  return r.flatMap((r) => {
9005
- switch (r.type?.value.toLowerCase()) {
9006
- case "note":
9022
+ switch (r.getElementKind()) {
9023
+ case ElementKind.Note:
9007
9024
  this.tableGroup.note = {
9008
9025
  value: normalizeNote(extractQuotedStringToken(r.body instanceof BlockExpressionNode ? r.body.body[0].callee : r.body.callee)),
9009
9026
  token: getTokenPosition(r)
@@ -9214,7 +9231,7 @@ var TablePartialBinder = class {
9214
9231
  this.tablePartial.headerColor = M ? extractColor(M.value) : void 0;
9215
9232
  let [N] = j[SettingName.Note] || [];
9216
9233
  return this.tablePartial.note = N && {
9217
- value: extractQuotedStringToken(N?.value) ? normalizeNote(extractQuotedStringToken(N?.value)) : "",
9234
+ value: normalizeNote(extractQuotedStringToken(N?.value)),
9218
9235
  token: getTokenPosition(N)
9219
9236
  }, [];
9220
9237
  }
@@ -9224,7 +9241,7 @@ var TablePartialBinder = class {
9224
9241
  }
9225
9242
  interpretSubElements(r) {
9226
9243
  return r.flatMap((r) => {
9227
- switch (r.type?.value.toLowerCase()) {
9244
+ switch (r.getElementKind()) {
9228
9245
  case ElementKind.Note: return this.tablePartial.note = {
9229
9246
  value: normalizeNote(extractQuotedStringToken(r.body instanceof BlockExpressionNode ? r.body.body[0].callee : r.body.callee)),
9230
9247
  token: getTokenPosition(r)
@@ -9342,13 +9359,13 @@ function nodeRefereeOfEnumType(r, j, N) {
9342
9359
  if (F) {
9343
9360
  if (F.isKind(SymbolKind.Schema)) {
9344
9361
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
9345
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
9362
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
9346
9363
  }
9347
9364
  return new Report(void 0);
9348
9365
  }
9349
9366
  if (N.parentNode.leftExpression === N) {
9350
9367
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
9351
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9368
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9352
9369
  }
9353
9370
  return new Report(void 0);
9354
9371
  }
@@ -9361,21 +9378,21 @@ function nodeRefereeOfInlineRef(r, P, F) {
9361
9378
  let N = r.nodeSymbol(L).getFiltered(UNHANDLED);
9362
9379
  if (N) {
9363
9380
  let j = r.lookupMembers(N, SymbolKind.Column, I);
9364
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${I}' does not exist in TablePartial '${N.name}'`, F)]);
9381
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${I}' does not exist in TablePartial '${N.name}'`, F)]);
9365
9382
  }
9366
9383
  }
9367
9384
  let R = r.lookupMembers(P, SymbolKind.Column, I);
9368
- return R ? Report.create(R) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${I}' does not exist`, F)]);
9385
+ return R ? Report.create(R) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${I}' does not exist`, F)]);
9369
9386
  }
9370
9387
  let L = nodeRefereeOfLeftExpression(r, F);
9371
9388
  if (L) {
9372
9389
  if (L.isKind(SymbolKind.Schema)) {
9373
9390
  let j = r.lookupMembers(L, [SymbolKind.Table, SymbolKind.Schema], I);
9374
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table or schema '${I}' does not exist`, F)]);
9391
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table or schema '${I}' does not exist`, F)]);
9375
9392
  }
9376
9393
  if (L.isKind(SymbolKind.Table)) {
9377
9394
  let j = r.lookupMembers(L, SymbolKind.Column, I);
9378
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Column '${I}' does not exist in Table 'public.${L.name}'`, F)]);
9395
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Column '${I}' does not exist in Table 'public.${L.name}'`, F)]);
9379
9396
  }
9380
9397
  return new Report(void 0);
9381
9398
  }
@@ -9383,10 +9400,10 @@ function nodeRefereeOfInlineRef(r, P, F) {
9383
9400
  if (R.leftExpression === F) {
9384
9401
  if (isAccessExpression(R.parentNode) && R.parentNode.leftExpression === R) {
9385
9402
  let j = r.lookupMembers(P, SymbolKind.Schema, I);
9386
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${I}' does not exist in Schema 'public'`, F)]);
9403
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${I}' does not exist in Schema 'public'`, F)]);
9387
9404
  }
9388
9405
  let j = r.lookupMembers(P, SymbolKind.Table, I);
9389
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Table '${I}' does not exist in Schema 'public'`, F)]);
9406
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Table '${I}' does not exist in Schema 'public'`, F)]);
9390
9407
  }
9391
9408
  return new Report(void 0);
9392
9409
  }
@@ -9398,11 +9415,11 @@ function nodeRefereeOfEnumDefault(r, j, N) {
9398
9415
  if (F) {
9399
9416
  if (F.isKind(SymbolKind.Schema)) {
9400
9417
  let j = r.lookupMembers(F, [SymbolKind.Enum, SymbolKind.Schema], P);
9401
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum or schema '${P}' does not exist`, N)]);
9418
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum or schema '${P}' does not exist`, N)]);
9402
9419
  }
9403
9420
  if (F.isKind(SymbolKind.Enum)) {
9404
9421
  let j = r.lookupMembers(F, SymbolKind.EnumField, P);
9405
- return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
9422
+ return j ? Report.create(j) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum field '${P}' does not exist in Enum 'public.${F.name}'`, N)]);
9406
9423
  }
9407
9424
  return new Report(void 0);
9408
9425
  }
@@ -9410,10 +9427,10 @@ function nodeRefereeOfEnumDefault(r, j, N) {
9410
9427
  if (I.leftExpression === N) {
9411
9428
  if (isAccessExpression(I.parentNode) && I.parentNode.leftExpression === I) {
9412
9429
  let F = r.lookupMembers(j, SymbolKind.Schema, P);
9413
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9430
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Schema '${P}' does not exist in Schema 'public'`, N)]);
9414
9431
  }
9415
9432
  let F = r.lookupMembers(j, SymbolKind.Enum, P);
9416
- return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.NAME_NOT_FOUND, `Enum '${P}' does not exist in Schema 'public'`, N)]);
9433
+ return F ? Report.create(F) : new Report(void 0, [new CompileError(CompileErrorCode.BINDING_ERROR, `Enum '${P}' does not exist in Schema 'public'`, N)]);
9417
9434
  }
9418
9435
  return new Report(void 0);
9419
9436
  }
@@ -9489,11 +9506,15 @@ function handleMemberSelectiveUses(r, N, P, F) {
9489
9506
  function mergeImportedSchema(r, M, N, P = /* @__PURE__ */ new Set()) {
9490
9507
  if (N.getSymbolKind() !== SymbolKind.Schema || !N.name) return [];
9491
9508
  let F = r.nodeReferee(N.name).getFiltered(UNHANDLED);
9492
- if (!F || !F.isKind(SymbolKind.Schema) || (r.nodeAlias(N).getFiltered(UNHANDLED) ?? r.nodeFullname(N).getFiltered(UNHANDLED)?.at(0)) !== M.name) return [];
9493
- let I = F.intern();
9494
- if (P.has(I)) return [];
9495
- P.add(I);
9496
- let L = r.usableMembers(F).getFiltered(UNHANDLED);
9509
+ if (!F || !F.isKind(SymbolKind.Schema)) return [];
9510
+ let I = useUtils.visibleName(r, N), L = M.qualifiedName;
9511
+ return !I || I.length !== L.length || !I.every((r, j) => r === L[j]) ? [] : mergeSchemaMembers(r, M, N, F, P);
9512
+ }
9513
+ function mergeSchemaMembers(r, M, N, P, F) {
9514
+ let I = P.intern();
9515
+ if (F.has(I)) return [];
9516
+ F.add(I);
9517
+ let L = r.usableMembers(P).getFiltered(UNHANDLED);
9497
9518
  if (!L) return [];
9498
9519
  let R = L.nonSchemaMembers.filter((r) => r.canBeImported).map((j) => r.symbolFactory.create(UseSymbol, {
9499
9520
  kind: j.kind,
@@ -9503,7 +9524,20 @@ function mergeImportedSchema(r, M, N, P = /* @__PURE__ */ new Set()) {
9503
9524
  name: j.name
9504
9525
  }, M.filepath));
9505
9526
  for (let j of [...R]) j.isKind(SymbolKind.TableGroup) && R.push(...expandTableGroup(r, M, j));
9506
- for (let j of L.reuses.selective) R.push(...mergeImportedSchema(r, M, j, P));
9527
+ for (let P of L.reuses.selective) if (R.push(...mergeImportedSchema(r, M, P, F)), schemaMembership(r, M, P).kind === "direct") {
9528
+ let F = r.nodeSymbol(P).getFiltered(UNHANDLED);
9529
+ F && F.canBeImported && R.push(r.symbolFactory.create(UseSymbol, {
9530
+ kind: F.kind,
9531
+ declaration: F.originalSymbol.declaration,
9532
+ usedSymbol: F.originalSymbol,
9533
+ useSpecifierDeclaration: N,
9534
+ name: F.name
9535
+ }, M.filepath));
9536
+ }
9537
+ for (let { importPath: j } of L.reuses.wildcard) {
9538
+ let P = findSchemaSymbolInFilepath(r, j, M.qualifiedName);
9539
+ P && R.push(...mergeSchemaMembers(r, M, N, P, F));
9540
+ }
9507
9541
  return R;
9508
9542
  }
9509
9543
  function handleMemberWildcardUses(r, M, N, P, F, I = /* @__PURE__ */ new Set()) {
@@ -9891,6 +9925,10 @@ var DiagramViewValidator = class {
9891
9925
  }
9892
9926
  validateSubBlock(r) {
9893
9927
  let j = [], M = [];
9928
+ if (hasSimpleBody(r)) return j.push(new CompileError(CompileErrorCode.UNEXPECTED_SIMPLE_BODY, `${r.type?.value} body must be a block`, r.bodyColon)), {
9929
+ errors: j,
9930
+ warnings: M
9931
+ };
9894
9932
  if (!r.body || !(r.body instanceof BlockExpressionNode)) return {
9895
9933
  errors: j,
9896
9934
  warnings: M
@@ -11904,14 +11942,12 @@ var DBMLDefinitionProvider = class {
11904
11942
  for (; I.length !== 0;) {
11905
11943
  let r = I.pop();
11906
11944
  if (!r) continue;
11907
- let M = this.compiler.nodeReferee(r);
11908
- if (M.hasValue(UNHANDLED)) continue;
11909
- let P = M.getValue();
11910
- if (!P) continue;
11911
- let F;
11912
- if (P.declaration && [SyntaxNodeKind.PRIMARY_EXPRESSION, SyntaxNodeKind.VARIABLE].includes(r.kind) && ({declaration: F} = P), F) {
11913
- let { startPos: r, endPos: j } = F, M = N;
11914
- return N && F.filepath && (M = URI.parse(F.filepath.toUri())), [{
11945
+ let M = this.compiler.nodeReferee(r).getFiltered(UNHANDLED);
11946
+ if (!M) continue;
11947
+ let P;
11948
+ if (M.declaration && [SyntaxNodeKind.PRIMARY_EXPRESSION, SyntaxNodeKind.VARIABLE].includes(r.kind) && ({declaration: P} = M), P) {
11949
+ let { startPos: r, endPos: j } = P, M = N;
11950
+ return N && P.filepath && (M = URI.parse(P.filepath.toUri())), [{
11915
11951
  range: {
11916
11952
  startColumn: r.column + 1,
11917
11953
  startLineNumber: r.line + 1,
@@ -12004,81 +12040,47 @@ function isOffsetWithinElementHeader(r, j) {
12004
12040
  let M = j.bodyColon?.start ?? j.body?.start;
12005
12041
  return M === void 0 ? r >= j.start && r <= j.end : r >= j.start && r < M;
12006
12042
  }
12007
- function scanExistingUses(r, j, M) {
12008
- let N = [], P = r.parseFile(j).getValue()?.ast;
12009
- if (!P) return N;
12010
- for (let r of P.uses) {
12011
- let j = r.importPath?.value ?? "";
12012
- if (!j) {
12013
- let N = r.useKeyword?.start ?? r.fullStart, P = M.lastIndexOf("\n", N - 1) + 1, F = M.indexOf("\n", N), I = M.slice(P, F === -1 ? void 0 : F).match(/from\s*(['"])(.*?)\1/);
12014
- I && (j = I[2]);
12015
- }
12016
- let P = [];
12017
- if (r.specifiers instanceof UseSpecifierListNode) {
12018
- let j = new Set([
12019
- "table",
12020
- "enum",
12021
- "tablepartial",
12022
- "tablegroup",
12023
- "note",
12024
- "schema",
12025
- "from"
12026
- ]);
12027
- for (let N of r.specifiers.specifiers) {
12028
- let r, F;
12029
- if (N.name) r = N.importKind?.value, F = extractVariableFromExpression(N.name), !F && N.name.start !== void 0 && N.name.end !== void 0 && (F = M.slice(N.name.start, N.name.end).trim());
12030
- else if (N.importKind) {
12031
- let r = N.importKind.value ?? void 0;
12032
- r && !j.has(r.toLowerCase()) && (F = r);
12033
- }
12034
- if (F && F.toLowerCase() !== "from") {
12035
- let j = N.alias ? extractVariableFromExpression(N.alias) : void 0;
12036
- P.push({
12037
- kind: r,
12038
- name: F,
12039
- alias: j
12040
- });
12041
- }
12043
+ function scanExistingUses(r, j) {
12044
+ let M = [], N = r.parseFile(j).getValue()?.ast;
12045
+ if (!N) return M;
12046
+ for (let r of N.uses) {
12047
+ let j = r.importPath?.value ?? "", N = [];
12048
+ if (r.specifiers instanceof UseSpecifierListNode) for (let j of r.specifiers.specifiers) {
12049
+ let r, M;
12050
+ if (j.name ? (r = j.importKind?.value, M = extractVariableFromExpression(j.name)) : j.importKind && (M = j.importKind.value ?? void 0), M) {
12051
+ let P = j.alias ? extractVariableFromExpression(j.alias) : void 0;
12052
+ N.push({
12053
+ kind: r,
12054
+ name: M,
12055
+ alias: P
12056
+ });
12042
12057
  }
12043
- } else r.specifiers instanceof WildcardNode && P.push({ name: "*" });
12044
- if (P.length === 0 && !(r.specifiers instanceof WildcardNode)) continue;
12045
- let F = r.fullEnd;
12046
- if (!r.importPath) {
12047
- let j = r.useKeyword?.start ?? r.fullStart, N = M.indexOf("\n", j);
12048
- F = N === -1 ? M.length : N;
12049
12058
  }
12050
- N.push({
12059
+ else r.specifiers instanceof WildcardNode && N.push({ name: "*" });
12060
+ N.length === 0 && !(r.specifiers instanceof WildcardNode) || M.push({
12051
12061
  startOffset: r.fullStart,
12052
- endOffset: F,
12062
+ endOffset: r.fullEnd,
12053
12063
  sourceFile: j,
12054
- specifiers: P,
12064
+ specifiers: N,
12055
12065
  node: r
12056
12066
  });
12057
12067
  }
12058
- return N;
12068
+ return M;
12059
12069
  }
12060
12070
  function mergeSymbolIntoUses(r, j, M, N, P, F) {
12061
- let I = scanExistingUses(r, P, F), L = normalizeSourcePath(N, P), R = I.findIndex((r) => r.sourceFile === L), z = `${M} ${j}`, B = detectLineEnding(F);
12071
+ let I = scanExistingUses(r, P), L = N.relativeTo(P.dirname).replace(/\.dbml$/, ""), R = I.findIndex((r) => r.sourceFile === L), z = `${M} ${j}`;
12062
12072
  if (R !== -1) {
12063
12073
  let r = I[R];
12064
12074
  return r.specifiers.some((r) => r.name === j) ? { topInsert: "" } : {
12065
- topInsert: buildUseStatement(uniqueInOrder([...r.specifiers.filter((r) => r.name !== "*").map((r) => `${r.kind ?? M} ${r.name}${r.alias ? ` as ${r.alias}` : ""}`), z]), L, B),
12075
+ topInsert: buildUseStatement(uniq_default([...r.specifiers.filter((r) => r.name !== "*").map((r) => `${r.kind ?? M} ${r.name}${r.alias ? ` as ${r.alias}` : ""}`), z]), L, "\n"),
12066
12076
  removeRange: expandToFullLines(F, r.startOffset, r.endOffset)
12067
12077
  };
12068
12078
  }
12069
- return { topInsert: buildUseStatement([z], L, B) };
12079
+ return { topInsert: buildUseStatement([z], L, "\n") };
12070
12080
  }
12071
12081
  function buildUseStatement(r, j, M) {
12072
12082
  return `use {${M}${r.map((r) => ` ${r}`).join(M)}${M}} from '${j}'${M}${M}`;
12073
12083
  }
12074
- function uniqueInOrder(r) {
12075
- let j = /* @__PURE__ */ new Set(), M = [];
12076
- for (let N of r) {
12077
- let r = N.trim();
12078
- j.has(r) || (j.add(r), M.push(N));
12079
- }
12080
- return M;
12081
- }
12082
12084
  function expandToFullLines(r, j, M) {
12083
12085
  let N = r.lastIndexOf("\n", j - 1) + 1, P = M;
12084
12086
  for (; P < r.length && (r[P] === " " || r[P] === " ");) P++;
@@ -12087,12 +12089,6 @@ function expandToFullLines(r, j, M) {
12087
12089
  endOffset: P
12088
12090
  };
12089
12091
  }
12090
- function detectLineEnding(r) {
12091
- return r.includes("\r\n") ? "\r\n" : "\n";
12092
- }
12093
- function normalizeSourcePath(r, j) {
12094
- return r.relativeTo(j.dirname).replace(/\.dbml$/, "");
12095
- }
12096
12092
  function pickCompletionItemKind(r) {
12097
12093
  switch (r) {
12098
12094
  case SymbolKind.Schema: return CompletionItemKind.Module;
@@ -12152,7 +12148,7 @@ function addSuggestAllSuggestion(r, j = ", ") {
12152
12148
  }
12153
12149
  function collectCrossFileSuggestions(r, M, N) {
12154
12150
  let P = [], F = /* @__PURE__ */ new Set(), I = r.layout.getSource(N) ?? "";
12155
- for (let L of r.layout.getEntryPoints()) {
12151
+ for (let L of r.layout.getEntrypoints()) {
12156
12152
  if (L.equals(N)) continue;
12157
12153
  let R = r.usableMembers(L).getFiltered(UNHANDLED);
12158
12154
  if (R) for (let j of [...R.nonSchemaMembers, ...R.schemaMembers]) {
@@ -12272,6 +12268,7 @@ function suggestUseCompletion(r, j, M, N, P) {
12272
12268
  }
12273
12269
  function suggestInUseDeclaration(r, j, M, N, P, F) {
12274
12270
  if (N.importPath && isOffsetWithinSpan(M, N.importPath)) return suggestUseFilepath(r, j, N.importPath, P, N);
12271
+ if (N.importPath && M >= N.importPath.end) return noSuggestions();
12275
12272
  if (N.fromKeyword && M > N.fromKeyword.end) return suggestUseFilepath(r, j, void 0, P, N);
12276
12273
  if (N.specifiers instanceof UseSpecifierListNode) {
12277
12274
  if (!N.specifiers.openBrace) {
@@ -12312,24 +12309,16 @@ function suggestUseElementNames(r, M, N, P) {
12312
12309
  if (!F) return noSuggestions();
12313
12310
  let I = r.usableMembers(F).getFiltered(UNHANDLED);
12314
12311
  if (!I) return noSuggestions();
12315
- let L = [];
12316
- for (let r of I.nonSchemaMembers) {
12317
- if (r.kind !== P) continue;
12318
- let j = r.name;
12319
- j && L.push(j);
12320
- }
12312
+ let L = /* @__PURE__ */ new Set();
12321
12313
  for (let M of I.schemaMembers) {
12322
- if (M.isPublicSchema()) continue;
12323
- let N = M.name;
12324
- if (!N) continue;
12325
- let F = r.symbolMembers(M).getFiltered(UNHANDLED) ?? [];
12326
- for (let r of F) {
12327
- if (r.kind !== P) continue;
12314
+ let N = r.symbolMembers(M).getFiltered(UNHANDLED) ?? [];
12315
+ for (let r of N) {
12316
+ if (r.kind !== P || r instanceof UseSymbol && !r.canBeImported) continue;
12328
12317
  let j = r.name;
12329
- j && L.push(`${N}.${j}`);
12318
+ j && (M.isPublicSchema() ? L.add(j) : L.add(`${M.name}.${j}`));
12330
12319
  }
12331
12320
  }
12332
- return { suggestions: L.map((r) => ({
12321
+ return { suggestions: [...L].map((r) => ({
12333
12322
  label: r,
12334
12323
  insertText: r,
12335
12324
  insertTextRules: CompletionItemInsertTextRule.KeepWhitespace,
@@ -12341,14 +12330,14 @@ function suggestUseSpecifierStart() {
12341
12330
  return { suggestions: [{
12342
12331
  label: "* from",
12343
12332
  detail: "import all",
12344
- insertText: "* from '${1:path}'",
12333
+ insertText: "* from '${1:./path}'",
12345
12334
  insertTextRules: CompletionItemInsertTextRule.InsertAsSnippet,
12346
12335
  kind: CompletionItemKind.Snippet,
12347
12336
  range: void 0
12348
12337
  }, {
12349
12338
  label: "{ } from",
12350
12339
  detail: "import named",
12351
- insertText: "{ ${1:table} ${2:name} } from '${3:path}'",
12340
+ insertText: "{ ${1:kind} ${2:name} } from '${3:./path}'",
12352
12341
  insertTextRules: CompletionItemInsertTextRule.InsertAsSnippet,
12353
12342
  kind: CompletionItemKind.Snippet,
12354
12343
  range: void 0
@@ -12365,7 +12354,7 @@ function suggestUseFilepath(r, j, M, N, P) {
12365
12354
  endColumn: r.endColumn - 1
12366
12355
  };
12367
12356
  }
12368
- let z = /* @__PURE__ */ new Map(), B = /* @__PURE__ */ new Map(), V = uniqBy_default(r.layout.getEntryPoints().flatMap((j) => [...r.reachableFiles(j)]), (r) => r.intern());
12357
+ let z = /* @__PURE__ */ new Map(), B = /* @__PURE__ */ new Map(), V = uniqBy_default(r.layout.getEntrypoints().flatMap((j) => [...r.reachableFiles(j)]), (r) => r.intern());
12369
12358
  for (let M of V) {
12370
12359
  if (M.equals(j)) continue;
12371
12360
  let N = M.relativeTo(F);
@@ -12902,11 +12891,11 @@ function suggestInDiagramViewBody() {
12902
12891
  range: void 0
12903
12892
  })), wildcardSuggestion] };
12904
12893
  }
12905
- function suggestInDiagramViewSubBlock(r, j, M, N) {
12906
- switch (N.type?.value.toLowerCase()) {
12907
- case "tables": return { suggestions: [wildcardSuggestion, ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.Table, SymbolKind.Schema]).suggestions] };
12908
- case "tablegroups": return { suggestions: [wildcardSuggestion, ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.TableGroup]).suggestions] };
12909
- case "schemas": return { suggestions: [
12894
+ function suggestInDiagramViewSubBlock(r, j, M, P) {
12895
+ switch (P.getElementKind()) {
12896
+ case ElementKind.DiagramViewTables: return { suggestions: [wildcardSuggestion, ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.Table, SymbolKind.Schema]).suggestions] };
12897
+ case ElementKind.DiagramViewTableGroups: return { suggestions: [wildcardSuggestion, ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.TableGroup]).suggestions] };
12898
+ case ElementKind.DiagramViewSchemas: return { suggestions: [
12910
12899
  wildcardSuggestion,
12911
12900
  {
12912
12901
  label: DEFAULT_SCHEMA_NAME,
@@ -12917,7 +12906,7 @@ function suggestInDiagramViewSubBlock(r, j, M, N) {
12917
12906
  },
12918
12907
  ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.Schema]).suggestions
12919
12908
  ] };
12920
- case "notes": return { suggestions: [wildcardSuggestion, ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.StickyNote]).suggestions] };
12909
+ case ElementKind.DiagramViewNotes: return { suggestions: [wildcardSuggestion, ...suggestNamesInScope(r, j, M, r.parse.ast(j), [SymbolKind.StickyNote]).suggestions] };
12921
12910
  default: return noSuggestions();
12922
12911
  }
12923
12912
  }
@@ -13185,48 +13174,6 @@ var dbmlMonarchTokensProvider = {
13185
13174
  [/[/*]/, "comment"]
13186
13175
  ]
13187
13176
  }
13188
- }, MemoryProjectLayout = class r {
13189
- constructor(r = {}) {
13190
- this.files = r instanceof Map ? new Map(r) : new Map(Object.entries(r));
13191
- }
13192
- setSource(r, j) {
13193
- this.files.set(r.absolute, j);
13194
- }
13195
- getSource(r) {
13196
- return this.files.get(r.absolute) ?? void 0;
13197
- }
13198
- deleteSource(r) {
13199
- this.files.delete(r.absolute);
13200
- }
13201
- clearSource() {
13202
- this.files.clear();
13203
- }
13204
- exists(r) {
13205
- return this.isFile(r) || this.isDirectory(r);
13206
- }
13207
- isFile(r) {
13208
- return this.files.has(r.absolute);
13209
- }
13210
- isDirectory(r) {
13211
- let j = r.absolute.endsWith("/") ? r.absolute : `${r.absolute}/`;
13212
- for (let r of this.files.keys()) if (r.startsWith(j)) return !0;
13213
- return !1;
13214
- }
13215
- listDirectory(r) {
13216
- let j = r?.absolute ?? "/", M = j.endsWith("/") ? j : j + "/", N = /* @__PURE__ */ new Set();
13217
- for (let r of this.files.keys()) {
13218
- if (!r.startsWith(M)) continue;
13219
- let j = r.slice(M.length), P = j.indexOf("/");
13220
- N.add(M + (P === -1 ? j : j.slice(0, P)));
13221
- }
13222
- return [...N].sort().map(Filepath.from);
13223
- }
13224
- getEntryPoints() {
13225
- return [...this.files.keys()].map(Filepath.from).sort((r, j) => r.absolute.localeCompare(j.absolute));
13226
- }
13227
- clone() {
13228
- return new r(this.files);
13229
- }
13230
13177
  };
13231
13178
  function containerStack(r, j) {
13232
13179
  let M = this.token.flatStream(r), { index: N, token: P } = this.container.token(r, j), F = N === void 0 ? -1 : findLastIndex_default(M, (r) => !r.isInvalid, N);
@@ -13241,7 +13188,7 @@ function containerStack(r, j) {
13241
13188
  for (; R.length > 0;) {
13242
13189
  let M = !1, N = last_default(R);
13243
13190
  if (N instanceof FunctionApplicationNode) {
13244
- let P = this.layout.getSource(r) || "";
13191
+ let P = this.getSource(r) || "";
13245
13192
  for (let r = N.end; r < j; r += 1) P[r] === "\n" && (R.pop(), M = !0);
13246
13193
  } else N instanceof PrefixExpressionNode || N instanceof InfixExpressionNode ? this.container.token(r, j).token !== N.op && (R.pop(), M = !0) : N instanceof ListExpressionNode ? N.listCloseBracket && N.end <= j && (R.pop(), M = !0) : N instanceof TupleExpressionNode ? N.tupleCloseParen && N.end <= j && (R.pop(), M = !0) : N instanceof CommaExpressionNode ? N.end <= j && (R.pop(), M = !0) : N instanceof BlockExpressionNode ? N.blockCloseBrace && N.end <= j && (R.pop(), M = !0) : N instanceof IdentifierStreamNode || N.end < j && (R.pop(), M = !0);
13247
13194
  if (M) {
@@ -13357,16 +13304,19 @@ function fileDependencies(r) {
13357
13304
  }
13358
13305
  return M;
13359
13306
  }
13360
- function reachableFiles(r) {
13361
- let j = /* @__PURE__ */ new Set(), M = [], N = r ? [r] : this.layout.getEntryPoints(), P = 0;
13362
- for (; P < N.length;) {
13363
- let r = N[P++], F = r.intern();
13364
- if (!j.has(F)) {
13365
- j.add(F), M.push(r);
13366
- for (let j of this.fileDependencies(r)) N.push(j);
13307
+ function walkDependencies(r, j) {
13308
+ let M = /* @__PURE__ */ new Set(), N = [], P = [...j], F = 0;
13309
+ for (; F < P.length;) {
13310
+ let j = P[F++], I = j.intern();
13311
+ if (!M.has(I)) {
13312
+ M.add(I), N.push(j);
13313
+ for (let M of r.fileDependencies(j)) P.push(M);
13367
13314
  }
13368
13315
  }
13369
- return M;
13316
+ return N;
13317
+ }
13318
+ function reachableFiles(r) {
13319
+ return walkDependencies(this, r ? [r] : this.layout.getEntrypoints());
13370
13320
  }
13371
13321
  function ast(r) {
13372
13322
  return this.bindFile(r), this.parseFile(r).getValue().ast;
@@ -13385,13 +13335,8 @@ function rawDb(r) {
13385
13335
  if (N) return this.interpretSymbol(N, r).getFiltered(UNHANDLED);
13386
13336
  }
13387
13337
  function publicSymbolTable(r) {
13388
- let M = this.parseFile(r).getValue().ast, N = this.nodeSymbol(M);
13389
- if (N.hasValue(UNHANDLED)) return;
13390
- let P = this.symbolMembers(N.getValue());
13391
- if (P.hasValue(UNHANDLED)) return;
13392
- let F = [];
13393
- for (let r of P.getValue()) F.push(r);
13394
- return F;
13338
+ let M = this.parseFile(r).getValue().ast, N = this.nodeSymbol(M).getFiltered(UNHANDLED);
13339
+ if (N) return this.symbolMembers(N).getFiltered(UNHANDLED);
13395
13340
  }
13396
13341
  function flatStream(r) {
13397
13342
  return this.parseFile(r).getValue().tokens.flatMap((r) => [
@@ -13417,7 +13362,7 @@ function bindFile(r) {
13417
13362
  return new Report(void 0, [...j.getErrors(), ...P.getErrors()], [...j.getWarnings(), ...P.getWarnings()]);
13418
13363
  }
13419
13364
  function bindProject() {
13420
- let r = collectTransitiveDependencies(this, this.layout.getEntryPoints()), j = /* @__PURE__ */ new Map();
13365
+ let r = collectTransitiveDependencies(this, this.layout.getEntrypoints()), j = /* @__PURE__ */ new Map();
13421
13366
  for (let M of r) {
13422
13367
  let r = this.bindFile(M);
13423
13368
  j.set(M.absolute, r);
@@ -13425,14 +13370,12 @@ function bindProject() {
13425
13370
  return j;
13426
13371
  }
13427
13372
  function interpretFile(r) {
13428
- let N = this.bindFile(r);
13429
- if (N.getErrors().length > 0) return N.map(() => void 0);
13430
- let P = this.parseFile(r).getValue().ast, F = this.nodeSymbol(P).getFiltered(UNHANDLED);
13431
- return F ? this.interpretSymbol(F, r).map((r) => r === UNHANDLED || !r ? void 0 : r) : Report.create(void 0);
13373
+ let { ast: N } = this.parseFile(r).getValue(), P = this.nodeSymbol(N).getFiltered(UNHANDLED);
13374
+ return P ? this.interpretSymbol(P, r).map((r) => r === UNHANDLED || !r ? void 0 : r) : Report.create(void 0);
13432
13375
  }
13433
13376
  function interpretProject() {
13434
13377
  let r = [], N = [], P = /* @__PURE__ */ new Set(), F = [];
13435
- for (let r of this.layout.getEntryPoints()) for (let j of this.reachableFiles(r)) {
13378
+ for (let r of this.layout.getEntrypoints()) for (let j of this.reachableFiles(r)) {
13436
13379
  let r = j.intern();
13437
13380
  P.has(r) || (P.add(r), F.push(j));
13438
13381
  }
@@ -13440,8 +13383,10 @@ function interpretProject() {
13440
13383
  for (let P of F) {
13441
13384
  let F = this.parseFile(P);
13442
13385
  r.push(...F.getErrors()), N.push(...F.getWarnings());
13443
- let L = F.getValue().ast, R = this.nodeSymbol(L).getFiltered(UNHANDLED), z = R ? this.interpretSymbol(R, P) : Report.create(UNHANDLED), B = z.getFiltered(UNHANDLED);
13444
- B && I.set(P.absolute, B), r.push(...z.getErrors()), N.push(...z.getWarnings());
13386
+ let L = this.bindFile(P);
13387
+ r.push(...L.getErrors()), N.push(...L.getWarnings());
13388
+ let { ast: R } = F.getValue(), z = this.nodeSymbol(R).getFiltered(UNHANDLED), B = z ? this.interpretSymbol(z, P) : Report.create(UNHANDLED), V = B.getFiltered(UNHANDLED);
13389
+ V && I.set(P.absolute, V), r.push(...B.getErrors()), N.push(...B.getWarnings());
13445
13390
  }
13446
13391
  let L = {};
13447
13392
  for (let [r, j] of I) L[r] = j;
@@ -13795,7 +13740,47 @@ var ParsingContextStack = class {
13795
13740
  }
13796
13741
  }, Parser = class {
13797
13742
  constructor(r, j, M, N) {
13798
- this.current = 0, this.errors = [], this.contextStack = new ParsingContextStack(), this.synchronizeProgram = () => {
13743
+ this.current = 0, this.errors = [], this.contextStack = new ParsingContextStack(), this.useSpecifierList = this.contextStack.withContextDo(ParsingContext.BlockExpression, () => {
13744
+ let r = { specifiers: [] }, j = () => this.nodeFactory.create(UseSpecifierListNode, r);
13745
+ try {
13746
+ this.consume("Expect an opening brace '{'", SyntaxTokenKind.LBRACE), r.openBrace = this.previous();
13747
+ } catch (r) {
13748
+ if (!(r instanceof PartialParsingError)) throw r;
13749
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
13750
+ this.synchronizeUseSpecifierList();
13751
+ }
13752
+ for (; !this.isAtEnd() && !this.check(SyntaxTokenKind.RBRACE);) {
13753
+ try {
13754
+ r.specifiers.push(this.useSpecifier());
13755
+ } catch (M) {
13756
+ if (!(M instanceof PartialParsingError)) throw M;
13757
+ if (M.partialNode instanceof UseSpecifierNode && r.specifiers.push(M.partialNode), !this.canHandle(M)) throw new PartialParsingError(M.token, j(), M.handlerContext);
13758
+ this.synchronizeUseSpecifierList();
13759
+ }
13760
+ if (this.check(SyntaxTokenKind.IDENTIFIER) && isFromKeyword(this.peek())) break;
13761
+ !hasTrailingNewLines(this.previous()) && !this.check(SyntaxTokenKind.RBRACE) && this.logError(this.peek(), CompileErrorCode.MISSING_SPACES, "Expect a newline between use specifiers");
13762
+ }
13763
+ try {
13764
+ this.consume("Expect a closing brace '}'", SyntaxTokenKind.RBRACE), r.closeBrace = this.previous();
13765
+ } catch (r) {
13766
+ if (!(r instanceof PartialParsingError)) throw r;
13767
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
13768
+ this.synchronizeUseSpecifierList();
13769
+ }
13770
+ return j();
13771
+ }), this.synchronizeUseSpecifierList = () => {
13772
+ if (!this.check(SyntaxTokenKind.RBRACE)) for (markInvalid(this.advance()); !this.isAtEnd();) {
13773
+ let r = this.peek();
13774
+ if (this.check(SyntaxTokenKind.RBRACE) || this.check(SyntaxTokenKind.STRING_LITERAL) || this.check(SyntaxTokenKind.IDENTIFIER) && isFromKeyword(r) || isAtStartOfLine(this.previous(), r)) break;
13775
+ markInvalid(r), this.advance();
13776
+ }
13777
+ }, this.synchronizeUseSpecifier = () => {
13778
+ for (; !this.isAtEnd();) {
13779
+ let r = this.peek();
13780
+ if (this.check(SyntaxTokenKind.RBRACE) || isAtStartOfLine(this.previous(), r)) break;
13781
+ markInvalid(r), this.advance();
13782
+ }
13783
+ }, this.synchronizeProgram = () => {
13799
13784
  let r = this.peek();
13800
13785
  r.kind === SyntaxTokenKind.EOF ? this.logError(r, CompileErrorCode.UNEXPECTED_EOF, "Unexpected EOF") : markInvalid(this.advance());
13801
13786
  }, this.synchronizeElementDeclarationName = () => {
@@ -14017,59 +14002,39 @@ var ParsingContextStack = class {
14017
14002
  if (this.advance(), r.useKeyword = this.previous(), this.peek().kind === SyntaxTokenKind.WILDCARD) this.advance(), r.specifiers = this.nodeFactory.create(WildcardNode, { token: this.previous() });
14018
14003
  else try {
14019
14004
  r.specifiers = this.useSpecifierList();
14020
- } catch (M) {
14021
- throw M instanceof PartialParsingError ? (r.specifiers = M.partialNode, new PartialParsingError(M.token, j(), M.handlerContext)) : M;
14005
+ } catch (j) {
14006
+ if (!(j instanceof PartialParsingError)) throw j;
14007
+ j.partialNode instanceof UseSpecifierListNode && (r.specifiers = j.partialNode);
14022
14008
  }
14023
14009
  let M = r.specifiers instanceof WildcardNode ? "'*'" : "specifier list";
14024
- if (isFromKeyword(this.peek())) r.fromKeyword = this.advance();
14025
- else throw this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, `Expect 'from' after ${M}`), new PartialParsingError(this.peek(), j(), this.contextStack.findHandlerContext(this.tokens, this.current));
14026
- if (this.match(SyntaxTokenKind.STRING_LITERAL)) r.importPath = this.previous();
14027
- else throw this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect a string literal path"), new PartialParsingError(this.peek(), j(), this.contextStack.findHandlerContext(this.tokens, this.current));
14028
- return j();
14029
- }
14030
- useSpecifierList() {
14031
- let r = { specifiers: [] }, j = () => this.nodeFactory.create(UseSpecifierListNode, r);
14032
- try {
14033
- this.consume("Expect an opening brace '{'", SyntaxTokenKind.LBRACE), r.openBrace = this.previous();
14034
- } catch (r) {
14035
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14036
- }
14037
- for (; !this.isAtEnd() && !this.check(SyntaxTokenKind.RBRACE);) {
14038
- try {
14039
- r.specifiers.push(this.useSpecifier());
14040
- } catch (M) {
14041
- throw M instanceof PartialParsingError ? (M.partialNode instanceof UseSpecifierNode && r.specifiers.push(M.partialNode), new PartialParsingError(M.token, j(), M.handlerContext)) : M;
14042
- }
14043
- if (this.check(SyntaxTokenKind.IDENTIFIER) && isFromKeyword(this.peek())) break;
14044
- !hasTrailingNewLines(this.previous()) && !this.check(SyntaxTokenKind.RBRACE) && this.logError(this.peek(), CompileErrorCode.MISSING_SPACES, "Expect a newline between use specifiers");
14045
- }
14046
- try {
14047
- this.consume("Expect a closing brace '}'", SyntaxTokenKind.RBRACE), r.closeBrace = this.previous();
14048
- } catch (r) {
14049
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14050
- }
14051
- return j();
14010
+ return isFromKeyword(this.peek()) ? r.fromKeyword = this.advance() : this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, `Expect 'from' after ${M}`), this.match(SyntaxTokenKind.STRING_LITERAL) ? r.importPath = this.previous() : this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect a string literal path"), j();
14052
14011
  }
14053
14012
  useSpecifier() {
14054
14013
  let r = {}, j = () => this.nodeFactory.create(UseSpecifierNode, r);
14055
14014
  try {
14056
14015
  this.consume("Expect an import kind specifier (e.g. table, enum)", SyntaxTokenKind.IDENTIFIER), r.importKind = this.previous();
14057
14016
  } catch (r) {
14058
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14017
+ if (!(r instanceof PartialParsingError)) throw r;
14018
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
14019
+ this.synchronizeUseSpecifier();
14059
14020
  }
14060
14021
  if (this.check(SyntaxTokenKind.IDENTIFIER, SyntaxTokenKind.QUOTED_STRING)) try {
14061
14022
  r.name = this.normalExpression();
14062
- } catch (r) {
14063
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14023
+ } catch (M) {
14024
+ if (!(M instanceof PartialParsingError)) throw M;
14025
+ if (M.partialNode instanceof SyntaxNode && (r.name = M.partialNode), !this.canHandle(M)) throw new PartialParsingError(M.token, j(), M.handlerContext);
14026
+ this.synchronizeUseSpecifier();
14064
14027
  }
14065
- else this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect an element name");
14028
+ else r.importKind && this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect an element name");
14066
14029
  if (isAsKeyword(this.peek())) {
14067
14030
  r.asKeyword = this.advance();
14068
14031
  try {
14069
14032
  if (!this.check(SyntaxTokenKind.IDENTIFIER, SyntaxTokenKind.QUOTED_STRING)) throw this.logError(this.peek(), CompileErrorCode.UNEXPECTED_TOKEN, "Expect an alias name after 'as'"), new PartialParsingError(this.peek(), j(), this.contextStack.findHandlerContext(this.tokens, this.current));
14070
14033
  r.alias = this.normalExpression();
14071
14034
  } catch (r) {
14072
- throw r instanceof PartialParsingError ? new PartialParsingError(r.token, j(), r.handlerContext) : r;
14035
+ if (!(r instanceof PartialParsingError)) throw r;
14036
+ if (!this.canHandle(r)) throw new PartialParsingError(r.token, j(), r.handlerContext);
14037
+ this.synchronizeUseSpecifier();
14073
14038
  }
14074
14039
  }
14075
14040
  return j();
@@ -14449,7 +14414,7 @@ function postfixBindingPower(r) {
14449
14414
  };
14450
14415
  }
14451
14416
  function parseFile(r) {
14452
- let j = this.layout.getSource(r);
14417
+ let j = this.getSource(r);
14453
14418
  return new Lexer(j ?? "", r).lex().chain((M) => new Parser(j ?? "", M, this.nodeIdGenerator, r).parse()).map(({ ast: j, tokens: M }) => ({
14454
14419
  ast: j,
14455
14420
  tokens: M,
@@ -14457,7 +14422,7 @@ function parseFile(r) {
14457
14422
  }));
14458
14423
  }
14459
14424
  function parseProject() {
14460
- let r = collectTransitiveDependencies(this, this.layout.getEntryPoints()), j = /* @__PURE__ */ new Map();
14425
+ let r = collectTransitiveDependencies(this, this.layout.getEntrypoints()), j = /* @__PURE__ */ new Map();
14461
14426
  for (let M of r) {
14462
14427
  let r = this.parseFile(M);
14463
14428
  j.set(M.absolute, r);
@@ -14660,59 +14625,60 @@ function findReplacements(r, j, M, N, P) {
14660
14625
  return F;
14661
14626
  }
14662
14627
  function renameTable(r, M, N) {
14663
- let P = this.layout.clone(), F = normalizeTableName(M), I = normalizeTableName(N), L = F.schema, R = F.table, z = I.schema, B = I.table, V = lookupTableSymbol(this, r, L, R);
14664
- if (!V || V instanceof AliasSymbol || checkForNameCollision(this, r, L, R, z, B)) return P;
14665
- if (V instanceof UseSymbol && V.useSpecifierDeclaration instanceof UseSpecifierNode && V.useSpecifierDeclaration.alias) {
14666
- let M = V.useSpecifierDeclaration;
14667
- if (this.nodeAlias(M).getFiltered(UNHANDLED) === R) return renameAlias(this, P, r, M, V, z, B);
14668
- }
14669
- return renameRealDeclaration(this, P, V, L, R, z, B);
14670
- }
14671
- function renameAlias(r, j, M, N, P, F, I) {
14672
- let L = r.layout.getSource(M) ?? "";
14673
- if (!N.alias) return j;
14674
- let R = formatTableName(F, I, L.substring(N.alias.start, N.alias.end).includes("\"")), z = [];
14675
- z.push({
14676
- start: N.alias.start,
14677
- end: N.alias.end,
14678
- newText: R.formattedTable
14628
+ let P = normalizeTableName(M), F = normalizeTableName(N), I = P.schema, L = P.table, R = F.schema, z = F.table, B = lookupTableSymbol(this, r, I, L);
14629
+ if (!B || B instanceof AliasSymbol || checkForNameCollision(this, r, I, L, R, z)) return /* @__PURE__ */ new Map();
14630
+ if (B instanceof UseSymbol && B.useSpecifierDeclaration instanceof UseSpecifierNode && B.useSpecifierDeclaration.alias) {
14631
+ let M = B.useSpecifierDeclaration;
14632
+ if (this.nodeAlias(M).getFiltered(UNHANDLED) === L) return renameAlias(this, r, M, B, R, z);
14633
+ }
14634
+ return renameRealDeclaration(this, B, I, L, R, z);
14635
+ }
14636
+ function renameAlias(r, j, M, N, P, F) {
14637
+ let I = r.getSource(j) ?? "";
14638
+ if (!M.alias) return /* @__PURE__ */ new Map();
14639
+ let L = formatTableName(P, F, I.substring(M.alias.start, M.alias.end).includes("\"")), R = [];
14640
+ R.push({
14641
+ start: M.alias.start,
14642
+ end: M.alias.end,
14643
+ newText: L.formattedTable
14679
14644
  });
14680
- let B = r.symbolReferences(P);
14681
- for (let r of B) r.filepath.absolute === M.absolute && r !== N.alias && z.push({
14645
+ let z = r.symbolReferences(N);
14646
+ for (let r of z) r.filepath.absolute === j.absolute && r !== M.alias && R.push({
14682
14647
  start: r.start,
14683
14648
  end: r.end,
14684
- newText: R.formattedTable
14649
+ newText: L.formattedTable
14685
14650
  });
14686
- return j.setSource(M, applyTextEdits(L, z)), j;
14687
- }
14688
- function renameRealDeclaration(r, j, M, N, P, F, I) {
14689
- let L = M.originalSymbol, R = L.declaration;
14690
- if (!R || !(R instanceof ElementDeclarationNode)) return j;
14691
- let z = R.filepath;
14692
- if (!z) return j;
14693
- let B = formatTableName(F, I, checkIfDeclarationUsesQuotes(R, r.layout.getSource(z) ?? "")), V = /* @__PURE__ */ new Map(), H = (r, j) => {
14694
- let M = r.absolute, N = V.get(M);
14651
+ return new Map([[j.absolute, applyTextEdits(I, R)]]);
14652
+ }
14653
+ function renameRealDeclaration(r, j, M, N, P, F) {
14654
+ let I = j.originalSymbol, L = I.declaration;
14655
+ if (!L || !(L instanceof ElementDeclarationNode)) return /* @__PURE__ */ new Map();
14656
+ let R = L.filepath;
14657
+ if (!R) return /* @__PURE__ */ new Map();
14658
+ let z = formatTableName(P, F, checkIfDeclarationUsesQuotes(L, r.getSource(R) ?? "")), B = /* @__PURE__ */ new Map(), V = (r, j) => {
14659
+ let M = r.absolute, N = B.get(M);
14695
14660
  N || (N = {
14696
14661
  fp: r,
14697
14662
  edits: []
14698
- }, V.set(M, N)), N.edits.push(j);
14699
- }, U = R.name;
14700
- U && H(z, {
14701
- start: U.start,
14702
- end: U.end,
14703
- newText: buildReplacementText(B)
14663
+ }, B.set(M, N)), N.edits.push(j);
14664
+ }, H = L.name;
14665
+ H && V(R, {
14666
+ start: H.start,
14667
+ end: H.end,
14668
+ newText: buildReplacementText(z)
14704
14669
  });
14705
- let W = r.symbolReferences(L);
14706
- for (let j of W) {
14707
- if (j === U) continue;
14708
- let M = j.filepath, F = r.layout.getSource(M) ?? "";
14709
- for (let r of findReplacements([j], N, P, B, F)) H(M, r);
14670
+ let U = r.symbolReferences(I);
14671
+ for (let j of U) {
14672
+ if (j === H) continue;
14673
+ let P = j.filepath, F = r.getSource(P) ?? "";
14674
+ for (let r of findReplacements([j], M, N, z, F)) V(P, r);
14710
14675
  }
14711
- for (let { fp: M, edits: N } of V.values()) {
14712
- let P = r.layout.getSource(M) ?? "";
14713
- j.setSource(M, applyTextEdits(P, N));
14676
+ let W = /* @__PURE__ */ new Map();
14677
+ for (let { fp: j, edits: M } of B.values()) {
14678
+ let N = r.getSource(j) ?? "";
14679
+ W.set(j.absolute, applyTextEdits(N, M));
14714
14680
  }
14715
- return j;
14681
+ return W;
14716
14682
  }
14717
14683
  function findDiagramViewBlocks(r) {
14718
14684
  let j = [], M = new Lexer(r, DEFAULT_ENTRY).lex();
@@ -14775,7 +14741,7 @@ function generateDiagramViewBlock(r, j) {
14775
14741
  return B.length > 0 && emitTablesBlock(G, B), V.length > 0 && emitTableGroupsBlock(G, V), H.length > 0 && emitSchemasBlock(G, H), W && emitNotesBlock(G, I), G.push("}"), G.join("\n");
14776
14742
  }
14777
14743
  function syncDiagramView(r, j, M) {
14778
- let N = this.layout.getSource(r) ?? "", P = M ?? findDiagramViewBlocks(N), F = [];
14744
+ let N = this.getSource(r) ?? "", P = M ?? findDiagramViewBlocks(N), F = [];
14779
14745
  for (let r of j) {
14780
14746
  let j = applyOperation(N, r, P);
14781
14747
  F.push(...j);
@@ -14835,12 +14801,12 @@ function computeDeleteEdit(r, j, M) {
14835
14801
  }];
14836
14802
  }
14837
14803
  var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14838
- constructor() {
14839
- this.nodeIdGenerator = new SyntaxNodeIdGenerator(), this.symbolIdGenerator = new NodeSymbolIdGenerator(), this.symbolFactory = new SymbolFactory(this.symbolIdGenerator), this.layout = new MemoryProjectLayout(), this.globalCache = /* @__PURE__ */ new Map(), this.localCache = /* @__PURE__ */ new Map(), this.parseFile = this.localQuery(parseFile), this.parseProject = this.globalQuery(parseProject), this.validateNode = this.localQuery(validateNode), this.validateFile = this.localQuery(validateFile), this.nodeFullname = this.localQuery(nodeFullname), this.nodeAlias = this.localQuery(nodeAlias), this.nodeSettings = this.localQuery(nodeSettings), this.canonicalName = this.globalQuery(canonicalName), this.bindNode = this.globalQuery(bindNode), this.bindFile = this.globalQuery(bindFile), this.bindProject = this.globalQuery(bindProject), this.interpretSymbol = this.globalQuery(interpretSymbol), this.interpretMetadata = interpretMetadata.bind(this), this.interpretFile = this.globalQuery(interpretFile), this.interpretProject = this.globalQuery(interpretProject), this.nodeSymbol = this.globalQuery(nodeSymbol), this.nodeMetadata = this.globalQuery(nodeMetadata), this.symbolMembers = this.globalQuery(symbolMembers), this.lookupMembers = this.globalQuery(lookupMembers), this.nodeReferee = this.globalQuery(nodeReferee), this.resolutionIndex = this.globalQuery(resolutionIndex), this.symbolReferences = this.globalQuery(symbolReferences), this.symbolParent = this.globalQuery(symbolParent), this.symbolMetadata = this.globalQuery(symbolMetadata), this.symbolAliases = this.globalQuery(symbolAliases), this.symbolUses = this.globalQuery(symbolUses), this.fileDependencies = this.localQuery(fileDependencies), this.reachableFiles = this.globalQuery(reachableFiles), this.usableMembers = this.globalQuery(usableMembers), this.nodeAtPosition = this.localQuery(nodeAtPosition), this.renameTable = renameTable.bind(this), this.syncDiagramView = syncDiagramView.bind(this), this.token = {
14804
+ constructor(r) {
14805
+ this.layout = r, this.nodeIdGenerator = new SyntaxNodeIdGenerator(), this.symbolIdGenerator = new NodeSymbolIdGenerator(), this.symbolFactory = new SymbolFactory(this.symbolIdGenerator), this.sourceSnapshot = /* @__PURE__ */ new Map(), this.globalCache = /* @__PURE__ */ new Map(), this.localCache = /* @__PURE__ */ new Map(), this.parseFile = this.localQuery(parseFile), this.parseProject = this.globalQuery(parseProject), this.validateNode = this.localQuery(validateNode), this.validateFile = this.localQuery(validateFile), this.nodeFullname = this.localQuery(nodeFullname), this.nodeAlias = this.localQuery(nodeAlias), this.nodeSettings = this.localQuery(nodeSettings), this.canonicalName = this.globalQuery(canonicalName), this.bindNode = this.globalQuery(bindNode), this.bindFile = this.globalQuery(bindFile), this.bindProject = this.globalQuery(bindProject), this.interpretSymbol = this.globalQuery(interpretSymbol), this.interpretMetadata = interpretMetadata.bind(this), this.interpretFile = this.globalQuery(interpretFile), this.interpretProject = this.globalQuery(interpretProject), this.nodeSymbol = this.globalQuery(nodeSymbol), this.nodeMetadata = this.globalQuery(nodeMetadata), this.symbolMembers = this.globalQuery(symbolMembers), this.lookupMembers = this.globalQuery(lookupMembers), this.nodeReferee = this.globalQuery(nodeReferee), this.resolutionIndex = this.globalQuery(resolutionIndex), this.symbolReferences = this.globalQuery(symbolReferences), this.symbolParent = this.globalQuery(symbolParent), this.symbolMetadata = this.globalQuery(symbolMetadata), this.symbolAliases = this.globalQuery(symbolAliases), this.symbolUses = this.globalQuery(symbolUses), this.fileDependencies = this.localQuery(fileDependencies), this.reachableFiles = this.globalQuery(reachableFiles), this.usableMembers = this.globalQuery(usableMembers), this.nodeAtPosition = this.localQuery(nodeAtPosition), this.renameTable = renameTable.bind(this), this.syncDiagramView = syncDiagramView.bind(this), this.token = {
14840
14806
  invalidStream: this.localQuery(invalidStream),
14841
14807
  flatStream: this.localQuery(flatStream)
14842
14808
  }, this.parse = {
14843
- source: (r) => this.layout.getSource(r) || "",
14809
+ source: (r) => this.getSource(r) || "",
14844
14810
  ast: (r = DEFAULT_ENTRY) => ast.call(this, r),
14845
14811
  errors: (r = DEFAULT_ENTRY) => errors.call(this, r),
14846
14812
  warnings: (r = DEFAULT_ENTRY) => warnings.call(this, r),
@@ -14855,19 +14821,23 @@ var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14855
14821
  scopeKind: this.localQuery(containerScopeKind)
14856
14822
  };
14857
14823
  }
14858
- setSource(r, j) {
14859
- this.layout.setSource(r, j), this.localCache.delete(r.intern()), this.globalCache.clear();
14824
+ getSource(r) {
14825
+ return this.layout.getSource(r);
14860
14826
  }
14861
- clearSource() {
14862
- this.layout = new MemoryProjectLayout(), this.localCache.clear(), this.globalCache.clear();
14827
+ cleanStaleLocalCache(r) {
14828
+ let j = this.layout.getSource(r), M = r.absolute;
14829
+ this.sourceSnapshot.has(M) && this.sourceSnapshot.get(M) !== j && (this.localCache.delete(r.intern()), this.globalCache.clear()), this.sourceSnapshot.set(M, j);
14863
14830
  }
14864
- deleteSource(r) {
14865
- this.layout.deleteSource(r), this.localCache.delete(r.intern()), this.globalCache.clear();
14831
+ cleanStaleGlobalCache() {
14832
+ let r = this.layout.getEntrypoints(), j = !1;
14833
+ this.entrypointsSnapshot !== void 0 && (this.entrypointsSnapshot.length !== r.length || this.entrypointsSnapshot.some((j, M) => j.absolute !== r[M].absolute)) && (j = !0), this.entrypointsSnapshot = r;
14834
+ for (let j of walkDependencies(this, r)) this.cleanStaleLocalCache(j);
14835
+ j && this.globalCache.clear();
14866
14836
  }
14867
14837
  globalQuery(r) {
14868
14838
  let j = Symbol();
14869
14839
  return ((...M) => {
14870
- this.globalCache.has(j) || this.globalCache.set(j, /* @__PURE__ */ new Map());
14840
+ this.cleanStaleGlobalCache(), this.globalCache.has(j) || this.globalCache.set(j, /* @__PURE__ */ new Map());
14871
14841
  let N = M.map((r) => intern(r)).join("\0"), P = this.globalCache.get(j);
14872
14842
  if (P.has(N)) {
14873
14843
  let j = P.get(N);
@@ -14886,25 +14856,27 @@ var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14886
14856
  localQuery(r) {
14887
14857
  let j = Symbol();
14888
14858
  return ((...M) => {
14889
- let [N] = M, P = (N instanceof SyntaxNode ? N.filepath : N).intern(), F = M.map((r) => intern(r)).join("\0"), I = this.localCache.get(P);
14890
- I || (I = /* @__PURE__ */ new Map(), this.localCache.set(P, I));
14891
- let L = I.get(j);
14892
- if (L || (L = /* @__PURE__ */ new Map(), I.set(j, L)), L.has(F)) {
14893
- let j = L.get(F);
14894
- if (j === COMPUTING) throw Error(`Cycle detected in query: ${r.name}(${P}, ${F})`);
14859
+ let [N] = M, P = N instanceof SyntaxNode ? N.filepath : N;
14860
+ this.cleanStaleLocalCache(P);
14861
+ let F = P.intern(), I = M.map((r) => intern(r)).join("\0"), L = this.localCache.get(F);
14862
+ L || (L = /* @__PURE__ */ new Map(), this.localCache.set(F, L));
14863
+ let R = L.get(j);
14864
+ if (R || (R = /* @__PURE__ */ new Map(), L.set(j, R)), R.has(I)) {
14865
+ let j = R.get(I);
14866
+ if (j === COMPUTING) throw Error(`Cycle detected in query: ${r.name}(${F}, ${I})`);
14895
14867
  return j;
14896
14868
  }
14897
- L.set(F, COMPUTING);
14869
+ R.set(I, COMPUTING);
14898
14870
  try {
14899
14871
  let j = r.apply(this, M);
14900
- return L.set(F, j), j;
14872
+ return R.set(I, j), j;
14901
14873
  } catch (r) {
14902
- throw L.delete(F), r;
14874
+ throw R.delete(I), r;
14903
14875
  }
14904
14876
  });
14905
14877
  }
14906
14878
  findDiagramViewBlocks(r) {
14907
- return findDiagramViewBlocks(this.layout.getSource(r) ?? "");
14879
+ return findDiagramViewBlocks(this.getSource(r) ?? "");
14908
14880
  }
14909
14881
  initMonacoServices(r) {
14910
14882
  let j = r?.autocompletion?.triggerCharacters ?? [
@@ -14929,5 +14901,47 @@ var COMPUTING = Symbol("COMPUTING"), Compiler = class {
14929
14901
  diagnosticsProvider: new DBMLDiagnosticsProvider(this)
14930
14902
  };
14931
14903
  }
14904
+ }, MemoryProjectLayout = class r {
14905
+ constructor(r = {}) {
14906
+ this.files = r instanceof Map ? new Map(r) : new Map(Object.entries(r));
14907
+ }
14908
+ setSource(r, j) {
14909
+ this.files.set(r.absolute, j);
14910
+ }
14911
+ getSource(r) {
14912
+ return this.files.get(r.absolute) ?? void 0;
14913
+ }
14914
+ deleteSource(r) {
14915
+ this.files.delete(r.absolute);
14916
+ }
14917
+ clearSource() {
14918
+ this.files.clear();
14919
+ }
14920
+ exists(r) {
14921
+ return this.isFile(r) || this.isDirectory(r);
14922
+ }
14923
+ isFile(r) {
14924
+ return this.files.has(r.absolute);
14925
+ }
14926
+ isDirectory(r) {
14927
+ let j = r.absolute.endsWith("/") ? r.absolute : `${r.absolute}/`;
14928
+ for (let r of this.files.keys()) if (r.startsWith(j)) return !0;
14929
+ return !1;
14930
+ }
14931
+ listDirectory(r) {
14932
+ let j = r?.absolute ?? "/", M = j.endsWith("/") ? j : j + "/", N = /* @__PURE__ */ new Set();
14933
+ for (let r of this.files.keys()) {
14934
+ if (!r.startsWith(M)) continue;
14935
+ let j = r.slice(M.length), P = j.indexOf("/");
14936
+ N.add(M + (P === -1 ? j : j.slice(0, P)));
14937
+ }
14938
+ return [...N].sort().map(Filepath.from);
14939
+ }
14940
+ getEntrypoints() {
14941
+ return [...this.files.keys()].map(Filepath.from).sort((r, j) => r.absolute.localeCompare(j.absolute));
14942
+ }
14943
+ clone() {
14944
+ return new r(this.files);
14945
+ }
14932
14946
  };
14933
14947
  export { ArrayNode, AttributeNode, BlockExpressionNode, CallExpressionNode, CommaExpressionNode, CompileError, CompileErrorCode, Compiler, CompletionItemInsertTextRule, CompletionItemKind, DBMLCompletionItemProvider, DBMLDefinitionProvider, DBMLDiagnosticsProvider, DBMLReferencesProvider, DEFAULT_ENTRY, ElementDeclarationNode, ElementKind, EmptyNode, FALSY_VALUES, Filepath, FunctionApplicationNode, FunctionExpressionNode, GroupExpressionNode, IdentifierStreamNode, InfixExpressionNode, ListExpressionNode, LiteralNode, MarkerSeverity, MemoryProjectLayout, PostfixExpressionNode, PrefixExpressionNode, PrimaryExpressionNode, ProgramNode, ScopeKind, SymbolKind, SyntaxNode, SyntaxNodeIdGenerator, SyntaxNodeKind, SyntaxToken, SyntaxTokenKind, TRUTHY_VALUES, TupleExpressionNode, UNHANDLED, URI as Uri, UseDeclarationNode, UseSpecifierListNode, UseSpecifierNode, VariableNode, WildcardNode, addDoubleQuoteIfNeeded, dbmlLanguageConfig, dbmlMonarchTokensProvider, escapeString, extractNumericLiteral, extractSignedNumber, formatRecordValue, getRecordValueType, isBinaryType, isBooleanType, isDateTimeType, isEmptyStringLiteral, isFloatType, isFunctionExpression, isIntegerType, isNullish, isNumericType, isOp, isOpToken, isSerialType, isStringType, isTriviaToken, isValidIdentifier, normalizeTypeName, splitQualifiedIdentifier, tryExtractBoolean, tryExtractDateTime, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString, unescapeString, validateForeignKeys, validatePrimaryKey, validateUnique };