@dancrumb/cant-compiler 0.0.0-stage → 0.10.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.
Files changed (78) hide show
  1. package/dist/bodies.d.ts +33 -0
  2. package/dist/bodies.d.ts.map +1 -0
  3. package/dist/bodies.js +512 -0
  4. package/dist/bodies.js.map +1 -0
  5. package/dist/check.d.ts +18 -0
  6. package/dist/check.d.ts.map +1 -0
  7. package/dist/check.js +30 -0
  8. package/dist/check.js.map +1 -0
  9. package/dist/cli.d.ts +3 -0
  10. package/dist/cli.d.ts.map +1 -0
  11. package/dist/cli.js +90 -0
  12. package/dist/cli.js.map +1 -0
  13. package/dist/compile.d.ts +20 -0
  14. package/dist/compile.d.ts.map +1 -0
  15. package/dist/compile.js +777 -0
  16. package/dist/compile.js.map +1 -0
  17. package/dist/constants.d.ts +22 -0
  18. package/dist/constants.d.ts.map +1 -0
  19. package/dist/constants.js +262 -0
  20. package/dist/constants.js.map +1 -0
  21. package/dist/declarations.d.ts +54 -0
  22. package/dist/declarations.d.ts.map +1 -0
  23. package/dist/declarations.js +202 -0
  24. package/dist/declarations.js.map +1 -0
  25. package/dist/diagnostic.d.ts +19 -0
  26. package/dist/diagnostic.d.ts.map +1 -0
  27. package/dist/diagnostic.js +27 -0
  28. package/dist/diagnostic.js.map +1 -0
  29. package/dist/entities.d.ts +32 -0
  30. package/dist/entities.d.ts.map +1 -0
  31. package/dist/entities.js +235 -0
  32. package/dist/entities.js.map +1 -0
  33. package/dist/evaluate.d.ts +30 -0
  34. package/dist/evaluate.d.ts.map +1 -0
  35. package/dist/evaluate.js +79 -0
  36. package/dist/evaluate.js.map +1 -0
  37. package/dist/host.d.ts +8 -0
  38. package/dist/host.d.ts.map +1 -0
  39. package/dist/host.js +13 -0
  40. package/dist/host.js.map +1 -0
  41. package/dist/index.d.ts +31 -0
  42. package/dist/index.d.ts.map +1 -0
  43. package/dist/index.js +16 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/manifest.d.ts +23 -0
  46. package/dist/manifest.d.ts.map +1 -0
  47. package/dist/manifest.js +93 -0
  48. package/dist/manifest.js.map +1 -0
  49. package/dist/modules.d.ts +84 -0
  50. package/dist/modules.d.ts.map +1 -0
  51. package/dist/modules.js +259 -0
  52. package/dist/modules.js.map +1 -0
  53. package/dist/program.d.ts +24 -0
  54. package/dist/program.d.ts.map +1 -0
  55. package/dist/program.js +38 -0
  56. package/dist/program.js.map +1 -0
  57. package/dist/resolve.d.ts +24 -0
  58. package/dist/resolve.d.ts.map +1 -0
  59. package/dist/resolve.js +203 -0
  60. package/dist/resolve.js.map +1 -0
  61. package/dist/stdlib.d.ts +12 -0
  62. package/dist/stdlib.d.ts.map +1 -0
  63. package/dist/stdlib.js +38 -0
  64. package/dist/stdlib.js.map +1 -0
  65. package/dist/typecheck.d.ts +52 -0
  66. package/dist/typecheck.d.ts.map +1 -0
  67. package/dist/typecheck.js +1408 -0
  68. package/dist/typecheck.js.map +1 -0
  69. package/dist/types.d.ts +130 -0
  70. package/dist/types.d.ts.map +1 -0
  71. package/dist/types.js +199 -0
  72. package/dist/types.js.map +1 -0
  73. package/dist/weave.d.ts +3 -0
  74. package/dist/weave.d.ts.map +1 -0
  75. package/dist/weave.js +40 -0
  76. package/dist/weave.js.map +1 -0
  77. package/package.json +47 -3
  78. package/README.md +0 -3
