@eriknaslund/skiss 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cli.js +1218 -699
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +50 -4
- package/dist/index.js +1195 -693
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,443 @@
|
|
|
1
|
+
// src/parse.ts
|
|
2
|
+
var isWs = (c) => c === " " || c === " ";
|
|
3
|
+
var trimWs = (s) => s.replace(/^[ \t]+/, "").replace(/[ \t]+$/, "");
|
|
4
|
+
var CLASS_NAME = /^[A-Z][A-Za-z0-9]*$/;
|
|
5
|
+
var FIELD_NAME = /^[a-z][A-Za-z0-9]*$/;
|
|
6
|
+
var SYSTEM_OR_VALUE = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
7
|
+
var PRIMITIVES = {
|
|
8
|
+
string: "string",
|
|
9
|
+
int: "int",
|
|
10
|
+
float: "float",
|
|
11
|
+
bool: "bool",
|
|
12
|
+
date: "date",
|
|
13
|
+
datetime: "datetime",
|
|
14
|
+
uri: "uri",
|
|
15
|
+
text: "string",
|
|
16
|
+
integer: "int",
|
|
17
|
+
boolean: "bool"
|
|
18
|
+
};
|
|
19
|
+
function primitiveFor(word) {
|
|
20
|
+
return Object.hasOwn(PRIMITIVES, word) ? PRIMITIVES[word] : void 0;
|
|
21
|
+
}
|
|
22
|
+
var FIELD_ORDER = "name, `*`, `: type`, `@System`, `= Class.field`, then `#` and `?`";
|
|
23
|
+
var CLASS_ORDER = "name, `@System`, `~ Class`, then `#` and `?`";
|
|
24
|
+
function parse(source) {
|
|
25
|
+
const classes = [];
|
|
26
|
+
const diagnostics = [];
|
|
27
|
+
let current;
|
|
28
|
+
const text0 = source.startsWith("\uFEFF") ? source.slice(1) : source;
|
|
29
|
+
const lines = text0.split(/\r\n|\n|\r/);
|
|
30
|
+
for (let i = 0; i < lines.length; i++) {
|
|
31
|
+
const line = i + 1;
|
|
32
|
+
const text = (lines[i] ?? "").replace(/[ \t]+$/, "");
|
|
33
|
+
const first = text[0];
|
|
34
|
+
if (first === void 0 || first === "#") continue;
|
|
35
|
+
if (isWs(first)) {
|
|
36
|
+
const start = text.search(/[^ \t]/);
|
|
37
|
+
if (current === void 0) {
|
|
38
|
+
diagnostics.push({
|
|
39
|
+
severity: "error",
|
|
40
|
+
code: "E_FIELD_WITHOUT_CLASS",
|
|
41
|
+
message: "An indented line is a field, but there is no valid class line above it",
|
|
42
|
+
line,
|
|
43
|
+
col: start,
|
|
44
|
+
end: text.length
|
|
45
|
+
});
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const result = parseFieldLine(text, line);
|
|
49
|
+
if (result.ok) current.fields.push(result.value);
|
|
50
|
+
else diagnostics.push(result.diagnostic);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (/\p{L}/u.test(first)) {
|
|
54
|
+
const result = parseClassLine(text, line);
|
|
55
|
+
if (result.ok) {
|
|
56
|
+
classes.push(result.value);
|
|
57
|
+
current = result.value;
|
|
58
|
+
} else {
|
|
59
|
+
diagnostics.push(result.diagnostic);
|
|
60
|
+
current = void 0;
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
diagnostics.push({
|
|
65
|
+
severity: "error",
|
|
66
|
+
code: "E_UNPARSABLE",
|
|
67
|
+
message: "A line at column 0 must start with a class name or `#` for a comment",
|
|
68
|
+
line,
|
|
69
|
+
col: 0,
|
|
70
|
+
end: text.length
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return { classes, diagnostics };
|
|
74
|
+
}
|
|
75
|
+
function isDoubtMarker(text, i) {
|
|
76
|
+
return text[i] === "?" && isWs(text[i - 1]) && (i + 1 === text.length || isWs(text[i + 1]));
|
|
77
|
+
}
|
|
78
|
+
function splitTrailer(text) {
|
|
79
|
+
let start = -1;
|
|
80
|
+
for (let i = 1; i < text.length; i++) {
|
|
81
|
+
if (!isWs(text[i - 1])) continue;
|
|
82
|
+
if (text[i] === "#" || isDoubtMarker(text, i)) {
|
|
83
|
+
start = i;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (start < 0) return { head: text };
|
|
88
|
+
const trailer = { head: text.slice(0, start).replace(/[ \t]+$/, "") };
|
|
89
|
+
if (text[start] === "?") {
|
|
90
|
+
const note = trimWs(text.slice(start + 1));
|
|
91
|
+
if (note !== "") trailer.note = note;
|
|
92
|
+
return trailer;
|
|
93
|
+
}
|
|
94
|
+
let doubt = -1;
|
|
95
|
+
for (let i = start + 1; i < text.length; i++) {
|
|
96
|
+
if (isDoubtMarker(text, i)) {
|
|
97
|
+
doubt = i;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const description = trimWs(text.slice(start + 1, doubt < 0 ? text.length : doubt));
|
|
102
|
+
if (description !== "") trailer.description = description;
|
|
103
|
+
if (doubt >= 0) {
|
|
104
|
+
const note = trimWs(text.slice(doubt + 1));
|
|
105
|
+
if (note !== "") trailer.note = note;
|
|
106
|
+
}
|
|
107
|
+
return trailer;
|
|
108
|
+
}
|
|
109
|
+
var WORD = /[\p{L}\p{N}_-]+/uy;
|
|
110
|
+
var OPERATORS = ["*", ":", "@", "~", "=", ".", "|", "["];
|
|
111
|
+
function tokenize(head) {
|
|
112
|
+
const tokens = [];
|
|
113
|
+
let i = 0;
|
|
114
|
+
while (i < head.length) {
|
|
115
|
+
const c = head[i] ?? "";
|
|
116
|
+
if (isWs(c)) {
|
|
117
|
+
i++;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
WORD.lastIndex = i;
|
|
121
|
+
const word = WORD.exec(head);
|
|
122
|
+
if (word !== null) {
|
|
123
|
+
tokens.push({ kind: "word", text: word[0], col: i, end: i + word[0].length });
|
|
124
|
+
i += word[0].length;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (c === "[" && head[i + 1] === "]") {
|
|
128
|
+
tokens.push({ kind: "[]", text: "[]", col: i, end: i + 2 });
|
|
129
|
+
i += 2;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const ch = String.fromCodePoint(head.codePointAt(i) ?? 0);
|
|
133
|
+
const kind = OPERATORS.find((op) => op === ch) ?? "other";
|
|
134
|
+
tokens.push({ kind, text: ch, col: i, end: i + ch.length });
|
|
135
|
+
i += ch.length;
|
|
136
|
+
}
|
|
137
|
+
return tokens;
|
|
138
|
+
}
|
|
139
|
+
var peek = (c) => c.tokens[c.pos];
|
|
140
|
+
function take(c) {
|
|
141
|
+
const t = c.tokens[c.pos];
|
|
142
|
+
if (t !== void 0) c.pos++;
|
|
143
|
+
return t;
|
|
144
|
+
}
|
|
145
|
+
var name = (t, line) => ({ text: t.text, line, col: t.col, end: t.end });
|
|
146
|
+
function fail(code, message, line, span) {
|
|
147
|
+
return {
|
|
148
|
+
ok: false,
|
|
149
|
+
diagnostic: { severity: "error", code, message, line, col: span.col, end: span.end }
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
var afterSpan = (after) => ({ col: after.end, end: after.end });
|
|
153
|
+
function parseClassLine(text, line) {
|
|
154
|
+
const trailer = splitTrailer(text);
|
|
155
|
+
const c = { tokens: tokenize(trailer.head), pos: 0, line };
|
|
156
|
+
const head = take(c);
|
|
157
|
+
if (head === void 0 || head.kind !== "word") {
|
|
158
|
+
return fail("E_UNPARSABLE", "A class line must start with a class name", line, {
|
|
159
|
+
col: 0,
|
|
160
|
+
end: Math.max(1, trailer.head.length)
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (!CLASS_NAME.test(head.text)) {
|
|
164
|
+
return fail(
|
|
165
|
+
"E_BAD_NAME",
|
|
166
|
+
`Class names are UpperCamelCase (letters and digits, uppercase first): \`${head.text}\``,
|
|
167
|
+
line,
|
|
168
|
+
head
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const node = { name: name(head, line), line };
|
|
172
|
+
const at = peek(c);
|
|
173
|
+
if (at?.kind === "@") {
|
|
174
|
+
take(c);
|
|
175
|
+
const system = parseSystem(c, at);
|
|
176
|
+
if (!system.ok) return system;
|
|
177
|
+
node.system = system.value;
|
|
178
|
+
}
|
|
179
|
+
const tilde = peek(c);
|
|
180
|
+
if (tilde?.kind === "~") {
|
|
181
|
+
take(c);
|
|
182
|
+
const target = take(c);
|
|
183
|
+
if (target === void 0 || target.kind !== "word") {
|
|
184
|
+
return fail(
|
|
185
|
+
"E_UNPARSABLE",
|
|
186
|
+
"Expected a class name after `~`, e.g. `CharacterPage @Community ~ Character`",
|
|
187
|
+
line,
|
|
188
|
+
target ?? afterSpan(tilde)
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
if (!CLASS_NAME.test(target.text)) {
|
|
192
|
+
return fail(
|
|
193
|
+
"E_BAD_NAME",
|
|
194
|
+
`The class after \`~\` must be UpperCamelCase: \`${target.text}\``,
|
|
195
|
+
line,
|
|
196
|
+
target
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
node.similarTo = name(target, line);
|
|
200
|
+
}
|
|
201
|
+
const rest = peek(c);
|
|
202
|
+
if (rest !== void 0) return unexpected(rest, line, "class", CLASS_ORDER);
|
|
203
|
+
if (trailer.description !== void 0) node.description = trailer.description;
|
|
204
|
+
if (trailer.note !== void 0) node.note = trailer.note;
|
|
205
|
+
return { ok: true, value: { ...node, fields: [] } };
|
|
206
|
+
}
|
|
207
|
+
function parseFieldLine(text, line) {
|
|
208
|
+
const trailer = splitTrailer(text);
|
|
209
|
+
const c = { tokens: tokenize(trailer.head), pos: 0, line };
|
|
210
|
+
const head = take(c);
|
|
211
|
+
if (head === void 0) {
|
|
212
|
+
const col = text.search(/[^ \t]/);
|
|
213
|
+
const message = text[col] === "#" ? "A `#` comment must start at column 0; an indented line is a field and needs a name" : "A field line needs a field name before its trailer";
|
|
214
|
+
return fail("E_UNPARSABLE", message, line, { col, end: Math.max(col + 1, text.length) });
|
|
215
|
+
}
|
|
216
|
+
if (head.kind !== "word") return unexpected(head, line, "field", FIELD_ORDER);
|
|
217
|
+
if (!FIELD_NAME.test(head.text)) {
|
|
218
|
+
return fail(
|
|
219
|
+
"E_BAD_NAME",
|
|
220
|
+
`Field names are lowerCamelCase (letters and digits, lowercase first): \`${head.text}\``,
|
|
221
|
+
line,
|
|
222
|
+
head
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
const node = { name: name(head, line), line, identifier: false };
|
|
226
|
+
const star = peek(c);
|
|
227
|
+
if (star?.kind === "*") {
|
|
228
|
+
take(c);
|
|
229
|
+
node.identifier = true;
|
|
230
|
+
node.identifierAt = name(star, line);
|
|
231
|
+
}
|
|
232
|
+
const colon = peek(c);
|
|
233
|
+
if (colon?.kind === ":") {
|
|
234
|
+
take(c);
|
|
235
|
+
const type = parseType(c, colon);
|
|
236
|
+
if (!type.ok) return type;
|
|
237
|
+
node.type = type.value;
|
|
238
|
+
}
|
|
239
|
+
const at = peek(c);
|
|
240
|
+
if (at?.kind === "@") {
|
|
241
|
+
take(c);
|
|
242
|
+
const system = parseSystem(c, at);
|
|
243
|
+
if (!system.ok) return system;
|
|
244
|
+
node.system = system.value;
|
|
245
|
+
}
|
|
246
|
+
const equals = peek(c);
|
|
247
|
+
if (equals?.kind === "=") {
|
|
248
|
+
take(c);
|
|
249
|
+
const example = "e.g. `characterId = Character.id`";
|
|
250
|
+
const className = take(c);
|
|
251
|
+
if (className === void 0 || className.kind !== "word") {
|
|
252
|
+
return fail(
|
|
253
|
+
"E_UNPARSABLE",
|
|
254
|
+
`Expected \`Class.field\` after \`=\`, ${example}`,
|
|
255
|
+
line,
|
|
256
|
+
className ?? afterSpan(equals)
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (!CLASS_NAME.test(className.text)) {
|
|
260
|
+
return fail(
|
|
261
|
+
"E_BAD_NAME",
|
|
262
|
+
`The class after \`=\` must be UpperCamelCase: \`${className.text}\``,
|
|
263
|
+
line,
|
|
264
|
+
className
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
const dot = take(c);
|
|
268
|
+
if (dot === void 0 || dot.kind !== ".") {
|
|
269
|
+
return fail(
|
|
270
|
+
"E_UNPARSABLE",
|
|
271
|
+
`Expected \`.\` and a field name after \`= ${className.text}\`, ${example}`,
|
|
272
|
+
line,
|
|
273
|
+
dot ?? afterSpan(className)
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
const fieldName = take(c);
|
|
277
|
+
if (fieldName === void 0 || fieldName.kind !== "word") {
|
|
278
|
+
return fail(
|
|
279
|
+
"E_UNPARSABLE",
|
|
280
|
+
`Expected a field name after \`= ${className.text}.\`, ${example}`,
|
|
281
|
+
line,
|
|
282
|
+
fieldName ?? afterSpan(dot)
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
if (!FIELD_NAME.test(fieldName.text)) {
|
|
286
|
+
return fail(
|
|
287
|
+
"E_BAD_NAME",
|
|
288
|
+
`The field after \`= ${className.text}.\` must be lowerCamelCase: \`${fieldName.text}\``,
|
|
289
|
+
line,
|
|
290
|
+
fieldName
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
node.joinsTo = { className: name(className, line), fieldName: name(fieldName, line) };
|
|
294
|
+
}
|
|
295
|
+
const rest = peek(c);
|
|
296
|
+
if (rest !== void 0) return unexpected(rest, line, "field", FIELD_ORDER);
|
|
297
|
+
if (trailer.description !== void 0) node.description = trailer.description;
|
|
298
|
+
if (trailer.note !== void 0) node.note = trailer.note;
|
|
299
|
+
return { ok: true, value: node };
|
|
300
|
+
}
|
|
301
|
+
function parseSystem(c, at) {
|
|
302
|
+
const system = take(c);
|
|
303
|
+
if (system === void 0 || system.kind !== "word") {
|
|
304
|
+
return fail(
|
|
305
|
+
"E_UNPARSABLE",
|
|
306
|
+
"Expected a system name after `@`, e.g. `@Catalog`",
|
|
307
|
+
c.line,
|
|
308
|
+
system ?? afterSpan(at)
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
if (!SYSTEM_OR_VALUE.test(system.text)) {
|
|
312
|
+
return fail(
|
|
313
|
+
"E_UNPARSABLE",
|
|
314
|
+
`A system name is letters, digits, \`-\` and \`_\`, starting with a letter: \`${system.text}\``,
|
|
315
|
+
c.line,
|
|
316
|
+
system
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
return { ok: true, value: name(system, c.line) };
|
|
320
|
+
}
|
|
321
|
+
function parseType(c, colon) {
|
|
322
|
+
const first = peek(c);
|
|
323
|
+
if (first === void 0 || first.kind === "*" || first.kind === "@" || first.kind === "=") {
|
|
324
|
+
return fail(
|
|
325
|
+
"E_MISSING_TYPE",
|
|
326
|
+
"A colon needs a type after it, e.g. `crewSize: int`",
|
|
327
|
+
c.line,
|
|
328
|
+
colon
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
if (first.kind !== "word") {
|
|
332
|
+
return fail(
|
|
333
|
+
"E_UNPARSABLE",
|
|
334
|
+
`Expected a type after \`:\`, found \`${first.text}\``,
|
|
335
|
+
c.line,
|
|
336
|
+
first
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
take(c);
|
|
340
|
+
const words = [first];
|
|
341
|
+
for (let pipe = peek(c); pipe?.kind === "|"; pipe = peek(c)) {
|
|
342
|
+
take(c);
|
|
343
|
+
const value = take(c);
|
|
344
|
+
if (value === void 0 || value.kind !== "word") {
|
|
345
|
+
return fail(
|
|
346
|
+
"E_UNPARSABLE",
|
|
347
|
+
"Expected an enum value after `|`, e.g. `climate: arid|temperate|frozen`",
|
|
348
|
+
c.line,
|
|
349
|
+
value ?? afterSpan(pipe)
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
words.push(value);
|
|
353
|
+
}
|
|
354
|
+
let many = false;
|
|
355
|
+
const bracket = peek(c);
|
|
356
|
+
if (bracket?.kind === "[]") {
|
|
357
|
+
take(c);
|
|
358
|
+
many = true;
|
|
359
|
+
} else if (bracket?.kind === "[") {
|
|
360
|
+
return fail(
|
|
361
|
+
"E_UNCLOSED_MANY",
|
|
362
|
+
"`[` must be closed: `[]` marks a many-valued field",
|
|
363
|
+
c.line,
|
|
364
|
+
bracket
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
if (words.length > 1) {
|
|
368
|
+
for (const w of words) {
|
|
369
|
+
if (!SYSTEM_OR_VALUE.test(w.text)) {
|
|
370
|
+
return fail(
|
|
371
|
+
"E_UNPARSABLE",
|
|
372
|
+
`An enum value is letters, digits, \`-\` and \`_\`, starting with a letter: \`${w.text}\``,
|
|
373
|
+
c.line,
|
|
374
|
+
w
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return { ok: true, value: { kind: "enum", values: words.map((w) => name(w, c.line)), many } };
|
|
379
|
+
}
|
|
380
|
+
const word = first.text;
|
|
381
|
+
const primitive = primitiveFor(word);
|
|
382
|
+
if (primitive !== void 0) {
|
|
383
|
+
return {
|
|
384
|
+
ok: true,
|
|
385
|
+
value: { kind: "primitive", name: primitive, written: name(first, c.line), many }
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
if (/^[A-Z]/.test(word)) {
|
|
389
|
+
if (!CLASS_NAME.test(word)) {
|
|
390
|
+
return fail(
|
|
391
|
+
"E_BAD_NAME",
|
|
392
|
+
`A referenced class must be UpperCamelCase: \`${word}\``,
|
|
393
|
+
c.line,
|
|
394
|
+
first
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
return { ok: true, value: { kind: "class", name: name(first, c.line), many } };
|
|
398
|
+
}
|
|
399
|
+
if (/^[a-z]/.test(word)) {
|
|
400
|
+
return { ok: true, value: { kind: "unknown", name: name(first, c.line), many } };
|
|
401
|
+
}
|
|
402
|
+
return fail(
|
|
403
|
+
"E_UNPARSABLE",
|
|
404
|
+
`A type is a primitive, a class name or an enum, not \`${word}\``,
|
|
405
|
+
c.line,
|
|
406
|
+
first
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
function unexpected(token, line, kind, order) {
|
|
410
|
+
const modifiers = kind === "field" ? ["*", ":", "@", "="] : ["@", "~"];
|
|
411
|
+
if (modifiers.includes(token.kind)) {
|
|
412
|
+
return fail(
|
|
413
|
+
"E_UNPARSABLE",
|
|
414
|
+
`\`${token.text}\` is out of order; a ${kind} line reads: ${order}`,
|
|
415
|
+
line,
|
|
416
|
+
token
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
if (token.text === "<") {
|
|
420
|
+
return fail(
|
|
421
|
+
"E_UNPARSABLE",
|
|
422
|
+
"`<` is reserved for inheritance and not yet part of the language",
|
|
423
|
+
line,
|
|
424
|
+
token
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
if (token.text === "?") {
|
|
428
|
+
return fail(
|
|
429
|
+
"E_UNPARSABLE",
|
|
430
|
+
"`?` attached to a word is not a marker; a doubt needs whitespace before `?` and after it",
|
|
431
|
+
line,
|
|
432
|
+
token
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
if (token.text === "#") {
|
|
436
|
+
return fail("E_UNPARSABLE", "A description needs whitespace before `#`", line, token);
|
|
437
|
+
}
|
|
438
|
+
return fail("E_UNPARSABLE", `Unexpected \`${token.text}\` on a ${kind} line`, line, token);
|
|
439
|
+
}
|
|
440
|
+
|
|
1
441
|
// src/resolve.ts
|
|
2
442
|
function resolve(doc) {
|
|
3
443
|
const out = {
|
|
@@ -16,7 +456,9 @@ function resolve(doc) {
|
|
|
16
456
|
const reference = (name2) => {
|
|
17
457
|
if (declared.has(name2.text)) return true;
|
|
18
458
|
if (!undeclared.has(name2.text)) undeclared.set(name2.text, name2);
|
|
19
|
-
|
|
459
|
+
const lower = name2.text.toLowerCase();
|
|
460
|
+
const hint = Object.hasOwn(PRIMITIVES, lower) ? `. Did you mean \`${lower}\`?` : "";
|
|
461
|
+
warn("W_UNDECLARED_CLASS", `class \`${name2.text}\` is not declared${hint}`, name2);
|
|
20
462
|
return false;
|
|
21
463
|
};
|
|
22
464
|
const firstClass = /* @__PURE__ */ new Map();
|
|
@@ -49,7 +491,7 @@ function resolve(doc) {
|
|
|
49
491
|
warn(
|
|
50
492
|
"W_MULTIPLE_IDENTIFIERS",
|
|
51
493
|
`\`*\` on \`${field.name.text}\` is ignored: class \`${cls.name.text}\` already has the identifier \`${identifier.name.text}\` on line ${identifier.line}`,
|
|
52
|
-
field.name
|
|
494
|
+
field.identifierAt ?? field.name
|
|
53
495
|
);
|
|
54
496
|
field.identifier = false;
|
|
55
497
|
}
|
|
@@ -78,9 +520,9 @@ function resolve(doc) {
|
|
|
78
520
|
}
|
|
79
521
|
}
|
|
80
522
|
}
|
|
81
|
-
const
|
|
523
|
+
const present2 = new Set(out.diagnostics.map(key));
|
|
82
524
|
for (const w of warnings) {
|
|
83
|
-
if (!
|
|
525
|
+
if (!present2.has(key(w))) out.diagnostics.push(w);
|
|
84
526
|
}
|
|
85
527
|
out.diagnostics.sort((a, b) => a.line - b.line);
|
|
86
528
|
out.undeclared = [...undeclared.values()].map((n) => ({ ...n }));
|
|
@@ -104,6 +546,7 @@ var copyJoin = (j) => ({
|
|
|
104
546
|
});
|
|
105
547
|
function copyField(f) {
|
|
106
548
|
const out = { ...f, name: copyName(f.name) };
|
|
549
|
+
if (f.identifierAt !== void 0) out.identifierAt = copyName(f.identifierAt);
|
|
107
550
|
if (f.type !== void 0) out.type = copyType(f.type);
|
|
108
551
|
if (f.joinsTo !== void 0) out.joinsTo = copyJoin(f.joinsTo);
|
|
109
552
|
return out;
|
|
@@ -115,18 +558,7 @@ function copyClass(c) {
|
|
|
115
558
|
return out;
|
|
116
559
|
}
|
|
117
560
|
var key = (d) => [d.severity, d.code, d.line, d.col, d.end, d.message].join(" ");
|
|
118
|
-
var TYPE_WORDS =
|
|
119
|
-
"string",
|
|
120
|
-
"int",
|
|
121
|
-
"float",
|
|
122
|
-
"bool",
|
|
123
|
-
"date",
|
|
124
|
-
"datetime",
|
|
125
|
-
"uri",
|
|
126
|
-
"text",
|
|
127
|
-
"integer",
|
|
128
|
-
"boolean"
|
|
129
|
-
];
|
|
561
|
+
var TYPE_WORDS = Object.keys(PRIMITIVES);
|
|
130
562
|
var MAX_DISTANCE = 2;
|
|
131
563
|
function closestTypeWord(word) {
|
|
132
564
|
let best;
|
|
@@ -136,723 +568,288 @@ function closestTypeWord(word) {
|
|
|
136
568
|
const distance = levenshtein(word, candidate);
|
|
137
569
|
if (distance > MAX_DISTANCE || distance > bestDistance) continue;
|
|
138
570
|
const prefix = commonPrefixLength(word, candidate);
|
|
139
|
-
if (distance < bestDistance || prefix > bestPrefix) {
|
|
140
|
-
best = candidate;
|
|
141
|
-
bestDistance = distance;
|
|
142
|
-
bestPrefix = prefix;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return best;
|
|
146
|
-
}
|
|
147
|
-
function commonPrefixLength(a, b) {
|
|
148
|
-
let i = 0;
|
|
149
|
-
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
150
|
-
return i;
|
|
151
|
-
}
|
|
152
|
-
function levenshtein(a, b) {
|
|
153
|
-
if (a === b) return 0;
|
|
154
|
-
if (a.length === 0) return b.length;
|
|
155
|
-
if (b.length === 0) return a.length;
|
|
156
|
-
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
157
|
-
for (let i = 1; i <= a.length; i++) {
|
|
158
|
-
const current = [i];
|
|
159
|
-
for (let j = 1; j <= b.length; j++) {
|
|
160
|
-
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
161
|
-
current[j] = Math.min(
|
|
162
|
-
(previous[j] ?? 0) + 1,
|
|
163
|
-
(current[j - 1] ?? 0) + 1,
|
|
164
|
-
(previous[j - 1] ?? 0) + cost
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
previous = current;
|
|
168
|
-
}
|
|
169
|
-
return previous[b.length] ?? 0;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// src/generators/linkml.ts
|
|
173
|
-
var BASE_URI = "https://example.org";
|
|
174
|
-
var RANGE = {
|
|
175
|
-
string: "string",
|
|
176
|
-
int: "integer",
|
|
177
|
-
float: "float",
|
|
178
|
-
bool: "boolean",
|
|
179
|
-
date: "date",
|
|
180
|
-
datetime: "datetime",
|
|
181
|
-
uri: "uri"
|
|
182
|
-
};
|
|
183
|
-
function toLinkML(doc, opts) {
|
|
184
|
-
const resolved = resolve(doc);
|
|
185
|
-
const schemaName = linkmlName(opts.schemaName);
|
|
186
|
-
const classes = emittedClasses(resolved.classes);
|
|
187
|
-
const classNames = new Set(classes.map((cls) => cls.node.name.text));
|
|
188
|
-
for (const name2 of resolved.undeclared) classNames.add(name2.text);
|
|
189
|
-
const enums = nameEnums(classes, classNames);
|
|
190
|
-
const prefixes = { linkml: "https://w3id.org/linkml/" };
|
|
191
|
-
if (!Object.hasOwn(prefixes, schemaName)) prefixes[schemaName] = `${BASE_URI}/${schemaName}/`;
|
|
192
|
-
const systemPrefix = /* @__PURE__ */ new Map();
|
|
193
|
-
const addSystem = (system) => {
|
|
194
|
-
const prefix = linkmlName(system);
|
|
195
|
-
if (!systemPrefix.has(system)) systemPrefix.set(system, prefix);
|
|
196
|
-
if (!Object.hasOwn(prefixes, prefix)) prefixes[prefix] = `${BASE_URI}/system/${prefix}/`;
|
|
197
|
-
};
|
|
198
|
-
for (const { node, fields } of classes) {
|
|
199
|
-
if (node.system !== void 0) addSystem(node.system.text);
|
|
200
|
-
for (const field of fields) {
|
|
201
|
-
if (field.system !== void 0) addSystem(field.system.text);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
const declaredSystem = /* @__PURE__ */ new Map();
|
|
205
|
-
for (const { node } of classes) {
|
|
206
|
-
if (!declaredSystem.has(node.name.text)) declaredSystem.set(node.name.text, node.system?.text);
|
|
207
|
-
}
|
|
208
|
-
const mappingPrefix = (target) => {
|
|
209
|
-
const system = declaredSystem.get(target);
|
|
210
|
-
if (system === void 0) return schemaName;
|
|
211
|
-
return systemPrefix.get(system) ?? schemaName;
|
|
212
|
-
};
|
|
213
|
-
const schema = {
|
|
214
|
-
id: `${BASE_URI}/${schemaName}`,
|
|
215
|
-
name: schemaName,
|
|
216
|
-
default_prefix: schemaName,
|
|
217
|
-
default_range: "string",
|
|
218
|
-
prefixes,
|
|
219
|
-
imports: ["linkml:types"]
|
|
220
|
-
};
|
|
221
|
-
const out = {};
|
|
222
|
-
for (const cls of classes) {
|
|
223
|
-
out[cls.node.name.text] = buildClass(cls, mappingPrefix, enums);
|
|
224
|
-
}
|
|
225
|
-
for (const name2 of resolved.undeclared) {
|
|
226
|
-
if (!(name2.text in out)) out[name2.text] = { annotations: { undeclared: true } };
|
|
227
|
-
}
|
|
228
|
-
if (Object.keys(out).length > 0) schema.classes = out;
|
|
229
|
-
const enumBlock = buildEnums(classes, enums);
|
|
230
|
-
if (Object.keys(enumBlock).length > 0) schema.enums = enumBlock;
|
|
231
|
-
return schema;
|
|
232
|
-
}
|
|
233
|
-
function emittedClasses(classes) {
|
|
234
|
-
const out = [];
|
|
235
|
-
const seen = /* @__PURE__ */ new Set();
|
|
236
|
-
for (const node of classes) {
|
|
237
|
-
if (seen.has(node.name.text)) continue;
|
|
238
|
-
seen.add(node.name.text);
|
|
239
|
-
const fields = [];
|
|
240
|
-
const seenField = /* @__PURE__ */ new Set();
|
|
241
|
-
for (const field of node.fields) {
|
|
242
|
-
if (seenField.has(field.name.text)) continue;
|
|
243
|
-
seenField.add(field.name.text);
|
|
244
|
-
fields.push(field);
|
|
245
|
-
}
|
|
246
|
-
out.push({ node, fields });
|
|
247
|
-
}
|
|
248
|
-
return out;
|
|
249
|
-
}
|
|
250
|
-
var useKey = (className, fieldName) => `${className}\0${fieldName}`;
|
|
251
|
-
function nameEnums(classes, classNames) {
|
|
252
|
-
const uses = [];
|
|
253
|
-
for (const { node, fields } of classes) {
|
|
254
|
-
for (const field of fields) {
|
|
255
|
-
if (field.type?.kind !== "enum") continue;
|
|
256
|
-
uses.push({
|
|
257
|
-
className: node.name.text,
|
|
258
|
-
fieldName: field.name.text,
|
|
259
|
-
values: field.type.values.map((v) => v.text).join("|")
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
const valueSets = /* @__PURE__ */ new Map();
|
|
264
|
-
for (const use of uses) {
|
|
265
|
-
const seen = valueSets.get(use.fieldName) ?? /* @__PURE__ */ new Set();
|
|
266
|
-
seen.add(use.values);
|
|
267
|
-
valueSets.set(use.fieldName, seen);
|
|
268
|
-
}
|
|
269
|
-
const taken = /* @__PURE__ */ new Map();
|
|
270
|
-
const names = /* @__PURE__ */ new Map();
|
|
271
|
-
for (const use of uses) {
|
|
272
|
-
const shared = (valueSets.get(use.fieldName)?.size ?? 0) <= 1;
|
|
273
|
-
const base = shared ? `${capitalise(use.fieldName)}Enum` : `${capitalise(use.className)}${capitalise(use.fieldName)}Enum`;
|
|
274
|
-
let name2 = base;
|
|
275
|
-
for (let n = 2; classNames.has(name2) || taken.has(name2) && taken.get(name2) !== use.values; n++) {
|
|
276
|
-
name2 = `${base}${n}`;
|
|
277
|
-
}
|
|
278
|
-
taken.set(name2, use.values);
|
|
279
|
-
names.set(useKey(use.className, use.fieldName), name2);
|
|
280
|
-
}
|
|
281
|
-
return names;
|
|
282
|
-
}
|
|
283
|
-
function buildEnums(classes, enums) {
|
|
284
|
-
const out = {};
|
|
285
|
-
for (const { node, fields } of classes) {
|
|
286
|
-
for (const field of fields) {
|
|
287
|
-
if (field.type?.kind !== "enum") continue;
|
|
288
|
-
const name2 = enums.get(useKey(node.name.text, field.name.text));
|
|
289
|
-
if (name2 === void 0 || name2 in out) continue;
|
|
290
|
-
const values = {};
|
|
291
|
-
for (const value of field.type.values) values[value.text] = null;
|
|
292
|
-
out[name2] = { permissible_values: values };
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
return out;
|
|
296
|
-
}
|
|
297
|
-
function buildClass(cls, mappingPrefix, enums) {
|
|
298
|
-
const { node, fields } = cls;
|
|
299
|
-
const out = {};
|
|
300
|
-
if (node.description !== void 0) out.description = node.description;
|
|
301
|
-
const annotations = {};
|
|
302
|
-
if (node.system !== void 0) annotations.system = node.system.text;
|
|
303
|
-
if (node.note !== void 0) annotations.note = node.note;
|
|
304
|
-
if (Object.keys(annotations).length > 0) out.annotations = annotations;
|
|
305
|
-
if (node.similarTo !== void 0) {
|
|
306
|
-
const target = node.similarTo.text;
|
|
307
|
-
out.close_mappings = [`${mappingPrefix(target)}:${target}`];
|
|
308
|
-
}
|
|
309
|
-
const attributes = {};
|
|
310
|
-
for (const field of fields) {
|
|
311
|
-
attributes[field.name.text] = buildAttribute(field, node.name.text, enums);
|
|
312
|
-
}
|
|
313
|
-
if (Object.keys(attributes).length > 0) out.attributes = attributes;
|
|
314
|
-
return out;
|
|
315
|
-
}
|
|
316
|
-
function buildAttribute(field, className, enums) {
|
|
317
|
-
const out = {};
|
|
318
|
-
if (field.description !== void 0) out.description = field.description;
|
|
319
|
-
const range = rangeOf(field, className, enums);
|
|
320
|
-
if (range !== void 0) out.range = range;
|
|
321
|
-
if (field.type?.many === true) out.multivalued = true;
|
|
322
|
-
if (field.identifier) out.identifier = true;
|
|
323
|
-
const annotations = {};
|
|
324
|
-
if (field.system !== void 0) annotations.system = field.system.text;
|
|
325
|
-
if (field.joinsTo !== void 0) {
|
|
326
|
-
annotations.joins_to = `${field.joinsTo.className.text}.${field.joinsTo.fieldName.text}`;
|
|
327
|
-
}
|
|
328
|
-
if (field.note !== void 0) annotations.note = field.note;
|
|
329
|
-
if (Object.keys(annotations).length > 0) out.annotations = annotations;
|
|
330
|
-
return out;
|
|
331
|
-
}
|
|
332
|
-
function rangeOf(field, className, enums) {
|
|
333
|
-
const type = field.type;
|
|
334
|
-
if (type === void 0) return void 0;
|
|
335
|
-
switch (type.kind) {
|
|
336
|
-
case "primitive":
|
|
337
|
-
return RANGE[type.name];
|
|
338
|
-
case "class":
|
|
339
|
-
return type.name.text;
|
|
340
|
-
case "unknown":
|
|
341
|
-
return "string";
|
|
342
|
-
case "enum":
|
|
343
|
-
return enums.get(useKey(className, field.name.text));
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
var RESERVED_PREFIXES = /* @__PURE__ */ new Set(["linkml", "xsd", "shex", "schema"]);
|
|
347
|
-
var linkmlName = (text) => {
|
|
348
|
-
const name2 = text.toLowerCase().replace(/[^a-z0-9]/g, "_");
|
|
349
|
-
if (!/[a-z0-9]/.test(name2)) return "sketch";
|
|
350
|
-
if (RESERVED_PREFIXES.has(name2)) return `${name2}_`;
|
|
351
|
-
return /^[0-9]/.test(name2) ? `_${name2}` : name2;
|
|
352
|
-
};
|
|
353
|
-
var capitalise = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
354
|
-
|
|
355
|
-
// src/generators/mermaid.ts
|
|
356
|
-
function toMermaid(doc, opts) {
|
|
357
|
-
const resolved = resolve(doc);
|
|
358
|
-
const notes = opts?.notes === true;
|
|
359
|
-
const lines = ["classDiagram"];
|
|
360
|
-
for (const cls of resolved.classes) {
|
|
361
|
-
lines.push(` class ${cls.name.text} {`);
|
|
362
|
-
if (cls.system !== void 0) lines.push(` <<${cls.system.text}>>`);
|
|
363
|
-
for (const field of cls.fields) lines.push(` ${member(field)}`);
|
|
364
|
-
lines.push(" }");
|
|
365
|
-
}
|
|
366
|
-
for (const name2 of resolved.undeclared) {
|
|
367
|
-
lines.push(` class ${name2.text} {`, " <<undeclared>>", " }");
|
|
368
|
-
}
|
|
369
|
-
if (notes) {
|
|
370
|
-
for (const cls of resolved.classes) {
|
|
371
|
-
if (cls.note !== void 0) lines.push(` note for ${cls.name.text} ${quote(cls.note)}`);
|
|
372
|
-
for (const field of cls.fields) {
|
|
373
|
-
if (field.note !== void 0) {
|
|
374
|
-
lines.push(` note for ${cls.name.text} ${quote(`${field.name.text}: ${field.note}`)}`);
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
for (const cls of resolved.classes) {
|
|
380
|
-
const from = cls.name.text;
|
|
381
|
-
if (cls.similarTo !== void 0) lines.push(` ${from} ..> ${cls.similarTo.text} : similar`);
|
|
382
|
-
for (const field of cls.fields) {
|
|
383
|
-
const type = field.type;
|
|
384
|
-
if (type?.kind === "class") {
|
|
385
|
-
lines.push(
|
|
386
|
-
type.many ? ` ${from} "1" --> "*" ${type.name.text} : ${field.name.text}` : ` ${from} --> ${type.name.text} : ${field.name.text}`
|
|
387
|
-
);
|
|
388
|
-
}
|
|
389
|
-
if (field.joinsTo !== void 0) {
|
|
390
|
-
const { className, fieldName } = field.joinsTo;
|
|
391
|
-
lines.push(` ${from} ..> ${className.text} : ${field.name.text} = ${fieldName.text}`);
|
|
392
|
-
}
|
|
571
|
+
if (distance < bestDistance || prefix > bestPrefix) {
|
|
572
|
+
best = candidate;
|
|
573
|
+
bestDistance = distance;
|
|
574
|
+
bestPrefix = prefix;
|
|
393
575
|
}
|
|
394
576
|
}
|
|
395
|
-
return
|
|
396
|
-
`;
|
|
577
|
+
return best;
|
|
397
578
|
}
|
|
398
|
-
function
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return `${marker}${type}${field.name.text}${system}`;
|
|
579
|
+
function commonPrefixLength(a, b) {
|
|
580
|
+
let i = 0;
|
|
581
|
+
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
582
|
+
return i;
|
|
403
583
|
}
|
|
404
|
-
function
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
584
|
+
function levenshtein(a, b) {
|
|
585
|
+
if (a === b) return 0;
|
|
586
|
+
if (a.length === 0) return b.length;
|
|
587
|
+
if (b.length === 0) return a.length;
|
|
588
|
+
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
589
|
+
for (let i = 1; i <= a.length; i++) {
|
|
590
|
+
const current = [i];
|
|
591
|
+
for (let j = 1; j <= b.length; j++) {
|
|
592
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
593
|
+
current[j] = Math.min(
|
|
594
|
+
(previous[j] ?? 0) + 1,
|
|
595
|
+
(current[j - 1] ?? 0) + 1,
|
|
596
|
+
(previous[j - 1] ?? 0) + cost
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
previous = current;
|
|
417
600
|
}
|
|
418
|
-
return
|
|
601
|
+
return previous[b.length] ?? 0;
|
|
419
602
|
}
|
|
420
|
-
var quote = (text) => `"${text.replaceAll('"', "'")}"`;
|
|
421
603
|
|
|
422
|
-
// src/
|
|
423
|
-
var
|
|
424
|
-
var
|
|
425
|
-
var CLASS_NAME = /^[A-Z][A-Za-z0-9]*$/;
|
|
426
|
-
var FIELD_NAME = /^[a-z][A-Za-z0-9]*$/;
|
|
427
|
-
var SYSTEM_OR_VALUE = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
428
|
-
var PRIMITIVES = {
|
|
604
|
+
// src/generators/linkml.ts
|
|
605
|
+
var BASE_URI = "https://example.org";
|
|
606
|
+
var RANGE = {
|
|
429
607
|
string: "string",
|
|
430
|
-
int: "
|
|
608
|
+
int: "integer",
|
|
431
609
|
float: "float",
|
|
432
|
-
bool: "
|
|
610
|
+
bool: "boolean",
|
|
433
611
|
date: "date",
|
|
434
612
|
datetime: "datetime",
|
|
435
|
-
uri: "uri"
|
|
436
|
-
text: "string",
|
|
437
|
-
integer: "int",
|
|
438
|
-
boolean: "bool"
|
|
613
|
+
uri: "uri"
|
|
439
614
|
};
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
const classes =
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
const
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
const
|
|
452
|
-
if (
|
|
453
|
-
if (
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
message: "An indented line is a field, but there is no valid class line above it",
|
|
460
|
-
line,
|
|
461
|
-
col: start,
|
|
462
|
-
end: text.length
|
|
463
|
-
});
|
|
464
|
-
continue;
|
|
465
|
-
}
|
|
466
|
-
const result = parseFieldLine(text, line);
|
|
467
|
-
if (result.ok) current.fields.push(result.value);
|
|
468
|
-
else diagnostics.push(result.diagnostic);
|
|
469
|
-
continue;
|
|
470
|
-
}
|
|
471
|
-
if (/\p{L}/u.test(first)) {
|
|
472
|
-
const result = parseClassLine(text, line);
|
|
473
|
-
if (result.ok) {
|
|
474
|
-
classes.push(result.value);
|
|
475
|
-
current = result.value;
|
|
476
|
-
} else {
|
|
477
|
-
diagnostics.push(result.diagnostic);
|
|
478
|
-
current = void 0;
|
|
479
|
-
}
|
|
480
|
-
continue;
|
|
481
|
-
}
|
|
482
|
-
diagnostics.push({
|
|
483
|
-
severity: "error",
|
|
484
|
-
code: "E_UNPARSABLE",
|
|
485
|
-
message: "A line at column 0 must start with a class name or `#` for a comment",
|
|
486
|
-
line,
|
|
487
|
-
col: 0,
|
|
488
|
-
end: text.length
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
return { classes, diagnostics };
|
|
492
|
-
}
|
|
493
|
-
function isDoubtMarker(text, i) {
|
|
494
|
-
return text[i] === "?" && isWs(text[i - 1]) && (i + 1 === text.length || isWs(text[i + 1]));
|
|
495
|
-
}
|
|
496
|
-
function splitTrailer(text) {
|
|
497
|
-
let start = -1;
|
|
498
|
-
for (let i = 1; i < text.length; i++) {
|
|
499
|
-
if (!isWs(text[i - 1])) continue;
|
|
500
|
-
if (text[i] === "#" || isDoubtMarker(text, i)) {
|
|
501
|
-
start = i;
|
|
502
|
-
break;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
if (start < 0) return { head: text };
|
|
506
|
-
const trailer = { head: text.slice(0, start).replace(/[ \t]+$/, "") };
|
|
507
|
-
if (text[start] === "?") {
|
|
508
|
-
const note = trimWs(text.slice(start + 1));
|
|
509
|
-
if (note !== "") trailer.note = note;
|
|
510
|
-
return trailer;
|
|
511
|
-
}
|
|
512
|
-
let doubt = -1;
|
|
513
|
-
for (let i = start + 1; i < text.length; i++) {
|
|
514
|
-
if (isDoubtMarker(text, i)) {
|
|
515
|
-
doubt = i;
|
|
516
|
-
break;
|
|
615
|
+
function toLinkML(doc, opts) {
|
|
616
|
+
const resolved = resolve(doc);
|
|
617
|
+
const schemaName = linkmlName(opts.schemaName);
|
|
618
|
+
const classes = emittedClasses(resolved.classes);
|
|
619
|
+
const classNames = new Set(classes.map((cls) => cls.node.name.text));
|
|
620
|
+
for (const name2 of resolved.undeclared) classNames.add(name2.text);
|
|
621
|
+
const enums = nameEnums(classes, classNames);
|
|
622
|
+
const prefixes = { linkml: "https://w3id.org/linkml/" };
|
|
623
|
+
if (!Object.hasOwn(prefixes, schemaName)) prefixes[schemaName] = `${BASE_URI}/${schemaName}/`;
|
|
624
|
+
const systemPrefix = /* @__PURE__ */ new Map();
|
|
625
|
+
const addSystem = (system) => {
|
|
626
|
+
const prefix = linkmlName(system);
|
|
627
|
+
if (!systemPrefix.has(system)) systemPrefix.set(system, prefix);
|
|
628
|
+
if (!Object.hasOwn(prefixes, prefix)) prefixes[prefix] = `${BASE_URI}/system/${prefix}/`;
|
|
629
|
+
};
|
|
630
|
+
for (const { node, fields } of classes) {
|
|
631
|
+
if (node.system !== void 0) addSystem(node.system.text);
|
|
632
|
+
for (const field of fields) {
|
|
633
|
+
if (field.system !== void 0) addSystem(field.system.text);
|
|
517
634
|
}
|
|
518
635
|
}
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
const note = trimWs(text.slice(doubt + 1));
|
|
523
|
-
if (note !== "") trailer.note = note;
|
|
524
|
-
}
|
|
525
|
-
return trailer;
|
|
526
|
-
}
|
|
527
|
-
var WORD = /[\p{L}\p{N}_-]+/uy;
|
|
528
|
-
var OPERATORS = ["*", ":", "@", "~", "=", ".", "|", "["];
|
|
529
|
-
function tokenize(head) {
|
|
530
|
-
const tokens = [];
|
|
531
|
-
let i = 0;
|
|
532
|
-
while (i < head.length) {
|
|
533
|
-
const c = head[i] ?? "";
|
|
534
|
-
if (isWs(c)) {
|
|
535
|
-
i++;
|
|
536
|
-
continue;
|
|
537
|
-
}
|
|
538
|
-
WORD.lastIndex = i;
|
|
539
|
-
const word = WORD.exec(head);
|
|
540
|
-
if (word !== null) {
|
|
541
|
-
tokens.push({ kind: "word", text: word[0], col: i, end: i + word[0].length });
|
|
542
|
-
i += word[0].length;
|
|
543
|
-
continue;
|
|
544
|
-
}
|
|
545
|
-
if (c === "[" && head[i + 1] === "]") {
|
|
546
|
-
tokens.push({ kind: "[]", text: "[]", col: i, end: i + 2 });
|
|
547
|
-
i += 2;
|
|
548
|
-
continue;
|
|
549
|
-
}
|
|
550
|
-
const ch = String.fromCodePoint(head.codePointAt(i) ?? 0);
|
|
551
|
-
const kind = OPERATORS.find((op) => op === ch) ?? "other";
|
|
552
|
-
tokens.push({ kind, text: ch, col: i, end: i + ch.length });
|
|
553
|
-
i += ch.length;
|
|
636
|
+
const declaredSystem = /* @__PURE__ */ new Map();
|
|
637
|
+
for (const { node } of classes) {
|
|
638
|
+
if (!declaredSystem.has(node.name.text)) declaredSystem.set(node.name.text, node.system?.text);
|
|
554
639
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
const t = c.tokens[c.pos];
|
|
560
|
-
if (t !== void 0) c.pos++;
|
|
561
|
-
return t;
|
|
562
|
-
}
|
|
563
|
-
var name = (t, line) => ({ text: t.text, line, col: t.col, end: t.end });
|
|
564
|
-
function fail(code, message, line, span) {
|
|
565
|
-
return {
|
|
566
|
-
ok: false,
|
|
567
|
-
diagnostic: { severity: "error", code, message, line, col: span.col, end: span.end }
|
|
640
|
+
const mappingPrefix = (target) => {
|
|
641
|
+
const system = declaredSystem.get(target);
|
|
642
|
+
if (system === void 0) return schemaName;
|
|
643
|
+
return systemPrefix.get(system) ?? schemaName;
|
|
568
644
|
};
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
}
|
|
581
|
-
if (!CLASS_NAME.test(head.text)) {
|
|
582
|
-
return fail(
|
|
583
|
-
"E_BAD_NAME",
|
|
584
|
-
`Class names are UpperCamelCase (letters and digits, uppercase first): \`${head.text}\``,
|
|
585
|
-
line,
|
|
586
|
-
head
|
|
587
|
-
);
|
|
645
|
+
const schema = {
|
|
646
|
+
id: `${BASE_URI}/${schemaName}`,
|
|
647
|
+
name: schemaName,
|
|
648
|
+
default_prefix: schemaName,
|
|
649
|
+
default_range: "string",
|
|
650
|
+
prefixes,
|
|
651
|
+
imports: ["linkml:types"]
|
|
652
|
+
};
|
|
653
|
+
const out = {};
|
|
654
|
+
for (const cls of classes) {
|
|
655
|
+
out[cls.node.name.text] = buildClass(cls, mappingPrefix, enums);
|
|
588
656
|
}
|
|
589
|
-
const
|
|
590
|
-
|
|
591
|
-
if (at?.kind === "@") {
|
|
592
|
-
take(c);
|
|
593
|
-
const system = parseSystem(c, at);
|
|
594
|
-
if (!system.ok) return system;
|
|
595
|
-
node.system = system.value;
|
|
657
|
+
for (const name2 of resolved.undeclared) {
|
|
658
|
+
if (!(name2.text in out)) out[name2.text] = { annotations: { undeclared: true } };
|
|
596
659
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
target
|
|
615
|
-
);
|
|
660
|
+
if (Object.keys(out).length > 0) schema.classes = out;
|
|
661
|
+
const enumBlock = buildEnums(classes, enums);
|
|
662
|
+
if (Object.keys(enumBlock).length > 0) schema.enums = enumBlock;
|
|
663
|
+
return schema;
|
|
664
|
+
}
|
|
665
|
+
function emittedClasses(classes) {
|
|
666
|
+
const out = [];
|
|
667
|
+
const seen = /* @__PURE__ */ new Set();
|
|
668
|
+
for (const node of classes) {
|
|
669
|
+
if (seen.has(node.name.text)) continue;
|
|
670
|
+
seen.add(node.name.text);
|
|
671
|
+
const fields = [];
|
|
672
|
+
const seenField = /* @__PURE__ */ new Set();
|
|
673
|
+
for (const field of node.fields) {
|
|
674
|
+
if (seenField.has(field.name.text)) continue;
|
|
675
|
+
seenField.add(field.name.text);
|
|
676
|
+
fields.push(field);
|
|
616
677
|
}
|
|
617
|
-
|
|
678
|
+
out.push({ node, fields });
|
|
618
679
|
}
|
|
619
|
-
|
|
620
|
-
if (rest !== void 0) return unexpected(rest, line, "class", CLASS_ORDER);
|
|
621
|
-
if (trailer.description !== void 0) node.description = trailer.description;
|
|
622
|
-
if (trailer.note !== void 0) node.note = trailer.note;
|
|
623
|
-
return { ok: true, value: { ...node, fields: [] } };
|
|
680
|
+
return out;
|
|
624
681
|
}
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
const
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
682
|
+
var useKey = (className, fieldName) => `${className}\0${fieldName}`;
|
|
683
|
+
function nameEnums(classes, classNames) {
|
|
684
|
+
const uses = [];
|
|
685
|
+
for (const { node, fields } of classes) {
|
|
686
|
+
for (const field of fields) {
|
|
687
|
+
if (field.type?.kind !== "enum") continue;
|
|
688
|
+
uses.push({
|
|
689
|
+
className: node.name.text,
|
|
690
|
+
fieldName: field.name.text,
|
|
691
|
+
values: field.type.values.map((v) => v.text).join("|")
|
|
692
|
+
});
|
|
693
|
+
}
|
|
633
694
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
line,
|
|
640
|
-
head
|
|
641
|
-
);
|
|
695
|
+
const valueSets = /* @__PURE__ */ new Map();
|
|
696
|
+
for (const use of uses) {
|
|
697
|
+
const seen = valueSets.get(use.fieldName) ?? /* @__PURE__ */ new Set();
|
|
698
|
+
seen.add(use.values);
|
|
699
|
+
valueSets.set(use.fieldName, seen);
|
|
642
700
|
}
|
|
643
|
-
const
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
701
|
+
const taken = /* @__PURE__ */ new Map();
|
|
702
|
+
const names = /* @__PURE__ */ new Map();
|
|
703
|
+
for (const use of uses) {
|
|
704
|
+
const shared = (valueSets.get(use.fieldName)?.size ?? 0) <= 1;
|
|
705
|
+
const base = shared ? `${capitalise(use.fieldName)}Enum` : `${capitalise(use.className)}${capitalise(use.fieldName)}Enum`;
|
|
706
|
+
let name2 = base;
|
|
707
|
+
for (let n = 2; classNames.has(name2) || taken.has(name2) && taken.get(name2) !== use.values; n++) {
|
|
708
|
+
name2 = `${base}${n}`;
|
|
709
|
+
}
|
|
710
|
+
taken.set(name2, use.values);
|
|
711
|
+
names.set(useKey(use.className, use.fieldName), name2);
|
|
647
712
|
}
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
713
|
+
return names;
|
|
714
|
+
}
|
|
715
|
+
function buildEnums(classes, enums) {
|
|
716
|
+
const out = {};
|
|
717
|
+
for (const { node, fields } of classes) {
|
|
718
|
+
for (const field of fields) {
|
|
719
|
+
if (field.type?.kind !== "enum") continue;
|
|
720
|
+
const name2 = enums.get(useKey(node.name.text, field.name.text));
|
|
721
|
+
if (name2 === void 0 || name2 in out) continue;
|
|
722
|
+
const values = {};
|
|
723
|
+
for (const value of field.type.values) values[value.text] = null;
|
|
724
|
+
out[name2] = { permissible_values: values };
|
|
725
|
+
}
|
|
654
726
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
727
|
+
return out;
|
|
728
|
+
}
|
|
729
|
+
function buildClass(cls, mappingPrefix, enums) {
|
|
730
|
+
const { node, fields } = cls;
|
|
731
|
+
const out = {};
|
|
732
|
+
if (node.description !== void 0) out.description = node.description;
|
|
733
|
+
const annotations = {};
|
|
734
|
+
if (node.system !== void 0) annotations.system = node.system.text;
|
|
735
|
+
if (node.note !== void 0) annotations.note = node.note;
|
|
736
|
+
if (Object.keys(annotations).length > 0) out.annotations = annotations;
|
|
737
|
+
if (node.similarTo !== void 0) {
|
|
738
|
+
const target = node.similarTo.text;
|
|
739
|
+
out.close_mappings = [`${mappingPrefix(target)}:${target}`];
|
|
661
740
|
}
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
const example = "e.g. `characterId = Character.id`";
|
|
666
|
-
const className = take(c);
|
|
667
|
-
if (className === void 0 || className.kind !== "word") {
|
|
668
|
-
return fail(
|
|
669
|
-
"E_UNPARSABLE",
|
|
670
|
-
`Expected \`Class.field\` after \`=\`, ${example}`,
|
|
671
|
-
line,
|
|
672
|
-
className ?? afterSpan(equals)
|
|
673
|
-
);
|
|
674
|
-
}
|
|
675
|
-
if (!CLASS_NAME.test(className.text)) {
|
|
676
|
-
return fail(
|
|
677
|
-
"E_BAD_NAME",
|
|
678
|
-
`The class after \`=\` must be UpperCamelCase: \`${className.text}\``,
|
|
679
|
-
line,
|
|
680
|
-
className
|
|
681
|
-
);
|
|
682
|
-
}
|
|
683
|
-
const dot = take(c);
|
|
684
|
-
if (dot === void 0 || dot.kind !== ".") {
|
|
685
|
-
return fail(
|
|
686
|
-
"E_UNPARSABLE",
|
|
687
|
-
`Expected \`.\` and a field name after \`= ${className.text}\`, ${example}`,
|
|
688
|
-
line,
|
|
689
|
-
dot ?? afterSpan(className)
|
|
690
|
-
);
|
|
691
|
-
}
|
|
692
|
-
const fieldName = take(c);
|
|
693
|
-
if (fieldName === void 0 || fieldName.kind !== "word") {
|
|
694
|
-
return fail(
|
|
695
|
-
"E_UNPARSABLE",
|
|
696
|
-
`Expected a field name after \`= ${className.text}.\`, ${example}`,
|
|
697
|
-
line,
|
|
698
|
-
fieldName ?? afterSpan(dot)
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
if (!FIELD_NAME.test(fieldName.text)) {
|
|
702
|
-
return fail(
|
|
703
|
-
"E_BAD_NAME",
|
|
704
|
-
`The field after \`= ${className.text}.\` must be lowerCamelCase: \`${fieldName.text}\``,
|
|
705
|
-
line,
|
|
706
|
-
fieldName
|
|
707
|
-
);
|
|
708
|
-
}
|
|
709
|
-
node.joinsTo = { className: name(className, line), fieldName: name(fieldName, line) };
|
|
741
|
+
const attributes = {};
|
|
742
|
+
for (const field of fields) {
|
|
743
|
+
attributes[field.name.text] = buildAttribute(field, node.name.text, enums);
|
|
710
744
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
if (trailer.description !== void 0) node.description = trailer.description;
|
|
714
|
-
if (trailer.note !== void 0) node.note = trailer.note;
|
|
715
|
-
return { ok: true, value: node };
|
|
745
|
+
if (Object.keys(attributes).length > 0) out.attributes = attributes;
|
|
746
|
+
return out;
|
|
716
747
|
}
|
|
717
|
-
function
|
|
718
|
-
const
|
|
719
|
-
if (
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
748
|
+
function buildAttribute(field, className, enums) {
|
|
749
|
+
const out = {};
|
|
750
|
+
if (field.description !== void 0) out.description = field.description;
|
|
751
|
+
const range = rangeOf(field, className, enums);
|
|
752
|
+
if (range !== void 0) out.range = range;
|
|
753
|
+
if (field.type?.many === true) out.multivalued = true;
|
|
754
|
+
if (field.identifier) out.identifier = true;
|
|
755
|
+
const annotations = {};
|
|
756
|
+
if (field.system !== void 0) annotations.system = field.system.text;
|
|
757
|
+
if (field.joinsTo !== void 0) {
|
|
758
|
+
annotations.joins_to = `${field.joinsTo.className.text}.${field.joinsTo.fieldName.text}`;
|
|
726
759
|
}
|
|
727
|
-
if (
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
760
|
+
if (field.note !== void 0) annotations.note = field.note;
|
|
761
|
+
if (Object.keys(annotations).length > 0) out.annotations = annotations;
|
|
762
|
+
return out;
|
|
763
|
+
}
|
|
764
|
+
function rangeOf(field, className, enums) {
|
|
765
|
+
const type = field.type;
|
|
766
|
+
if (type === void 0) return void 0;
|
|
767
|
+
switch (type.kind) {
|
|
768
|
+
case "primitive":
|
|
769
|
+
return RANGE[type.name];
|
|
770
|
+
case "class":
|
|
771
|
+
return type.name.text;
|
|
772
|
+
case "unknown":
|
|
773
|
+
return "string";
|
|
774
|
+
case "enum":
|
|
775
|
+
return enums.get(useKey(className, field.name.text));
|
|
734
776
|
}
|
|
735
|
-
return { ok: true, value: name(system, c.line) };
|
|
736
777
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
778
|
+
var RESERVED_PREFIXES = /* @__PURE__ */ new Set(["linkml", "xsd", "shex", "schema"]);
|
|
779
|
+
var linkmlName = (text) => {
|
|
780
|
+
const name2 = text.toLowerCase().replace(/[^a-z0-9]/g, "_");
|
|
781
|
+
if (!/[a-z0-9]/.test(name2)) return "sketch";
|
|
782
|
+
if (RESERVED_PREFIXES.has(name2)) return `${name2}_`;
|
|
783
|
+
return /^[0-9]/.test(name2) ? `_${name2}` : name2;
|
|
784
|
+
};
|
|
785
|
+
var capitalise = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
786
|
+
|
|
787
|
+
// src/generators/mermaid.ts
|
|
788
|
+
function toMermaid(doc, opts) {
|
|
789
|
+
const resolved = resolve(doc);
|
|
790
|
+
const notes = opts?.notes === true;
|
|
791
|
+
const lines = ["classDiagram"];
|
|
792
|
+
for (const cls of resolved.classes) {
|
|
793
|
+
lines.push(` class ${cls.name.text} {`);
|
|
794
|
+
if (cls.system !== void 0) lines.push(` <<${cls.system.text}>>`);
|
|
795
|
+
for (const field of cls.fields) lines.push(` ${member(field)}`);
|
|
796
|
+
lines.push(" }");
|
|
746
797
|
}
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
"E_UNPARSABLE",
|
|
750
|
-
`Expected a type after \`:\`, found \`${first.text}\``,
|
|
751
|
-
c.line,
|
|
752
|
-
first
|
|
753
|
-
);
|
|
798
|
+
for (const name2 of resolved.undeclared) {
|
|
799
|
+
lines.push(` class ${name2.text} {`, " <<undeclared>>", " }");
|
|
754
800
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
"Expected an enum value after `|`, e.g. `climate: arid|temperate|frozen`",
|
|
764
|
-
c.line,
|
|
765
|
-
value ?? afterSpan(pipe)
|
|
766
|
-
);
|
|
801
|
+
if (notes) {
|
|
802
|
+
for (const cls of resolved.classes) {
|
|
803
|
+
if (cls.note !== void 0) lines.push(` note for ${cls.name.text} ${quote(cls.note)}`);
|
|
804
|
+
for (const field of cls.fields) {
|
|
805
|
+
if (field.note !== void 0) {
|
|
806
|
+
lines.push(` note for ${cls.name.text} ${quote(`${field.name.text}: ${field.note}`)}`);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
767
809
|
}
|
|
768
|
-
words.push(value);
|
|
769
|
-
}
|
|
770
|
-
let many = false;
|
|
771
|
-
const bracket = peek(c);
|
|
772
|
-
if (bracket?.kind === "[]") {
|
|
773
|
-
take(c);
|
|
774
|
-
many = true;
|
|
775
|
-
} else if (bracket?.kind === "[") {
|
|
776
|
-
return fail(
|
|
777
|
-
"E_UNCLOSED_MANY",
|
|
778
|
-
"`[` must be closed: `[]` marks a many-valued field",
|
|
779
|
-
c.line,
|
|
780
|
-
bracket
|
|
781
|
-
);
|
|
782
810
|
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
811
|
+
for (const cls of resolved.classes) {
|
|
812
|
+
const from = cls.name.text;
|
|
813
|
+
if (cls.similarTo !== void 0) lines.push(` ${from} ..> ${cls.similarTo.text} : similar`);
|
|
814
|
+
for (const field of cls.fields) {
|
|
815
|
+
const type = field.type;
|
|
816
|
+
if (type?.kind === "class") {
|
|
817
|
+
lines.push(
|
|
818
|
+
type.many ? ` ${from} "1" --> "*" ${type.name.text} : ${field.name.text}` : ` ${from} --> ${type.name.text} : ${field.name.text}`
|
|
791
819
|
);
|
|
792
820
|
}
|
|
821
|
+
if (field.joinsTo !== void 0) {
|
|
822
|
+
const { className, fieldName } = field.joinsTo;
|
|
823
|
+
lines.push(` ${from} ..> ${className.text} : ${field.name.text} = ${fieldName.text}`);
|
|
824
|
+
}
|
|
793
825
|
}
|
|
794
|
-
return { ok: true, value: { kind: "enum", values: words.map((w) => name(w, c.line)), many } };
|
|
795
|
-
}
|
|
796
|
-
const word = first.text;
|
|
797
|
-
const primitive = PRIMITIVES[word];
|
|
798
|
-
if (primitive !== void 0) {
|
|
799
|
-
return {
|
|
800
|
-
ok: true,
|
|
801
|
-
value: { kind: "primitive", name: primitive, written: name(first, c.line), many }
|
|
802
|
-
};
|
|
803
|
-
}
|
|
804
|
-
if (/^[A-Z]/.test(word)) {
|
|
805
|
-
if (!CLASS_NAME.test(word)) {
|
|
806
|
-
return fail(
|
|
807
|
-
"E_BAD_NAME",
|
|
808
|
-
`A referenced class must be UpperCamelCase: \`${word}\``,
|
|
809
|
-
c.line,
|
|
810
|
-
first
|
|
811
|
-
);
|
|
812
|
-
}
|
|
813
|
-
return { ok: true, value: { kind: "class", name: name(first, c.line), many } };
|
|
814
|
-
}
|
|
815
|
-
if (/^[a-z]/.test(word)) {
|
|
816
|
-
return { ok: true, value: { kind: "unknown", name: name(first, c.line), many } };
|
|
817
826
|
}
|
|
818
|
-
return
|
|
819
|
-
|
|
820
|
-
`A type is a primitive, a class name or an enum, not \`${word}\``,
|
|
821
|
-
c.line,
|
|
822
|
-
first
|
|
823
|
-
);
|
|
827
|
+
return `${lines.join("\n")}
|
|
828
|
+
`;
|
|
824
829
|
}
|
|
825
|
-
function
|
|
826
|
-
const
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
return fail(
|
|
845
|
-
"E_UNPARSABLE",
|
|
846
|
-
"`?` attached to a word is not a marker; a doubt needs whitespace before `?` and after it",
|
|
847
|
-
line,
|
|
848
|
-
token
|
|
849
|
-
);
|
|
850
|
-
}
|
|
851
|
-
if (token.text === "#") {
|
|
852
|
-
return fail("E_UNPARSABLE", "A description needs whitespace before `#`", line, token);
|
|
830
|
+
function member(field) {
|
|
831
|
+
const marker = field.identifier ? "*" : "+";
|
|
832
|
+
const type = field.type === void 0 ? "" : `${typeText(field.type)} `;
|
|
833
|
+
const system = field.system === void 0 ? "" : ` @${field.system.text}`;
|
|
834
|
+
return `${marker}${type}${field.name.text}${system}`;
|
|
835
|
+
}
|
|
836
|
+
function typeText(type) {
|
|
837
|
+
let text;
|
|
838
|
+
switch (type.kind) {
|
|
839
|
+
case "primitive":
|
|
840
|
+
text = type.written.text;
|
|
841
|
+
break;
|
|
842
|
+
case "class":
|
|
843
|
+
case "unknown":
|
|
844
|
+
text = type.name.text;
|
|
845
|
+
break;
|
|
846
|
+
case "enum":
|
|
847
|
+
text = type.values.map((v) => v.text).join("|");
|
|
848
|
+
break;
|
|
853
849
|
}
|
|
854
|
-
return
|
|
850
|
+
return type.many ? `${text}[]` : text;
|
|
855
851
|
}
|
|
852
|
+
var quote = (text) => `"${text.replaceAll('"', "'")}"`;
|
|
856
853
|
|
|
857
854
|
// src/serialize.ts
|
|
858
855
|
import { isMap, isScalar, Scalar, visit, Document as YAMLDocument } from "yaml";
|
|
@@ -911,16 +908,521 @@ function formatDiagnostic(d, file) {
|
|
|
911
908
|
return `${where}: ${d.severity} ${d.code} ${d.message}`;
|
|
912
909
|
}
|
|
913
910
|
|
|
911
|
+
// src/generators/skiss.ts
|
|
912
|
+
var TRAILER_COLUMN = 40;
|
|
913
|
+
function toSkiss(doc) {
|
|
914
|
+
if (doc.classes.length === 0) return "";
|
|
915
|
+
return `${doc.classes.map(classBlock).join("\n\n")}
|
|
916
|
+
`;
|
|
917
|
+
}
|
|
918
|
+
function classBlock(cls) {
|
|
919
|
+
const head = cls.name.text + (cls.system === void 0 ? "" : ` @${cls.system.text}`) + (cls.similarTo === void 0 ? "" : ` ~ ${cls.similarTo.text}`);
|
|
920
|
+
return [withTrailer(head, cls), ...cls.fields.map(fieldLine)].join("\n");
|
|
921
|
+
}
|
|
922
|
+
function fieldLine(field) {
|
|
923
|
+
const type = typeText2(field.type);
|
|
924
|
+
const head = ` ${field.name.text}` + (field.identifier ? "*" : "") + (type === void 0 ? "" : `: ${type}`) + (field.system === void 0 ? "" : ` @${field.system.text}`) + (field.joinsTo === void 0 ? "" : ` = ${field.joinsTo.className.text}.${field.joinsTo.fieldName.text}`);
|
|
925
|
+
return withTrailer(head, field);
|
|
926
|
+
}
|
|
927
|
+
function typeText2(type) {
|
|
928
|
+
if (type === void 0) return void 0;
|
|
929
|
+
let text;
|
|
930
|
+
switch (type.kind) {
|
|
931
|
+
case "primitive":
|
|
932
|
+
if (type.name === "string" && !type.many) return void 0;
|
|
933
|
+
text = type.name;
|
|
934
|
+
break;
|
|
935
|
+
case "class":
|
|
936
|
+
case "unknown":
|
|
937
|
+
text = type.name.text;
|
|
938
|
+
break;
|
|
939
|
+
case "enum":
|
|
940
|
+
text = type.values.map((v) => v.text).join("|");
|
|
941
|
+
break;
|
|
942
|
+
}
|
|
943
|
+
return type.many ? `${text}[]` : text;
|
|
944
|
+
}
|
|
945
|
+
function withTrailer(head, node) {
|
|
946
|
+
const parts2 = [];
|
|
947
|
+
if (node.description !== void 0 && node.description !== "")
|
|
948
|
+
parts2.push(`# ${node.description}`);
|
|
949
|
+
if (node.note !== void 0 && node.note !== "") parts2.push(`? ${node.note}`);
|
|
950
|
+
if (parts2.length === 0) return head;
|
|
951
|
+
const gap = head.length < TRAILER_COLUMN - 1 ? " ".repeat(TRAILER_COLUMN - head.length) : " ";
|
|
952
|
+
return head + gap + parts2.join(" ");
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
// src/generators/from-linkml.ts
|
|
956
|
+
var PRIMITIVE_RANGE = {
|
|
957
|
+
string: "string",
|
|
958
|
+
integer: "int",
|
|
959
|
+
float: "float",
|
|
960
|
+
double: "float",
|
|
961
|
+
decimal: "float",
|
|
962
|
+
boolean: "bool",
|
|
963
|
+
date: "date",
|
|
964
|
+
datetime: "datetime",
|
|
965
|
+
uri: "uri",
|
|
966
|
+
uriorcurie: "uri"
|
|
967
|
+
};
|
|
968
|
+
var CLASS_KEYS = /* @__PURE__ */ new Set(["description", "annotations", "close_mappings", "attributes", "slots"]);
|
|
969
|
+
var SLOT_KEYS = /* @__PURE__ */ new Set(["description", "annotations", "range", "multivalued", "identifier"]);
|
|
970
|
+
var CLASS_TAGS = /* @__PURE__ */ new Set(["system", "note", "undeclared"]);
|
|
971
|
+
var SLOT_TAGS = /* @__PURE__ */ new Set(["system", "note", "joins_to"]);
|
|
972
|
+
var QUIET_SCHEMA_KEYS = /* @__PURE__ */ new Set([
|
|
973
|
+
"id",
|
|
974
|
+
"name",
|
|
975
|
+
"prefixes",
|
|
976
|
+
"imports",
|
|
977
|
+
"default_prefix",
|
|
978
|
+
"default_range",
|
|
979
|
+
"title",
|
|
980
|
+
"license",
|
|
981
|
+
"version",
|
|
982
|
+
"classes",
|
|
983
|
+
"slots",
|
|
984
|
+
"enums"
|
|
985
|
+
]);
|
|
986
|
+
var UNKNOWN_TYPE = /^[a-z][a-z0-9_-]*$/;
|
|
987
|
+
function fromLinkML(schema) {
|
|
988
|
+
const root = asRecord(schema);
|
|
989
|
+
if (root === void 0) return nothing("schema", "the schema is not an object");
|
|
990
|
+
const schemaName = schemaElement(root);
|
|
991
|
+
const classBlock2 = asRecord(get(root, "classes"));
|
|
992
|
+
if (classBlock2 === void 0) return nothing(schemaName, "the schema has no `classes`");
|
|
993
|
+
const classDefs = classBlock2;
|
|
994
|
+
const dropped = [];
|
|
995
|
+
const report = (kind, element, detail) => {
|
|
996
|
+
dropped.push(detail === void 0 ? { kind, element } : { kind, element, detail });
|
|
997
|
+
};
|
|
998
|
+
const slotDefs = asRecord(get(root, "slots")) ?? {};
|
|
999
|
+
const enumDefs = asRecord(get(root, "enums")) ?? {};
|
|
1000
|
+
const defaultRange = stringOf(get(root, "default_range")) ?? "string";
|
|
1001
|
+
const classNames = /* @__PURE__ */ new Map();
|
|
1002
|
+
const takenClasses = /* @__PURE__ */ new Set();
|
|
1003
|
+
for (const key2 of Object.keys(classDefs)) {
|
|
1004
|
+
classNames.set(key2, unique(toClassName(key2), takenClasses));
|
|
1005
|
+
}
|
|
1006
|
+
const fieldNames = /* @__PURE__ */ new Map();
|
|
1007
|
+
for (const key2 of Object.keys(classDefs)) {
|
|
1008
|
+
const names = /* @__PURE__ */ new Map();
|
|
1009
|
+
const taken = /* @__PURE__ */ new Set();
|
|
1010
|
+
for (const slot of slotKeysOf(asRecord(get(classDefs, key2)) ?? {})) {
|
|
1011
|
+
names.set(slot, unique(toFieldName(slot), taken));
|
|
1012
|
+
}
|
|
1013
|
+
fieldNames.set(key2, names);
|
|
1014
|
+
}
|
|
1015
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
1016
|
+
const reference = (element, from, to) => {
|
|
1017
|
+
if (from === to || referenced.has(`${element}\0${to}`)) return;
|
|
1018
|
+
referenced.add(`${element}\0${to}`);
|
|
1019
|
+
report("renamed", element, `\`${to}\` from \`${from}\``);
|
|
1020
|
+
};
|
|
1021
|
+
const classNameOf = (key2) => classNames.get(key2) ?? toClassName(key2);
|
|
1022
|
+
const fieldNameOf = (classKey, slot) => fieldNames.get(classKey)?.get(slot) ?? toFieldName(slot);
|
|
1023
|
+
const enumValues = /* @__PURE__ */ new Map();
|
|
1024
|
+
const unwritable = /* @__PURE__ */ new Map();
|
|
1025
|
+
for (const key2 of Object.keys(enumDefs)) {
|
|
1026
|
+
const rawValues = get(asRecord(get(enumDefs, key2)) ?? {}, "permissible_values");
|
|
1027
|
+
const values = asRecord(rawValues);
|
|
1028
|
+
const keys = Object.keys(values ?? {});
|
|
1029
|
+
const bad = keys.find((value) => !SYSTEM_OR_VALUE.test(value));
|
|
1030
|
+
if (values === void 0 && present(rawValues)) {
|
|
1031
|
+
unwritable.set(key2, `${shown(rawValues)} is not a map of permissible values`);
|
|
1032
|
+
} else if (keys.length < 2) {
|
|
1033
|
+
const count = `${keys.length} permissible value${keys.length === 1 ? "" : "s"}`;
|
|
1034
|
+
unwritable.set(key2, `${count} cannot be an inline enum (\xA73.4)`);
|
|
1035
|
+
} else if (bad !== void 0) {
|
|
1036
|
+
unwritable.set(key2, `\`${bad}\` is not a value Skiss can write (\xA74)`);
|
|
1037
|
+
} else {
|
|
1038
|
+
enumValues.set(key2, keys);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
const enumUses = [];
|
|
1042
|
+
const classes = [];
|
|
1043
|
+
for (const key2 of Object.keys(classDefs)) {
|
|
1044
|
+
const raw = get(classDefs, key2);
|
|
1045
|
+
const body = asRecord(raw);
|
|
1046
|
+
const name2 = classNameOf(key2);
|
|
1047
|
+
if (body === void 0 && raw !== null && raw !== void 0) {
|
|
1048
|
+
report("class", name2, "is not a class definition");
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
const def = body ?? {};
|
|
1052
|
+
if (name2 !== key2) report("renamed", name2, `from \`${key2}\``);
|
|
1053
|
+
const annotations = annotationsOf(def);
|
|
1054
|
+
if (annotations.get("undeclared") === true) {
|
|
1055
|
+
for (const other of Object.keys(def)) {
|
|
1056
|
+
if (other !== "annotations") report(other, name2);
|
|
1057
|
+
}
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
const node = { name: at0(name2), fields: [], line: 0 };
|
|
1061
|
+
const description = oneLine(stringOf(get(def, "description")));
|
|
1062
|
+
if (description !== void 0) node.description = description;
|
|
1063
|
+
const system = annotations.get("system");
|
|
1064
|
+
if (typeof system === "string" && SYSTEM_OR_VALUE.test(system)) node.system = at0(system);
|
|
1065
|
+
else if (system !== void 0) report("annotation", name2, "`system` is not a system name");
|
|
1066
|
+
const note = oneLine(stringOf(annotations.get("note")));
|
|
1067
|
+
if (note !== void 0) node.note = note;
|
|
1068
|
+
const mappings = stringList(get(def, "close_mappings"));
|
|
1069
|
+
const first = mappings[0];
|
|
1070
|
+
const target = first === void 0 ? "" : localName(first);
|
|
1071
|
+
if (target !== "") {
|
|
1072
|
+
const similar = Object.hasOwn(classDefs, target) ? classNameOf(target) : toClassName(target);
|
|
1073
|
+
node.similarTo = at0(similar);
|
|
1074
|
+
reference(name2, target, similar);
|
|
1075
|
+
}
|
|
1076
|
+
for (const other of Object.keys(def)) {
|
|
1077
|
+
if (CLASS_KEYS.has(other)) {
|
|
1078
|
+
if (other === "annotations") {
|
|
1079
|
+
for (const tag of annotations.keys()) {
|
|
1080
|
+
if (!CLASS_TAGS.has(tag)) report("annotation", name2, `\`${tag}\``);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
if (other === "close_mappings") {
|
|
1084
|
+
if (target === "") report("close_mappings", name2, "no mapping this can read");
|
|
1085
|
+
else if (mappings.length > 1) {
|
|
1086
|
+
report("close_mappings", name2, `${mappings.length - 1} beyond the first`);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const block = get(def, other);
|
|
1090
|
+
if (other === "attributes" && present(block) && asRecord(block) === void 0) {
|
|
1091
|
+
report("attributes", name2, `${shown(block)} is not a map`);
|
|
1092
|
+
}
|
|
1093
|
+
if (other === "slots" && present(block) && !Array.isArray(block)) {
|
|
1094
|
+
report("slots", name2, `${shown(block)} is not a list`);
|
|
1095
|
+
}
|
|
1096
|
+
continue;
|
|
1097
|
+
}
|
|
1098
|
+
if (other === "slot_usage") {
|
|
1099
|
+
const usage = asRecord(get(def, other));
|
|
1100
|
+
if (usage === void 0) {
|
|
1101
|
+
report("slot_usage", name2);
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
for (const slot of Object.keys(usage)) {
|
|
1105
|
+
if (fieldNames.get(key2)?.has(slot) === true) {
|
|
1106
|
+
report("slot_usage", `${name2}.${fieldNameOf(key2, slot)}`);
|
|
1107
|
+
} else {
|
|
1108
|
+
report("slot_usage", name2, `\`${slot}\` is inherited and not in the sketch`);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
continue;
|
|
1112
|
+
}
|
|
1113
|
+
report(other, name2);
|
|
1114
|
+
}
|
|
1115
|
+
for (const slot of slotKeysOf(def)) {
|
|
1116
|
+
const attributes = asRecord(get(def, "attributes")) ?? {};
|
|
1117
|
+
const rawSlot = Object.hasOwn(attributes, slot) ? get(attributes, slot) : get(slotDefs, slot);
|
|
1118
|
+
const slotDef = asRecord(rawSlot);
|
|
1119
|
+
if (slotDef === void 0 && present(rawSlot)) {
|
|
1120
|
+
report("slot", `${name2}.${fieldNameOf(key2, slot)}`, "is not a slot definition");
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
node.fields.push(buildField(key2, name2, slot, slotDef ?? {}));
|
|
1124
|
+
}
|
|
1125
|
+
classes.push(node);
|
|
1126
|
+
}
|
|
1127
|
+
for (const key2 of Object.keys(enumDefs)) {
|
|
1128
|
+
const def = asRecord(get(enumDefs, key2)) ?? {};
|
|
1129
|
+
const uses = enumUses.filter((use) => use.enumName === key2);
|
|
1130
|
+
const why = unwritable.get(key2);
|
|
1131
|
+
if (why !== void 0) report("enum", key2, `${why}; those attributes keep no type`);
|
|
1132
|
+
else if (uses.length === 0) report("unused_enum", key2, "no attribute has it as its range");
|
|
1133
|
+
else if (uses.length > 1 && sharingLost(uses, enumUses)) {
|
|
1134
|
+
report("inlined", key2, `inlined at ${uses.length} attributes`);
|
|
1135
|
+
}
|
|
1136
|
+
const detail = [];
|
|
1137
|
+
const values = asRecord(get(def, "permissible_values")) ?? {};
|
|
1138
|
+
const detailed = Object.keys(values).filter((value) => {
|
|
1139
|
+
const valueBody = asRecord(get(values, value));
|
|
1140
|
+
return valueBody !== void 0 && Object.keys(valueBody).length > 0;
|
|
1141
|
+
});
|
|
1142
|
+
if (detailed.length > 0) {
|
|
1143
|
+
const bodies = detailed.length === 1 ? "carries a body of its own" : "carry bodies of their own";
|
|
1144
|
+
detail.push(`${list(detailed)} ${bodies}`);
|
|
1145
|
+
}
|
|
1146
|
+
const others = Object.keys(def).filter((other) => other !== "permissible_values");
|
|
1147
|
+
if (others.length > 0) detail.push(`the enum carries ${list(others)}`);
|
|
1148
|
+
if (detail.length > 0) report("enum_detail", key2, detail.join("; "));
|
|
1149
|
+
}
|
|
1150
|
+
for (const slot of Object.keys(slotDefs)) {
|
|
1151
|
+
if (!listedSomewhere(slot)) report("slot", slot, "no class lists it");
|
|
1152
|
+
}
|
|
1153
|
+
for (const key2 of Object.keys(root)) {
|
|
1154
|
+
if (!QUIET_SCHEMA_KEYS.has(key2)) report("schema", schemaName, `\`${key2}\``);
|
|
1155
|
+
}
|
|
1156
|
+
const source = toSkiss({ classes, diagnostics: [] });
|
|
1157
|
+
return { document: parse(source), source, dropped };
|
|
1158
|
+
function buildField(classKey, className, slot, def) {
|
|
1159
|
+
const name2 = fieldNameOf(classKey, slot);
|
|
1160
|
+
const element = `${className}.${name2}`;
|
|
1161
|
+
if (name2 !== slot) report("renamed", element, `from \`${slot}\``);
|
|
1162
|
+
const identifier = get(def, "identifier");
|
|
1163
|
+
if (present(identifier) && typeof identifier !== "boolean") {
|
|
1164
|
+
report("identifier", element, `${shown(identifier)} is not a boolean`);
|
|
1165
|
+
}
|
|
1166
|
+
const field = {
|
|
1167
|
+
name: at0(name2),
|
|
1168
|
+
identifier: identifier === true,
|
|
1169
|
+
line: 0
|
|
1170
|
+
};
|
|
1171
|
+
const description = oneLine(stringOf(get(def, "description")));
|
|
1172
|
+
if (description !== void 0) field.description = description;
|
|
1173
|
+
const type = typeFor(element, name2, def);
|
|
1174
|
+
if (type !== void 0) field.type = type;
|
|
1175
|
+
const annotations = annotationsOf(def);
|
|
1176
|
+
const system = annotations.get("system");
|
|
1177
|
+
if (typeof system === "string" && SYSTEM_OR_VALUE.test(system)) field.system = at0(system);
|
|
1178
|
+
else if (system !== void 0) report("annotation", element, "`system` is not a system name");
|
|
1179
|
+
const joins = annotations.get("joins_to");
|
|
1180
|
+
if (typeof joins === "string") {
|
|
1181
|
+
const dot = joins.indexOf(".");
|
|
1182
|
+
const target = joins.slice(0, dot);
|
|
1183
|
+
const targetField = joins.slice(dot + 1);
|
|
1184
|
+
if (dot > 0 && targetField !== "") {
|
|
1185
|
+
const targetClass = classNameOf(target);
|
|
1186
|
+
const targetName = fieldNameOf(target, targetField);
|
|
1187
|
+
field.joinsTo = { className: at0(targetClass), fieldName: at0(targetName) };
|
|
1188
|
+
reference(element, target, targetClass);
|
|
1189
|
+
reference(element, targetField, targetName);
|
|
1190
|
+
} else report("annotation", element, "`joins_to` is not `Class.field`");
|
|
1191
|
+
} else if (joins !== void 0) report("annotation", element, "`joins_to` is not text");
|
|
1192
|
+
const note = oneLine(stringOf(annotations.get("note")));
|
|
1193
|
+
if (note !== void 0) field.note = note;
|
|
1194
|
+
for (const other of Object.keys(def)) {
|
|
1195
|
+
if (!SLOT_KEYS.has(other)) {
|
|
1196
|
+
report(other, element);
|
|
1197
|
+
continue;
|
|
1198
|
+
}
|
|
1199
|
+
if (other !== "annotations") continue;
|
|
1200
|
+
for (const tag of annotations.keys()) {
|
|
1201
|
+
if (!SLOT_TAGS.has(tag)) report("annotation", element, `\`${tag}\``);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
return field;
|
|
1205
|
+
}
|
|
1206
|
+
function typeFor(element, field, def) {
|
|
1207
|
+
const rawMany = get(def, "multivalued");
|
|
1208
|
+
if (present(rawMany) && typeof rawMany !== "boolean") {
|
|
1209
|
+
report("multivalued", element, `${shown(rawMany)} is not a boolean`);
|
|
1210
|
+
}
|
|
1211
|
+
const many = rawMany === true;
|
|
1212
|
+
const rawRange = get(def, "range");
|
|
1213
|
+
if (present(rawRange) && typeof rawRange !== "string") {
|
|
1214
|
+
report("range", element, `${shown(rawRange)} is not a string`);
|
|
1215
|
+
} else if (rawRange === "") report("range", element, "the range is empty");
|
|
1216
|
+
const range = stringOf(rawRange) ?? defaultRange;
|
|
1217
|
+
if (Object.hasOwn(classDefs, range)) {
|
|
1218
|
+
const target = classNameOf(range);
|
|
1219
|
+
reference(element, range, target);
|
|
1220
|
+
return { kind: "class", name: at0(target), many };
|
|
1221
|
+
}
|
|
1222
|
+
if (Object.hasOwn(enumDefs, range)) {
|
|
1223
|
+
const values = enumValues.get(range);
|
|
1224
|
+
if (values === void 0) return many ? stringType(true) : void 0;
|
|
1225
|
+
enumUses.push({ enumName: range, field, values: values.join("|") });
|
|
1226
|
+
return { kind: "enum", values: values.map(at0), many };
|
|
1227
|
+
}
|
|
1228
|
+
if (Object.hasOwn(PRIMITIVE_RANGE, range)) {
|
|
1229
|
+
const primitive = PRIMITIVE_RANGE[range] ?? "string";
|
|
1230
|
+
if (primitive === "string") return many ? stringType(true) : void 0;
|
|
1231
|
+
return { kind: "primitive", name: primitive, written: at0(primitive), many };
|
|
1232
|
+
}
|
|
1233
|
+
if (/^[A-Z]/.test(range)) {
|
|
1234
|
+
const target = toClassName(range);
|
|
1235
|
+
reference(element, range, target);
|
|
1236
|
+
return { kind: "class", name: at0(target), many };
|
|
1237
|
+
}
|
|
1238
|
+
const word = range.toLowerCase();
|
|
1239
|
+
if (!UNKNOWN_TYPE.test(word)) {
|
|
1240
|
+
report("narrowed", element, `\`${range}\` is not a type Skiss can write; it reads as string`);
|
|
1241
|
+
return many ? stringType(true) : void 0;
|
|
1242
|
+
}
|
|
1243
|
+
report("narrowed", element, `\`${range}\` is not a Skiss type; it falls back to string`);
|
|
1244
|
+
return { kind: "unknown", name: at0(word), many };
|
|
1245
|
+
}
|
|
1246
|
+
function listedSomewhere(slot) {
|
|
1247
|
+
for (const key2 of Object.keys(classDefs)) {
|
|
1248
|
+
const def = asRecord(get(classDefs, key2)) ?? {};
|
|
1249
|
+
if (stringList(get(def, "slots")).includes(slot)) return true;
|
|
1250
|
+
}
|
|
1251
|
+
return false;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
function sharingLost(uses, all) {
|
|
1255
|
+
const first = uses[0];
|
|
1256
|
+
if (first === void 0) return false;
|
|
1257
|
+
if (uses.some((use) => use.field !== first.field)) return true;
|
|
1258
|
+
return all.some((use) => use.field === first.field && use.values !== first.values);
|
|
1259
|
+
}
|
|
1260
|
+
var COUNTED = {
|
|
1261
|
+
renamed: ["name", "names"],
|
|
1262
|
+
inlined: ["enum", "enums"],
|
|
1263
|
+
enum: ["unwritable enum", "unwritable enums"],
|
|
1264
|
+
unused_enum: ["unused enum", "unused enums"],
|
|
1265
|
+
class: ["class", "classes"],
|
|
1266
|
+
slot: ["slot", "slots"],
|
|
1267
|
+
schema: ["schema key", "schema keys"],
|
|
1268
|
+
// `mixin: true` marks the class; `mixins: [M]` points at another one.
|
|
1269
|
+
mixin: ["mixin class", "mixin classes"],
|
|
1270
|
+
mixins: ["mixin", "mixins"],
|
|
1271
|
+
pattern: ["pattern", "patterns"],
|
|
1272
|
+
unit: ["unit", "units"]
|
|
1273
|
+
};
|
|
1274
|
+
var ENUM_KINDS = /* @__PURE__ */ new Set(["enum_detail"]);
|
|
1275
|
+
function formatDropped(dropped) {
|
|
1276
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1277
|
+
for (const entry of dropped) {
|
|
1278
|
+
const group = groups.get(entry.kind);
|
|
1279
|
+
if (group === void 0) groups.set(entry.kind, [entry]);
|
|
1280
|
+
else group.push(entry);
|
|
1281
|
+
}
|
|
1282
|
+
const drops = [];
|
|
1283
|
+
let renamed = "";
|
|
1284
|
+
let inlined = "";
|
|
1285
|
+
for (const [kind, entries] of groups) {
|
|
1286
|
+
const phrase = phraseFor(kind, entries);
|
|
1287
|
+
if (kind === "renamed") renamed = phrase;
|
|
1288
|
+
else if (kind === "inlined") inlined = phrase;
|
|
1289
|
+
else drops.push(phrase);
|
|
1290
|
+
}
|
|
1291
|
+
const sentences = [];
|
|
1292
|
+
if (drops.length > 0) sentences.push(`Dropped: ${drops.join(", ")}.`);
|
|
1293
|
+
if (renamed !== "") sentences.push(`Renamed: ${renamed}.`);
|
|
1294
|
+
if (inlined !== "") sentences.push(`Inlined: ${inlined}.`);
|
|
1295
|
+
return sentences.join(" ");
|
|
1296
|
+
}
|
|
1297
|
+
function phraseFor(kind, entries) {
|
|
1298
|
+
const n = entries.length;
|
|
1299
|
+
const counted = Object.hasOwn(COUNTED, kind) ? COUNTED[kind] : void 0;
|
|
1300
|
+
if (counted !== void 0) return `${n} ${n === 1 ? counted[0] : counted[1]}`;
|
|
1301
|
+
return `${kind} on ${n} ${nounFor(kind, entries)}`;
|
|
1302
|
+
}
|
|
1303
|
+
function nounFor(kind, entries) {
|
|
1304
|
+
const one = entries.length === 1;
|
|
1305
|
+
if (ENUM_KINDS.has(kind)) return one ? "enum" : "enums";
|
|
1306
|
+
const slots = entries.filter((entry) => entry.element.includes(".")).length;
|
|
1307
|
+
if (slots === entries.length) return one ? "slot" : "slots";
|
|
1308
|
+
if (slots === 0) return one ? "class" : "classes";
|
|
1309
|
+
return one ? "element" : "elements";
|
|
1310
|
+
}
|
|
1311
|
+
var capitalise2 = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
1312
|
+
var decapitalise = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
1313
|
+
var parts = (text) => text.split(/[^A-Za-z0-9]+/).filter((part) => part !== "");
|
|
1314
|
+
function toClassName(text) {
|
|
1315
|
+
if (CLASS_NAME.test(text)) return text;
|
|
1316
|
+
const name2 = parts(text).map(capitalise2).join("");
|
|
1317
|
+
if (name2 === "") return "Class";
|
|
1318
|
+
return CLASS_NAME.test(name2) ? name2 : `X${name2}`;
|
|
1319
|
+
}
|
|
1320
|
+
function toFieldName(text) {
|
|
1321
|
+
if (FIELD_NAME.test(text)) return text;
|
|
1322
|
+
const name2 = parts(text).map((part, i) => i === 0 ? decapitalise(part) : capitalise2(part)).join("");
|
|
1323
|
+
if (name2 === "") return "field";
|
|
1324
|
+
return FIELD_NAME.test(name2) ? name2 : `x${capitalise2(name2)}`;
|
|
1325
|
+
}
|
|
1326
|
+
function unique(base, taken) {
|
|
1327
|
+
let name2 = base;
|
|
1328
|
+
for (let n = 2; taken.has(name2); n++) name2 = `${base}${n}`;
|
|
1329
|
+
taken.add(name2);
|
|
1330
|
+
return name2;
|
|
1331
|
+
}
|
|
1332
|
+
var get = (record, key2) => Object.hasOwn(record, key2) ? record[key2] : void 0;
|
|
1333
|
+
function asRecord(value) {
|
|
1334
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
1335
|
+
return value;
|
|
1336
|
+
}
|
|
1337
|
+
var present = (value) => value !== void 0 && value !== null;
|
|
1338
|
+
function shown(value) {
|
|
1339
|
+
if (Array.isArray(value)) return "a list";
|
|
1340
|
+
if (typeof value === "object" && value !== null) return "a map";
|
|
1341
|
+
return `\`${String(value)}\``;
|
|
1342
|
+
}
|
|
1343
|
+
function localName(mapping) {
|
|
1344
|
+
let cut = -1;
|
|
1345
|
+
for (const mark of [":", "/", "#"]) cut = Math.max(cut, mapping.lastIndexOf(mark));
|
|
1346
|
+
return mapping.slice(cut + 1);
|
|
1347
|
+
}
|
|
1348
|
+
var stringOf = (value) => typeof value === "string" && value !== "" ? value : void 0;
|
|
1349
|
+
var stringList = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
|
|
1350
|
+
function annotationsOf(def) {
|
|
1351
|
+
const out = /* @__PURE__ */ new Map();
|
|
1352
|
+
const annotations = asRecord(get(def, "annotations"));
|
|
1353
|
+
if (annotations === void 0) return out;
|
|
1354
|
+
for (const tag of Object.keys(annotations)) {
|
|
1355
|
+
const raw = get(annotations, tag);
|
|
1356
|
+
const value = asRecord(raw) === void 0 ? raw : get(asRecord(raw) ?? {}, "value");
|
|
1357
|
+
if (typeof value === "string" || typeof value === "boolean") out.set(tag, value);
|
|
1358
|
+
else out.set(tag, String(value));
|
|
1359
|
+
}
|
|
1360
|
+
return out;
|
|
1361
|
+
}
|
|
1362
|
+
function slotKeysOf(def) {
|
|
1363
|
+
const keys = Object.keys(asRecord(get(def, "attributes")) ?? {});
|
|
1364
|
+
for (const slot of stringList(get(def, "slots"))) {
|
|
1365
|
+
if (!keys.includes(slot)) keys.push(slot);
|
|
1366
|
+
}
|
|
1367
|
+
return keys;
|
|
1368
|
+
}
|
|
1369
|
+
function oneLine(text) {
|
|
1370
|
+
if (text === void 0) return void 0;
|
|
1371
|
+
const line = text.replace(/\s*[\r\n]+\s*/g, " ").trim();
|
|
1372
|
+
return line === "" ? void 0 : line;
|
|
1373
|
+
}
|
|
1374
|
+
var list = (items) => items.map((item) => `\`${item}\``).join(", ");
|
|
1375
|
+
var at0 = (text) => ({ text, line: 0, col: 0, end: 0 });
|
|
1376
|
+
var stringType = (many) => ({
|
|
1377
|
+
kind: "primitive",
|
|
1378
|
+
name: "string",
|
|
1379
|
+
written: at0("string"),
|
|
1380
|
+
many
|
|
1381
|
+
});
|
|
1382
|
+
var schemaElement = (root) => stringOf(get(root, "name")) ?? "schema";
|
|
1383
|
+
var nothing = (element, detail) => ({
|
|
1384
|
+
document: parse(""),
|
|
1385
|
+
source: "",
|
|
1386
|
+
dropped: [{ kind: "schema", element, detail }]
|
|
1387
|
+
});
|
|
1388
|
+
|
|
1389
|
+
// src/import.ts
|
|
1390
|
+
import { parse as parseYAML } from "yaml";
|
|
1391
|
+
function importLinkML(text) {
|
|
1392
|
+
let schema;
|
|
1393
|
+
try {
|
|
1394
|
+
schema = parseYAML(text);
|
|
1395
|
+
} catch (error) {
|
|
1396
|
+
return { output: "", dropped: [], diagnostics: [notYAML(error)] };
|
|
1397
|
+
}
|
|
1398
|
+
const { document, source, dropped } = fromLinkML(schema);
|
|
1399
|
+
return { output: source, dropped, diagnostics: resolve(document).diagnostics };
|
|
1400
|
+
}
|
|
1401
|
+
function notYAML(error) {
|
|
1402
|
+
const first = error instanceof Error ? error.message.split("\n")[0] ?? "" : String(error);
|
|
1403
|
+
const detail = first.replace(/:$/, "");
|
|
1404
|
+
return {
|
|
1405
|
+
severity: "error",
|
|
1406
|
+
code: "E_NOT_YAML",
|
|
1407
|
+
message: `the input is not YAML: ${detail}`,
|
|
1408
|
+
line: 1
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
|
|
914
1412
|
// src/index.ts
|
|
915
|
-
var VERSION = "0.
|
|
1413
|
+
var VERSION = "0.3.0";
|
|
916
1414
|
export {
|
|
917
1415
|
VERSION,
|
|
918
1416
|
compile,
|
|
919
1417
|
formatDiagnostic,
|
|
1418
|
+
formatDropped,
|
|
1419
|
+
fromLinkML,
|
|
1420
|
+
importLinkML,
|
|
920
1421
|
parse,
|
|
921
1422
|
resolve,
|
|
922
1423
|
serialize,
|
|
923
1424
|
toLinkML,
|
|
924
|
-
toMermaid
|
|
1425
|
+
toMermaid,
|
|
1426
|
+
toSkiss
|
|
925
1427
|
};
|
|
926
1428
|
//# sourceMappingURL=index.js.map
|