@eriknaslund/skiss 0.1.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/LICENSE +21 -0
- package/README.md +88 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1071 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +174 -0
- package/dist/index.js +926 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1071 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { readFile, writeFile } from "fs/promises";
|
|
5
|
+
import { basename, extname } from "path";
|
|
6
|
+
import { parseArgs } from "util";
|
|
7
|
+
|
|
8
|
+
// src/resolve.ts
|
|
9
|
+
function resolve(doc) {
|
|
10
|
+
const out = {
|
|
11
|
+
...doc,
|
|
12
|
+
classes: doc.classes.map(copyClass),
|
|
13
|
+
diagnostics: doc.diagnostics.map((d) => ({ ...d })),
|
|
14
|
+
undeclared: []
|
|
15
|
+
};
|
|
16
|
+
const classes = out.classes;
|
|
17
|
+
const declared = new Set(classes.map((c) => c.name.text));
|
|
18
|
+
const warnings = [];
|
|
19
|
+
const undeclared = /* @__PURE__ */ new Map();
|
|
20
|
+
const warn = (code, message, at) => {
|
|
21
|
+
warnings.push({ severity: "warning", code, message, line: at.line, col: at.col, end: at.end });
|
|
22
|
+
};
|
|
23
|
+
const reference = (name2) => {
|
|
24
|
+
if (declared.has(name2.text)) return true;
|
|
25
|
+
if (!undeclared.has(name2.text)) undeclared.set(name2.text, name2);
|
|
26
|
+
warn("W_UNDECLARED_CLASS", `class \`${name2.text}\` is not declared`, name2);
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
29
|
+
const firstClass = /* @__PURE__ */ new Map();
|
|
30
|
+
for (const cls of classes) {
|
|
31
|
+
const earlier = firstClass.get(cls.name.text);
|
|
32
|
+
if (earlier === void 0) firstClass.set(cls.name.text, cls);
|
|
33
|
+
else {
|
|
34
|
+
warn(
|
|
35
|
+
"W_DUPLICATE_CLASS",
|
|
36
|
+
`class \`${cls.name.text}\` is already declared on line ${earlier.line}`,
|
|
37
|
+
cls.name
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
if (cls.similarTo !== void 0) reference(cls.similarTo);
|
|
41
|
+
const firstField = /* @__PURE__ */ new Map();
|
|
42
|
+
let identifier;
|
|
43
|
+
for (const field of cls.fields) {
|
|
44
|
+
const earlierField = firstField.get(field.name.text);
|
|
45
|
+
if (earlierField === void 0) firstField.set(field.name.text, field);
|
|
46
|
+
else {
|
|
47
|
+
warn(
|
|
48
|
+
"W_DUPLICATE_FIELD",
|
|
49
|
+
`field \`${field.name.text}\` is already declared on line ${earlierField.line} in class \`${cls.name.text}\``,
|
|
50
|
+
field.name
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (field.identifier) {
|
|
54
|
+
if (identifier === void 0) identifier = field;
|
|
55
|
+
else {
|
|
56
|
+
warn(
|
|
57
|
+
"W_MULTIPLE_IDENTIFIERS",
|
|
58
|
+
`\`*\` on \`${field.name.text}\` is ignored: class \`${cls.name.text}\` already has the identifier \`${identifier.name.text}\` on line ${identifier.line}`,
|
|
59
|
+
field.name
|
|
60
|
+
);
|
|
61
|
+
field.identifier = false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (field.type?.kind === "unknown") {
|
|
65
|
+
const word = field.type.name.text;
|
|
66
|
+
const suggestion = closestTypeWord(word);
|
|
67
|
+
warn(
|
|
68
|
+
"W_UNKNOWN_TYPE",
|
|
69
|
+
suggestion === void 0 ? `unknown type \`${word}\`, treated as \`string\`` : `unknown type \`${word}\`, did you mean \`${suggestion}\`?`,
|
|
70
|
+
field.type.name
|
|
71
|
+
);
|
|
72
|
+
} else if (field.type?.kind === "class") {
|
|
73
|
+
reference(field.type.name);
|
|
74
|
+
}
|
|
75
|
+
if (field.joinsTo !== void 0) {
|
|
76
|
+
const { className, fieldName } = field.joinsTo;
|
|
77
|
+
const hasField = (c) => c.name.text === className.text && c.fields.some((f) => f.name.text === fieldName.text);
|
|
78
|
+
if (reference(className) && !classes.some(hasField)) {
|
|
79
|
+
warn(
|
|
80
|
+
"W_UNDECLARED_FIELD",
|
|
81
|
+
`class \`${className.text}\` has no field \`${fieldName.text}\``,
|
|
82
|
+
fieldName
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const present = new Set(out.diagnostics.map(key));
|
|
89
|
+
for (const w of warnings) {
|
|
90
|
+
if (!present.has(key(w))) out.diagnostics.push(w);
|
|
91
|
+
}
|
|
92
|
+
out.diagnostics.sort((a, b) => a.line - b.line);
|
|
93
|
+
out.undeclared = [...undeclared.values()].map((n) => ({ ...n }));
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
var copyName = (n) => ({ ...n });
|
|
97
|
+
function copyType(t) {
|
|
98
|
+
switch (t.kind) {
|
|
99
|
+
case "primitive":
|
|
100
|
+
return { ...t, written: copyName(t.written) };
|
|
101
|
+
case "class":
|
|
102
|
+
case "unknown":
|
|
103
|
+
return { ...t, name: copyName(t.name) };
|
|
104
|
+
case "enum":
|
|
105
|
+
return { ...t, values: t.values.map(copyName) };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
var copyJoin = (j) => ({
|
|
109
|
+
className: copyName(j.className),
|
|
110
|
+
fieldName: copyName(j.fieldName)
|
|
111
|
+
});
|
|
112
|
+
function copyField(f) {
|
|
113
|
+
const out = { ...f, name: copyName(f.name) };
|
|
114
|
+
if (f.type !== void 0) out.type = copyType(f.type);
|
|
115
|
+
if (f.joinsTo !== void 0) out.joinsTo = copyJoin(f.joinsTo);
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
function copyClass(c) {
|
|
119
|
+
const out = { ...c, name: copyName(c.name), fields: c.fields.map(copyField) };
|
|
120
|
+
if (c.system !== void 0) out.system = copyName(c.system);
|
|
121
|
+
if (c.similarTo !== void 0) out.similarTo = copyName(c.similarTo);
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
var key = (d) => [d.severity, d.code, d.line, d.col, d.end, d.message].join(" ");
|
|
125
|
+
var TYPE_WORDS = [
|
|
126
|
+
"string",
|
|
127
|
+
"int",
|
|
128
|
+
"float",
|
|
129
|
+
"bool",
|
|
130
|
+
"date",
|
|
131
|
+
"datetime",
|
|
132
|
+
"uri",
|
|
133
|
+
"text",
|
|
134
|
+
"integer",
|
|
135
|
+
"boolean"
|
|
136
|
+
];
|
|
137
|
+
var MAX_DISTANCE = 2;
|
|
138
|
+
function closestTypeWord(word) {
|
|
139
|
+
let best;
|
|
140
|
+
let bestDistance = MAX_DISTANCE + 1;
|
|
141
|
+
let bestPrefix = -1;
|
|
142
|
+
for (const candidate of TYPE_WORDS) {
|
|
143
|
+
const distance = levenshtein(word, candidate);
|
|
144
|
+
if (distance > MAX_DISTANCE || distance > bestDistance) continue;
|
|
145
|
+
const prefix = commonPrefixLength(word, candidate);
|
|
146
|
+
if (distance < bestDistance || prefix > bestPrefix) {
|
|
147
|
+
best = candidate;
|
|
148
|
+
bestDistance = distance;
|
|
149
|
+
bestPrefix = prefix;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return best;
|
|
153
|
+
}
|
|
154
|
+
function commonPrefixLength(a, b) {
|
|
155
|
+
let i = 0;
|
|
156
|
+
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
157
|
+
return i;
|
|
158
|
+
}
|
|
159
|
+
function levenshtein(a, b) {
|
|
160
|
+
if (a === b) return 0;
|
|
161
|
+
if (a.length === 0) return b.length;
|
|
162
|
+
if (b.length === 0) return a.length;
|
|
163
|
+
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
164
|
+
for (let i = 1; i <= a.length; i++) {
|
|
165
|
+
const current = [i];
|
|
166
|
+
for (let j = 1; j <= b.length; j++) {
|
|
167
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
168
|
+
current[j] = Math.min(
|
|
169
|
+
(previous[j] ?? 0) + 1,
|
|
170
|
+
(current[j - 1] ?? 0) + 1,
|
|
171
|
+
(previous[j - 1] ?? 0) + cost
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
previous = current;
|
|
175
|
+
}
|
|
176
|
+
return previous[b.length] ?? 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/generators/linkml.ts
|
|
180
|
+
var BASE_URI = "https://example.org";
|
|
181
|
+
var RANGE = {
|
|
182
|
+
string: "string",
|
|
183
|
+
int: "integer",
|
|
184
|
+
float: "float",
|
|
185
|
+
bool: "boolean",
|
|
186
|
+
date: "date",
|
|
187
|
+
datetime: "datetime",
|
|
188
|
+
uri: "uri"
|
|
189
|
+
};
|
|
190
|
+
function toLinkML(doc, opts) {
|
|
191
|
+
const resolved = resolve(doc);
|
|
192
|
+
const schemaName = linkmlName(opts.schemaName);
|
|
193
|
+
const classes = emittedClasses(resolved.classes);
|
|
194
|
+
const classNames = new Set(classes.map((cls) => cls.node.name.text));
|
|
195
|
+
for (const name2 of resolved.undeclared) classNames.add(name2.text);
|
|
196
|
+
const enums = nameEnums(classes, classNames);
|
|
197
|
+
const prefixes = { linkml: "https://w3id.org/linkml/" };
|
|
198
|
+
if (!Object.hasOwn(prefixes, schemaName)) prefixes[schemaName] = `${BASE_URI}/${schemaName}/`;
|
|
199
|
+
const systemPrefix = /* @__PURE__ */ new Map();
|
|
200
|
+
const addSystem = (system) => {
|
|
201
|
+
const prefix = linkmlName(system);
|
|
202
|
+
if (!systemPrefix.has(system)) systemPrefix.set(system, prefix);
|
|
203
|
+
if (!Object.hasOwn(prefixes, prefix)) prefixes[prefix] = `${BASE_URI}/system/${prefix}/`;
|
|
204
|
+
};
|
|
205
|
+
for (const { node, fields } of classes) {
|
|
206
|
+
if (node.system !== void 0) addSystem(node.system.text);
|
|
207
|
+
for (const field of fields) {
|
|
208
|
+
if (field.system !== void 0) addSystem(field.system.text);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const declaredSystem = /* @__PURE__ */ new Map();
|
|
212
|
+
for (const { node } of classes) {
|
|
213
|
+
if (!declaredSystem.has(node.name.text)) declaredSystem.set(node.name.text, node.system?.text);
|
|
214
|
+
}
|
|
215
|
+
const mappingPrefix = (target) => {
|
|
216
|
+
const system = declaredSystem.get(target);
|
|
217
|
+
if (system === void 0) return schemaName;
|
|
218
|
+
return systemPrefix.get(system) ?? schemaName;
|
|
219
|
+
};
|
|
220
|
+
const schema = {
|
|
221
|
+
id: `${BASE_URI}/${schemaName}`,
|
|
222
|
+
name: schemaName,
|
|
223
|
+
default_prefix: schemaName,
|
|
224
|
+
default_range: "string",
|
|
225
|
+
prefixes,
|
|
226
|
+
imports: ["linkml:types"]
|
|
227
|
+
};
|
|
228
|
+
const out = {};
|
|
229
|
+
for (const cls of classes) {
|
|
230
|
+
out[cls.node.name.text] = buildClass(cls, mappingPrefix, enums);
|
|
231
|
+
}
|
|
232
|
+
for (const name2 of resolved.undeclared) {
|
|
233
|
+
if (!(name2.text in out)) out[name2.text] = { annotations: { undeclared: true } };
|
|
234
|
+
}
|
|
235
|
+
if (Object.keys(out).length > 0) schema.classes = out;
|
|
236
|
+
const enumBlock = buildEnums(classes, enums);
|
|
237
|
+
if (Object.keys(enumBlock).length > 0) schema.enums = enumBlock;
|
|
238
|
+
return schema;
|
|
239
|
+
}
|
|
240
|
+
function emittedClasses(classes) {
|
|
241
|
+
const out = [];
|
|
242
|
+
const seen = /* @__PURE__ */ new Set();
|
|
243
|
+
for (const node of classes) {
|
|
244
|
+
if (seen.has(node.name.text)) continue;
|
|
245
|
+
seen.add(node.name.text);
|
|
246
|
+
const fields = [];
|
|
247
|
+
const seenField = /* @__PURE__ */ new Set();
|
|
248
|
+
for (const field of node.fields) {
|
|
249
|
+
if (seenField.has(field.name.text)) continue;
|
|
250
|
+
seenField.add(field.name.text);
|
|
251
|
+
fields.push(field);
|
|
252
|
+
}
|
|
253
|
+
out.push({ node, fields });
|
|
254
|
+
}
|
|
255
|
+
return out;
|
|
256
|
+
}
|
|
257
|
+
var useKey = (className, fieldName) => `${className}\0${fieldName}`;
|
|
258
|
+
function nameEnums(classes, classNames) {
|
|
259
|
+
const uses = [];
|
|
260
|
+
for (const { node, fields } of classes) {
|
|
261
|
+
for (const field of fields) {
|
|
262
|
+
if (field.type?.kind !== "enum") continue;
|
|
263
|
+
uses.push({
|
|
264
|
+
className: node.name.text,
|
|
265
|
+
fieldName: field.name.text,
|
|
266
|
+
values: field.type.values.map((v) => v.text).join("|")
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const valueSets = /* @__PURE__ */ new Map();
|
|
271
|
+
for (const use of uses) {
|
|
272
|
+
const seen = valueSets.get(use.fieldName) ?? /* @__PURE__ */ new Set();
|
|
273
|
+
seen.add(use.values);
|
|
274
|
+
valueSets.set(use.fieldName, seen);
|
|
275
|
+
}
|
|
276
|
+
const taken = /* @__PURE__ */ new Map();
|
|
277
|
+
const names = /* @__PURE__ */ new Map();
|
|
278
|
+
for (const use of uses) {
|
|
279
|
+
const shared = (valueSets.get(use.fieldName)?.size ?? 0) <= 1;
|
|
280
|
+
const base = shared ? `${capitalise(use.fieldName)}Enum` : `${capitalise(use.className)}${capitalise(use.fieldName)}Enum`;
|
|
281
|
+
let name2 = base;
|
|
282
|
+
for (let n = 2; classNames.has(name2) || taken.has(name2) && taken.get(name2) !== use.values; n++) {
|
|
283
|
+
name2 = `${base}${n}`;
|
|
284
|
+
}
|
|
285
|
+
taken.set(name2, use.values);
|
|
286
|
+
names.set(useKey(use.className, use.fieldName), name2);
|
|
287
|
+
}
|
|
288
|
+
return names;
|
|
289
|
+
}
|
|
290
|
+
function buildEnums(classes, enums) {
|
|
291
|
+
const out = {};
|
|
292
|
+
for (const { node, fields } of classes) {
|
|
293
|
+
for (const field of fields) {
|
|
294
|
+
if (field.type?.kind !== "enum") continue;
|
|
295
|
+
const name2 = enums.get(useKey(node.name.text, field.name.text));
|
|
296
|
+
if (name2 === void 0 || name2 in out) continue;
|
|
297
|
+
const values = {};
|
|
298
|
+
for (const value of field.type.values) values[value.text] = null;
|
|
299
|
+
out[name2] = { permissible_values: values };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
function buildClass(cls, mappingPrefix, enums) {
|
|
305
|
+
const { node, fields } = cls;
|
|
306
|
+
const out = {};
|
|
307
|
+
if (node.description !== void 0) out.description = node.description;
|
|
308
|
+
const annotations = {};
|
|
309
|
+
if (node.system !== void 0) annotations.system = node.system.text;
|
|
310
|
+
if (node.note !== void 0) annotations.note = node.note;
|
|
311
|
+
if (Object.keys(annotations).length > 0) out.annotations = annotations;
|
|
312
|
+
if (node.similarTo !== void 0) {
|
|
313
|
+
const target = node.similarTo.text;
|
|
314
|
+
out.close_mappings = [`${mappingPrefix(target)}:${target}`];
|
|
315
|
+
}
|
|
316
|
+
const attributes = {};
|
|
317
|
+
for (const field of fields) {
|
|
318
|
+
attributes[field.name.text] = buildAttribute(field, node.name.text, enums);
|
|
319
|
+
}
|
|
320
|
+
if (Object.keys(attributes).length > 0) out.attributes = attributes;
|
|
321
|
+
return out;
|
|
322
|
+
}
|
|
323
|
+
function buildAttribute(field, className, enums) {
|
|
324
|
+
const out = {};
|
|
325
|
+
if (field.description !== void 0) out.description = field.description;
|
|
326
|
+
const range = rangeOf(field, className, enums);
|
|
327
|
+
if (range !== void 0) out.range = range;
|
|
328
|
+
if (field.type?.many === true) out.multivalued = true;
|
|
329
|
+
if (field.identifier) out.identifier = true;
|
|
330
|
+
const annotations = {};
|
|
331
|
+
if (field.system !== void 0) annotations.system = field.system.text;
|
|
332
|
+
if (field.joinsTo !== void 0) {
|
|
333
|
+
annotations.joins_to = `${field.joinsTo.className.text}.${field.joinsTo.fieldName.text}`;
|
|
334
|
+
}
|
|
335
|
+
if (field.note !== void 0) annotations.note = field.note;
|
|
336
|
+
if (Object.keys(annotations).length > 0) out.annotations = annotations;
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
function rangeOf(field, className, enums) {
|
|
340
|
+
const type = field.type;
|
|
341
|
+
if (type === void 0) return void 0;
|
|
342
|
+
switch (type.kind) {
|
|
343
|
+
case "primitive":
|
|
344
|
+
return RANGE[type.name];
|
|
345
|
+
case "class":
|
|
346
|
+
return type.name.text;
|
|
347
|
+
case "unknown":
|
|
348
|
+
return "string";
|
|
349
|
+
case "enum":
|
|
350
|
+
return enums.get(useKey(className, field.name.text));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
var RESERVED_PREFIXES = /* @__PURE__ */ new Set(["linkml", "xsd", "shex", "schema"]);
|
|
354
|
+
var linkmlName = (text) => {
|
|
355
|
+
const name2 = text.toLowerCase().replace(/[^a-z0-9]/g, "_");
|
|
356
|
+
if (!/[a-z0-9]/.test(name2)) return "sketch";
|
|
357
|
+
if (RESERVED_PREFIXES.has(name2)) return `${name2}_`;
|
|
358
|
+
return /^[0-9]/.test(name2) ? `_${name2}` : name2;
|
|
359
|
+
};
|
|
360
|
+
var capitalise = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
361
|
+
|
|
362
|
+
// src/generators/mermaid.ts
|
|
363
|
+
function toMermaid(doc, opts) {
|
|
364
|
+
const resolved = resolve(doc);
|
|
365
|
+
const notes = opts?.notes === true;
|
|
366
|
+
const lines = ["classDiagram"];
|
|
367
|
+
for (const cls of resolved.classes) {
|
|
368
|
+
lines.push(` class ${cls.name.text} {`);
|
|
369
|
+
if (cls.system !== void 0) lines.push(` <<${cls.system.text}>>`);
|
|
370
|
+
for (const field of cls.fields) lines.push(` ${member(field)}`);
|
|
371
|
+
lines.push(" }");
|
|
372
|
+
}
|
|
373
|
+
for (const name2 of resolved.undeclared) {
|
|
374
|
+
lines.push(` class ${name2.text} {`, " <<undeclared>>", " }");
|
|
375
|
+
}
|
|
376
|
+
if (notes) {
|
|
377
|
+
for (const cls of resolved.classes) {
|
|
378
|
+
if (cls.note !== void 0) lines.push(` note for ${cls.name.text} ${quote(cls.note)}`);
|
|
379
|
+
for (const field of cls.fields) {
|
|
380
|
+
if (field.note !== void 0) {
|
|
381
|
+
lines.push(` note for ${cls.name.text} ${quote(`${field.name.text}: ${field.note}`)}`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
for (const cls of resolved.classes) {
|
|
387
|
+
const from = cls.name.text;
|
|
388
|
+
if (cls.similarTo !== void 0) lines.push(` ${from} ..> ${cls.similarTo.text} : similar`);
|
|
389
|
+
for (const field of cls.fields) {
|
|
390
|
+
const type = field.type;
|
|
391
|
+
if (type?.kind === "class") {
|
|
392
|
+
lines.push(
|
|
393
|
+
type.many ? ` ${from} "1" --> "*" ${type.name.text} : ${field.name.text}` : ` ${from} --> ${type.name.text} : ${field.name.text}`
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
if (field.joinsTo !== void 0) {
|
|
397
|
+
const { className, fieldName } = field.joinsTo;
|
|
398
|
+
lines.push(` ${from} ..> ${className.text} : ${field.name.text} = ${fieldName.text}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return `${lines.join("\n")}
|
|
403
|
+
`;
|
|
404
|
+
}
|
|
405
|
+
function member(field) {
|
|
406
|
+
const marker = field.identifier ? "*" : "+";
|
|
407
|
+
const type = field.type === void 0 ? "" : `${typeText(field.type)} `;
|
|
408
|
+
const system = field.system === void 0 ? "" : ` @${field.system.text}`;
|
|
409
|
+
return `${marker}${type}${field.name.text}${system}`;
|
|
410
|
+
}
|
|
411
|
+
function typeText(type) {
|
|
412
|
+
let text;
|
|
413
|
+
switch (type.kind) {
|
|
414
|
+
case "primitive":
|
|
415
|
+
text = type.written.text;
|
|
416
|
+
break;
|
|
417
|
+
case "class":
|
|
418
|
+
case "unknown":
|
|
419
|
+
text = type.name.text;
|
|
420
|
+
break;
|
|
421
|
+
case "enum":
|
|
422
|
+
text = type.values.map((v) => v.text).join("|");
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
return type.many ? `${text}[]` : text;
|
|
426
|
+
}
|
|
427
|
+
var quote = (text) => `"${text.replaceAll('"', "'")}"`;
|
|
428
|
+
|
|
429
|
+
// src/parse.ts
|
|
430
|
+
var isWs = (c) => c === " " || c === " ";
|
|
431
|
+
var trimWs = (s) => s.replace(/^[ \t]+/, "").replace(/[ \t]+$/, "");
|
|
432
|
+
var CLASS_NAME = /^[A-Z][A-Za-z0-9]*$/;
|
|
433
|
+
var FIELD_NAME = /^[a-z][A-Za-z0-9]*$/;
|
|
434
|
+
var SYSTEM_OR_VALUE = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
435
|
+
var PRIMITIVES = {
|
|
436
|
+
string: "string",
|
|
437
|
+
int: "int",
|
|
438
|
+
float: "float",
|
|
439
|
+
bool: "bool",
|
|
440
|
+
date: "date",
|
|
441
|
+
datetime: "datetime",
|
|
442
|
+
uri: "uri",
|
|
443
|
+
text: "string",
|
|
444
|
+
integer: "int",
|
|
445
|
+
boolean: "bool"
|
|
446
|
+
};
|
|
447
|
+
var FIELD_ORDER = "name, `*`, `: type`, `@System`, `= Class.field`, then `#` and `?`";
|
|
448
|
+
var CLASS_ORDER = "name, `@System`, `~ Class`, then `#` and `?`";
|
|
449
|
+
function parse(source) {
|
|
450
|
+
const classes = [];
|
|
451
|
+
const diagnostics = [];
|
|
452
|
+
let current;
|
|
453
|
+
const text0 = source.startsWith("\uFEFF") ? source.slice(1) : source;
|
|
454
|
+
const lines = text0.split(/\r\n|\n|\r/);
|
|
455
|
+
for (let i = 0; i < lines.length; i++) {
|
|
456
|
+
const line = i + 1;
|
|
457
|
+
const text = (lines[i] ?? "").replace(/[ \t]+$/, "");
|
|
458
|
+
const first = text[0];
|
|
459
|
+
if (first === void 0 || first === "#") continue;
|
|
460
|
+
if (isWs(first)) {
|
|
461
|
+
const start = text.search(/[^ \t]/);
|
|
462
|
+
if (current === void 0) {
|
|
463
|
+
diagnostics.push({
|
|
464
|
+
severity: "error",
|
|
465
|
+
code: "E_FIELD_WITHOUT_CLASS",
|
|
466
|
+
message: "An indented line is a field, but there is no valid class line above it",
|
|
467
|
+
line,
|
|
468
|
+
col: start,
|
|
469
|
+
end: text.length
|
|
470
|
+
});
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const result = parseFieldLine(text, line);
|
|
474
|
+
if (result.ok) current.fields.push(result.value);
|
|
475
|
+
else diagnostics.push(result.diagnostic);
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (/\p{L}/u.test(first)) {
|
|
479
|
+
const result = parseClassLine(text, line);
|
|
480
|
+
if (result.ok) {
|
|
481
|
+
classes.push(result.value);
|
|
482
|
+
current = result.value;
|
|
483
|
+
} else {
|
|
484
|
+
diagnostics.push(result.diagnostic);
|
|
485
|
+
current = void 0;
|
|
486
|
+
}
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
diagnostics.push({
|
|
490
|
+
severity: "error",
|
|
491
|
+
code: "E_UNPARSABLE",
|
|
492
|
+
message: "A line at column 0 must start with a class name or `#` for a comment",
|
|
493
|
+
line,
|
|
494
|
+
col: 0,
|
|
495
|
+
end: text.length
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
return { classes, diagnostics };
|
|
499
|
+
}
|
|
500
|
+
function isDoubtMarker(text, i) {
|
|
501
|
+
return text[i] === "?" && isWs(text[i - 1]) && (i + 1 === text.length || isWs(text[i + 1]));
|
|
502
|
+
}
|
|
503
|
+
function splitTrailer(text) {
|
|
504
|
+
let start = -1;
|
|
505
|
+
for (let i = 1; i < text.length; i++) {
|
|
506
|
+
if (!isWs(text[i - 1])) continue;
|
|
507
|
+
if (text[i] === "#" || isDoubtMarker(text, i)) {
|
|
508
|
+
start = i;
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
if (start < 0) return { head: text };
|
|
513
|
+
const trailer = { head: text.slice(0, start).replace(/[ \t]+$/, "") };
|
|
514
|
+
if (text[start] === "?") {
|
|
515
|
+
const note = trimWs(text.slice(start + 1));
|
|
516
|
+
if (note !== "") trailer.note = note;
|
|
517
|
+
return trailer;
|
|
518
|
+
}
|
|
519
|
+
let doubt = -1;
|
|
520
|
+
for (let i = start + 1; i < text.length; i++) {
|
|
521
|
+
if (isDoubtMarker(text, i)) {
|
|
522
|
+
doubt = i;
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const description = trimWs(text.slice(start + 1, doubt < 0 ? text.length : doubt));
|
|
527
|
+
if (description !== "") trailer.description = description;
|
|
528
|
+
if (doubt >= 0) {
|
|
529
|
+
const note = trimWs(text.slice(doubt + 1));
|
|
530
|
+
if (note !== "") trailer.note = note;
|
|
531
|
+
}
|
|
532
|
+
return trailer;
|
|
533
|
+
}
|
|
534
|
+
var WORD = /[\p{L}\p{N}_-]+/uy;
|
|
535
|
+
var OPERATORS = ["*", ":", "@", "~", "=", ".", "|", "["];
|
|
536
|
+
function tokenize(head) {
|
|
537
|
+
const tokens = [];
|
|
538
|
+
let i = 0;
|
|
539
|
+
while (i < head.length) {
|
|
540
|
+
const c = head[i] ?? "";
|
|
541
|
+
if (isWs(c)) {
|
|
542
|
+
i++;
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
WORD.lastIndex = i;
|
|
546
|
+
const word = WORD.exec(head);
|
|
547
|
+
if (word !== null) {
|
|
548
|
+
tokens.push({ kind: "word", text: word[0], col: i, end: i + word[0].length });
|
|
549
|
+
i += word[0].length;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (c === "[" && head[i + 1] === "]") {
|
|
553
|
+
tokens.push({ kind: "[]", text: "[]", col: i, end: i + 2 });
|
|
554
|
+
i += 2;
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
const ch = String.fromCodePoint(head.codePointAt(i) ?? 0);
|
|
558
|
+
const kind = OPERATORS.find((op) => op === ch) ?? "other";
|
|
559
|
+
tokens.push({ kind, text: ch, col: i, end: i + ch.length });
|
|
560
|
+
i += ch.length;
|
|
561
|
+
}
|
|
562
|
+
return tokens;
|
|
563
|
+
}
|
|
564
|
+
var peek = (c) => c.tokens[c.pos];
|
|
565
|
+
function take(c) {
|
|
566
|
+
const t = c.tokens[c.pos];
|
|
567
|
+
if (t !== void 0) c.pos++;
|
|
568
|
+
return t;
|
|
569
|
+
}
|
|
570
|
+
var name = (t, line) => ({ text: t.text, line, col: t.col, end: t.end });
|
|
571
|
+
function fail(code, message, line, span) {
|
|
572
|
+
return {
|
|
573
|
+
ok: false,
|
|
574
|
+
diagnostic: { severity: "error", code, message, line, col: span.col, end: span.end }
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
var afterSpan = (after) => ({ col: after.end, end: after.end });
|
|
578
|
+
function parseClassLine(text, line) {
|
|
579
|
+
const trailer = splitTrailer(text);
|
|
580
|
+
const c = { tokens: tokenize(trailer.head), pos: 0, line };
|
|
581
|
+
const head = take(c);
|
|
582
|
+
if (head === void 0 || head.kind !== "word") {
|
|
583
|
+
return fail("E_UNPARSABLE", "A class line must start with a class name", line, {
|
|
584
|
+
col: 0,
|
|
585
|
+
end: Math.max(1, trailer.head.length)
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
if (!CLASS_NAME.test(head.text)) {
|
|
589
|
+
return fail(
|
|
590
|
+
"E_BAD_NAME",
|
|
591
|
+
`Class names are UpperCamelCase (letters and digits, uppercase first): \`${head.text}\``,
|
|
592
|
+
line,
|
|
593
|
+
head
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
const node = { name: name(head, line), line };
|
|
597
|
+
const at = peek(c);
|
|
598
|
+
if (at?.kind === "@") {
|
|
599
|
+
take(c);
|
|
600
|
+
const system = parseSystem(c, at);
|
|
601
|
+
if (!system.ok) return system;
|
|
602
|
+
node.system = system.value;
|
|
603
|
+
}
|
|
604
|
+
const tilde = peek(c);
|
|
605
|
+
if (tilde?.kind === "~") {
|
|
606
|
+
take(c);
|
|
607
|
+
const target = take(c);
|
|
608
|
+
if (target === void 0 || target.kind !== "word") {
|
|
609
|
+
return fail(
|
|
610
|
+
"E_UNPARSABLE",
|
|
611
|
+
"Expected a class name after `~`, e.g. `CharacterPage @Community ~ Character`",
|
|
612
|
+
line,
|
|
613
|
+
target ?? afterSpan(tilde)
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
if (!CLASS_NAME.test(target.text)) {
|
|
617
|
+
return fail(
|
|
618
|
+
"E_BAD_NAME",
|
|
619
|
+
`The class after \`~\` must be UpperCamelCase: \`${target.text}\``,
|
|
620
|
+
line,
|
|
621
|
+
target
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
node.similarTo = name(target, line);
|
|
625
|
+
}
|
|
626
|
+
const rest = peek(c);
|
|
627
|
+
if (rest !== void 0) return unexpected(rest, line, "class", CLASS_ORDER);
|
|
628
|
+
if (trailer.description !== void 0) node.description = trailer.description;
|
|
629
|
+
if (trailer.note !== void 0) node.note = trailer.note;
|
|
630
|
+
return { ok: true, value: { ...node, fields: [] } };
|
|
631
|
+
}
|
|
632
|
+
function parseFieldLine(text, line) {
|
|
633
|
+
const trailer = splitTrailer(text);
|
|
634
|
+
const c = { tokens: tokenize(trailer.head), pos: 0, line };
|
|
635
|
+
const head = take(c);
|
|
636
|
+
if (head === void 0) {
|
|
637
|
+
const col = text.search(/[^ \t]/);
|
|
638
|
+
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";
|
|
639
|
+
return fail("E_UNPARSABLE", message, line, { col, end: Math.max(col + 1, text.length) });
|
|
640
|
+
}
|
|
641
|
+
if (head.kind !== "word") return unexpected(head, line, "field", FIELD_ORDER);
|
|
642
|
+
if (!FIELD_NAME.test(head.text)) {
|
|
643
|
+
return fail(
|
|
644
|
+
"E_BAD_NAME",
|
|
645
|
+
`Field names are lowerCamelCase (letters and digits, lowercase first): \`${head.text}\``,
|
|
646
|
+
line,
|
|
647
|
+
head
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
const node = { name: name(head, line), line, identifier: false };
|
|
651
|
+
if (peek(c)?.kind === "*") {
|
|
652
|
+
take(c);
|
|
653
|
+
node.identifier = true;
|
|
654
|
+
}
|
|
655
|
+
const colon = peek(c);
|
|
656
|
+
if (colon?.kind === ":") {
|
|
657
|
+
take(c);
|
|
658
|
+
const type = parseType(c, colon);
|
|
659
|
+
if (!type.ok) return type;
|
|
660
|
+
node.type = type.value;
|
|
661
|
+
}
|
|
662
|
+
const at = peek(c);
|
|
663
|
+
if (at?.kind === "@") {
|
|
664
|
+
take(c);
|
|
665
|
+
const system = parseSystem(c, at);
|
|
666
|
+
if (!system.ok) return system;
|
|
667
|
+
node.system = system.value;
|
|
668
|
+
}
|
|
669
|
+
const equals = peek(c);
|
|
670
|
+
if (equals?.kind === "=") {
|
|
671
|
+
take(c);
|
|
672
|
+
const example = "e.g. `characterId = Character.id`";
|
|
673
|
+
const className = take(c);
|
|
674
|
+
if (className === void 0 || className.kind !== "word") {
|
|
675
|
+
return fail(
|
|
676
|
+
"E_UNPARSABLE",
|
|
677
|
+
`Expected \`Class.field\` after \`=\`, ${example}`,
|
|
678
|
+
line,
|
|
679
|
+
className ?? afterSpan(equals)
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
if (!CLASS_NAME.test(className.text)) {
|
|
683
|
+
return fail(
|
|
684
|
+
"E_BAD_NAME",
|
|
685
|
+
`The class after \`=\` must be UpperCamelCase: \`${className.text}\``,
|
|
686
|
+
line,
|
|
687
|
+
className
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
const dot = take(c);
|
|
691
|
+
if (dot === void 0 || dot.kind !== ".") {
|
|
692
|
+
return fail(
|
|
693
|
+
"E_UNPARSABLE",
|
|
694
|
+
`Expected \`.\` and a field name after \`= ${className.text}\`, ${example}`,
|
|
695
|
+
line,
|
|
696
|
+
dot ?? afterSpan(className)
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
const fieldName = take(c);
|
|
700
|
+
if (fieldName === void 0 || fieldName.kind !== "word") {
|
|
701
|
+
return fail(
|
|
702
|
+
"E_UNPARSABLE",
|
|
703
|
+
`Expected a field name after \`= ${className.text}.\`, ${example}`,
|
|
704
|
+
line,
|
|
705
|
+
fieldName ?? afterSpan(dot)
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
if (!FIELD_NAME.test(fieldName.text)) {
|
|
709
|
+
return fail(
|
|
710
|
+
"E_BAD_NAME",
|
|
711
|
+
`The field after \`= ${className.text}.\` must be lowerCamelCase: \`${fieldName.text}\``,
|
|
712
|
+
line,
|
|
713
|
+
fieldName
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
node.joinsTo = { className: name(className, line), fieldName: name(fieldName, line) };
|
|
717
|
+
}
|
|
718
|
+
const rest = peek(c);
|
|
719
|
+
if (rest !== void 0) return unexpected(rest, line, "field", FIELD_ORDER);
|
|
720
|
+
if (trailer.description !== void 0) node.description = trailer.description;
|
|
721
|
+
if (trailer.note !== void 0) node.note = trailer.note;
|
|
722
|
+
return { ok: true, value: node };
|
|
723
|
+
}
|
|
724
|
+
function parseSystem(c, at) {
|
|
725
|
+
const system = take(c);
|
|
726
|
+
if (system === void 0 || system.kind !== "word") {
|
|
727
|
+
return fail(
|
|
728
|
+
"E_UNPARSABLE",
|
|
729
|
+
"Expected a system name after `@`, e.g. `@Catalog`",
|
|
730
|
+
c.line,
|
|
731
|
+
system ?? afterSpan(at)
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
if (!SYSTEM_OR_VALUE.test(system.text)) {
|
|
735
|
+
return fail(
|
|
736
|
+
"E_UNPARSABLE",
|
|
737
|
+
`A system name is letters, digits, \`-\` and \`_\`, starting with a letter: \`${system.text}\``,
|
|
738
|
+
c.line,
|
|
739
|
+
system
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
return { ok: true, value: name(system, c.line) };
|
|
743
|
+
}
|
|
744
|
+
function parseType(c, colon) {
|
|
745
|
+
const first = peek(c);
|
|
746
|
+
if (first === void 0 || first.kind === "*" || first.kind === "@" || first.kind === "=") {
|
|
747
|
+
return fail(
|
|
748
|
+
"E_MISSING_TYPE",
|
|
749
|
+
"A colon needs a type after it, e.g. `crewSize: int`",
|
|
750
|
+
c.line,
|
|
751
|
+
colon
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
if (first.kind !== "word") {
|
|
755
|
+
return fail(
|
|
756
|
+
"E_UNPARSABLE",
|
|
757
|
+
`Expected a type after \`:\`, found \`${first.text}\``,
|
|
758
|
+
c.line,
|
|
759
|
+
first
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
take(c);
|
|
763
|
+
const words = [first];
|
|
764
|
+
for (let pipe = peek(c); pipe?.kind === "|"; pipe = peek(c)) {
|
|
765
|
+
take(c);
|
|
766
|
+
const value = take(c);
|
|
767
|
+
if (value === void 0 || value.kind !== "word") {
|
|
768
|
+
return fail(
|
|
769
|
+
"E_UNPARSABLE",
|
|
770
|
+
"Expected an enum value after `|`, e.g. `climate: arid|temperate|frozen`",
|
|
771
|
+
c.line,
|
|
772
|
+
value ?? afterSpan(pipe)
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
words.push(value);
|
|
776
|
+
}
|
|
777
|
+
let many = false;
|
|
778
|
+
const bracket = peek(c);
|
|
779
|
+
if (bracket?.kind === "[]") {
|
|
780
|
+
take(c);
|
|
781
|
+
many = true;
|
|
782
|
+
} else if (bracket?.kind === "[") {
|
|
783
|
+
return fail(
|
|
784
|
+
"E_UNCLOSED_MANY",
|
|
785
|
+
"`[` must be closed: `[]` marks a many-valued field",
|
|
786
|
+
c.line,
|
|
787
|
+
bracket
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
if (words.length > 1) {
|
|
791
|
+
for (const w of words) {
|
|
792
|
+
if (!SYSTEM_OR_VALUE.test(w.text)) {
|
|
793
|
+
return fail(
|
|
794
|
+
"E_UNPARSABLE",
|
|
795
|
+
`An enum value is letters, digits, \`-\` and \`_\`, starting with a letter: \`${w.text}\``,
|
|
796
|
+
c.line,
|
|
797
|
+
w
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
return { ok: true, value: { kind: "enum", values: words.map((w) => name(w, c.line)), many } };
|
|
802
|
+
}
|
|
803
|
+
const word = first.text;
|
|
804
|
+
const primitive = PRIMITIVES[word];
|
|
805
|
+
if (primitive !== void 0) {
|
|
806
|
+
return {
|
|
807
|
+
ok: true,
|
|
808
|
+
value: { kind: "primitive", name: primitive, written: name(first, c.line), many }
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
if (/^[A-Z]/.test(word)) {
|
|
812
|
+
if (!CLASS_NAME.test(word)) {
|
|
813
|
+
return fail(
|
|
814
|
+
"E_BAD_NAME",
|
|
815
|
+
`A referenced class must be UpperCamelCase: \`${word}\``,
|
|
816
|
+
c.line,
|
|
817
|
+
first
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
return { ok: true, value: { kind: "class", name: name(first, c.line), many } };
|
|
821
|
+
}
|
|
822
|
+
if (/^[a-z]/.test(word)) {
|
|
823
|
+
return { ok: true, value: { kind: "unknown", name: name(first, c.line), many } };
|
|
824
|
+
}
|
|
825
|
+
return fail(
|
|
826
|
+
"E_UNPARSABLE",
|
|
827
|
+
`A type is a primitive, a class name or an enum, not \`${word}\``,
|
|
828
|
+
c.line,
|
|
829
|
+
first
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
function unexpected(token, line, kind, order) {
|
|
833
|
+
const modifiers = kind === "field" ? ["*", ":", "@", "="] : ["@", "~"];
|
|
834
|
+
if (modifiers.includes(token.kind)) {
|
|
835
|
+
return fail(
|
|
836
|
+
"E_UNPARSABLE",
|
|
837
|
+
`\`${token.text}\` is out of order; a ${kind} line reads: ${order}`,
|
|
838
|
+
line,
|
|
839
|
+
token
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
if (token.text === "<") {
|
|
843
|
+
return fail(
|
|
844
|
+
"E_UNPARSABLE",
|
|
845
|
+
"`<` is reserved for inheritance and not yet part of the language",
|
|
846
|
+
line,
|
|
847
|
+
token
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
if (token.text === "?") {
|
|
851
|
+
return fail(
|
|
852
|
+
"E_UNPARSABLE",
|
|
853
|
+
"`?` attached to a word is not a marker; a doubt needs whitespace before `?` and after it",
|
|
854
|
+
line,
|
|
855
|
+
token
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
if (token.text === "#") {
|
|
859
|
+
return fail("E_UNPARSABLE", "A description needs whitespace before `#`", line, token);
|
|
860
|
+
}
|
|
861
|
+
return fail("E_UNPARSABLE", `Unexpected \`${token.text}\` on a ${kind} line`, line, token);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// src/serialize.ts
|
|
865
|
+
import { isMap, isScalar, Scalar, visit, Document as YAMLDocument } from "yaml";
|
|
866
|
+
function serialize(schema, format) {
|
|
867
|
+
if (format === "json") return `${JSON.stringify(schema, null, 2)}
|
|
868
|
+
`;
|
|
869
|
+
const doc = new YAMLDocument(schema, { version: "1.1" });
|
|
870
|
+
const root = doc.contents;
|
|
871
|
+
if (isMap(root)) {
|
|
872
|
+
for (const pair of root.items) {
|
|
873
|
+
const key2 = pair.key;
|
|
874
|
+
if (!isScalar(key2)) continue;
|
|
875
|
+
if (key2.value !== "classes" && key2.value !== "enums") continue;
|
|
876
|
+
key2.spaceBefore = true;
|
|
877
|
+
if (key2.value === "classes" && isMap(pair.value)) {
|
|
878
|
+
for (const entry of pair.value.items) {
|
|
879
|
+
if (isScalar(entry.key)) entry.key.spaceBefore = true;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
visit(doc, {
|
|
885
|
+
Scalar(_, node) {
|
|
886
|
+
if (typeof node.value === "string" && hasControlCharacter(node.value)) {
|
|
887
|
+
node.type = Scalar.QUOTE_DOUBLE;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
});
|
|
891
|
+
const text = doc.toString({
|
|
892
|
+
directives: false,
|
|
893
|
+
indent: 2,
|
|
894
|
+
lineWidth: 0,
|
|
895
|
+
// A permissible value carries no body: `arid:`, not `arid: null`.
|
|
896
|
+
nullStr: ""
|
|
897
|
+
});
|
|
898
|
+
return text.replace(/^[ \t]+$/gm, "");
|
|
899
|
+
}
|
|
900
|
+
function hasControlCharacter(text) {
|
|
901
|
+
for (const character of text) {
|
|
902
|
+
const code = character.codePointAt(0) ?? 0;
|
|
903
|
+
if (code < 32 || code === 127) return true;
|
|
904
|
+
}
|
|
905
|
+
return false;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// src/compile.ts
|
|
909
|
+
function compile(source, opts) {
|
|
910
|
+
const doc = resolve(parse(source));
|
|
911
|
+
const output = opts.target === "linkml" ? serialize(toLinkML(doc, { schemaName: opts.schemaName }), opts.format ?? "yaml") : toMermaid(doc, { notes: opts.notes });
|
|
912
|
+
return { output, diagnostics: doc.diagnostics };
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// src/diagnostics.ts
|
|
916
|
+
function formatDiagnostic(d, file) {
|
|
917
|
+
const where = [file, d.line, d.col === void 0 ? void 0 : d.col + 1].filter((part) => part !== void 0).join(":");
|
|
918
|
+
return `${where}: ${d.severity} ${d.code} ${d.message}`;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// src/index.ts
|
|
922
|
+
var VERSION = "0.1.0";
|
|
923
|
+
|
|
924
|
+
// src/cli.ts
|
|
925
|
+
var HELP = `Usage: skiss <command> [options]
|
|
926
|
+
|
|
927
|
+
Commands:
|
|
928
|
+
diagram <file> Write a Mermaid class diagram for a .skiss file.
|
|
929
|
+
compile <file> Write a LinkML schema for a .skiss file.
|
|
930
|
+
<file> may be - to read standard input.
|
|
931
|
+
|
|
932
|
+
Options:
|
|
933
|
+
-o, --output <path> Write the output to <path> instead of standard output
|
|
934
|
+
--notes diagram: emit \`? text\` doubts as Mermaid notes
|
|
935
|
+
--json compile: write the schema as JSON instead of YAML
|
|
936
|
+
--name <name> compile: the schema name. Defaults to the file's
|
|
937
|
+
basename without its extension, or \`sketch\` for
|
|
938
|
+
standard input.
|
|
939
|
+
--strict Exit 1 when the input has any diagnostic
|
|
940
|
+
-h, --help Show this help and exit
|
|
941
|
+
--version Print the version and exit
|
|
942
|
+
|
|
943
|
+
Diagnostics go to standard error, one per line:
|
|
944
|
+
<file>:<line>:<col>: <severity> <CODE> <message>
|
|
945
|
+
|
|
946
|
+
Exit codes:
|
|
947
|
+
0 the output was produced
|
|
948
|
+
1 --strict and the input has diagnostics (the output is still written)
|
|
949
|
+
2 usage error, missing file or unreadable input
|
|
950
|
+
70 internal error, a bug in skiss
|
|
951
|
+
`;
|
|
952
|
+
var EXIT_OK = 0;
|
|
953
|
+
var EXIT_STRICT = 1;
|
|
954
|
+
var EXIT_USAGE = 2;
|
|
955
|
+
var EXIT_INTERNAL = 70;
|
|
956
|
+
var COMMANDS = {
|
|
957
|
+
diagram: ["output", "notes", "strict"],
|
|
958
|
+
compile: ["output", "json", "name", "strict"]
|
|
959
|
+
};
|
|
960
|
+
var isCommand = (text) => Object.hasOwn(COMMANDS, text);
|
|
961
|
+
var UsageError = class extends Error {
|
|
962
|
+
};
|
|
963
|
+
function isUsageError(error) {
|
|
964
|
+
if (error instanceof UsageError) return true;
|
|
965
|
+
if (typeof error !== "object" || error === null || !("code" in error)) return false;
|
|
966
|
+
return typeof error.code === "string" && error.code.startsWith("ERR_PARSE_ARGS");
|
|
967
|
+
}
|
|
968
|
+
async function main(argv) {
|
|
969
|
+
if (argv.length === 0) {
|
|
970
|
+
process.stderr.write(HELP);
|
|
971
|
+
return EXIT_USAGE;
|
|
972
|
+
}
|
|
973
|
+
const { values, positionals } = parseArgs({
|
|
974
|
+
args: argv,
|
|
975
|
+
options: {
|
|
976
|
+
output: { type: "string", short: "o" },
|
|
977
|
+
notes: { type: "boolean" },
|
|
978
|
+
json: { type: "boolean" },
|
|
979
|
+
name: { type: "string" },
|
|
980
|
+
strict: { type: "boolean" },
|
|
981
|
+
help: { type: "boolean", short: "h" },
|
|
982
|
+
version: { type: "boolean" }
|
|
983
|
+
},
|
|
984
|
+
allowPositionals: true,
|
|
985
|
+
strict: true
|
|
986
|
+
});
|
|
987
|
+
if (values.help === true) {
|
|
988
|
+
process.stdout.write(HELP);
|
|
989
|
+
return EXIT_OK;
|
|
990
|
+
}
|
|
991
|
+
if (values.version === true) {
|
|
992
|
+
process.stdout.write(`${VERSION}
|
|
993
|
+
`);
|
|
994
|
+
return EXIT_OK;
|
|
995
|
+
}
|
|
996
|
+
const [command, file, ...extra] = positionals;
|
|
997
|
+
if (command === void 0) throw new UsageError("missing command");
|
|
998
|
+
if (!isCommand(command)) throw new UsageError(`unknown command \`${command}\``);
|
|
999
|
+
if (file === void 0)
|
|
1000
|
+
throw new UsageError(`${command} needs a <file>, or - for standard input`);
|
|
1001
|
+
if (extra.length > 0) throw new UsageError(`unexpected argument \`${extra[0]}\``);
|
|
1002
|
+
const accepted = COMMANDS[command];
|
|
1003
|
+
for (const option of Object.keys(values)) {
|
|
1004
|
+
if (!accepted.includes(option)) {
|
|
1005
|
+
throw new UsageError(`\`${command}\` does not take \`--${option}\``);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
const source = file === "-" ? await readStdin() : await readSource(file);
|
|
1009
|
+
const { output, diagnostics } = command === "compile" ? compile(source, {
|
|
1010
|
+
target: "linkml",
|
|
1011
|
+
schemaName: values.name ?? defaultSchemaName(file),
|
|
1012
|
+
format: values.json === true ? "json" : "yaml"
|
|
1013
|
+
}) : compile(source, { target: "mermaid", notes: values.notes });
|
|
1014
|
+
const label = file === "-" ? "<stdin>" : file;
|
|
1015
|
+
for (const d of diagnostics) process.stderr.write(`${formatDiagnostic(d, label)}
|
|
1016
|
+
`);
|
|
1017
|
+
if (values.output === void 0) process.stdout.write(output);
|
|
1018
|
+
else await writeOutput(values.output, output);
|
|
1019
|
+
return values.strict === true && diagnostics.length > 0 ? EXIT_STRICT : EXIT_OK;
|
|
1020
|
+
}
|
|
1021
|
+
var defaultSchemaName = (file) => file === "-" ? "sketch" : basename(file, extname(file));
|
|
1022
|
+
async function readSource(file) {
|
|
1023
|
+
try {
|
|
1024
|
+
return await readFile(file, "utf8");
|
|
1025
|
+
} catch (error) {
|
|
1026
|
+
throw new UsageError(`cannot read \`${file}\`: ${reason(error)}`);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
async function readStdin() {
|
|
1030
|
+
try {
|
|
1031
|
+
const chunks = [];
|
|
1032
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
1033
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
throw new UsageError(`cannot read standard input: ${reason(error)}`);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
async function writeOutput(path, text) {
|
|
1039
|
+
try {
|
|
1040
|
+
await writeFile(path, text, "utf8");
|
|
1041
|
+
} catch (error) {
|
|
1042
|
+
throw new UsageError(`cannot write \`${path}\`: ${reason(error)}`);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
function reason(error) {
|
|
1046
|
+
if (error instanceof Error) {
|
|
1047
|
+
const code = error.code;
|
|
1048
|
+
return code ?? error.message;
|
|
1049
|
+
}
|
|
1050
|
+
return String(error);
|
|
1051
|
+
}
|
|
1052
|
+
main(process.argv.slice(2)).then(
|
|
1053
|
+
(code) => {
|
|
1054
|
+
process.exitCode = code;
|
|
1055
|
+
},
|
|
1056
|
+
(error) => {
|
|
1057
|
+
if (isUsageError(error)) {
|
|
1058
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1059
|
+
process.stderr.write(`skiss: ${message}
|
|
1060
|
+
Try 'skiss --help' for more information.
|
|
1061
|
+
`);
|
|
1062
|
+
process.exitCode = EXIT_USAGE;
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
const detail = error instanceof Error && error.stack !== void 0 ? error.stack : String(error);
|
|
1066
|
+
process.stderr.write(`skiss: internal error: ${detail}
|
|
1067
|
+
`);
|
|
1068
|
+
process.exitCode = EXIT_INTERNAL;
|
|
1069
|
+
}
|
|
1070
|
+
);
|
|
1071
|
+
//# sourceMappingURL=cli.js.map
|