@prisma-next/psl-parser 0.14.0 → 0.15.0-dev.10

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.
Files changed (75) hide show
  1. package/README.md +28 -10
  2. package/dist/declarations-DR6To8_k.mjs +1060 -0
  3. package/dist/declarations-DR6To8_k.mjs.map +1 -0
  4. package/dist/format.d.mts +19 -0
  5. package/dist/format.d.mts.map +1 -0
  6. package/dist/format.mjs +470 -0
  7. package/dist/format.mjs.map +1 -0
  8. package/dist/index.d.mts +144 -3
  9. package/dist/index.d.mts.map +1 -1
  10. package/dist/index.mjs +849 -3
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/interpret.d.mts +39 -0
  13. package/dist/interpret.d.mts.map +1 -0
  14. package/dist/interpret.mjs +25 -0
  15. package/dist/interpret.mjs.map +1 -0
  16. package/dist/parse-BazJr7Ye.d.mts +426 -0
  17. package/dist/parse-BazJr7Ye.d.mts.map +1 -0
  18. package/dist/parse-CdeXr0T3.mjs +626 -0
  19. package/dist/parse-CdeXr0T3.mjs.map +1 -0
  20. package/dist/symbol-table-C-AH04Ug.d.mts +127 -0
  21. package/dist/symbol-table-C-AH04Ug.d.mts.map +1 -0
  22. package/dist/syntax.d.mts +21 -347
  23. package/dist/syntax.d.mts.map +1 -1
  24. package/dist/syntax.mjs +26 -1401
  25. package/dist/syntax.mjs.map +1 -1
  26. package/package.json +11 -8
  27. package/src/attribute-spec/combinators/bool.ts +19 -0
  28. package/src/attribute-spec/combinators/diagnostic.ts +15 -0
  29. package/src/attribute-spec/combinators/entity-ref.ts +24 -0
  30. package/src/attribute-spec/combinators/field-ref.ts +36 -0
  31. package/src/attribute-spec/combinators/func-call.ts +65 -0
  32. package/src/attribute-spec/combinators/identifier.ts +16 -0
  33. package/src/attribute-spec/combinators/int.ts +35 -0
  34. package/src/attribute-spec/combinators/list.ts +43 -0
  35. package/src/attribute-spec/combinators/num.ts +26 -0
  36. package/src/attribute-spec/combinators/one-of.ts +29 -0
  37. package/src/attribute-spec/combinators/record.ts +43 -0
  38. package/src/attribute-spec/combinators/str.ts +19 -0
  39. package/src/attribute-spec/field-attribute.ts +27 -0
  40. package/src/attribute-spec/interpret.ts +177 -0
  41. package/src/attribute-spec/model-attribute.ts +27 -0
  42. package/src/attribute-spec/optional.ts +8 -0
  43. package/src/attribute-spec/types.ts +72 -0
  44. package/src/block-reconstruction.ts +140 -0
  45. package/src/exports/format.ts +3 -0
  46. package/src/exports/index.ts +61 -3
  47. package/src/exports/interpret.ts +2 -0
  48. package/src/exports/syntax.ts +25 -5
  49. package/src/extension-block.ts +107 -0
  50. package/src/format/emit.ts +603 -0
  51. package/src/format/error.ts +13 -0
  52. package/src/format/format.ts +13 -0
  53. package/src/format/options.ts +28 -0
  54. package/src/interpret.ts +67 -0
  55. package/src/parse.ts +32 -5
  56. package/src/resolve.ts +123 -0
  57. package/src/source-file.ts +25 -0
  58. package/src/symbol-table.ts +446 -0
  59. package/src/syntax/ast/attributes.ts +5 -6
  60. package/src/syntax/ast/declarations.ts +51 -26
  61. package/src/syntax/ast/expressions.ts +12 -13
  62. package/src/syntax/ast/identifier.ts +2 -3
  63. package/src/syntax/ast/qualified-name.ts +28 -19
  64. package/src/syntax/ast/type-annotation.ts +4 -5
  65. package/src/syntax/ast-helpers.ts +27 -3
  66. package/src/syntax/navigation.ts +55 -0
  67. package/src/syntax/red.ts +317 -42
  68. package/dist/parser-CaplKvRs.mjs +0 -1145
  69. package/dist/parser-CaplKvRs.mjs.map +0 -1
  70. package/dist/parser-Dfi3Wfdq.d.mts +0 -7
  71. package/dist/parser-Dfi3Wfdq.d.mts.map +0 -1
  72. package/dist/parser.d.mts +0 -2
  73. package/dist/parser.mjs +0 -2
  74. package/src/exports/parser.ts +0 -1
  75. package/src/parser.ts +0 -1642
package/dist/index.mjs CHANGED
@@ -1,5 +1,8 @@
1
- import { t as parsePslDocument } from "./parser-CaplKvRs.mjs";
2
- import { flatPslModels, namespacePslExtensionBlocks } from "@prisma-next/framework-components/psl-ast";
1
+ import { _ as FunctionCallAst, b as ObjectLiteralExprAst, c as NamespaceDeclarationAst, g as BooleanLiteralExprAst, i as GenericBlockDeclarationAst, k as printSyntax, l as TypesBlockAst, m as ArrayLiteralAst, o as ModelDeclarationAst, t as CompositeTypeDeclarationAst, v as NumberLiteralExprAst, w as IdentifierAst, x as StringLiteralExprAst } from "./declarations-DR6To8_k.mjs";
2
+ import { UNSPECIFIED_PSL_NAMESPACE_ID, flatPslModels, makePslNamespace, makePslNamespaceEntries, namespacePslExtensionBlocks, validateExtensionBlock } from "@prisma-next/framework-components/psl-ast";
3
+ import { notOk, ok } from "@prisma-next/utils/result";
4
+ import { blindCast } from "@prisma-next/utils/casts";
5
+ import { isAuthoringPslBlockDescriptor } from "@prisma-next/framework-components/authoring";
3
6
  //#region src/attribute-helpers.ts
