@prisma-next/psl-parser 0.14.0-dev.5 → 0.14.0-dev.50

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