@@ -0,0 +1,202 @@
1
+ import { error } from './diagnostic.js';
2
+ import { constantKey } from './types.js';
3
+ /** The namespace a module's declarations are in until a `namespace` line (docs/adr/0005-module-namespaces.md). */
4
+ export const MAIN_NAMESPACE = '__main__';
5
+ const KIND = {
6
+ enumDeclaration: 'an enum',
7
+ model: 'a model',
8
+ entity: 'an entity',
9
+ errorDeclaration: 'an error',
10
+ };
11
+ const describe = (declaration) => declaration.type === 'action' ? 'an action' : declaration.type === 'const' ? 'a constant' : KIND[declaration.type];
12
+ const line = (position) => `line ${String(position.start.line)}`;
13
+ /**
14
+ * Gather a module's declarations into a symbol table, one per namespace, and report the names
15
+ * that are declared twice where the ADRs allow only one:
16
+ *
17
+ * - two types with the same name in a namespace (docs/adr/0013-enums.md, 0023, 0027);
18
+ * - two actions with the same name in a namespace;
19
+ * - a constant with the same name as an action, a type or another constant (docs/adr/0034-top-level-constants.md);
20
+ * - two exported symbols with the same name (docs/adr/0006-imports.md);
21
+ * - inside a declaration: two parameters, model fields or error data fields with the same name,
22
+ * two enum members with the same name or value, two roles with the same name, and more than
23
+ * one `identity`, `membership`, `expose` or `hide` in an entity.
24
+ */
25
+ export function collectDeclarations(program) {
26
+ const diagnostics = [];
27
+ const namespaces = new Map();
28
+ const exports = new Map();
29
+ const symbols = {
30
+ namespaces,
31
+ exports,
32
+ imports: [],
33
+ reExports: [],
34
+ triggers: [],
35
+ permissions: [],
36
+ relations: [],
37
+ };
38
+ const namespaceNamed = (name) => {
39
+ let namespace = namespaces.get(name);
40
+ if (namespace === undefined) {
41
+ namespace = { name, types: new Map(), actions: new Map(), constants: new Map() };
42
+ namespaces.set(name, namespace);
43
+ }
44
+ return namespace;
45
+ };
46
+ const addExport = (name, declaration) => {
47
+ const earlier = exports.get(name.value);
48
+ if (earlier === undefined) {
49
+ exports.set(name.value, declaration);
50
+ return;
51
+ }
52
+ diagnostics.push(error('duplicate-export', `The module already exports a symbol called ${name.value}, on ${line(earlier.position)}. A module can't export two symbols with the same name; rename one with \`as\` if it is re-exported.`, name.position));
53
+ };
54
+ let current = namespaceNamed(MAIN_NAMESPACE);
55
+ for (const declaration of program.children) {
56
+ switch (declaration.type) {
57
+ case 'namespace':
58
+ current = namespaceNamed(declaration.name.value);
59
+ break;
60
+ case 'import':
61
+ symbols.imports.push({ namespace: current.name, declaration });
62
+ break;
63
+ case 'reExport':
64
+ symbols.reExports.push({ namespace: current.name, declaration });
65
+ addExport(declaration.alias ?? (declaration.name.type === 'qualifiedIdentifier' ? declaration.name.name : declaration.name), declaration);
66
+ break;
67
+ case 'trigger':
68
+ symbols.triggers.push({ namespace: current.name, declaration });
69
+ break;
70
+ case 'permissions':
71
+ symbols.permissions.push({ namespace: current.name, declaration });
72
+ break;
73
+ case 'relation':
74
+ symbols.relations.push({ namespace: current.name, declaration });
75
+ break;
76
+ case 'action':
77
+ addAction(current, declaration, diagnostics);
78
+ checkUnique(declaration.parameters.map((parameter) => parameter.name), 'duplicate-parameter', 'a parameter', diagnostics);
79
+ if (declaration.exported === true)
80
+ addExport(declaration.name, declaration);
81
+ break;
82
+ case 'const':
83
+ addConstant(current, declaration, diagnostics);
84
+ if (declaration.exported === true)
85
+ addExport(declaration.name, declaration);
86
+ break;
87
+ case 'enumDeclaration':
88
+ case 'model':
89
+ case 'entity':
90
+ case 'errorDeclaration':
91
+ addType(current, declaration, diagnostics);
92
+ checkInsideType(declaration, diagnostics);
93
+ if (declaration.exported === true)
94
+ addExport(declaration.name, declaration);
95
+ break;
96
+ }
97
+ }
98
+ return { symbols, diagnostics };
99
+ }
100
+ const clash = (name, declaration, earlier, namespace, rule) => error('duplicate-declaration', `${capitalise(describe(declaration))} called ${name.value} clashes with ${describe(earlier)} with the same name in namespace ${namespace.name}, on ${line(earlier.position)}. ${rule}`, name.position);
101
+ const capitalise = (text) => text.charAt(0).toUpperCase() + text.slice(1);
102
+ function addType(namespace, declaration, diagnostics) {
103
+ const name = declaration.name.value;
104
+ const earlier = namespace.types.get(name) ?? namespace.constants.get(name);
105
+ if (earlier !== undefined) {
106
+ diagnostics.push(clash(declaration.name, declaration, earlier, namespace, 'Enums, models, entities, errors and constants share one set of names in a namespace.'));
107
+ return;
108
+ }
109
+ namespace.types.set(name, declaration);
110
+ }
111
+ function addAction(namespace, declaration, diagnostics) {
112
+ const name = declaration.name.value;
113
+ const earlier = namespace.actions.get(name) ?? namespace.constants.get(name);
114
+ if (earlier !== undefined) {
115
+ diagnostics.push(clash(declaration.name, declaration, earlier, namespace, 'Each action in a namespace needs its own name.'));
116
+ return;
117
+ }
118
+ namespace.actions.set(name, declaration);
119
+ }
120
+ function addConstant(namespace, declaration, diagnostics) {
121
+ const name = declaration.name.value;
122
+ const earlier = namespace.constants.get(name) ?? namespace.actions.get(name) ?? namespace.types.get(name);
123
+ if (earlier !== undefined) {
124
+ diagnostics.push(clash(declaration.name, declaration, earlier, namespace, "A constant's name must be unique among the names in its namespace."));
125
+ return;
126
+ }
127
+ namespace.constants.set(name, declaration);
128
+ }
129
+ /** Report every name after the first that repeats an earlier one. */
130
+ function checkUnique(names, code, what, diagnostics) {
131
+ const seen = new Map();
132
+ for (const name of names) {
133
+ const earlier = seen.get(name.value);
134
+ if (earlier === undefined) {
135
+ seen.set(name.value, name);
136
+ continue;
137
+ }
138
+ diagnostics.push(error(code, `There is already ${what} called ${name.value}, on ${line(earlier.position)}.`, name.position));
139
+ }
140
+ }
141
+ function checkInsideType(declaration, diagnostics) {
142
+ switch (declaration.type) {
143
+ case 'model':
144
+ checkUnique(fieldNames(declaration.fields), 'duplicate-field', 'a field', diagnostics);
145
+ break;
146
+ case 'errorDeclaration':
147
+ if (Array.isArray(declaration.data)) {
148
+ checkUnique(fieldNames(declaration.data), 'duplicate-field', 'a data field', diagnostics);
149
+ }
150
+ break;
151
+ case 'enumDeclaration':
152
+ checkEnum(declaration, diagnostics);
153
+ break;
154
+ case 'entity':
155
+ checkEntity(declaration, diagnostics);
156
+ break;
157
+ }
158
+ }
159
+ const fieldNames = (fields) => fields.map((field) => field.name);
160
+ /** An enum's members need distinct names and distinct values (docs/adr/0013-enums.md). */
161
+ function checkEnum(declaration, diagnostics) {
162
+ checkUnique(declaration.members.map((member) => member.name), 'duplicate-enum-member', 'a member', diagnostics);
163
+ const values = new Map();
164
+ const names = new Set();
165
+ for (const member of declaration.members) {
166
+ // A member whose name repeats an earlier one has already been reported.
167
+ if (names.has(member.name.value))
168
+ continue;
169
+ names.add(member.name.value);
170
+ // A member with no `:=` has its name as its `TEXT` value.
171
+ const key = member.value === undefined ? `TEXT:${member.name.value}` : constantKey(member.value).key;
172
+ const earlier = values.get(key);
173
+ if (earlier === undefined) {
174
+ values.set(key, member.name);
175
+ continue;
176
+ }
177
+ diagnostics.push(error('duplicate-enum-value', `${member.name.value} has the same value as ${earlier.value}, on ${line(earlier.position)}. An enum's members need distinct values.`, member.value?.position ?? member.name.position));
178
+ }
179
+ }
180
+ /** Lines an entity can have at most one of (docs/adr/0010-security.md, 0022, 0032). */
181
+ const AT_MOST_ONE = {
182
+ identityLine: 'an `identity` line',
183
+ membershipLine: 'a `membership` line',
184
+ exposeBlock: 'an `expose` block',
185
+ hideBlock: 'a `hide` block',
186
+ };
187
+ function checkEntity(declaration, diagnostics) {
188
+ const seen = new Map();
189
+ for (const entityLine of declaration.lines) {
190
+ if (!(entityLine.type in AT_MOST_ONE))
191
+ continue;
192
+ const what = AT_MOST_ONE[entityLine.type];
193
+ const earlier = seen.get(entityLine.type);
194
+ if (earlier === undefined) {
195
+ seen.set(entityLine.type, entityLine.position);
196
+ continue;
197
+ }
198
+ diagnostics.push(error('duplicate-entity-line', `${declaration.name.value} already has ${what}, on ${line(earlier)}. An entity can have at most one.`, entityLine.position));
199
+ }
200
+ checkUnique(declaration.lines.flatMap((entityLine) => (entityLine.type === 'roleLine' ? [entityLine.name] : [])), 'duplicate-role', 'a role', diagnostics);
201
+ }
202
+ //# sourceMappingURL=declarations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"declarations.js","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,KAAK,EAAmB,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,kHAAkH;AAClH,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC;AA4CzC,MAAM,IAAI,GAA4C;IACpD,eAAe,EAAE,SAAS;IAC1B,KAAK,EAAE,SAAS;IAChB,MAAM,EAAE,WAAW;IACnB,gBAAgB,EAAE,UAAU;CAC7B,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,WAAmE,EAAU,EAAE,CAC/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,EAAE,gBAAgB;CACnB,CAAC;AAEX,SAAS,WAAW,CAAC,WAA8B,EAAE,WAAyB;IAC5E,MAAM,IAAI,GAAG,IAAI,GAAG,EAAoB,CAAC;IACzC,KAAK,MAAM,UAAU,IAAI,WAAW,CAAC,KAAK,EAAE,CAAC;QAC3C,IAAI,CAAC,CAAC,UAAU,CAAC,IAAI,IAAI,WAAW,CAAC;YAAE,SAAS;QAChD,MAAM,IAAI,GAAG,WAAW,CAAC,UAAU,CAAC,IAAgC,CAAC,CAAC;QACtE,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC1C,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;YAC/C,SAAS;QACX,CAAC;QACD,WAAW,CAAC,IAAI,CACd,KAAK,CACH,uBAAuB,EACvB,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,gBAAgB,IAAI,QAAQ,IAAI,CAAC,OAAO,CAAC,mCAAmC,EACrG,UAAU,CAAC,QAAQ,CACpB,CACF,CAAC;IACJ,CAAC;IAED,WAAW,CACT,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EACpG,gBAAgB,EAChB,QAAQ,EACR,WAAW,CACZ,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { Position } from 'unist';
2
+ /** A problem the compiler found in a module, and where in its source it is. */
3
+ export interface Diagnostic {
4
+ severity: 'error';
5
+ /** A short, stable name for the kind of problem, such as `duplicate-type`. */
6
+ code: string;
7
+ message: string;
8
+ /** Where the problem is. Lines and columns are 1-based and offsets are 0-based, as in unist. */
9
+ position: Position;
10
+ /** The file the problem is in, when the compiler checks more than one. */
11
+ file?: string;
12
+ }
13
+ export declare const error: (code: string, message: string, position: Position) => Diagnostic;
14
+ /**
15
+ * Write a diagnostic for people: `file:line:column: error: message`, then the line of source it
16
+ * points to, with carets under the part it is about.
17
+ */
18
+ export declare function formatDiagnostic(file: string, source: string, diagnostic: Diagnostic): string;
19
+ //# sourceMappingURL=diagnostic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diagnostic.d.ts","sourceRoot":"","sources":["../src/diagnostic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEtC,+EAA+E;AAC/E,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,OAAO,CAAC;IAClB,8EAA8E;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,gGAAgG;IAChG,QAAQ,EAAE,QAAQ,CAAC;IACnB,0EAA0E;IAC1E,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,eAAO,MAAM,KAAK,SAAU,MAAM,WAAW,MAAM,YAAY,QAAQ,KAAG,UAKxE,CAAC;AAEH;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,MAAM,CAe7F"}
@@ -0,0 +1,27 @@
1
+ export const error = (code, message, position) => ({
2
+ severity: 'error',
3
+ code,
4
+ message,
5
+ position,
6
+ });
7
+ /**
8
+ * Write a diagnostic for people: `file:line:column: error: message`, then the line of source it
9
+ * points to, with carets under the part it is about.
10
+ */
11
+ export function formatDiagnostic(file, source, diagnostic) {
12
+ const { start, end } = diagnostic.position;
13
+ const header = `${file}:${String(start.line)}:${String(start.column)}: ${diagnostic.severity}: ${diagnostic.message}`;
14
+ const line = source.split(/\r?\n/)[start.line - 1];
15
+ if (line === undefined)
16
+ return header;
17
+ // Underline to the end of the position, or to the end of the line if it runs onto later lines.
18
+ const width = end.line === start.line ? Math.max(1, end.column - start.column) : Math.max(1, line.length - start.column + 1);
19
+ const gutter = String(start.line).length;
20
+ return [
21
+ header,
22
+ `${' '.repeat(gutter)} |`,
23
+ `${String(start.line)} | ${line}`,
24
+ `${' '.repeat(gutter)} | ${' '.repeat(start.column - 1)}${'^'.repeat(width)}`,
25
+ ].join('\n');
26
+ }
27
+ //# sourceMappingURL=diagnostic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diagnostic.js","sourceRoot":"","sources":["../src/diagnostic.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,IAAY,EAAE,OAAe,EAAE,QAAkB,EAAc,EAAE,CAAC,CAAC;IACvF,QAAQ,EAAE,OAAO;IACjB,IAAI;IACJ,OAAO;IACP,QAAQ;CACT,CAAC,CAAC;AAEH;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,MAAc,EAAE,UAAsB;IACnF,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,UAAU,CAAC,QAAQ,CAAC;IAC3C,MAAM,MAAM,GAAG,GAAG,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,UAAU,CAAC,QAAQ,KAAK,UAAU,CAAC,OAAO,EAAE,CAAC;IACtH,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;IACnD,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC;IAEtC,+FAA+F;IAC/F,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC7H,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;IACzC,OAAO;QACL,MAAM;QACN,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI;QACzB,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,EAAE;QACjC,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;KAC9E,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { Cardinality, Identifier, RelationDeclaration } from '@dancrumb/cant-shared';
2
+ import { type Diagnostic } from './diagnostic.ts';
3
+ import type { LoadedProgram, ModuleSymbol } from './modules.ts';
4
+ import type { References } from './resolve.ts';
5
+ /** A navigation field that a relation adds to an entity (docs/adr/0035-entity-relationships.md). */
6
+ export interface NavigationField {
7
+ name: Identifier;
8
+ /** The entity at the far end. */
9
+ far: ModuleSymbol;
10
+ /** The cardinality written beside the far end, which decides the field's type. */
11
+ farCardinality: Cardinality;
12
+ relation: RelationDeclaration;
13
+ }
14
+ /**
15
+ * Check what entities, relations and the security declarations refer to, across the whole
16
+ * program:
17
+ *
18
+ * - each relation's navigation fields, which must not clash with each other or with the
19
+ * entity's model fields (docs/adr/0035-entity-relationships.md);
20
+ * - the fields an entity's `expose`, `hide`, `identity` and `membership` lines name, which must
21
+ * be fields of its model or, for `identity` and `membership`, navigation fields that reach one
22
+ * entity (docs/adr/0022-entities.md, 0010, 0032);
23
+ * - that subject entities have an `identity`, whose fields claims can carry
24
+ * (docs/adr/0010-security.md, 0032), and that roles are only declared beside a `group` line;
25
+ * - the group kinds and roles that `permissions` declarations and role predicates name, and the
26
+ * permissions that `can` predicates check (docs/adr/0010-security.md).
27
+ */
28
+ export declare function checkEntities(program: LoadedProgram, references: References): {
29
+ navigation: Map<string, Map<string, NavigationField>>;
30
+ diagnostics: Diagnostic[];
31
+ };
32
+ //# sourceMappingURL=entities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entities.d.ts","sourceRoot":"","sources":["../src/entities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EAGX,UAAU,EAEV,mBAAmB,EACpB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAS,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAU,YAAY,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,oGAAoG;AACpG,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,UAAU,CAAC;IACjB,iCAAiC;IACjC,GAAG,EAAE,YAAY,CAAC;IAClB,kFAAkF;IAClF,cAAc,EAAE,WAAW,CAAC;IAC5B,QAAQ,EAAE,mBAAmB,CAAC;CAC/B;AAWD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,GAAG;IAAE,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;IAAC,WAAW,EAAE,UAAU,EAAE,CAAA;CAAE,CA8HlK"}
@@ -0,0 +1,235 @@
1
+ import { error } from './diagnostic.js';
2
+ const keyOf = (symbol) => `${symbol.module}::${symbol.name}`;
3
+ /**
4
+ * Check what entities, relations and the security declarations refer to, across the whole
5
+ * program:
6
+ *
7
+ * - each relation's navigation fields, which must not clash with each other or with the
8
+ * entity's model fields (docs/adr/0035-entity-relationships.md);
9
+ * - the fields an entity's `expose`, `hide`, `identity` and `membership` lines name, which must
10
+ * be fields of its model or, for `identity` and `membership`, navigation fields that reach one
11
+ * entity (docs/adr/0022-entities.md, 0010, 0032);
12
+ * - that subject entities have an `identity`, whose fields claims can carry
13
+ * (docs/adr/0010-security.md, 0032), and that roles are only declared beside a `group` line;
14
+ * - the group kinds and roles that `permissions` declarations and role predicates name, and the
15
+ * permissions that `can` predicates check (docs/adr/0010-security.md).
16
+ */
17
+ export function checkEntities(program, references) {
18
+ const diagnostics = [];
19
+ const reportIn = (module) => (diagnostic) => {
20
+ diagnostics.push({ ...diagnostic, file: module.path });
21
+ };
22
+ const entities = new Map();
23
+ for (const module of program.modules.values()) {
24
+ for (const namespace of module.symbols?.namespaces.values() ?? []) {
25
+ for (const declaration of namespace.types.values()) {
26
+ if (declaration.type !== 'entity')
27
+ continue;
28
+ const modelName = declaration.lines.find((line) => line.type === 'modelsBlock')?.models[0];
29
+ const model = modelName === undefined ? undefined : references.get(modelName)?.declaration;
30
+ entities.set(`${module.path}::${declaration.name.value}`, {
31
+ module,
32
+ declaration,
33
+ ...(model?.type === 'model' && { model }),
34
+ });
35
+ }
36
+ }
37
+ }
38
+ // Each entity's navigation fields, from both ends of every relation.
39
+ const navigation = new Map();
40
+ for (const module of program.modules.values()) {
41
+ const report = reportIn(module);
42
+ for (const { declaration: relation } of module.symbols?.relations ?? []) {
43
+ const left = references.get(relation.left.entity);
44
+ const right = references.get(relation.right.entity);
45
+ if (left === undefined || right === undefined)
46
+ continue;
47
+ const ends = [
48
+ [left, relation.left.field, right, relation.right.cardinality],
49
+ [right, relation.right.field, left, relation.left.cardinality],
50
+ ];
51
+ for (const [entity, field, far, farCardinality] of ends) {
52
+ let fields = navigation.get(keyOf(entity));
53
+ if (fields === undefined) {
54
+ fields = new Map();
55
+ navigation.set(keyOf(entity), fields);
56
+ }
57
+ const earlier = fields.get(field.value);
58
+ const modelField = entities.get(keyOf(entity))?.model?.fields.find((candidate) => candidate.name.value === field.value);
59
+ if (earlier !== undefined || modelField !== undefined) {
60
+ report(error('navigation-clash', `${entity.name} already has a field called ${field.value}${earlier === undefined ? ', from its model' : ', from another relation'}. A navigation field needs a name of its own.`, field.position));
61
+ continue;
62
+ }
63
+ fields.set(field.value, { name: field, far, farCardinality, relation });
64
+ }
65
+ }
66
+ }
67
+ // Group kinds and their roles, across the program.
68
+ const groups = new Map();
69
+ for (const { module, declaration } of entities.values()) {
70
+ const group = declaration.lines.find((line) => line.type === 'groupLine');
71
+ if (group === undefined)
72
+ continue;
73
+ const earlier = groups.get(group.name.value);
74
+ if (earlier !== undefined) {
75
+ reportIn(module)(error('duplicate-group', `${earlier.entity.name.value} already declares a group kind called ${group.name.value}. Each group kind needs its own name.`, group.name.position));
76
+ continue;
77
+ }
78
+ groups.set(group.name.value, {
79
+ name: group.name,
80
+ entity: declaration,
81
+ roles: new Set(declaration.lines.flatMap((line) => (line.type === 'roleLine' ? [line.name.value] : []))),
82
+ });
83
+ }
84
+ for (const [key, { module, declaration, model }] of entities) {
85
+ checkEntity(declaration, model, navigation.get(key) ?? new Map(), entities, references, reportIn(module));
86
+ }
87
+ // Permissions name a group kind, and maybe a role in it.
88
+ const permissions = new Set();
89
+ for (const module of program.modules.values()) {
90
+ const report = reportIn(module);
91
+ for (const { declaration } of module.symbols?.permissions ?? []) {
92
+ for (const permission of declaration.permissions)
93
+ permissions.add(textOf(permission));
94
+ const group = groups.get(declaration.group.value);
95
+ if (group === undefined) {
96
+ report(error('unknown-group', `No entity declares a group kind called ${declaration.group.value}.`, declaration.group.position));
97
+ }
98
+ else if (declaration.role !== undefined && !group.roles.has(declaration.role.value)) {
99
+ report(error('unknown-role', `The ${group.name.value} group kind, declared by ${group.entity.name.value}, has no role called ${declaration.role.value}.`, declaration.role.position));
100
+ }
101
+ }
102
+ }
103
+ // Predicates check permissions that some declaration grants, and roles some group kind has.
104
+ const roles = new Set([...groups.values()].flatMap((group) => [...group.roles]));
105
+ for (const module of program.modules.values()) {
106
+ const report = reportIn(module);
107
+ visitPredicates(module, (predicate) => {
108
+ if (predicate.type === 'canPredicate' && !permissions.has(textOf(predicate.permission))) {
109
+ report(error('unknown-permission', `No \`permissions\` declaration grants "${textOf(predicate.permission)}", so no subject can ever have it.`, predicate.permission.position));
110
+ }
111
+ if (predicate.type === 'rolePredicate' && !roles.has(predicate.role.value)) {
112
+ report(error('unknown-role', `No group kind has a role called ${predicate.role.value}.`, predicate.role.position));
113
+ }
114
+ });
115
+ }
116
+ return { navigation, diagnostics };
117
+ }
118
+ /** The types claims can carry, so a subject entity's `identity` fields must have one (docs/adr/0032-authentication.md). */
119
+ const CLAIM_TYPES = new Set(['TEXT', 'NUMBER', 'BOOLEAN']);
120
+ function checkEntity(declaration, model, navigation, entities, references, report) {
121
+ const has = (type) => declaration.lines.some((line) => line.type === type);
122
+ const isSubject = has('subjectLine');
123
+ if (has('roleLine') && !has('groupLine')) {
124
+ const role = declaration.lines.find((line) => line.type === 'roleLine');
125
+ report(error('role-without-group', `${declaration.name.value} declares a role but has no \`group\` line, so there is no group for a member to hold the role in.`, role?.position ?? declaration.name.position));
126
+ }
127
+ if (isSubject && !has('identityLine')) {
128
+ report(error('missing-identity', `${declaration.name.value} is a subject, so it needs an \`identity\` line: the fields that match an authenticated request to one ${declaration.name.value}.`, declaration.name.position));
129
+ }
130
+ const membership = declaration.lines.find((line) => line.type === 'membershipLine');
131
+ if (membership !== undefined && (isSubject || has('groupLine'))) {
132
+ report(error('membership-conflict', `${declaration.name.value} has a \`membership\` line, so it can't also be a subject or a group.`, membership.position));
133
+ }
134
+ // Without a model, the field names can't be checked.
135
+ if (model === undefined)
136
+ return;
137
+ const fields = new Map(model.fields.map((field) => [field.name.value, field]));
138
+ const unknownField = (name, where) => {
139
+ report(error('unknown-field', `${declaration.name.value}'s model, ${model.name.value}, has no field called ${name.value}, so ${where} can't name it.`, name.position));
140
+ };
141
+ const exposed = new Set();
142
+ for (const line of declaration.lines) {
143
+ if (line.type === 'exposeBlock') {
144
+ for (const item of line.items) {
145
+ if (item.type === 'builtIn')
146
+ continue;
147
+ if (!fields.has(item.field.value))
148
+ unknownField(item.field, 'its `expose` block');
149
+ exposed.add(item.field.value);
150
+ }
151
+ }
152
+ }
153
+ for (const line of declaration.lines) {
154
+ if (line.type !== 'hideBlock')
155
+ continue;
156
+ for (const item of line.items) {
157
+ if (item.type === 'builtIn')
158
+ continue;
159
+ if (!fields.has(item.value))
160
+ unknownField(item, 'its `hide` block');
161
+ else if (exposed.has(item.value)) {
162
+ report(error('exposed-and-hidden', `${item.value} is listed in both the \`expose\` and \`hide\` blocks.`, item.position));
163
+ }
164
+ }
165
+ }
166
+ // A field that reaches exactly one entity: a model field, or a navigation field whose far end is `1`.
167
+ const single = (name, where) => {
168
+ if (fields.has(name.value))
169
+ return 'model';
170
+ const field = navigation.get(name.value);
171
+ if (field === undefined) {
172
+ unknownField(name, where);
173
+ return undefined;
174
+ }
175
+ if (field.farCardinality !== '1') {
176
+ report(error('not-single', `${name.value} is a navigation field that can reach ${field.farCardinality === '0..1' ? 'no' : 'several'} entities, so ${where} can't name it: it must reach exactly one.`, name.position));
177
+ return undefined;
178
+ }
179
+ return field;
180
+ };
181
+ const identity = declaration.lines.find((line) => line.type === 'identityLine');
182
+ for (const name of identity?.fields ?? []) {
183
+ const field = single(name, 'its `identity` line');
184
+ if (!isSubject || field === undefined)
185
+ continue;
186
+ // A subject's identity is matched against claims, which carry text, numbers, booleans and enum values.
187
+ const type = field === 'model' ? fields.get(name.value)?.valueType : undefined;
188
+ const claimable = type !== undefined &&
189
+ ((type.type === 'baseType' && CLAIM_TYPES.has(type.name)) ||
190
+ type.type === 'enum' ||
191
+ ((type.type === 'identifier' || type.type === 'qualifiedIdentifier') && references.get(type)?.typeKind === 'enum'));
192
+ if (!claimable) {
193
+ report(error('identity-type', `${declaration.name.value} is a subject, so its \`identity\` fields must be TEXT, NUMBER, BOOLEAN or an enum, the values claims can carry. ${name.value} isn't.`, name.position));
194
+ }
195
+ }
196
+ if (membership !== undefined) {
197
+ const member = single(membership.member, 'its `membership` line');
198
+ if (member === 'model') {
199
+ report(error('not-navigation', `${membership.member.value} must be a navigation field that reaches a subject entity.`, membership.member.position));
200
+ }
201
+ else if (member !== undefined && !entities.get(keyOf(member.far))?.declaration.lines.some((line) => line.type === 'subjectLine')) {
202
+ report(error('not-subject', `${membership.member.value} reaches ${member.far.name}, which isn't a subject entity.`, membership.member.position));
203
+ }
204
+ const group = single(membership.group, 'its `membership` line');
205
+ if (group === 'model') {
206
+ report(error('not-navigation', `${membership.group.value} must be a navigation field that reaches a group entity.`, membership.group.position));
207
+ }
208
+ else if (group !== undefined && !entities.get(keyOf(group.far))?.declaration.lines.some((line) => line.type === 'groupLine')) {
209
+ report(error('not-group', `${membership.group.value} reaches ${group.far.name}, which isn't a group entity.`, membership.group.position));
210
+ }
211
+ if (membership.roles !== undefined && !fields.has(membership.roles.value))
212
+ unknownField(membership.roles, 'its `membership` line');
213
+ }
214
+ }
215
+ const textOf = (literal) => literal.parts.map((part) => (part.type === 'text' && typeof part.value === 'string' ? part.value : '')).join('');
216
+ /** Call `visit` on every `can` and role predicate in a module, wherever it is. */
217
+ function visitPredicates(module, visit) {
218
+ const walk = (node) => {
219
+ if (Array.isArray(node)) {
220
+ for (const child of node)
221
+ walk(child);
222
+ return;
223
+ }
224
+ if (typeof node !== 'object' || node === null)
225
+ return;
226
+ const typed = node;
227
+ if (typed.type === 'canPredicate' || typed.type === 'rolePredicate')
228
+ visit(typed);
229
+ for (const [key, value] of Object.entries(typed))
230
+ if (key !== 'position')
231
+ walk(value);
232
+ };
233
+ walk(module.program?.children);
234
+ }
235
+ //# sourceMappingURL=entities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entities.js","sourceRoot":"","sources":["../src/entities.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,EAAmB,MAAM,iBAAiB,CAAC;AAqBzD,MAAM,KAAK,GAAG,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K,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAInH,kFAAkF;AAClF,SAAS,eAAe,CAAC,MAAc,EAAE,KAAqC;IAC5E,MAAM,IAAI,GAAG,CAAC,IAAa,EAAQ,EAAE;QACnC,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,KAAK,MAAM,KAAK,IAAI,IAAI;gBAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YACtC,OAAO;QACT,CAAC;QACD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO;QACtD,MAAM,KAAK,GAAG,IAAyE,CAAC;QACxF,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,IAAI,KAAK,CAAC,IAAI,KAAK,eAAe;YAAE,KAAK,CAAC,KAA6B,CAAC,CAAC;QAC1G,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;YAAE,IAAI,GAAG,KAAK,UAAU;gBAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IACxF,CAAC,CAAC;IACF,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,30 @@
1
+ import { type ConstDeclaration, type Evaluation, type Expression, type Identifier, type Value } from '@dancrumb/cant-shared';
2
+ import type { Locals } from './bodies.ts';
3
+ import type { ModuleSymbol } from './modules.ts';
4
+ import type { References } from './resolve.ts';
5
+ /** A symbol's ID in a compiled program, `<module>#<name>` (docs/adr/0039-compiled-programs.md). */
6
+ export declare const symbolId: (symbol: ModuleSymbol) => string;
7
+ /**
8
+ * Works out the values of constant expressions with the evaluator the interpreter shares
9
+ * (docs/adr/0039-compiled-programs.md), resolving names through the compiler's references.
10
+ * Each top-level constant is evaluated once.
11
+ */
12
+ export declare class ConstantValues {
13
+ private readonly references;
14
+ private readonly idOf;
15
+ private readonly values;
16
+ private readonly working;
17
+ /** `idOf` names an enum's symbol in the values it gives, as a compiled program names it. */
18
+ constructor(references: References, idOf?: (symbol: ModuleSymbol) => string);
19
+ /** What evaluating a top-level constant's value gives. A constant that depends on itself isn't constant. */
20
+ of(constant: ConstDeclaration): Evaluation;
21
+ /**
22
+ * Evaluate an expression. Inside an action, `locals` says which names are locals, and
23
+ * `localValues` gives the value of those given a constant value, by where they are declared.
24
+ */
25
+ evaluate(expression: Expression, locals?: Locals, localValues?: ReadonlyMap<Identifier, Value>): Evaluation;
26
+ /** The value of a constant expression, or `undefined` if it doesn't have one. */
27
+ value(expression: Expression, locals?: Locals, localValues?: ReadonlyMap<Identifier, Value>): Value | undefined;
28
+ private scope;
29
+ }
30
+ //# sourceMappingURL=evaluate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../src/evaluate.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAEf,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,KAAK,EACX,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,mGAAmG;AACnG,eAAO,MAAM,QAAQ,WAAY,YAAY,KAAG,MAA2C,CAAC;AAE5F;;;;GAIG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAmC;IACxD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2C;IAClE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;IAEvD,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