4
7
  function getPositionalArgument(attribute, index = 0) {
5
8
  return attribute.args.filter((arg) => arg.kind === "positional")[index]?.value;
@@ -10,6 +13,849 @@ function parseQuotedStringLiteral(value) {
10
13
  return match[2] ?? "";
11
14
  }
12
15
  //#endregion
13
- export { flatPslModels, getPositionalArgument, namespacePslExtensionBlocks, parsePslDocument, parseQuotedStringLiteral };
16
+ //#region src/resolve.ts
17
+ function readResolvedAttribute(attribute, sourceFile) {
18
+ return {
19
+ name: attributeName(attribute.name()),
20
+ args: readResolvedArgList(attribute.argList(), sourceFile),
21
+ span: nodePslSpan(attribute.syntax, sourceFile)
22
+ };
23
+ }
24
+ function readResolvedAttributes(attributes, sourceFile) {
25
+ return Array.from(attributes, (attribute) => readResolvedAttribute(attribute, sourceFile));
26
+ }
27
+ function readResolvedConstructorCall(annotation, sourceFile) {
28
+ const argList = annotation?.argList();
29
+ if (annotation === void 0 || argList === void 0) return void 0;
30
+ return {
31
+ path: annotation.name()?.path() ?? [],
32
+ args: readResolvedArgList(argList, sourceFile),
33
+ span: nodePslSpan(annotation.syntax, sourceFile)
34
+ };
35
+ }
36
+ function readResolvedArgList(argList, sourceFile) {
37
+ if (argList === void 0) return [];
38
+ const args = [];
39
+ for (const arg of argList.args()) {
40
+ const name = arg.name()?.name();
41
+ const expression = arg.value();
42
+ args.push({
43
+ kind: name !== void 0 ? "named" : "positional",
44
+ ...name !== void 0 ? { name } : {},
45
+ value: renderExpression(expression),
46
+ ...expression !== void 0 ? { expression } : {},
47
+ span: nodePslSpan(arg.syntax, sourceFile)
48
+ });
49
+ }
50
+ return args;
51
+ }
52
+ function attributeName(name) {
53
+ return name?.path().join(".") ?? "";
54
+ }
55
+ function renderExpression(expression) {
56
+ if (expression === void 0) return "";
57
+ return printSyntax(expression.syntax).trim();
58
+ }
59
+ function nodePslSpan(node, sourceFile) {
60
+ const start = node.offset;
61
+ const end = start + node.green.textLength;
62
+ return {
63
+ start: offsetToPslPosition(start, sourceFile),
64
+ end: offsetToPslPosition(end, sourceFile)
65
+ };
66
+ }
67
+ /** Unsupported-top-level-block diagnostics are anchored to the keyword token. */
68
+ function keywordPslSpan(node, keyword, sourceFile) {
69
+ const start = node.offset;
70
+ const end = start + keyword.length;
71
+ return {
72
+ start: offsetToPslPosition(start, sourceFile),
73
+ end: offsetToPslPosition(end, sourceFile)
74
+ };
75
+ }
76
+ function rangeToPslSpan(range, sourceFile) {
77
+ return {
78
+ start: offsetToPslPosition(sourceFile.offsetAt(range.start), sourceFile),
79
+ end: offsetToPslPosition(sourceFile.offsetAt(range.end), sourceFile)
80
+ };
81
+ }
82
+ function offsetToPslPosition(offset, sourceFile) {
83
+ const position = sourceFile.positionAt(offset);
84
+ return {
85
+ offset,
86
+ line: position.line + 1,
87
+ column: position.character + 1
88
+ };
89
+ }
90
+ //#endregion
91
+ //#region src/attribute-spec/combinators/diagnostic.ts
92
+ const ATTRIBUTE_DIAGNOSTIC_CODE = "PSL_INVALID_ATTRIBUTE_SYNTAX";
93
+ function leafDiagnostic(ctx, node, message) {
94
+ return {
95
+ code: ATTRIBUTE_DIAGNOSTIC_CODE,
96
+ message,
97
+ sourceId: ctx.sourceId,
98
+ span: nodePslSpan(node.syntax, ctx.sourceFile)
99
+ };
100
+ }
101
+ //#endregion
102
+ //#region src/attribute-spec/combinators/bool.ts
103
+ function bool() {
104
+ return {
105
+ kind: "bool",
106
+ label: "boolean",
107
+ parse: (arg, ctx) => {
108
+ if (arg instanceof BooleanLiteralExprAst) {
109
+ const value = arg.value();
110
+ if (value !== void 0) return ok(value);
111
+ }
112
+ return notOk([leafDiagnostic(ctx, arg, "Expected a boolean literal")]);
113
+ }
114
+ };
115
+ }
116
+ //#endregion
117
+ //#region src/attribute-spec/combinators/entity-ref.ts
118
+ function entityRef() {
119
+ return {
120
+ kind: "entityRef",
121
+ label: "model name",
122
+ parse: (arg, ctx) => {
123
+ if (!(arg instanceof IdentifierAst)) return notOk([leafDiagnostic(ctx, arg, "Expected a model name")]);
124
+ const name = arg.name();
125
+ if (name === void 0) return notOk([leafDiagnostic(ctx, arg, "Expected a model name")]);
126
+ return ok(name);
127
+ }
128
+ };
129
+ }
130
+ //#endregion
131
+ //#region src/attribute-spec/combinators/field-ref.ts
132
+ function fieldRef(scope) {
133
+ return {
134
+ kind: "fieldRef",
135
+ label: "field name",
136
+ scope,
137
+ parse: (arg, ctx) => {
138
+ if (!(arg instanceof IdentifierAst)) return notOk([leafDiagnostic(ctx, arg, "Expected a field name")]);
139
+ const name = arg.name();
140
+ if (name === void 0) return notOk([leafDiagnostic(ctx, arg, "Expected a field name")]);
141
+ const model = scope === "self" ? ctx.selfModel : ctx.resolveReferencedModel();
142
+ if (model !== void 0 && !Object.hasOwn(model.fields, name)) return notOk([leafDiagnostic(ctx, arg, `Field "${name}" does not exist on model "${model.name}"`)]);
143
+ return ok(name);
144
+ }
145
+ };
146
+ }
147
+ //#endregion
148
+ //#region src/attribute-spec/interpret.ts
149
+ function interpretArgs(args, spec, ctx, span) {
150
+ const diagnostics = [];
151
+ const output = {};
152
+ const seen = /* @__PURE__ */ new Set();
153
+ let positionalSlot = 0;
154
+ let reportedExcess = false;
155
+ for (const arg of args) {
156
+ const name = arg.name()?.name();
157
+ let key;
158
+ let param;
159
+ if (name === void 0) {
160
+ const posParam = spec.positional[positionalSlot];
161
+ if (posParam === void 0) {
162
+ if (!reportedExcess) {
163
+ diagnostics.push(diagnostic(`Attribute "${spec.name}" received too many positional arguments`, ctx, span));
164
+ reportedExcess = true;
165
+ }
166
+ continue;
167
+ }
168
+ positionalSlot += 1;
169
+ key = posParam.key;
170
+ param = posParam.type;
171
+ } else {
172
+ const namedParam = Object.hasOwn(spec.named, name) ? spec.named[name] : void 0;
173
+ if (namedParam === void 0) {
174
+ diagnostics.push(diagnostic(`Attribute "${spec.name}" received unknown argument "${name}"`, ctx, nodePslSpan(arg.syntax, ctx.sourceFile)));
175
+ continue;
176
+ }
177
+ key = name;
178
+ param = namedParam;
179
+ }
180
+ if (seen.has(key)) {
181
+ diagnostics.push(diagnostic(`Attribute "${spec.name}" received duplicate argument "${key}"`, ctx, nodePslSpan(arg.syntax, ctx.sourceFile)));
182
+ continue;
183
+ }
184
+ seen.add(key);
185
+ const result = parseArgValue(arg, param, ctx, diagnostics);
186
+ if (result.ok) output[key] = result.value;
187
+ }
188
+ const finalized = /* @__PURE__ */ new Set();
189
+ const finalizeAbsentKey = (key, positionalParam, namedParam) => {
190
+ if (finalized.has(key) || seen.has(key)) return;
191
+ finalized.add(key);
192
+ const effective = namedParam ?? positionalParam;
193
+ if (effective === void 0) return;
194
+ if (isOptionalArgType(effective)) {
195
+ if (effective.hasDefault) output[key] = effective.defaultValue;
196
+ return;
197
+ }
198
+ diagnostics.push(diagnostic(`Attribute "${spec.name}" is missing required argument "${key}"`, ctx, span));
199
+ };
200
+ for (const param of spec.positional) {
201
+ const namedParam = Object.hasOwn(spec.named, param.key) ? spec.named[param.key] : void 0;
202
+ finalizeAbsentKey(param.key, param.type, namedParam);
203
+ }
204
+ for (const key of Object.keys(spec.named)) finalizeAbsentKey(key, void 0, spec.named[key]);
205
+ if (diagnostics.length > 0) return notOk(diagnostics);
206
+ return ok(output);
207
+ }
208
+ function interpretAttribute(attrNode, spec, ctx) {
209
+ const attributeSpan = nodePslSpan(attrNode.syntax, ctx.sourceFile);
210
+ const bound = interpretArgs(attrNode.argList()?.args() ?? [], spec, ctx, attributeSpan);
211
+ if (!bound.ok) return notOk(bound.failure);
212
+ const value = blindCast(bound.value);
213
+ if (spec.refine !== void 0) {
214
+ const refineDiagnostics = spec.refine(value, ctx);
215
+ if (refineDiagnostics.length > 0) return notOk(refineDiagnostics);
216
+ }
217
+ return ok(value);
218
+ }
219
+ function parseArgValue(arg, argType, ctx, diagnostics) {
220
+ const value = arg.value();
221
+ if (value === void 0) {
222
+ const missing = diagnostic("Attribute argument is missing a value", ctx, nodePslSpan(arg.syntax, ctx.sourceFile));
223
+ diagnostics.push(missing);
224
+ return notOk([missing]);
225
+ }
226
+ const result = argType.parse(value, ctx);
227
+ if (!result.ok) for (const failure of result.failure) diagnostics.push(failure);
228
+ return result;
229
+ }
230
+ function isOptionalArgType(param) {
231
+ return "optional" in param && param.optional === true;
232
+ }
233
+ function diagnostic(message, ctx, span) {
234
+ return {
235
+ code: ATTRIBUTE_DIAGNOSTIC_CODE,
236
+ message,
237
+ sourceId: ctx.sourceId,
238
+ span
239
+ };
240
+ }
241
+ //#endregion
242
+ //#region src/attribute-spec/combinators/func-call.ts
243
+ function funcCall(name, sig) {
244
+ return {
245
+ kind: "funcCall",
246
+ label: "function call",
247
+ parse: (arg, ctx) => {
248
+ const guard = matchCallee(arg, name, ctx);
249
+ if (!guard.ok) return guard;
250
+ const span = nodePslSpan(guard.value.syntax, ctx.sourceFile);
251
+ const bound = interpretArgs(guard.value.args(), {
252
+ name,
253
+ positional: sig.positional ?? [],
254
+ named: sig.named ?? {}
255
+ }, ctx, span);
256
+ if (!bound.ok) return notOk(bound.failure);
257
+ return ok({
258
+ fn: name,
259
+ span,
260
+ args: bound.value
261
+ });
262
+ }
263
+ };
264
+ }
265
+ function matchCallee(arg, name, ctx) {
266
+ if (!(arg instanceof FunctionCallAst)) return notOk([leafDiagnostic(ctx, arg, "Expected a function call")]);
267
+ const qname = arg.name();
268
+ if (qname === void 0 || qname.dot() !== void 0 || qname.colon() !== void 0) return notOk([leafDiagnostic(ctx, arg, "Expected a function call")]);
269
+ const calleeName = qname.identifier()?.token()?.text;
270
+ if (calleeName === void 0) return notOk([leafDiagnostic(ctx, arg, "Expected a function call")]);
271
+ if (calleeName !== name) return notOk([leafDiagnostic(ctx, arg, `Expected ${name}()`)]);
272
+ return ok(arg);
273
+ }
274
+ //#endregion
275
+ //#region src/attribute-spec/combinators/identifier.ts
276
+ function identifier(name) {
277
+ return {
278
+ kind: "identifier",
279
+ label: name,
280
+ parse: (arg, ctx) => {
281
+ if (arg instanceof IdentifierAst && arg.name() === name) return ok(name);
282
+ return notOk([leafDiagnostic(ctx, arg, `Expected ${name}`)]);
283
+ }
284
+ };
285
+ }
286
+ //#endregion
287
+ //#region src/attribute-spec/combinators/int.ts
288
+ function int(opts) {
289
+ const min = opts?.min;
290
+ const max = opts?.max;
291
+ return {
292
+ kind: "int",
293
+ label: "integer",
294
+ parse: (arg, ctx) => {
295
+ if (arg instanceof NumberLiteralExprAst) {
296
+ const value = arg.value();
297
+ if (value !== void 0 && Number.isInteger(value)) {
298
+ if ((min === void 0 || value >= min) && (max === void 0 || value <= max)) return ok(value);
299
+ return notOk([leafDiagnostic(ctx, arg, rangeMessage(min, max))]);
300
+ }
301
+ }
302
+ return notOk([leafDiagnostic(ctx, arg, "Expected an integer literal")]);
303
+ }
304
+ };
305
+ }
306
+ function rangeMessage(min, max) {
307
+ if (min !== void 0 && max !== void 0) return `Expected an integer between ${min} and ${max}`;
308
+ if (min !== void 0) return `Expected an integer greater than or equal to ${min}`;
309
+ return `Expected an integer less than or equal to ${max}`;
310
+ }
311
+ //#endregion
312
+ //#region src/attribute-spec/combinators/list.ts
313
+ function list(of, opts) {
314
+ return {
315
+ kind: "list",
316
+ label: `${of.label}[]`,
317
+ parse: (arg, ctx) => {
318
+ if (!(arg instanceof ArrayLiteralAst)) return notOk([leafDiagnostic(ctx, arg, `Expected a list of ${of.label}`)]);
319
+ const diagnostics = [];
320
+ const parsed = [];
321
+ let count = 0;
322
+ for (const element of arg.elements()) {
323
+ count += 1;
324
+ const result = of.parse(element, ctx);
325
+ if (result.ok) parsed.push({
326
+ node: element,
327
+ value: result.value
328
+ });
329
+ else diagnostics.push(...result.failure);
330
+ }
331
+ if (opts?.nonEmpty === true && count === 0) diagnostics.push(leafDiagnostic(ctx, arg, "Expected a non-empty list"));
332
+ if (opts?.unique === true) {
333
+ const seen = /* @__PURE__ */ new Set();
334
+ for (const { node, value } of parsed) if (seen.has(value)) diagnostics.push(leafDiagnostic(ctx, node, "Duplicate list entry"));
335
+ else seen.add(value);
336
+ }
337
+ if (diagnostics.length > 0) return notOk(diagnostics);
338
+ return ok(parsed.map((entry) => entry.value));
339
+ }
340
+ };
341
+ }
342
+ //#endregion
343
+ //#region src/attribute-spec/combinators/num.ts
344
+ function num(value) {
345
+ return {
346
+ kind: "num",
347
+ label: value === void 0 ? "number" : String(value),
348
+ parse: (arg, ctx) => {
349
+ if (arg instanceof NumberLiteralExprAst) {
350
+ const parsed = arg.value();
351
+ if (parsed !== void 0 && (value === void 0 || parsed === value)) return ok(parsed);
352
+ }
353
+ return notOk([leafDiagnostic(ctx, arg, value === void 0 ? "Expected a number literal" : `Expected ${value}`)]);
354
+ }
355
+ };
356
+ }
357
+ //#endregion
358
+ //#region src/attribute-spec/combinators/one-of.ts
359
+ function oneOf(...alts) {
360
+ const label = alts.map((alt) => alt.label).join(" | ");
361
+ return {
362
+ kind: "oneOf",
363
+ label,
364
+ parse: (arg, ctx) => {
365
+ for (const alt of alts) {
366
+ const result = alt.parse(arg, ctx);
367
+ if (result.ok) return ok(blindCast(result.value));
368
+ }
369
+ return notOk([leafDiagnostic(ctx, arg, `Expected one of: ${label}`)]);
370
+ }
371
+ };
372
+ }
373
+ //#endregion
374
+ //#region src/attribute-spec/combinators/record.ts
375
+ function record(of) {
376
+ return {
377
+ kind: "record",
378
+ label: `{ [key]: ${of.label} }`,
379
+ parse: (arg, ctx) => {
380
+ if (!(arg instanceof ObjectLiteralExprAst)) return notOk([leafDiagnostic(ctx, arg, "Expected an object literal")]);
381
+ const diagnostics = [];
382
+ const result = {};
383
+ for (const field of arg.fields()) {
384
+ const key = field.keyName();
385
+ if (key === void 0) {
386
+ diagnostics.push(leafDiagnostic(ctx, field, "Expected a key"));
387
+ continue;
388
+ }
389
+ const value = field.value();
390
+ if (value === void 0) {
391
+ diagnostics.push(leafDiagnostic(ctx, field, `Expected a value for key "${key}"`));
392
+ continue;
393
+ }
394
+ const parsed = of.parse(value, ctx);
395
+ if (!parsed.ok) {
396
+ diagnostics.push(...parsed.failure);
397
+ continue;
398
+ }
399
+ if (Object.hasOwn(result, key)) {
400
+ diagnostics.push(leafDiagnostic(ctx, field, `Duplicate key "${key}"`));
401
+ continue;
402
+ }
403
+ result[key] = parsed.value;
404
+ }
405
+ if (diagnostics.length > 0) return notOk(diagnostics);
406
+ return ok(result);
407
+ }
408
+ };
409
+ }
410
+ //#endregion
411
+ //#region src/attribute-spec/combinators/str.ts
412
+ function str() {
413
+ return {
414
+ kind: "str",
415
+ label: "string",
416
+ parse: (arg, ctx) => {
417
+ if (arg instanceof StringLiteralExprAst) {
418
+ const value = arg.value();
419
+ if (value !== void 0) return ok(value);
420
+ }
421
+ return notOk([leafDiagnostic(ctx, arg, "Expected a string literal")]);
422
+ }
423
+ };
424
+ }
425
+ //#endregion
426
+ //#region src/attribute-spec/field-attribute.ts
427
+ function fieldAttribute(name, config) {
428
+ return {
429
+ level: "field",
430
+ name,
431
+ positional: config.positional ?? [],
432
+ named: config.named ?? {},
433
+ ...config.refine !== void 0 ? { refine: config.refine } : {}
434
+ };
435
+ }
436
+ //#endregion
437
+ //#region src/attribute-spec/model-attribute.ts
438
+ function modelAttribute(name, config) {
439
+ return {
440
+ level: "model",
441
+ name,
442
+ positional: config.positional ?? [],
443
+ named: config.named ?? {},
444
+ ...config.refine !== void 0 ? { refine: config.refine } : {}
445
+ };
446
+ }
447
+ //#endregion
448
+ //#region src/attribute-spec/optional.ts
449
+ function optional(type, ...rest) {
450
+ if (rest.length === 0) return {
451
+ ...type,
452
+ optional: true,
453
+ hasDefault: false
454
+ };
455
+ return {
456
+ ...type,
457
+ optional: true,
458
+ hasDefault: true,
459
+ defaultValue: rest[0]
460
+ };
461
+ }
462
+ //#endregion
463
+ //#region src/extension-block.ts
464
+ function findBlockDescriptor(descriptors, keyword) {
465
+ if (descriptors === void 0) return void 0;
466
+ for (const value of Object.values(descriptors)) {
467
+ if (value === void 0) continue;
468
+ if (isAuthoringPslBlockDescriptor(value)) {
469
+ if (value.keyword === keyword) return value;
470
+ continue;
471
+ }
472
+ const nested = findBlockDescriptor(value, keyword);
473
+ if (nested !== void 0) return nested;
474
+ }
475
+ }
476
+ function validateExtensionBlockFromSymbol(input) {
477
+ const refCtx = buildRefResolutionContext(input.symbolTable, input.block);
478
+ return validateExtensionBlock(input.block.block, input.descriptor, input.sourceId, input.codecLookup, refCtx);
479
+ }
480
+ const ZERO_SPAN = {
481
+ start: {
482
+ offset: 0,
483
+ line: 1,
484
+ column: 1
485
+ },
486
+ end: {
487
+ offset: 0,
488
+ line: 1,
489
+ column: 1
490
+ }
491
+ };
492
+ function buildRefResolutionContext(symbolTable, block) {
493
+ const unspecifiedNamespace = makeNamespace(UNSPECIFIED_PSL_NAMESPACE_ID, Object.values(symbolTable.topLevel.models));
494
+ const allNamespaces = [unspecifiedNamespace, ...Object.values(symbolTable.topLevel.namespaces).map((namespace) => makeNamespace(namespace.name, Object.values(namespace.models)))];
495
+ const ownerNamespaceName = findOwnerNamespaceName(symbolTable, block);
496
+ return {
497
+ ownerNamespace: allNamespaces.find((namespace) => namespace.name === ownerNamespaceName) ?? unspecifiedNamespace,
498
+ allNamespaces
499
+ };
500
+ }
501
+ function makeNamespace(name, models) {
502
+ return makePslNamespace({
503
+ kind: "namespace",
504
+ name,
505
+ entries: makePslNamespaceEntries(models.map((model) => ({
506
+ kind: "model",
507
+ name: model.name,
508
+ fields: [],
509
+ attributes: [],
510
+ span: ZERO_SPAN
511
+ })), [], []),
512
+ span: ZERO_SPAN
513
+ });
514
+ }
515
+ function findOwnerNamespaceName(symbolTable, block) {
516
+ for (const namespace of Object.values(symbolTable.topLevel.namespaces)) if (Object.values(namespace.blocks).some((candidate) => candidate === block)) return namespace.name;
517
+ return UNSPECIFIED_PSL_NAMESPACE_ID;
518
+ }
519
+ //#endregion
520
+ //#region src/block-reconstruction.ts
521
+ /**
522
+ * Descriptor-free and unknown parameters become `value` stubs so validation can
523
+ * report them via key-set comparison. Duplicate member names are first-wins.
524
+ */
525
+ function reconstructExtensionBlock(node, descriptor, sourceFile, diagnostics) {
526
+ const keyword = node.keyword()?.text ?? "";
527
+ const blockName = node.name()?.name() ?? "";
528
+ const blockAttributes = [];
529
+ for (const attribute of node.attributes()) {
530
+ const name = attribute.name()?.path().join(".") ?? "";
531
+ const args = Array.from(attribute.argList()?.args() ?? [], (arg) => {
532
+ const value = arg.value();
533
+ return {
534
+ kind: "positional",
535
+ value: value === void 0 ? "" : printSyntax(value.syntax).trim(),
536
+ span: nodePslSpan(arg.syntax, sourceFile)
537
+ };
538
+ });
539
+ blockAttributes.push({
540
+ name,
541
+ args,
542
+ span: nodePslSpan(attribute.syntax, sourceFile)
543
+ });
544
+ }
545
+ const parameters = {};
546
+ for (const entry of node.entries()) {
547
+ const key = entry.key()?.name();
548
+ if (key === void 0) continue;
549
+ const span = nodePslSpan(entry.syntax, sourceFile);
550
+ if (Object.hasOwn(parameters, key)) {
551
+ diagnostics.push({
552
+ code: "PSL_EXTENSION_DUPLICATE_PARAMETER",
553
+ message: `Duplicate parameter "${key}" in "${keyword}" block "${blockName}"; first occurrence wins`,
554
+ range: {
555
+ start: sourceFile.positionAt(entry.syntax.offset),
556
+ end: sourceFile.positionAt(entry.syntax.offset + entry.syntax.green.textLength)
557
+ }
558
+ });
559
+ continue;
560
+ }
561
+ parameters[key] = reconstructParamValue(entry, descriptor?.parameters[key], span, sourceFile, diagnostics);
562
+ }
563
+ return {
564
+ kind: descriptor?.discriminator ?? keyword,
565
+ keyword,
566
+ name: blockName,
567
+ parameters,
568
+ blockAttributes,
569
+ span: nodePslSpan(node.syntax, sourceFile)
570
+ };
571
+ }
572
+ function reconstructParamValue(entry, param, span, sourceFile, diagnostics) {
573
+ const value = entry.value();
574
+ if (value === void 0) return {
575
+ kind: "bare",
576
+ span
577
+ };
578
+ return reconstructFromExpression(value, param, span, sourceFile, diagnostics);
579
+ }
580
+ function reconstructFromExpression(value, param, span, sourceFile, diagnostics) {
581
+ const raw = printSyntax(value.syntax).trim();
582
+ if (param?.kind === "list") {
583
+ const array = ArrayLiteralAst.cast(value.syntax);
584
+ if (!array) {
585
+ diagnostics?.push({
586
+ code: "PSL_EXTENSION_INVALID_VALUE",
587
+ message: `List parameter expects an array literal, got ${raw}`,
588
+ range: {
589
+ start: sourceFile.positionAt(value.syntax.offset),
590
+ end: sourceFile.positionAt(value.syntax.offset + value.syntax.green.textLength)
591
+ }
592
+ });
593
+ return {
594
+ kind: "value",
595
+ raw,
596
+ span
597
+ };
598
+ }
599
+ const items = [];
600
+ for (const element of array.elements()) items.push(reconstructFromExpression(element, param.of, nodePslSpan(element.syntax, sourceFile), sourceFile, diagnostics));
601
+ return {
602
+ kind: "list",
603
+ items,
604
+ span
605
+ };
606
+ }
607
+ switch (param?.kind) {
608
+ case "ref": return {
609
+ kind: "ref",
610
+ identifier: raw,
611
+ span
612
+ };
613
+ case "option": return {
614
+ kind: "option",
615
+ token: raw,
616
+ span
617
+ };
618
+ default: return {
619
+ kind: "value",
620
+ raw,
621
+ span
622
+ };
623
+ }
624
+ }
625
+ //#endregion
626
+ //#region src/symbol-table.ts
627
+ /**
628
+ * Owns duplicate-declaration detection for all PSL scopes; downstream consumers
629
+ * should consume first-wins symbols rather than re-emitting duplicate diagnostics.
630
+ */
631
+ function buildSymbolTable(options) {
632
+ const { document, sourceFile, scalarTypes, pslBlockDescriptors } = options;
633
+ const diagnostics = [];
634
+ const scalarSet = new Set(scalarTypes);
635
+ const namespaces = {};
636
+ const scalars = {};
637
+ const typeAliases = {};
638
+ const blocks = {};
639
+ const models = {};
640
+ const compositeTypes = {};
641
+ const topLevelNames = /* @__PURE__ */ new Set();
642
+ const claim = (taken, name) => {
643
+ const text = name?.name();
644
+ if (text === void 0) return void 0;
645
+ if (taken.has(text)) {
646
+ const range = nameRange(name, sourceFile);
647
+ if (range) diagnostics.push({
648
+ code: "PSL_DUPLICATE_DECLARATION",
649
+ message: `Duplicate declaration of "${text}"`,
650
+ range
651
+ });
652
+ return;
653
+ }
654
+ taken.add(text);
655
+ return text;
656
+ };
657
+ for (const declaration of document.declarations()) if (declaration instanceof ModelDeclarationAst) {
658
+ const name = claim(topLevelNames, declaration.name());
659
+ if (name !== void 0) models[name] = buildModel(name, declaration, sourceFile, diagnostics);
660
+ } else if (declaration instanceof CompositeTypeDeclarationAst) {
661
+ const name = claim(topLevelNames, declaration.name());
662
+ if (name !== void 0) compositeTypes[name] = buildCompositeType(name, declaration, sourceFile, diagnostics);
663
+ } else if (declaration instanceof GenericBlockDeclarationAst) {
664
+ const name = claim(topLevelNames, declaration.name());
665
+ if (name !== void 0) blocks[name] = buildBlock(name, declaration, sourceFile, pslBlockDescriptors, diagnostics);
666
+ } else if (declaration instanceof NamespaceDeclarationAst) {
667
+ const name = claim(topLevelNames, declaration.name());
668
+ if (name !== void 0) namespaces[name] = buildNamespace(name, declaration, diagnostics, sourceFile, pslBlockDescriptors);
669
+ } else if (declaration instanceof TypesBlockAst) for (const binding of declaration.declarations()) {
670
+ const name = claim(topLevelNames, binding.name());
671
+ if (name === void 0) continue;
672
+ const resolved = resolveNamedTypeBinding(binding, sourceFile);
673
+ const span = nodePslSpan(binding.syntax, sourceFile);
674
+ if (isScalarBinding(binding, scalarSet)) scalars[name] = {
675
+ kind: "scalar",
676
+ name,
677
+ node: binding,
678
+ span,
679
+ ...resolved
680
+ };
681
+ else typeAliases[name] = {
682
+ kind: "typeAlias",
683
+ name,
684
+ node: binding,
685
+ span,
686
+ ...resolved
687
+ };
688
+ }
689
+ return {
690
+ table: { topLevel: {
691
+ namespaces,
692
+ scalars,
693
+ typeAliases,
694
+ blocks,
695
+ models,
696
+ compositeTypes
697
+ } },
698
+ diagnostics
699
+ };
700
+ }
701
+ function buildModel(name, node, sourceFile, diagnostics) {
702
+ return {
703
+ kind: "model",
704
+ name,
705
+ node,
706
+ span: nodePslSpan(node.syntax, sourceFile),
707
+ fields: buildFields(name, node.fields(), sourceFile, diagnostics),
708
+ attributes: readResolvedAttributes(node.attributes(), sourceFile)
709
+ };
710
+ }
711
+ function buildCompositeType(name, node, sourceFile, diagnostics) {
712
+ return {
713
+ kind: "compositeType",
714
+ name,
715
+ node,
716
+ span: nodePslSpan(node.syntax, sourceFile),
717
+ fields: buildFields(name, node.fields(), sourceFile, diagnostics),
718
+ attributes: readResolvedAttributes(node.attributes(), sourceFile)
719
+ };
720
+ }
721
+ function buildBlock(name, node, sourceFile, pslBlockDescriptors, diagnostics) {
722
+ const keyword = node.keyword()?.text ?? "";
723
+ const descriptor = findBlockDescriptor(pslBlockDescriptors, keyword);
724
+ return {
725
+ kind: "block",
726
+ name,
727
+ keyword,
728
+ node,
729
+ span: nodePslSpan(node.syntax, sourceFile),
730
+ block: reconstructExtensionBlock(node, descriptor, sourceFile, diagnostics)
731
+ };
732
+ }
733
+ function buildNamespace(name, node, diagnostics, sourceFile, pslBlockDescriptors) {
734
+ const models = {};
735
+ const compositeTypes = {};
736
+ const blocks = {};
737
+ const taken = /* @__PURE__ */ new Set();
738
+ for (const member of node.declarations()) {
739
+ const memberName = member.name()?.name();
740
+ if (memberName === void 0) continue;
741
+ if (taken.has(memberName)) {
742
+ const range = nameRange(member.name(), sourceFile);
743
+ if (range) diagnostics.push({
744
+ code: "PSL_DUPLICATE_DECLARATION",
745
+ message: `Duplicate declaration of "${memberName}"`,
746
+ range
747
+ });
748
+ continue;
749
+ }
750
+ taken.add(memberName);
751
+ if (member instanceof ModelDeclarationAst) models[memberName] = buildModel(memberName, member, sourceFile, diagnostics);
752
+ else if (member instanceof CompositeTypeDeclarationAst) compositeTypes[memberName] = buildCompositeType(memberName, member, sourceFile, diagnostics);
753
+ else if (member instanceof GenericBlockDeclarationAst) blocks[memberName] = buildBlock(memberName, member, sourceFile, pslBlockDescriptors, diagnostics);
754
+ }
755
+ return {
756
+ kind: "namespace",
757
+ name,
758
+ node,
759
+ span: nodePslSpan(node.syntax, sourceFile),
760
+ models,
761
+ compositeTypes,
762
+ blocks
763
+ };
764
+ }
765
+ function buildFields(ownerName, fields, sourceFile, diagnostics) {
766
+ const result = {};
767
+ for (const field of fields) {
768
+ const nameNode = field.name();
769
+ const name = nameNode?.name();
770
+ if (name === void 0) continue;
771
+ if (Object.hasOwn(result, name)) {
772
+ const range = nameRange(nameNode, sourceFile);
773
+ if (range) diagnostics.push({
774
+ code: "PSL_DUPLICATE_DECLARATION",
775
+ message: `Duplicate declaration of "${name}"`,
776
+ range
777
+ });
778
+ continue;
779
+ }
780
+ result[name] = buildField(ownerName, name, field, sourceFile, diagnostics);
781
+ }
782
+ return result;
783
+ }
784
+ function buildField(ownerName, name, node, sourceFile, diagnostics) {
785
+ const attributes = readResolvedAttributes(node.attributes(), sourceFile);
786
+ const span = nodePslSpan(node.syntax, sourceFile);
787
+ const annotation = node.typeAnnotation();
788
+ const typeName = annotation?.name();
789
+ if (typeName?.isOverQualified()) {
790
+ const path = typeName.path();
791
+ diagnostics.push({
792
+ code: "PSL_INVALID_QUALIFIED_TYPE",
793
+ message: `Field "${ownerName}.${name}" has an invalid qualified type "${path.join(".")}"; use at most one namespace qualifier (e.g. "ns.TypeName")`,
794
+ range: nodeRange(typeName.syntax, sourceFile)
795
+ });
796
+ return {
797
+ kind: "field",
798
+ name,
799
+ node,
800
+ span,
801
+ typeName: path[path.length - 1] ?? "",
802
+ optional: false,
803
+ list: false,
804
+ malformedType: true,
805
+ attributes
806
+ };
807
+ }
808
+ const typeConstructor = annotation?.isConstructor() ? readResolvedConstructorCall(annotation, sourceFile) : void 0;
809
+ const typeNamespaceId = typeName?.namespace()?.name();
810
+ const typeContractSpaceId = typeName?.space()?.name();
811
+ return {
812
+ kind: "field",
813
+ name,
814
+ node,
815
+ span,
816
+ typeName: typeName?.identifier()?.name() ?? "",
817
+ ...typeNamespaceId !== void 0 ? { typeNamespaceId } : {},
818
+ ...typeContractSpaceId !== void 0 ? { typeContractSpaceId } : {},
819
+ optional: annotation?.isOptional() ?? false,
820
+ list: annotation?.isList() ?? false,
821
+ ...typeConstructor !== void 0 ? { typeConstructor } : {},
822
+ attributes
823
+ };
824
+ }
825
+ function resolveNamedTypeBinding(node, sourceFile) {
826
+ const annotation = node.typeAnnotation();
827
+ const isConstructor = annotation?.isConstructor() ?? false;
828
+ const baseType = annotation?.name()?.identifier()?.name();
829
+ const typeConstructor = readResolvedConstructorCall(annotation, sourceFile);
830
+ return {
831
+ isConstructor,
832
+ ...!isConstructor && baseType !== void 0 ? { baseType } : {},
833
+ ...typeConstructor !== void 0 ? { typeConstructor } : {},
834
+ attributes: readResolvedAttributes(node.attributes(), sourceFile)
835
+ };
836
+ }
837
+ function isScalarBinding(node, scalarTypes) {
838
+ const annotation = node.typeAnnotation();
839
+ if (annotation === void 0 || annotation.isConstructor()) return false;
840
+ const base = annotation.name()?.identifier()?.name();
841
+ return base !== void 0 && scalarTypes.has(base);
842
+ }
843
+ function nameRange(name, sourceFile) {
844
+ if (name === void 0) return void 0;
845
+ for (const token of name.syntax.tokens()) if (token.kind === "Ident") return {
846
+ start: sourceFile.positionAt(token.offset),
847
+ end: sourceFile.positionAt(token.offset + token.text.length)
848
+ };
849
+ }
850
+ function nodeRange(node, sourceFile) {
851
+ const start = node.offset;
852
+ const end = start + node.green.textLength;
853
+ return {
854
+ start: sourceFile.positionAt(start),
855
+ end: sourceFile.positionAt(end)
856
+ };
857
+ }
858
+ //#endregion
859
+ export { bool, buildSymbolTable, entityRef, fieldAttribute, fieldRef, findBlockDescriptor, flatPslModels, funcCall, getPositionalArgument, identifier, int, interpretArgs, interpretAttribute, keywordPslSpan, leafDiagnostic, list, modelAttribute, namespacePslExtensionBlocks, nodePslSpan, num, oneOf, optional, parseQuotedStringLiteral, rangeToPslSpan, readResolvedAttribute, readResolvedAttributes, readResolvedConstructorCall, record, str, validateExtensionBlockFromSymbol };
14
860
 
15
861
  //# sourceMappingURL=index.mjs.map