@cbortech/cbor 0.27.1 → 0.27.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.ja.md +27 -0
  2. package/README.md +28 -0
  3. package/dist/ast/CborItem.d.ts +17 -2
  4. package/dist/ast/CborTag.d.ts +22 -0
  5. package/dist/ast/CborUnresolvedAppExt.d.ts +9 -0
  6. package/dist/ast/index.cjs +1 -1
  7. package/dist/ast/index.js +2 -2
  8. package/dist/cbor/tagLabels.d.ts +26 -0
  9. package/dist/cddl/eRefScope.d.ts +241 -0
  10. package/dist/cddl/eref.d.ts +64 -0
  11. package/dist/cddl/implicitTags.d.ts +28 -0
  12. package/dist/cddl/index.cjs +2 -2
  13. package/dist/cddl/index.cjs.map +1 -1
  14. package/dist/cddl/index.d.ts +2 -0
  15. package/dist/cddl/index.js +26 -25
  16. package/dist/cddl/index.js.map +1 -1
  17. package/dist/cddl/validator.d.ts +98 -2
  18. package/dist/cdn/index.cjs +1 -1
  19. package/dist/cdn/index.js +1 -1
  20. package/dist/cdn/serialize-utils.d.ts +20 -13
  21. package/dist/extensions/eref.d.ts +135 -0
  22. package/dist/index.cjs +7 -7
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +190 -81
  26. package/dist/index.js.map +1 -1
  27. package/dist/js/fromJS.d.ts +23 -1
  28. package/dist/mapEntries-8zshEr-t.js +7085 -0
  29. package/dist/mapEntries-8zshEr-t.js.map +1 -0
  30. package/dist/mapEntries-DwHPAzCN.cjs +68 -0
  31. package/dist/mapEntries-DwHPAzCN.cjs.map +1 -0
  32. package/dist/schema-7r2xjpMK.js +345 -0
  33. package/dist/schema-7r2xjpMK.js.map +1 -0
  34. package/dist/schema-YiigSWjR.cjs +8 -0
  35. package/dist/schema-YiigSWjR.cjs.map +1 -0
  36. package/dist/{serialize-utils-h-CVB9rg.js → serialize-utils-BQtutOo6.js} +14 -6
  37. package/dist/serialize-utils-BQtutOo6.js.map +1 -0
  38. package/dist/{serialize-utils-DhlW61ZX.cjs → serialize-utils-DQ8T3Mzw.cjs} +5 -5
  39. package/dist/serialize-utils-DQ8T3Mzw.cjs.map +1 -0
  40. package/dist/types.d.ts +153 -4
  41. package/package.json +8 -8
  42. package/dist/mapEntries-CCLaJSaJ.js +0 -4127
  43. package/dist/mapEntries-CCLaJSaJ.js.map +0 -1
  44. package/dist/mapEntries-CZZJScaj.cjs +0 -13
  45. package/dist/mapEntries-CZZJScaj.cjs.map +0 -1
  46. package/dist/schema-DN9inJny.js +0 -1977
  47. package/dist/schema-DN9inJny.js.map +0 -1
  48. package/dist/schema-zsg5yCPK.cjs +0 -63
  49. package/dist/schema-zsg5yCPK.cjs.map +0 -1
  50. package/dist/serialize-utils-DhlW61ZX.cjs.map +0 -1
  51. package/dist/serialize-utils-h-CVB9rg.js.map +0 -1
@@ -1,1977 +0,0 @@
1
- import { F as e, j as t } from "./serialize-utils-h-CVB9rg.js";
2
- import { A as n, C as r, D as i, E as a, F as o, I as s, M as c, N as l, O as u, P as d, T as f, _ as p, b as m, g as h, h as g, j as _, k as ee, u as v, v as te, w as ne, y } from "./mapEntries-CCLaJSaJ.js";
3
- //#region src/cddl/errors.ts
4
- var b = class extends SyntaxError {
5
- offset;
6
- line;
7
- column;
8
- endOffset;
9
- constructor(e, t) {
10
- let n = t?.line === void 0 ? "" : ` at line ${t.line}, column ${t.column}`;
11
- super(`CDDL parse error${n}: ${e}`), this.name = "CddlSyntaxError", this.offset = t?.offset, this.line = t?.line, this.column = t?.column, this.endOffset = t?.endOffset;
12
- }
13
- }, re = class extends Error {
14
- errors;
15
- warnings;
16
- constructor(e, t = []) {
17
- let n = e[0], r = n?.path ? ` at ${n.path}` : "", i = e.length > 1 ? ` (and ${e.length - 1} more)` : "";
18
- super(`CDDL validation failed${r}: ${n?.message ?? "unknown"}${i}`), this.name = "CddlMismatchError", this.errors = e, this.warnings = t;
19
- }
20
- }, ie = class extends Error {
21
- warnings;
22
- constructor(e) {
23
- let t = e[0], n = e.length > 1 ? ` (and ${e.length - 1} more)` : "";
24
- super(`CDDL semantic error: ${t?.message ?? "unknown"}${n}`), this.name = "CddlSemanticError", this.warnings = e;
25
- }
26
- }, ae = new TextEncoder(), x = (e) => e >= "0" && e <= "9", S = (e) => x(e) || e >= "a" && e <= "f" || e >= "A" && e <= "F", C = (e) => e >= "a" && e <= "z" || e >= "A" && e <= "Z" || e === "@" || e === "_" || e === "$", oe = (e) => e >= 160 && e <= 55295 || e >= 57344 && e <= 1114109, se = class {
27
- input;
28
- pos = 0;
29
- line = 1;
30
- col = 1;
31
- comments = [];
32
- lastEndOffset = 0;
33
- constructor(e) {
34
- this.input = e;
35
- }
36
- _eof() {
37
- return this.pos >= this.input.length;
38
- }
39
- _ch() {
40
- return this.input[this.pos] ?? "";
41
- }
42
- _advance() {
43
- let e = this.input[this.pos] ?? "";
44
- return this.pos++, e === "\n" ? (this.line++, this.col = 1) : this.col++, e;
45
- }
46
- _fail(e, t, n) {
47
- throw new b(e, {
48
- offset: this.pos,
49
- line: t ?? this.line,
50
- column: n ?? this.col
51
- });
52
- }
53
- _skipWs() {
54
- for (;;) {
55
- let e = this._ch();
56
- if (e === " " || e === "\n" || e === "\r") {
57
- this._advance();
58
- continue;
59
- }
60
- if (e === " " && this._fail("horizontal tab is not valid whitespace in CDDL (RFC 8610 ABNF allows only space and newline)"), e === ";") {
61
- this._scanComment();
62
- continue;
63
- }
64
- return;
65
- }
66
- }
67
- _scanComment() {
68
- let e = this.pos, t = this.line, n = this.col;
69
- this._advance();
70
- let r = this.pos;
71
- for (; r < this.input.length;) {
72
- let e = this.input[r];
73
- if (e === "\n" || e === "\r") break;
74
- r++;
75
- }
76
- let i = this.input.slice(this.pos, r);
77
- this.col += r - this.pos, this.pos = r, this.comments.push({
78
- text: i,
79
- start: e,
80
- end: r,
81
- line: t,
82
- col: n
83
- });
84
- }
85
- _make(e, t, n, r, i, a) {
86
- let o = {
87
- type: e,
88
- value: t,
89
- raw: this.input.slice(n, this.pos),
90
- line: r,
91
- col: i,
92
- offset: n,
93
- endOffset: this.pos,
94
- ...a
95
- };
96
- return this.lastEndOffset = this.pos, o;
97
- }
98
- consume() {
99
- this._skipWs();
100
- let e = this.pos, t = this.line, n = this.col;
101
- if (this._eof()) return this._make("EOF", "", e, t, n);
102
- let r = this._ch();
103
- if (C(r)) {
104
- let r = this._scanIdText();
105
- return (r === "h" || r === "b64") && this._ch() === "'" ? this._scanBytes(r, e, t, n) : this._make("ID", r, e, t, n);
106
- }
107
- if (x(r) || r === "-") return this._scanNumber(e, t, n);
108
- if (r === "\"") return this._scanText(e, t, n);
109
- if (r === "'") return this._scanBytes("", e, t, n);
110
- if (r === "#") return this._scanHash(e, t, n);
111
- switch (r) {
112
- case "=": return this._advance(), this._ch() === ">" ? (this._advance(), this._make("ARROW", "=>", e, t, n)) : this._make("ASSIGN", "=", e, t, n);
113
- case "/": return this._advance(), this._ch() === "/" ? (this._advance(), this._ch() === "=" ? (this._advance(), this._make("DSLASH_EQ", "//=", e, t, n)) : this._make("DSLASH", "//", e, t, n)) : this._ch() === "=" ? (this._advance(), this._make("SLASH_EQ", "/=", e, t, n)) : this._make("SLASH", "/", e, t, n);
114
- case ".": {
115
- if (this._advance(), this._ch() === ".") return this._advance(), this._ch() === "." ? (this._advance(), this._make("RANGE_EXCL", "...", e, t, n)) : this._make("RANGE_INCL", "..", e, t, n);
116
- C(this._ch()) || this._fail(`expected a control operator name after '.' (e.g. .size), got ${this._eof() ? "end of input" : JSON.stringify(this._ch())}`, t, n);
117
- let r = this._scanIdText();
118
- return this._make("CTLOP", r, e, t, n);
119
- }
120
- case ",": return this._advance(), this._make("COMMA", ",", e, t, n);
121
- case "(": return this._advance(), this._make("LPAREN", "(", e, t, n);
122
- case ")": return this._advance(), this._make("RPAREN", ")", e, t, n);
123
- case "{": return this._advance(), this._make("LBRACE", "{", e, t, n);
124
- case "}": return this._advance(), this._make("RBRACE", "}", e, t, n);
125
- case "[": return this._advance(), this._make("LBRACKET", "[", e, t, n);
126
- case "]": return this._advance(), this._make("RBRACKET", "]", e, t, n);
127
- case "<": return this._advance(), this._make("LT", "<", e, t, n);
128
- case ">": return this._advance(), this._make("GT", ">", e, t, n);
129
- case "~": return this._advance(), this._make("TILDE", "~", e, t, n);
130
- case "&": return this._advance(), this._make("AMP", "&", e, t, n);
131
- case "*": return this._advance(), this._make("STAR", "*", e, t, n);
132
- case "+": return this._advance(), this._make("PLUS", "+", e, t, n);
133
- case "?": return this._advance(), this._make("QUEST", "?", e, t, n);
134
- case ":": return this._advance(), this._make("COLON", ":", e, t, n);
135
- case "^": return this._advance(), this._make("CARET", "^", e, t, n);
136
- default: this._fail(`unexpected character ${JSON.stringify(r)}`);
137
- }
138
- }
139
- _scanIdText() {
140
- let e = this.pos;
141
- for (this._advance();;) {
142
- let e = this._ch();
143
- if (C(e) || x(e)) {
144
- this._advance();
145
- continue;
146
- }
147
- if (e === "-" || e === ".") {
148
- let e = this.pos;
149
- for (; this.input[e] === "-" || this.input[e] === ".";) e++;
150
- let t = this.input[e] ?? "";
151
- if (!(C(t) || x(t))) break;
152
- for (; this.pos <= e;) this._advance();
153
- continue;
154
- }
155
- break;
156
- }
157
- return this.input.slice(e, this.pos);
158
- }
159
- _scanNumber(e, t, n) {
160
- this._ch() === "-" && this._advance(), x(this._ch()) || this._fail("expected a digit after '-'", t, n);
161
- let r = !1;
162
- if (this._ch() === "0" && /[xX]/.test(this.input[this.pos + 1] ?? "")) {
163
- for (this._advance(), this._advance(), S(this._ch()) || this._fail("expected hex digits after 0x", t, n); S(this._ch());) this._advance();
164
- if (this._ch() === "." && S(this.input[this.pos + 1] ?? "") && this.input[this.pos + 1] !== void 0) {
165
- for (this._advance(); S(this._ch());) this._advance();
166
- /[pP]/.test(this._ch()) || this._fail("hexadecimal fraction requires a 'p' exponent (hexfloat)", t, n);
167
- }
168
- if (/[pP]/.test(this._ch())) {
169
- for (this._advance(), (this._ch() === "+" || this._ch() === "-") && this._advance(), x(this._ch()) || this._fail("expected exponent digits after 'p'", t, n); x(this._ch());) this._advance();
170
- r = !0;
171
- }
172
- } else if (this._ch() === "0" && /[bB]/.test(this.input[this.pos + 1] ?? "")) for (this._advance(), this._advance(), /[01]/.test(this._ch()) || this._fail("expected binary digits after 0b", t, n); /[01]/.test(this._ch());) this._advance();
173
- else {
174
- for (this._ch() === "0" && x(this.input[this.pos + 1] ?? "") && this._fail("leading zeros are not allowed in CDDL numbers", t, n); x(this._ch());) this._advance();
175
- if (this._ch() === "." && x(this.input[this.pos + 1] ?? "")) {
176
- for (this._advance(); x(this._ch());) this._advance();
177
- r = !0;
178
- }
179
- if (/[eE]/.test(this._ch())) {
180
- for (this._advance(), (this._ch() === "+" || this._ch() === "-") && this._advance(), x(this._ch()) || this._fail("expected exponent digits after 'e'", t, n); x(this._ch());) this._advance();
181
- r = !0;
182
- }
183
- }
184
- let i = this.input.slice(e, this.pos);
185
- return this._make(r ? "FLOAT" : "INT", i, e, t, n);
186
- }
187
- _scanUintRaw() {
188
- let e = this.pos;
189
- if (x(this._ch()) || this._fail("expected an unsigned integer head-number"), this._ch() === "0" && /[xX]/.test(this.input[this.pos + 1] ?? "")) for (this._advance(), this._advance(), S(this._ch()) || this._fail("expected hex digits after 0x"); S(this._ch());) this._advance();
190
- else if (this._ch() === "0" && /[bB]/.test(this.input[this.pos + 1] ?? "")) for (this._advance(), this._advance(), /[01]/.test(this._ch()) || this._fail("expected binary digits after 0b"); /[01]/.test(this._ch());) this._advance();
191
- else for (this._ch() === "0" && x(this.input[this.pos + 1] ?? "") && this._fail("leading zeros are not allowed in CDDL numbers"); x(this._ch());) this._advance();
192
- return this.input.slice(e, this.pos);
193
- }
194
- _scanHash(e, t, n) {
195
- this._advance();
196
- let r = {};
197
- return x(this._ch()) && (r.hashMajor = this._advance().charCodeAt(0) - 48, this._ch() === "." && (this.input[this.pos + 1] === "<" ? (this._advance(), r.hashAIExpr = !0) : (this._advance(), r.hashAI = BigInt(this._scanUintRaw())))), this._make("HASH", this.input.slice(e, this.pos), e, t, n, r);
198
- }
199
- _scanText(e, t, n) {
200
- let r = this._readStringBody("\"");
201
- return this._make("TSTR", r, e, t, n);
202
- }
203
- _scanBytes(n, r, i, a) {
204
- let o = this._readStringBody("'"), s;
205
- if (n === "") s = ae.encode(o);
206
- else {
207
- let c = ce(o);
208
- try {
209
- s = n === "h" ? e(c) : t(c);
210
- } catch (e) {
211
- throw !(e instanceof SyntaxError) || e instanceof b ? e : new b(e.message, {
212
- offset: r,
213
- line: i,
214
- column: a,
215
- endOffset: this.pos
216
- });
217
- }
218
- }
219
- return this._make("BYTES", o, r, i, a, {
220
- qualifier: n,
221
- bytes: s
222
- });
223
- }
224
- _readStringBody(e) {
225
- let t = e === "'";
226
- this._advance();
227
- let n = "";
228
- for (;;) {
229
- this._eof() && this._fail(`unterminated ${t ? "byte" : "text"} string literal`);
230
- let r = this._ch();
231
- if (r === e) return this._advance(), n;
232
- if (r === "\\") {
233
- let e = this.line, r = this.col;
234
- this._advance();
235
- let i = this._advance();
236
- switch (i) {
237
- case "\"":
238
- n += "\"";
239
- break;
240
- case "/":
241
- n += "/";
242
- break;
243
- case "\\":
244
- n += "\\";
245
- break;
246
- case "b":
247
- n += "\b";
248
- break;
249
- case "f":
250
- n += "\f";
251
- break;
252
- case "n":
253
- n += "\n";
254
- break;
255
- case "r":
256
- n += "\r";
257
- break;
258
- case "t":
259
- n += " ";
260
- break;
261
- case "u":
262
- n += this._readUnicodeEscape(e, r);
263
- break;
264
- case "'":
265
- t || this._fail("\\' is only valid in byte string literals; text strings do not need it", e, r), n += "'";
266
- break;
267
- default: this._fail(`invalid escape sequence \\${i}`, e, r);
268
- }
269
- continue;
270
- }
271
- if (r === "\n" || r === "\r") {
272
- t || this._fail("text string literals cannot span lines (escape the newline as \\n)"), r === "\r" ? (this._advance(), this._ch() !== "\n" && this._fail("bare CR is not allowed in byte string literals"), this._advance(), n += "\r\n") : (this._advance(), n += "\n");
273
- continue;
274
- }
275
- let i = this.input.codePointAt(this.pos);
276
- (i < 32 || i >= 127 && i <= 159) && this._fail(`unescaped control character U+${i.toString(16).padStart(4, "0").toUpperCase()} is not allowed in string literals`), i > 126 && !oe(i) && this._fail(`code point U+${i.toString(16).toUpperCase()} is not allowed in CDDL source`), this._advance(), i > 65535 && this._advance(), n += String.fromCodePoint(i);
277
- }
278
- }
279
- _readUnicodeEscape(e, t) {
280
- if (this._ch() === "{") {
281
- this._advance();
282
- let n = "";
283
- for (; S(this._ch());) n += this._advance();
284
- n.length === 0 && this._fail("\\u{} escape requires at least one hex digit", e, t), this._ch() !== "}" && this._fail("expected '}' to close \\u{...} escape", e, t), this._advance();
285
- let r = parseInt(n, 16);
286
- return r > 1114111 && this._fail(`\\u{${n}} is above the Unicode code point maximum U+10FFFF`, e, t), r >= 55296 && r <= 57343 && this._fail(`\\u{${n}} is a surrogate code point, not a Unicode scalar value`, e, t), String.fromCodePoint(r);
287
- }
288
- let n = () => {
289
- let n = "";
290
- for (let r = 0; r < 4; r++) S(this._ch()) || this._fail("\\u escape requires four hex digits", e, t), n += this._advance();
291
- return parseInt(n, 16);
292
- }, r = n();
293
- if (r >= 55296 && r <= 56319) {
294
- (this._ch() !== "\\" || this.input[this.pos + 1] !== "u") && this._fail("high surrogate \\u escape must be followed by a low surrogate \\u escape", e, t), this._advance(), this._advance();
295
- let i = n();
296
- return (i < 56320 || i > 57343) && this._fail("high surrogate \\u escape must be followed by a low surrogate \\u escape", e, t), String.fromCodePoint(65536 + (r - 55296 << 10) + (i - 56320));
297
- }
298
- return r >= 56320 && r <= 57343 && this._fail("lone low surrogate \\u escape", e, t), String.fromCodePoint(r);
299
- }
300
- };
301
- function ce(e) {
302
- let t = "", n = 0;
303
- for (; n < e.length;) {
304
- let r = e[n];
305
- if (r === " " || r === "\n" || r === "\r") {
306
- n++;
307
- continue;
308
- }
309
- if (r === ";") {
310
- for (; n < e.length && e[n] !== "\n";) n++;
311
- continue;
312
- }
313
- t += r, n++;
314
- }
315
- return t;
316
- }
317
- //#endregion
318
- //#region src/cddl/parser.ts
319
- function w(e) {
320
- let t = new se(e);
321
- return {
322
- rules: new ue(t).parse(),
323
- comments: t.comments
324
- };
325
- }
326
- var le = /* @__PURE__ */ new Set([
327
- "SLASH",
328
- "RANGE_INCL",
329
- "RANGE_EXCL",
330
- "CTLOP",
331
- "ARROW",
332
- "CARET"
333
- ]), ue = class {
334
- t;
335
- buf = [];
336
- constructor(e) {
337
- this.t = e;
338
- }
339
- peek(e = 0) {
340
- for (; this.buf.length <= e;) this.buf.push(this.t.consume());
341
- return this.buf[e];
342
- }
343
- consume() {
344
- return this.buf.shift() ?? this.t.consume();
345
- }
346
- expect(e, t) {
347
- let n = this.peek();
348
- return n.type !== e && this._fail(`expected ${t}`, n), this.consume();
349
- }
350
- _fail(e, t) {
351
- throw new b(`${e}, got ${t.type === "EOF" ? "end of input" : `${JSON.stringify(t.raw)}`}`, {
352
- offset: t.offset,
353
- endOffset: t.endOffset,
354
- line: t.line,
355
- column: t.col
356
- });
357
- }
358
- parse() {
359
- let e = [];
360
- for (; this.peek().type !== "EOF";) e.push(this.parseRule());
361
- return e;
362
- }
363
- parseRule() {
364
- let e = this.expect("ID", "a rule name"), t;
365
- if (this.peek().type === "LT") {
366
- for (this.consume(), t = [this.expect("ID", "a generic parameter name").value]; this.peek().type === "COMMA";) this.consume(), t.push(this.expect("ID", "a generic parameter name").value);
367
- this.expect("GT", "'>' after generic parameters");
368
- }
369
- let n = this.peek(), r;
370
- n.type === "ASSIGN" ? r = "=" : n.type === "SLASH_EQ" ? r = "/=" : n.type === "DSLASH_EQ" ? r = "//=" : this._fail("expected '=', '/=' or '//=' after rule name", n), this.consume();
371
- let i = r === "/=" ? this.parseTypeAsEntry() : this.parseGroupEntry();
372
- return {
373
- kind: "rule",
374
- name: e.value,
375
- ...t ? { generics: t } : {},
376
- assign: r,
377
- body: i,
378
- start: e.offset,
379
- end: i.end
380
- };
381
- }
382
- parseTypeAsEntry() {
383
- let e = this.parseType();
384
- return {
385
- kind: "entry",
386
- value: e,
387
- start: e.start,
388
- end: e.end
389
- };
390
- }
391
- parseGroup(e) {
392
- let t = this.peek().offset, n = [[]], r = !1;
393
- for (;;) {
394
- let t = this.peek();
395
- if (t.type === e) break;
396
- if (t.type === "EOF" && this._fail(`unterminated group; expected ${JSON.stringify(e)}`, t), t.type === "DSLASH") {
397
- this.consume(), n.push([]), r = !1;
398
- continue;
399
- }
400
- n[n.length - 1].push(this.parseGroupEntry()), r = this.peek().type === "COMMA", r && this.consume();
401
- }
402
- let i = this.peek();
403
- return {
404
- kind: "group",
405
- choices: n,
406
- ...r ? { trailingComma: !0 } : {},
407
- start: t,
408
- end: i.offset
409
- };
410
- }
411
- parseGroupEntry() {
412
- let e = this.peek().offset, t = this.tryParseOccur();
413
- if (this.peek().type === "LPAREN") {
414
- let n = this.consume(), r = this.parseGroup("RPAREN"), i = this.consume();
415
- if (le.has(this.peek().type)) {
416
- let a = this.groupAsParenType(r, n, i), o = this.continueType1(a);
417
- return this.finishEntryFromType1(e, t, o);
418
- }
419
- return {
420
- kind: "entry-group",
421
- ...t ? { occur: t } : {},
422
- group: r,
423
- start: e,
424
- end: i.endOffset
425
- };
426
- }
427
- let n = this.peek(0);
428
- if (this.peek(1).type === "COLON" && (n.type === "ID" || this.isValueToken(n))) {
429
- let r;
430
- if (n.type === "ID") {
431
- this.consume();
432
- let e = this.consume();
433
- r = {
434
- kind: "bareword",
435
- key: n.value,
436
- cut: !0,
437
- start: n.offset,
438
- end: e.endOffset
439
- };
440
- } else {
441
- let e = this.parseValue(), t = this.consume();
442
- r = {
443
- kind: "value",
444
- key: e,
445
- cut: !0,
446
- start: e.start,
447
- end: t.endOffset
448
- };
449
- }
450
- let i = this.parseType();
451
- return {
452
- kind: "entry",
453
- ...t ? { occur: t } : {},
454
- memberKey: r,
455
- value: i,
456
- start: e,
457
- end: i.end
458
- };
459
- }
460
- let r = this.parseType1();
461
- return this.finishEntryFromType1(e, t, r);
462
- }
463
- finishEntryFromType1(e, t, n) {
464
- let r;
465
- if (this.peek().type === "CARET" || this.peek().type === "ARROW") {
466
- let e = !1;
467
- this.peek().type === "CARET" && (this.consume(), e = !0);
468
- let t = this.expect("ARROW", "'=>' after member key");
469
- r = {
470
- kind: "type1",
471
- key: n,
472
- cut: e,
473
- start: n.start,
474
- end: t.endOffset
475
- };
476
- }
477
- let i = r ? this.parseType() : this.continueTypeAlternatives(n);
478
- return {
479
- kind: "entry",
480
- ...t ? { occur: t } : {},
481
- ...r ? { memberKey: r } : {},
482
- value: i,
483
- start: e,
484
- end: i.end
485
- };
486
- }
487
- tryParseOccur() {
488
- let e = this.peek(0);
489
- if (e.type === "QUEST") return this.consume(), {
490
- kind: "occur",
491
- marker: "?",
492
- start: e.offset,
493
- end: e.endOffset
494
- };
495
- if (e.type === "PLUS") return this.consume(), {
496
- kind: "occur",
497
- marker: "+",
498
- start: e.offset,
499
- end: e.endOffset
500
- };
501
- if (e.type === "STAR") {
502
- this.consume();
503
- let t, n = e.endOffset, r = this.peek();
504
- return r.type === "INT" && r.offset === e.endOffset && (t = this.occurBound(this.consume()), n = r.endOffset), {
505
- kind: "occur",
506
- marker: "*",
507
- ...t === void 0 ? {} : { max: t },
508
- start: e.offset,
509
- end: n
510
- };
511
- }
512
- if (e.type === "INT" && !e.value.startsWith("-") && this.peek(1).type === "STAR" && this.peek(1).offset === e.endOffset) {
513
- let t = this.occurBound(this.consume()), n = this.consume(), r, i = n.endOffset, a = this.peek();
514
- return a.type === "INT" && a.offset === n.endOffset && (r = this.occurBound(this.consume()), i = a.endOffset), {
515
- kind: "occur",
516
- marker: "*",
517
- min: t,
518
- max: r,
519
- start: e.offset,
520
- end: i
521
- };
522
- }
523
- }
524
- occurBound(e) {
525
- e.value.startsWith("-") && this._fail("occurrence bounds must be unsigned integers", e);
526
- let t = Number(e.value);
527
- return Number.isSafeInteger(t) || this._fail("occurrence bound is too large", e), t;
528
- }
529
- parseType() {
530
- return this.continueTypeAlternatives(this.parseType1());
531
- }
532
- continueTypeAlternatives(e) {
533
- let t = [e];
534
- for (; this.peek().type === "SLASH";) this.consume(), t.push(this.parseType1());
535
- return {
536
- kind: "type",
537
- alternatives: t,
538
- start: e.start,
539
- end: t[t.length - 1].end
540
- };
541
- }
542
- parseType1() {
543
- return this.continueType1(this.parseType2());
544
- }
545
- continueType1(e) {
546
- let t = this.peek(), n;
547
- if (t.type === "RANGE_INCL" || t.type === "RANGE_EXCL" ? n = {
548
- kind: "range",
549
- inclusive: t.type === "RANGE_INCL"
550
- } : t.type === "CTLOP" && (n = {
551
- kind: "ctl",
552
- name: t.value
553
- }), !n) return {
554
- kind: "type1",
555
- target: e,
556
- start: e.start,
557
- end: e.end
558
- };
559
- this.consume();
560
- let r = this.parseType2();
561
- return {
562
- kind: "type1",
563
- target: e,
564
- op: n,
565
- controller: r,
566
- start: e.start,
567
- end: r.end
568
- };
569
- }
570
- parseType2() {
571
- let e = this.peek();
572
- switch (e.type) {
573
- case "INT":
574
- case "FLOAT":
575
- case "TSTR":
576
- case "BYTES": return this.parseValue();
577
- case "ID": return this.consume(), this.parseRefTail(e);
578
- case "LPAREN": {
579
- let e = this.consume(), t = this.parseType(), n = this.expect("RPAREN", "')' to close the parenthesized type");
580
- return {
581
- kind: "paren",
582
- type: t,
583
- start: e.offset,
584
- end: n.endOffset
585
- };
586
- }
587
- case "LBRACE": {
588
- let e = this.consume(), t = this.parseGroup("RBRACE"), n = this.consume();
589
- return {
590
- kind: "map",
591
- group: t,
592
- start: e.offset,
593
- end: n.endOffset
594
- };
595
- }
596
- case "LBRACKET": {
597
- let e = this.consume(), t = this.parseGroup("RBRACKET"), n = this.consume();
598
- return {
599
- kind: "array",
600
- group: t,
601
- start: e.offset,
602
- end: n.endOffset
603
- };
604
- }
605
- case "TILDE": {
606
- let e = this.consume(), t = this.expect("ID", "a type name after '~'"), n = this.parseRefTail(t);
607
- return {
608
- kind: "unwrap",
609
- ref: n,
610
- start: e.offset,
611
- end: n.end
612
- };
613
- }
614
- case "AMP": {
615
- let e = this.consume();
616
- if (this.peek().type === "LPAREN") {
617
- this.consume();
618
- let t = this.parseGroup("RPAREN"), n = this.consume();
619
- return {
620
- kind: "enum",
621
- group: t,
622
- start: e.offset,
623
- end: n.endOffset
624
- };
625
- }
626
- let t = this.expect("ID", "a group name or '(' after '&'"), n = this.parseRefTail(t);
627
- return {
628
- kind: "enum",
629
- group: n,
630
- start: e.offset,
631
- end: n.end
632
- };
633
- }
634
- case "HASH": return this.parseHashType(this.consume());
635
- default: this._fail("expected a type", e);
636
- }
637
- }
638
- parseRefTail(e) {
639
- if (this.peek().type !== "LT") return {
640
- kind: "ref",
641
- name: e.value,
642
- start: e.offset,
643
- end: e.endOffset
644
- };
645
- this.consume();
646
- let t = [this.parseType1()];
647
- for (; this.peek().type === "COMMA";) this.consume(), t.push(this.parseType1());
648
- let n = this.expect("GT", "'>' after generic arguments");
649
- return {
650
- kind: "ref",
651
- name: e.value,
652
- genericArgs: t,
653
- start: e.offset,
654
- end: n.endOffset
655
- };
656
- }
657
- parseHashType(e) {
658
- if (e.hashMajor === void 0) return (e.hashAIExpr || e.hashAI !== void 0) && this._fail("'#' without a major type cannot take a head-number", e), {
659
- kind: "any",
660
- start: e.offset,
661
- end: e.endOffset
662
- };
663
- let t = e.hashMajor, n = e.hashAI, r = e.endOffset;
664
- e.hashAIExpr && (t !== 6 && t !== 7 && this._fail("a <type> head-number is only allowed for #6 and #7 (RFC 9682 §3.2)", e), this.expect("LT", "'<' for the head-number expression"), n = this.parseType(), r = this.expect("GT", "'>' after the head-number expression").endOffset);
665
- let i = e.hashAIExpr ? void 0 : e.raw;
666
- if (t === 6 && this.peek().type === "LPAREN") {
667
- this.consume();
668
- let t = this.parseType(), r = this.expect("RPAREN", "')' to close the tagged type");
669
- return {
670
- kind: "tagged",
671
- ...n === void 0 ? {} : { tag: n },
672
- item: t,
673
- ...i === void 0 ? {} : { raw: i },
674
- start: e.offset,
675
- end: r.endOffset
676
- };
677
- }
678
- return e.hashAIExpr && t === 6 && this._fail("#6.<…> must be followed by '(' type ')'", this.peek()), {
679
- kind: "major",
680
- major: t,
681
- ...n === void 0 ? {} : { ai: n },
682
- ...i === void 0 ? {} : { raw: i },
683
- start: e.offset,
684
- end: r
685
- };
686
- }
687
- isValueToken(e) {
688
- return e.type === "INT" || e.type === "FLOAT" || e.type === "TSTR" || e.type === "BYTES";
689
- }
690
- parseValue() {
691
- let e = this.consume(), t = {
692
- start: e.offset,
693
- end: e.endOffset,
694
- raw: e.raw
695
- };
696
- switch (e.type) {
697
- case "INT": {
698
- let n = e.value.startsWith("-") ? -BigInt(e.value.slice(1)) : BigInt(e.value);
699
- return {
700
- kind: "value",
701
- type: "int",
702
- value: n >= BigInt(-(2 ** 53 - 1)) && n <= BigInt(2 ** 53 - 1) ? Number(n) : n,
703
- ...t
704
- };
705
- }
706
- case "FLOAT": return {
707
- kind: "value",
708
- type: "float",
709
- value: /^-?0[xX]/.test(e.value) ? c(e.value) : parseFloat(e.value),
710
- ...t
711
- };
712
- case "TSTR": return {
713
- kind: "value",
714
- type: "text",
715
- value: e.value,
716
- ...t
717
- };
718
- case "BYTES": return {
719
- kind: "value",
720
- type: "bytes",
721
- value: e.bytes,
722
- qualifier: e.qualifier,
723
- ...t
724
- };
725
- default: this._fail("expected a literal value", e);
726
- }
727
- }
728
- groupAsParenType(e, t, n) {
729
- let r = e.choices.length === 1 && e.choices[0].length === 1 ? e.choices[0][0] : void 0;
730
- return (!r || r.kind !== "entry" || r.occur || r.memberKey || e.trailingComma) && this._fail("this parenthesized group is used as a type (an operator follows), but it contains group syntax", this.peek()), {
731
- kind: "paren",
732
- type: r.value,
733
- start: t.offset,
734
- end: n.endOffset
735
- };
736
- }
737
- }, de = "any = #\n\nuint = #0\nnint = #1\nint = uint / nint\n\nbstr = #2\nbytes = bstr\ntstr = #3\ntext = tstr\n\ntdate = #6.0(tstr)\ntime = #6.1(number)\nnumber = int / float\nbiguint = #6.2(bstr)\nbignint = #6.3(bstr)\nbigint = biguint / bignint\ninteger = int / bigint\nunsigned = uint / biguint\ndecfrac = #6.4([e10: int, m: integer])\nbigfloat = #6.5([e2: int, m: integer])\neb64url = #6.21(any)\neb64legacy = #6.22(any)\neb16 = #6.23(any)\nencoded-cbor = #6.24(bstr)\nuri = #6.32(tstr)\nb64url = #6.33(tstr)\nb64legacy = #6.34(tstr)\nregexp = #6.35(tstr)\nmime-message = #6.36(tstr)\ncbor-any = #6.55799(any)\n\nfloat16 = #7.25\nfloat32 = #7.26\nfloat64 = #7.27\nfloat16-32 = float16 / float32\nfloat32-64 = float32 / float64\nfloat = float16-32 / float64\n\nfalse = #7.20\ntrue = #7.21\nbool = false / true\nnil = #7.22\nnull = nil\nundefined = #7.23\n", T;
738
- function E() {
739
- if (!T) {
740
- T = /* @__PURE__ */ new Map();
741
- for (let e of w(de).rules) T.set(e.name, e);
742
- }
743
- return T;
744
- }
745
- //#endregion
746
- //#region src/cddl/writer.ts
747
- var fe = {
748
- leading: [],
749
- trailing: []
750
- };
751
- function pe(e, t) {
752
- let n = {
753
- unit: t?.indent === void 0 ? null : typeof t.indent == "string" ? t.indent : " ".repeat(t.indent),
754
- notes: null,
755
- endNotes: null,
756
- tail: []
757
- };
758
- if (t?.preserveComments && t.comments !== void 0 && t.comments.length > 0 && t.source !== void 0) {
759
- let r = me(e, t.comments, t.source);
760
- n.notes = r.notes, n.endNotes = r.endNotes, n.tail = r.tail;
761
- }
762
- let r = [], i = !1;
763
- e.forEach((e, t) => {
764
- let { text: a, multiline: o } = _e(e, n);
765
- t > 0 && n.unit !== null && (o || i) && r.push(""), r.push(a), i = o;
766
- });
767
- for (let e of n.tail) r.push(`;${e}`);
768
- return r.length === 0 ? "" : r.join("\n") + "\n";
769
- }
770
- function me(e, t, n) {
771
- let r = [0];
772
- for (let e = 0; e < n.length; e++) n.charCodeAt(e) === 10 && r.push(e + 1);
773
- let i = (e) => {
774
- let t = 0, n = r.length - 1;
775
- for (; t < n;) {
776
- let i = t + n + 1 >> 1;
777
- r[i] <= e ? t = i : n = i - 1;
778
- }
779
- return t + 1;
780
- }, a = [], o = [], s = (e, t, n) => {
781
- o.push({
782
- group: e,
783
- start: t,
784
- end: n
785
- });
786
- for (let t of e.choices) t.forEach(c);
787
- }, c = (e) => {
788
- if (a.push({
789
- node: e,
790
- start: e.start,
791
- end: e.end,
792
- isRule: !1
793
- }), e.kind === "entry-group") {
794
- s(e.group, e.start, e.end);
795
- return;
796
- }
797
- for (let t of e.value.alternatives) l(t);
798
- }, l = (e) => {
799
- u(e.target), e.controller && u(e.controller);
800
- }, u = (e) => {
801
- switch (e.kind) {
802
- case "map":
803
- case "array":
804
- s(e.group, e.start, e.end);
805
- return;
806
- case "paren":
807
- for (let t of e.type.alternatives) l(t);
808
- return;
809
- case "enum":
810
- e.group.kind === "group" && s(e.group, e.start, e.end);
811
- return;
812
- case "tagged":
813
- if (typeof e.tag == "object") for (let t of e.tag.alternatives) l(t);
814
- for (let t of e.item.alternatives) l(t);
815
- return;
816
- default: return;
817
- }
818
- };
819
- for (let t of e) a.push({
820
- node: t,
821
- start: t.start,
822
- end: t.end,
823
- isRule: !0
824
- }), c(t.body);
825
- let d = [...a].sort((e, t) => e.end - t.end || Number(e.isRule) - Number(t.isRule)), f = [...a].sort((e, t) => e.start - t.start || Number(t.isRule) - Number(e.isRule)), p = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Map(), h = [], g = (e) => {
826
- let t = p.get(e);
827
- return t || (t = {
828
- leading: [],
829
- trailing: []
830
- }, p.set(e, t)), t;
831
- };
832
- for (let n of t) {
833
- let t;
834
- for (let e = d.length - 1; e >= 0; e--) {
835
- let r = d[e];
836
- if (!(r.end > n.start)) {
837
- i(Math.max(r.start, r.end - 1)) === n.line && (t = r);
838
- break;
839
- }
840
- }
841
- if (t) {
842
- g(t.node).trailing.push(n.text);
843
- continue;
844
- }
845
- let r;
846
- for (let e of o) e.start <= n.start && n.end <= e.end && (!r || e.start >= r.start) && (r = e);
847
- let a = e.find((e) => e.start <= n.start && n.end <= e.end), s = f.find((e) => e.start >= n.end), c = r !== void 0 && (s === void 0 || s.start > r.end), l = a !== void 0 && (s === void 0 || s.start > a.end);
848
- if (s && !c && !l) g(s.node).leading.push(n.text);
849
- else if (c) {
850
- let e = m.get(r.group) ?? [];
851
- e.push(n.text), m.set(r.group, e);
852
- } else l ? g(a).trailing.push(n.text) : s ? g(s.node).leading.push(n.text) : h.push(n.text);
853
- }
854
- return {
855
- notes: p,
856
- endNotes: m,
857
- tail: h
858
- };
859
- }
860
- var he = (e, t) => e.notes?.get(t) ?? fe, ge = (e) => e.trailing.length === 0 ? "" : " " + e.trailing.map((e) => `;${e}`).join(" ");
861
- function _e(e, t) {
862
- let n = he(t, e), r = he(t, e.body), i = e.generics ? `<${e.generics.join(", ")}>` : "", a = `${e.name}${i} ${e.assign}`, o = ge({
863
- leading: [],
864
- trailing: [...r.trailing, ...n.trailing]
865
- }), s, c = n.leading;
866
- if (r.leading.length > 0 && t.unit !== null) {
867
- let n = t.unit;
868
- s = [
869
- a,
870
- ...r.leading.map((e) => `${n};${e}`),
871
- `${n}${D(e.body, t, 1)}${o}`
872
- ].join("\n");
873
- } else r.leading.length > 0 && (c = [...c, ...r.leading]), s = `${a} ${D(e.body, t, 0)}${o}`;
874
- let l = s.includes("\n");
875
- return c.length === 0 ? {
876
- text: s,
877
- multiline: l
878
- } : {
879
- text: [...c.map((e) => `;${e}`), s].join("\n"),
880
- multiline: l
881
- };
882
- }
883
- function D(e, t, n) {
884
- let r = e.occur ? `${ve(e.occur)} ` : "";
885
- return e.kind === "entry-group" ? `${r}${O(e.group, t, n, "(", ")")}` : `${r}${e.memberKey ? `${ye(e.memberKey, t, n)} ` : ""}${k(e.value, t, n)}`;
886
- }
887
- function ve(e) {
888
- return e.marker === "*" ? `${e.min ?? ""}*${e.max ?? ""}` : e.marker;
889
- }
890
- function ye(e, t, n) {
891
- switch (e.kind) {
892
- case "bareword": return `${e.key}:`;
893
- case "value": return `${e.key.raw}:`;
894
- case "type1": return `${be(e.key, t, n)} ${e.cut ? "^ " : ""}=>`;
895
- }
896
- }
897
- function O(e, t, n, r, i) {
898
- let a = e.choices.reduce((e, t) => e + t.length, 0), o = t.endNotes?.get(e) ?? [], s = o.length > 0 || t.notes !== null && e.choices.some((e) => e.some((e) => t.notes.has(e)));
899
- if (!(t.unit !== null && (a > 1 || e.choices.length > 1 || s))) return `${r}${e.choices.map((e) => e.map((e) => D(e, t, n)).join(", ")).join(" // ") + (e.trailingComma ? "," : "")}${i}`;
900
- let c = t.unit, l = c.repeat(n + 1), u = [r];
901
- e.choices.forEach((r, i) => {
902
- i > 0 && u.push(`${l}//`), r.forEach((a, o) => {
903
- let s = he(t, a);
904
- for (let e of s.leading) u.push(`${l};${e}`);
905
- let c = i !== e.choices.length - 1 || o !== r.length - 1 || e.trailingComma ? "," : "";
906
- u.push(`${l}${D(a, t, n + 1)}${c}${ge(s)}`);
907
- });
908
- });
909
- for (let e of o) u.push(`${l};${e}`);
910
- return u.push(`${c.repeat(n)}${i}`), u.join("\n");
911
- }
912
- function k(e, t, n) {
913
- return e.alternatives.map((e) => be(e, t, n)).join(" / ");
914
- }
915
- function be(e, t, n) {
916
- let r = Se(e.target, t, n);
917
- if (!e.op || !e.controller) return r;
918
- let i = Se(e.controller, t, n);
919
- return e.op.kind === "range" ? `${r}${e.op.inclusive ? ".." : "..."}${i}` : `${r} .${e.op.name} ${i}`;
920
- }
921
- function xe(e, t, n) {
922
- let r = e.genericArgs ? `<${e.genericArgs.map((e) => be(e, t, n)).join(", ")}>` : "";
923
- return `${e.name}${r}`;
924
- }
925
- function Se(e, t, n) {
926
- switch (e.kind) {
927
- case "value": return e.raw;
928
- case "ref": return xe(e, t, n);
929
- case "paren": return `(${k(e.type, t, n)})`;
930
- case "map": return O(e.group, t, n, "{", "}");
931
- case "array": return O(e.group, t, n, "[", "]");
932
- case "unwrap": return `~${xe(e.ref, t, n)}`;
933
- case "enum": return e.group.kind === "ref" ? `&${xe(e.group, t, n)}` : `&${O(e.group, t, n, "(", ")")}`;
934
- case "tagged": return `${e.raw ?? (typeof e.tag == "object" ? `#6.<${k(e.tag, t, n)}>` : `#6${e.tag === void 0 ? "" : `.${e.tag}`}`)}(${k(e.item, t, n)})`;
935
- case "major": return e.raw === void 0 ? typeof e.ai == "object" ? `#${e.major}.<${k(e.ai, t, n)}>` : `#${e.major}${e.ai === void 0 ? "" : `.${e.ai}`}` : e.raw;
936
- case "any": return "#";
937
- }
938
- }
939
- //#endregion
940
- //#region src/cddl/equal.ts
941
- function Ce(e) {
942
- if (e instanceof d || e instanceof l) return e.value;
943
- if (e instanceof te || e instanceof p) return e.bigValue;
944
- }
945
- function A(e) {
946
- if (e instanceof g) return e.value;
947
- if (e instanceof i) return e.chunks.map((e) => e.value).join("");
948
- }
949
- function j(e) {
950
- if (e instanceof ee) return e.value;
951
- if (e instanceof u) {
952
- let t = e.chunks.map((e) => e.value), n = t.reduce((e, t) => e + t.length, 0), r = new Uint8Array(n), i = 0;
953
- for (let e of t) r.set(e, i), i += e.length;
954
- return r;
955
- }
956
- if (e instanceof r) {
957
- let t = e.items.map((e) => e.toCBOR()), n = t.reduce((e, t) => e + t.length, 0), r = new Uint8Array(n), i = 0;
958
- for (let e of t) r.set(e, i), i += e.length;
959
- return r;
960
- }
961
- }
962
- function we(e, t) {
963
- if (e.length !== t.length) return !1;
964
- for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
965
- return !0;
966
- }
967
- //#endregion
968
- //#region src/cddl/controls.ts
969
- var Te = new TextEncoder(), Ee = new TextDecoder("utf-8", { fatal: !0 }), M = (e) => ({
970
- kind: "value",
971
- raw: "",
972
- start: 0,
973
- end: 0,
974
- ...e
975
- }), De = (e) => {
976
- let t = Ce(e);
977
- if (t !== void 0) return t;
978
- if (e instanceof _) return e.value;
979
- }, N = (e) => e.type === "int" || e.type === "float" ? e.value : void 0, Oe = (e, t) => {
980
- if (typeof e == "bigint" && typeof t == "bigint") return e < t ? -1 : +(e > t);
981
- let n = Number(e), r = Number(t);
982
- return n < r ? -1 : n > r ? 1 : n === r ? 0 : NaN;
983
- }, ke = (e, t, n, r, i, a) => {
984
- if (!e.matchType2(t, n, i)) return !1;
985
- let o = (n) => e.matchType2(e.uint(n), r, i) ? !0 : e.fail(i, t, a, `size ${n} does not match the .size constraint`), s = A(t);
986
- if (s !== void 0) return o(Te.encode(s).length);
987
- let c = j(t);
988
- if (c !== void 0) return o(c.length);
989
- if (t instanceof d) {
990
- let n = 0;
991
- for (let e = t.value; e > 0n; e >>= 8n) n++;
992
- let o = e.existsIntGE(r, BigInt(n));
993
- if (o !== void 0) return o ? !0 : e.fail(i, t, a, `${t.value} does not fit the .size constraint`);
994
- e.warnOnce(".size controller could not be analyzed; searching a bounded window of byte counts", a);
995
- for (let t = n; t <= n + 64; t++) if (e.matchType2(e.uint(t), r, i)) return !0;
996
- return e.fail(i, t, a, `${t.value} does not fit the .size constraint`);
997
- }
998
- return e.fail(i, t, a, ".size applies to strings and unsigned integers");
999
- }, Ae = (e, t, n, r, i, a) => {
1000
- if (!e.matchType2(t, n, i)) return !1;
1001
- let o = [], s = j(t);
1002
- if (s !== void 0) for (let e = 0; e < s.length; e++) for (let t = 0; t < 8; t++) s[e] & 1 << t && o.push(e * 8 + t);
1003
- else if (t instanceof d) {
1004
- let e = t.value;
1005
- for (let t = 0; e > 0n; t++, e >>= 1n) e & 1n && o.push(t);
1006
- } else return e.fail(i, t, a, ".bits applies to byte strings and unsigned integers");
1007
- for (let n of o) if (!e.matchType2(e.uint(n), r, i)) return e.fail(i, t, a, `bit ${n} is set but not allowed by .bits`);
1008
- return !0;
1009
- }, je = /* @__PURE__ */ new Map(), Me = (e, t, n, r, i, a) => {
1010
- if (!e.matchType2(t, n, i)) return !1;
1011
- let o = A(t);
1012
- if (o === void 0) return e.fail(i, t, a, ".regexp applies to text strings");
1013
- let s = e.resolveValue(r);
1014
- if (!s || s.type !== "text") return e.warnOnce(".regexp controller does not resolve to a text string; not checked", a), !0;
1015
- let c = je.get(s.value);
1016
- if (c === void 0) {
1017
- try {
1018
- c = RegExp(`^(?:${s.value})$`, "u");
1019
- } catch {
1020
- c = null;
1021
- }
1022
- je.set(s.value, c);
1023
- }
1024
- return c === null ? (e.warnOnce(`.regexp pattern is not a valid regular expression here: ${s.value}`, a), !0) : c.test(o) ? !0 : e.fail(i, t, a, `text does not match .regexp ${JSON.stringify(s.value)}`);
1025
- }, Ne = (e) => (t, n, r, i, a, o) => {
1026
- if (!t.matchType2(n, r, a)) return !1;
1027
- let s = j(n);
1028
- if (s === void 0) return t.fail(a, n, o, `.cbor${e ? "seq" : ""} applies to byte strings`);
1029
- let c;
1030
- try {
1031
- if (e) {
1032
- let e = new Uint8Array(s.length + 2);
1033
- e[0] = 159, e.set(s, 1), e[e.length - 1] = 255, c = v(e);
1034
- } else c = v(s);
1035
- } catch (e) {
1036
- return t.fail(a, n, o, `byte string does not contain valid CBOR: ${e instanceof Error ? e.message : String(e)}`);
1037
- }
1038
- return t.matchEmbedded(c, i, a) ? !0 : t.fail(a, n, o, `embedded CBOR does not match the .cbor${e ? "seq" : ""} type`);
1039
- }, Pe = (e, t, n, r, i, a) => e.matchType2(t, n, i) ? e.matchType2(t, r, i) ? !0 : e.fail(i, t, a, "value does not match the .within/.and constraint") : !1, P = (e) => (t, n, r, i, a, o) => {
1040
- if (!t.matchType2(n, r, a)) return !1;
1041
- let s = t.resolveValue(i), c = s && N(s);
1042
- if (c === void 0) return t.warnOnce(`.${e} controller does not resolve to a number; not checked`, o), !0;
1043
- let l = De(n);
1044
- if (l === void 0) return t.fail(a, n, o, `.${e} applies to numbers`);
1045
- let u = Oe(l, c);
1046
- return (e === "lt" ? u < 0 : e === "le" ? u <= 0 : e === "gt" ? u > 0 : u >= 0) || t.fail(a, n, o, `${l} does not satisfy .${e} ${c}`);
1047
- }, Fe = (e, t, n, r, i, a) => {
1048
- if (!e.matchType2(t, n, i)) return !1;
1049
- let o = e.resolveValue(r);
1050
- return o ? e.matchesLiteral(t, o) ? !0 : e.fail(i, t, a, `value does not equal ${o.raw || ".eq controller"}`) : (e.warnOnce(".eq controller does not resolve to a literal value; not checked", a), !0);
1051
- }, Ie = (e, t, n, r, i, a) => {
1052
- if (!e.matchType2(t, n, i)) return !1;
1053
- let o = e.resolveValue(r);
1054
- return o ? !e.matchesLiteral(t, o) || e.fail(i, t, a, `value must not equal ${o.raw || ".ne controller"}`) : (e.warnOnce(".ne controller does not resolve to a literal value; not checked", a), !0);
1055
- }, Le = (e, t, n, r, i, a) => {
1056
- if (!e.matchType2(t, n, i)) return !1;
1057
- let o = e.resolveValue(r);
1058
- return o ? !e.matchesLiteral(t, o) || e.fail(i, t, a, `value equals the default ${o.raw || "value"} (implied .ne, RFC 8610 §3.8.6)`) : (e.warnOnce(".default controller does not resolve to a literal value; the implied .ne is not checked", a), !0);
1059
- }, Re = (e, t, n, r, i, a) => {
1060
- let o = e.resolveValue(n), s = e.resolveValue(r), c = o && N(o), l = s && N(s);
1061
- if (o === void 0 || c === void 0 || l === void 0) return e.warnOnce(".plus operands do not resolve to numbers; not checked", a), e.fail(i, t, a, ".plus could not be computed");
1062
- if (o.type === "float") {
1063
- let n = Number(c) + Number(l);
1064
- return e.matchesLiteral(t, M({
1065
- type: "float",
1066
- value: n
1067
- })) ? !0 : e.fail(i, t, a, `expected ${n} (.plus)`);
1068
- }
1069
- if (typeof l == "number" && !Number.isFinite(l)) return e.fail(i, t, a, ".plus controller is not finite");
1070
- let u = (typeof c == "bigint" ? c : BigInt(c)) + (typeof l == "bigint" ? l : BigInt(Math.floor(l)));
1071
- return e.matchesLiteral(t, M({
1072
- type: "int",
1073
- value: u
1074
- })) ? !0 : e.fail(i, t, a, `expected ${u} (.plus)`);
1075
- }, ze = (e, t, n, r, i, a) => {
1076
- let o = e.resolveValue(n), s = e.resolveValue(r), c = (e) => e.type === "text" ? Te.encode(e.value) : e.type === "bytes" ? e.value : void 0, l = o && c(o), u = s && c(s);
1077
- if (!o || l === void 0 || u === void 0) return e.warnOnce(".cat operands do not resolve to strings; not checked", a), e.fail(i, t, a, ".cat could not be computed");
1078
- let d = new Uint8Array(l.length + u.length);
1079
- if (d.set(l, 0), d.set(u, l.length), o.type === "text") {
1080
- let n;
1081
- try {
1082
- n = Ee.decode(d);
1083
- } catch {
1084
- return e.fail(i, t, a, ".cat result is not valid UTF-8");
1085
- }
1086
- return e.matchesLiteral(t, M({
1087
- type: "text",
1088
- value: n
1089
- })) ? !0 : e.fail(i, t, a, `expected ${JSON.stringify(n)} (.cat)`);
1090
- }
1091
- return e.matchesLiteral(t, M({
1092
- type: "bytes",
1093
- value: d,
1094
- qualifier: "h"
1095
- })) ? !0 : e.fail(i, t, a, "byte string does not equal the .cat result");
1096
- }, Be = (e) => {
1097
- for (; e.kind === "paren" && e.type.alternatives.length === 1 && !e.type.alternatives[0].op;) e = e.type.alternatives[0].target;
1098
- return e;
1099
- }, Ve = (e, t) => {
1100
- let n = Be(t);
1101
- if (n.kind === "array") {
1102
- let e = n.group.choices[0]?.[0];
1103
- if (!e || e.kind !== "entry" || e.value.alternatives.length !== 1 || e.value.alternatives[0].op) return;
1104
- n = e.value.alternatives[0].target;
1105
- }
1106
- let r = e.resolveValue(n);
1107
- return r?.type === "text" ? r.value : void 0;
1108
- }, He = {
1109
- size: ke,
1110
- bits: Ae,
1111
- regexp: Me,
1112
- cbor: Ne(!1),
1113
- cborseq: Ne(!0),
1114
- within: Pe,
1115
- and: Pe,
1116
- lt: P("lt"),
1117
- le: P("le"),
1118
- gt: P("gt"),
1119
- ge: P("ge"),
1120
- eq: Fe,
1121
- ne: Ie,
1122
- default: Le,
1123
- plus: Re,
1124
- cat: ze,
1125
- feature: (e, t, n, r, i, a) => {
1126
- let o = Ve(e, r);
1127
- return o === void 0 ? (e.warnOnce(".feature controller does not resolve to a feature name", a), e.fail(i, t, a, "unresolvable .feature")) : e.features.has(o) ? e.matchType2(t, n, i) : (e.warnOnce(`feature "${o}" is not enabled (pass it in options.features to accept it)`, a), e.fail(i, t, a, `feature "${o}" is not enabled`));
1128
- }
1129
- };
1130
- function Ue(e) {
1131
- return Object.prototype.hasOwnProperty.call(He, e) ? He[e] : void 0;
1132
- }
1133
- //#endregion
1134
- //#region src/cddl/validator.ts
1135
- var F = class extends Error {}, We = class {
1136
- schema;
1137
- maxDepth;
1138
- maxSteps;
1139
- features;
1140
- steps = 0;
1141
- ruleName;
1142
- embeddedDepth = 0;
1143
- preludeDepth = 0;
1144
- preludeRef;
1145
- warnings = [];
1146
- warned = /* @__PURE__ */ new Set();
1147
- best;
1148
- constructor(e, t, n, r) {
1149
- this.schema = e, this.maxDepth = t, this.maxSteps = n, this.features = r;
1150
- }
1151
- step() {
1152
- if (++this.steps > this.maxSteps) throw new F(`validation aborted: step budget of ${this.maxSteps} exceeded (pathological backtracking?)`);
1153
- }
1154
- checkDepth(e) {
1155
- if (e > this.maxDepth) throw new F(`validation aborted: recursion depth ${this.maxDepth} exceeded (cyclic rules without progress?)`);
1156
- }
1157
- warnOnce(e, t) {
1158
- let n = `${e}@${t?.start ?? -1}`;
1159
- this.warned.has(n) || (this.warned.add(n), this.warnings.push({
1160
- message: e,
1161
- ...t ? {
1162
- schemaStart: t.start,
1163
- schemaEnd: t.end
1164
- } : {}
1165
- }));
1166
- }
1167
- fail(e, t, n, r) {
1168
- let i = this.embeddedDepth > 0, a = i ? -1 : t?.start ?? -1;
1169
- if (!this.best || a > this.best.startKey || a === this.best.startKey && e.length >= this.best.depth) {
1170
- let o = this.preludeDepth > 0 ? this.preludeRef : n;
1171
- this.best = {
1172
- depth: e.length,
1173
- startKey: a,
1174
- message: r,
1175
- path: e.length === 0 ? "/" : "/" + e.join("/"),
1176
- ...!i && t?.start !== void 0 ? {
1177
- start: t.start,
1178
- end: t.end
1179
- } : {},
1180
- ...this.ruleName === void 0 ? {} : { ruleName: this.ruleName },
1181
- ...o ? {
1182
- schemaStart: o.start,
1183
- schemaEnd: o.end
1184
- } : {}
1185
- };
1186
- }
1187
- return !1;
1188
- }
1189
- };
1190
- function Ge(e, t, n) {
1191
- let r = n?.rule ?? e.root?.name;
1192
- if (r === void 0) return {
1193
- valid: !1,
1194
- errors: [{
1195
- message: "schema has no rules (no root to validate against)",
1196
- path: "/"
1197
- }]
1198
- };
1199
- let i = new We(e, n?.maxDepth ?? 256, n?.maxSteps ?? 1e6, new Set(n?.features ?? [])), a = I(i, r)?.[0]?.generics?.length ?? 0;
1200
- if (a > 0) return {
1201
- valid: !1,
1202
- errors: [{
1203
- message: `rule '${r}' takes ${a} generic argument${a === 1 ? "" : "s"} and cannot be used as a validation target directly; reference it with concrete arguments from another rule instead`,
1204
- path: "/"
1205
- }]
1206
- };
1207
- let o;
1208
- try {
1209
- o = Je(t, r, void 0, [], i, 0);
1210
- } catch (e) {
1211
- if (!(e instanceof F)) throw e;
1212
- return {
1213
- valid: !1,
1214
- errors: [{
1215
- message: e.message,
1216
- path: "/"
1217
- }],
1218
- ...i.warnings.length ? { warnings: i.warnings } : {}
1219
- };
1220
- }
1221
- return o ? {
1222
- valid: !0,
1223
- errors: [],
1224
- ...i.warnings.length ? { warnings: i.warnings } : {}
1225
- } : {
1226
- valid: !1,
1227
- errors: i.best ? [(({ depth: e, startKey: t, ...n }) => n)(i.best)] : [{
1228
- message: `value does not match rule '${r}'`,
1229
- path: "/",
1230
- ...t.start === void 0 ? {} : {
1231
- start: t.start,
1232
- end: t.end
1233
- }
1234
- }],
1235
- ...i.warnings.length ? { warnings: i.warnings } : {}
1236
- };
1237
- }
1238
- function I(e, t) {
1239
- let n = e.schema.rules.get(t);
1240
- if (n) return n;
1241
- let r = E().get(t);
1242
- return r ? [r] : void 0;
1243
- }
1244
- function L(e) {
1245
- return e.kind === "entry" && !e.occur && !e.memberKey;
1246
- }
1247
- function Ke(e) {
1248
- return e.kind === "entry-group" && !e.occur ? e.group.choices : [[e]];
1249
- }
1250
- function qe(e) {
1251
- return e.some((e) => !L(e.body));
1252
- }
1253
- function R(e, t, n) {
1254
- let r = e.generics;
1255
- if (!r || !t) return;
1256
- let i = /* @__PURE__ */ new Map();
1257
- for (let e = 0; e < r.length && e < t.length; e++) i.set(r[e], {
1258
- type1: t[e],
1259
- env: n
1260
- });
1261
- return i;
1262
- }
1263
- function Je(e, t, n, r, i, a, o, s) {
1264
- let c = I(i, t);
1265
- if (!c) return i.fail(r, e, o, t.startsWith("$") ? `socket '${t}' has no definitions, so nothing matches it` : `'${t}' is not defined`);
1266
- i.checkDepth(a);
1267
- let l = i.ruleName;
1268
- i.ruleName = t;
1269
- let u = !i.schema.rules.has(t);
1270
- u && ++i.preludeDepth === 1 && (i.preludeRef = o);
1271
- try {
1272
- for (let o of c) {
1273
- let c = R(o, s, n);
1274
- if (L(o.body)) {
1275
- if (z(e, o.body.value, c, r, i, a + 1)) return !0;
1276
- continue;
1277
- }
1278
- let l = Ke(o.body), u = !0;
1279
- for (let e of l) if (!(e.length === 1 && L(e[0]))) {
1280
- u = !1;
1281
- break;
1282
- }
1283
- if (!u) {
1284
- i.fail(r, e, o.body, `group rule '${t}' cannot be used as a type`);
1285
- continue;
1286
- }
1287
- for (let t of l) {
1288
- let n = t[0];
1289
- if (z(e, n.value, c, r, i, a + 1)) return !0;
1290
- }
1291
- }
1292
- return !1;
1293
- } finally {
1294
- i.ruleName = l, u && --i.preludeDepth === 0 && (i.preludeRef = void 0);
1295
- }
1296
- }
1297
- function Ye(e) {
1298
- for (; e instanceof m;) e = e.inner;
1299
- return e;
1300
- }
1301
- function z(e, t, n, r, i, a) {
1302
- for (let o of t.alternatives) if (B(e, o, n, r, i, a)) return !0;
1303
- return !1;
1304
- }
1305
- function B(e, t, n, r, i, a) {
1306
- if (i.step(), e = Ye(e), !t.op || !t.controller) return V(e, t.target, n, r, i, a);
1307
- if (t.op.kind === "range") return rt(e, t, n, r, i, a);
1308
- let o = Ue(t.op.name), s = Ct(n, i, a);
1309
- return o ? o(s, e, t.target, t.controller, r, t) : (i.warnOnce(`control operator .${t.op.name} is not supported; matching the target only`, t), V(e, t.target, n, r, i, a));
1310
- }
1311
- function V(e, t, r, i, o, s) {
1312
- if (e = Ye(e), e instanceof y) return !0;
1313
- switch (t.kind) {
1314
- case "any": return !0;
1315
- case "value": return U(e, t) ? !0 : o.fail(i, e, t, `expected the value ${t.raw}`);
1316
- case "ref": {
1317
- let n = r?.get(t.name);
1318
- return n && !t.genericArgs ? B(e, n.type1, n.env, i, o, s + 1) : Je(e, t.name, r, i, o, s + 1, t, t.genericArgs);
1319
- }
1320
- case "paren": return z(e, t.type, r, i, o, s);
1321
- case "map": return e instanceof f ? _t(e, t.group, r, i, o, s + 1) : o.fail(i, e, t, "expected a map");
1322
- case "array": return e instanceof a ? pt(e, t.group, r, i, o, s + 1) : o.fail(i, e, t, "expected an array");
1323
- case "tagged":
1324
- if (!(e instanceof n)) return o.fail(i, e, t, "expected a tagged item");
1325
- if (typeof t.tag == "bigint") {
1326
- if (e.tag !== t.tag) return o.fail(i, e, t, `expected tag ${t.tag}, got ${e.tag}`);
1327
- } else if (t.tag !== void 0 && !z(new d(e.tag), t.tag, r, i, o, s + 1)) return o.fail(i, e, t, `tag number ${e.tag} does not match the head type`);
1328
- return z(e.content, t.item, r, i, o, s + 1);
1329
- case "major": return at(e, t, r, i, o, s);
1330
- case "unwrap": {
1331
- let n = dt(t.ref, r, o, 0);
1332
- return n ? z(e, n.type, n.env, i, o, s + 1) : (o.warnOnce(`~${t.ref.name} is used outside of an array/map group context`, t), o.fail(i, e, t, `~${t.ref.name} cannot match here`));
1333
- }
1334
- case "enum": {
1335
- let n = t.group.kind === "ref" ? ct(t.group, r, o) : X(t.group, r);
1336
- return n ? (o.checkDepth(s), H(e, n, i, o, s + 1) ? !0 : o.fail(i, e, t, "no enumeration value matches")) : o.fail(i, e, t, "'&' target does not resolve to a group");
1337
- }
1338
- }
1339
- }
1340
- function H(e, t, n, r, i) {
1341
- for (let { entries: a, env: o } of t) for (let t of a) {
1342
- if (r.step(), t.kind === "entry-group") {
1343
- if (H(e, X(t.group, o), n, r, i + 1)) return !0;
1344
- continue;
1345
- }
1346
- let a = lt(t, o, r);
1347
- if (a) {
1348
- if (r.checkDepth(i), H(e, a, n, r, i + 1)) return !0;
1349
- continue;
1350
- }
1351
- if (z(e, t.value, o, n, r, i + 1)) return !0;
1352
- }
1353
- return !1;
1354
- }
1355
- function U(e, t) {
1356
- switch (t.type) {
1357
- case "int": return Ce(e) === BigInt(t.value);
1358
- case "float": return e instanceof _ && (e.value === t.value || Number.isNaN(e.value) && Number.isNaN(t.value));
1359
- case "text": return A(e) === t.value;
1360
- case "bytes": {
1361
- let n = j(e);
1362
- return n !== void 0 && we(n, t.value);
1363
- }
1364
- }
1365
- }
1366
- function W(e, t, n, r = 0) {
1367
- if (r > 32) return;
1368
- if (e.kind === "value") return e;
1369
- if (e.kind === "paren") {
1370
- let i = e.type.alternatives.length === 1 ? e.type.alternatives[0] : void 0;
1371
- return !i || i.op ? void 0 : W(i.target, t, n, r + 1);
1372
- }
1373
- if (e.kind !== "ref") return;
1374
- let i = t?.get(e.name);
1375
- if (i && !e.genericArgs) return i.type1.op ? void 0 : W(i.type1.target, i.env, n, r + 1);
1376
- let a = I(n, e.name);
1377
- if (!a || a.length !== 1) return;
1378
- let o = a[0].body;
1379
- if (!L(o) || o.value.alternatives.length !== 1) return;
1380
- let s = o.value.alternatives[0];
1381
- if (s.op) return;
1382
- let c = R(a[0], e.genericArgs, t);
1383
- return W(s.target, c, n, r + 1);
1384
- }
1385
- function Xe(e, t, n, r, i = 0) {
1386
- return Ze(e, t, void 0, n, r, i);
1387
- }
1388
- function Ze(e, t, n, r, i, a = 0) {
1389
- let o = G(e, t, n, r, i, a);
1390
- return o === void 0 ? void 0 : o !== null;
1391
- }
1392
- function G(e, t, n, r, i, a = 0) {
1393
- if (n !== void 0 && t > n) return null;
1394
- if (!(a > 32)) switch (e.kind) {
1395
- case "value":
1396
- if (e.type === "int") {
1397
- let r = BigInt(e.value);
1398
- return r >= t && (n === void 0 || r <= n) ? r : null;
1399
- }
1400
- return null;
1401
- case "any": {
1402
- let e = t > 0n ? t : 0n;
1403
- return n === void 0 || e <= n ? e : null;
1404
- }
1405
- case "major":
1406
- if (e.ai !== void 0) return;
1407
- if (e.major === 0) {
1408
- let e = t > 0n ? t : 0n;
1409
- return e <= 18446744073709551615n && (n === void 0 || e <= n) ? e : null;
1410
- }
1411
- if (e.major === 1) {
1412
- let e = t > -18446744073709551616n ? t : -18446744073709551616n;
1413
- return e <= -1n && (n === void 0 || e <= n) ? e : null;
1414
- }
1415
- return null;
1416
- case "paren": return K(e.type, t, n, r, i, a + 1);
1417
- case "ref": {
1418
- let o = r?.get(e.name);
1419
- if (o && !e.genericArgs) return Qe(o.type1, t, n, o.env, i, a + 1);
1420
- let s = I(i, e.name);
1421
- if (!s) return;
1422
- let c = !1, l = null;
1423
- for (let o of s) {
1424
- if (!L(o.body)) {
1425
- c = !0;
1426
- continue;
1427
- }
1428
- let s = R(o, e.genericArgs, r), u = K(o.body.value, t, n, s, i, a + 1);
1429
- typeof u == "bigint" && (l === null || u < l) && (l = u), u === void 0 && (c = !0);
1430
- }
1431
- return l ?? (c ? void 0 : null);
1432
- }
1433
- case "enum": {
1434
- let o = e.group.kind === "ref" ? ct(e.group, r, i) : X(e.group, r);
1435
- if (!o) return;
1436
- let s = !1, c = null;
1437
- for (let { entries: e, env: r } of o) for (let o of e) {
1438
- if (o.kind !== "entry") {
1439
- s = !0;
1440
- continue;
1441
- }
1442
- let e = K(o.value, t, n, r, i, a + 1);
1443
- typeof e == "bigint" && (c === null || e < c) && (c = e), e === void 0 && (s = !0);
1444
- }
1445
- return c ?? (s ? void 0 : null);
1446
- }
1447
- default: return;
1448
- }
1449
- }
1450
- function K(e, t, n, r, i, a) {
1451
- let o = !1, s = null;
1452
- for (let c of e.alternatives) {
1453
- let e = Qe(c, t, n, r, i, a);
1454
- typeof e == "bigint" && (s === null || e < s) && (s = e), e === void 0 && (o = !0);
1455
- }
1456
- return s ?? (o ? void 0 : null);
1457
- }
1458
- function Qe(e, t, n, r, i, a) {
1459
- if (!e.op) return G(e.target, t, n, r, i, a);
1460
- if (e.op.kind === "ctl") {
1461
- if (e.op.name === "bits") return et(e.target, e.controller, t, n, r, i, a + 1);
1462
- if (e.op.name === "size") {
1463
- let o = G(e.target, t > 0n ? t : 0n, n, r, i, a + 1);
1464
- if (o == null) return o;
1465
- let s = Xe(e.controller, BigInt(nt(o)), r, i, a + 1);
1466
- return s === void 0 ? void 0 : s ? o : null;
1467
- }
1468
- if (e.op.name === "feature") {
1469
- let o = Ve(Ct(r, i, a), e.controller);
1470
- return o === void 0 ? void 0 : i.features.has(o) ? G(e.target, t, n, r, i, a + 1) : (i.warnOnce(`feature "${o}" is not enabled (pass it in options.features to accept it)`, e), null);
1471
- }
1472
- if (e.op.name === "and" || e.op.name === "within") return $e(e.target, e.controller, t, n, r, i, a + 1);
1473
- if (e.op.name === "plus") {
1474
- let a = W(e.target, r, i), o = W(e.controller, r, i);
1475
- if (!a || !o || a.type === "text" || a.type === "bytes" || o.type === "text" || o.type === "bytes") return;
1476
- if (a.type === "float") return null;
1477
- let s = o.type === "int" ? BigInt(a.value) + BigInt(o.value) : BigInt(Math.floor(Number(a.value) + o.value));
1478
- return s >= t && (n === void 0 || s <= n) ? s : null;
1479
- }
1480
- let o = W(e.controller, r, i);
1481
- if (!o || o.type !== "int") return;
1482
- let s = BigInt(o.value);
1483
- switch (e.op.name) {
1484
- case "eq": return G(e.target, s > t ? s : t, n === void 0 || s < n ? s : n, r, i, a + 1);
1485
- case "ne":
1486
- case "default": {
1487
- let o = G(e.target, t, n === void 0 || s - 1n < n ? s - 1n : n, r, i, a + 1);
1488
- if (typeof o == "bigint") return o;
1489
- let c = G(e.target, s + 1n > t ? s + 1n : t, n, r, i, a + 1);
1490
- return typeof c == "bigint" ? c : o === void 0 || c === void 0 ? void 0 : null;
1491
- }
1492
- case "lt":
1493
- case "le": {
1494
- let o = s - (e.op.name === "lt" ? 1n : 0n);
1495
- return G(e.target, t, n === void 0 || o < n ? o : n, r, i, a + 1);
1496
- }
1497
- case "gt":
1498
- case "ge": {
1499
- let o = s + (e.op.name === "gt" ? 1n : 0n);
1500
- return G(e.target, o > t ? o : t, n, r, i, a + 1);
1501
- }
1502
- default: return;
1503
- }
1504
- }
1505
- let o = W(e.target, r, i), s = W(e.controller, r, i);
1506
- if (!o || !s || o.type !== "int" || s.type !== "int") return;
1507
- let c = BigInt(o.value), l = BigInt(s.value) - (e.op.inclusive ? 0n : 1n), u = c > t ? c : t;
1508
- return u <= (n === void 0 || l < n ? l : n) ? u : null;
1509
- }
1510
- function $e(e, t, n, r, i, a, o) {
1511
- let s = n;
1512
- for (let n = 0; n < 256; n++) {
1513
- let n = G(e, s, r, i, a, o + 1), c = G(t, s, r, i, a, o + 1);
1514
- if (n === null || c === null) return null;
1515
- if (n === void 0 || c === void 0) return;
1516
- if (n === c) return n;
1517
- s = n > c ? n : c;
1518
- }
1519
- }
1520
- function et(e, t, n, r, i, a, o) {
1521
- let s = 0n;
1522
- for (let e = 0; e < 64; e++) {
1523
- let n = G(t, BigInt(e), BigInt(e), i, a, o + 1);
1524
- if (n === void 0) return;
1525
- n !== null && (s |= 1n << BigInt(e));
1526
- }
1527
- let c = n > 0n ? n : 0n, l = r === void 0 || r > 18446744073709551615n ? 18446744073709551615n : r;
1528
- for (let t = 0; t < 256; t++) {
1529
- let t = G(e, c, l, i, a, o + 1), n = tt(c, l, s);
1530
- if (t === void 0) return;
1531
- if (t === null || n === null) return null;
1532
- if (t === n) return t;
1533
- c = t > n ? t : n;
1534
- }
1535
- }
1536
- function tt(e, t, n) {
1537
- if (e < 0n && (e = 0n), e > t) return null;
1538
- let r = (i, a, o) => {
1539
- if (i < 0) return o <= t ? o : null;
1540
- let s = Number(e >> BigInt(i) & 1n);
1541
- for (let e = 0; e <= 1; e++) {
1542
- if (e === 1 && (n & 1n << BigInt(i)) == 0n || !a && e < s) continue;
1543
- let t = r(i - 1, a || e > s, e === 1 ? o | 1n << BigInt(i) : o);
1544
- if (t !== null) return t;
1545
- }
1546
- return null;
1547
- };
1548
- return r(63, !1, 0n);
1549
- }
1550
- function nt(e) {
1551
- let t = 0;
1552
- for (let n = e; n > 0n; n >>= 8n) t++;
1553
- return t;
1554
- }
1555
- function rt(e, t, n, r, i, a) {
1556
- let o = t.op, s = W(t.target, n, i), c = W(t.controller, n, i);
1557
- if (!s || !c) return i.warnOnce("range endpoints do not resolve to literal values", t), i.fail(r, e, t, "unresolvable range");
1558
- if (s.type === "int" && c.type === "int") {
1559
- let n = Ce(e);
1560
- if (n === void 0) return i.fail(r, e, t, `expected an integer in ${q(t)}`);
1561
- let a = BigInt(s.value), l = BigInt(c.value);
1562
- return n >= a && (o.inclusive ? n <= l : n < l) || i.fail(r, e, t, `${n} is outside ${q(t)}`);
1563
- }
1564
- if (s.type === "float" && c.type === "float") {
1565
- if (!(e instanceof _)) return i.fail(r, e, t, `expected a float in ${q(t)}`);
1566
- let n = e.value;
1567
- return n >= s.value && (o.inclusive ? n <= c.value : n < c.value) || i.fail(r, e, t, `${n} is outside ${q(t)}`);
1568
- }
1569
- return i.warnOnce("range endpoints mix integer and float types", t), i.fail(r, e, t, "invalid range");
1570
- }
1571
- function q(e) {
1572
- let t = e.op;
1573
- return `${J(e.target)}${t.inclusive ? ".." : "..."}${J(e.controller)}`;
1574
- }
1575
- function J(e) {
1576
- return e.kind === "value" ? e.raw : e.kind === "ref" ? e.name : "…";
1577
- }
1578
- var it = {
1579
- half: 25n,
1580
- single: 26n,
1581
- double: 27n
1582
- };
1583
- function at(e, t, r, i, o, c) {
1584
- let u = (e) => t.ai === void 0 ? !0 : typeof t.ai == "bigint" ? t.ai === e : z(new d(e), t.ai, r, i, o, c + 1), p = (n) => o.fail(i, e, t, `expected ${n} (#${t.major})`);
1585
- switch (t.major) {
1586
- case 0:
1587
- if (!(e instanceof d)) return p("an unsigned integer");
1588
- break;
1589
- case 1:
1590
- if (!(e instanceof l)) return p("a negative integer");
1591
- break;
1592
- case 2:
1593
- if (j(e) === void 0) return p("a byte string");
1594
- break;
1595
- case 3:
1596
- if (A(e) === void 0) return p("a text string");
1597
- break;
1598
- case 4:
1599
- if (!(e instanceof a)) return p("an array");
1600
- break;
1601
- case 5:
1602
- if (!(e instanceof f)) return p("a map");
1603
- break;
1604
- case 6: return e instanceof n ? u(e.tag) ? !0 : o.fail(i, e, t, `tag ${e.tag} does not match ${Y(t)}`) : p("a tagged item");
1605
- case 7: return e instanceof ne ? u(BigInt(e.value)) ? !0 : o.fail(i, e, t, `simple(${e.value}) does not match ${Y(t)}`) : e instanceof _ ? t.ai === void 0 || u(it[e.precision ?? s(e.value)]) ? !0 : o.fail(i, e, t, `float does not match ${Y(t)}`) : p("a simple value or float");
1606
- default: return o.fail(i, e, t, `#${t.major} is not a valid major type`);
1607
- }
1608
- return t.ai !== void 0 && t.major <= 5 && o.warnOnce(`the additional-information constraint ${Y(t)} is not checked (encoding-level)`, t), !0;
1609
- }
1610
- function Y(e) {
1611
- return e.raw ?? `#${e.major}`;
1612
- }
1613
- var X = (e, t) => e.choices.map((e) => ({
1614
- entries: e,
1615
- env: t
1616
- })), ot = {
1617
- min: 1,
1618
- max: 1
1619
- };
1620
- function st(e) {
1621
- let t = e.occur;
1622
- return t ? t.marker === "?" ? {
1623
- min: 0,
1624
- max: 1
1625
- } : t.marker === "+" ? {
1626
- min: 1,
1627
- max: Infinity
1628
- } : {
1629
- min: t.min ?? 0,
1630
- max: t.max ?? Infinity
1631
- } : ot;
1632
- }
1633
- function ct(e, t, n) {
1634
- let r = I(n, e.name);
1635
- if (!r || !qe(r)) return;
1636
- let i = [];
1637
- for (let n of r) {
1638
- let r = R(n, e.genericArgs, t);
1639
- for (let e of Ke(n.body)) i.push({
1640
- entries: e,
1641
- env: r
1642
- });
1643
- }
1644
- return i;
1645
- }
1646
- function lt(e, t, n) {
1647
- if (e.kind !== "entry" || e.memberKey || e.value.alternatives.length !== 1) return;
1648
- let r = e.value.alternatives[0];
1649
- if (r.op) return;
1650
- let i = r.target;
1651
- if (i.kind === "ref") return t?.has(i.name) && !i.genericArgs ? void 0 : ct(i, t, n) || (i.name.startsWith("$$") && !I(n, i.name) ? [] : void 0);
1652
- if (i.kind === "unwrap") return ut(i.ref, t, n, 0);
1653
- }
1654
- function ut(e, t, n, r) {
1655
- if (r > 32) return;
1656
- let i = I(n, e.name);
1657
- if (!i || i.length !== 1) return;
1658
- let a = i[0].body, o = R(i[0], e.genericArgs, t);
1659
- if (!L(a)) return Ke(a).map((e) => ({
1660
- entries: e,
1661
- env: o
1662
- }));
1663
- if (a.value.alternatives.length !== 1) return;
1664
- let s = a.value.alternatives[0];
1665
- if (!s.op) {
1666
- if (s.target.kind === "map" || s.target.kind === "array") return X(s.target.group, o);
1667
- if (s.target.kind === "ref") return ut(s.target, o, n, r + 1);
1668
- }
1669
- }
1670
- function dt(e, t, n, r) {
1671
- if (r > 32) return;
1672
- let i = I(n, e.name);
1673
- if (!i || i.length !== 1) return;
1674
- let a = i[0].body;
1675
- if (!L(a) || a.value.alternatives.length !== 1) return;
1676
- let o = a.value.alternatives[0];
1677
- if (o.op) return;
1678
- let s = R(i[0], e.genericArgs, t);
1679
- if (o.target.kind === "tagged") return {
1680
- type: o.target.item,
1681
- env: s
1682
- };
1683
- if (o.target.kind === "ref") return dt(o.target, s, n, r + 1);
1684
- }
1685
- function ft(e, t, n) {
1686
- let r = st(e);
1687
- if (e.kind === "entry-group") return {
1688
- kind: "group",
1689
- occur: r,
1690
- choices: X(e.group, t),
1691
- node: e
1692
- };
1693
- let i = lt(e, t, n);
1694
- return i ? {
1695
- kind: "group",
1696
- occur: r,
1697
- choices: i,
1698
- node: e
1699
- } : {
1700
- kind: "type",
1701
- occur: r,
1702
- ...e.memberKey ? { memberKey: e.memberKey } : {},
1703
- type: e.value,
1704
- env: t,
1705
- node: e
1706
- };
1707
- }
1708
- function pt(e, t, n, r, i, a) {
1709
- return i.checkDepth(a), mt(e.items, 0, X(t, n), r, i, a).has(e.items.length) ? !0 : i.fail(r, e, t, `array of ${e.items.length} item(s) does not match the group`);
1710
- }
1711
- function mt(e, t, n, r, i, a) {
1712
- let o = /* @__PURE__ */ new Set();
1713
- for (let s of n) ht(e, t, s.entries.map((e) => ft(e, s.env, i)), 0, r, i, a, o);
1714
- return o;
1715
- }
1716
- function ht(e, t, n, r, i, a, o, s) {
1717
- if (r === n.length) {
1718
- s.add(t);
1719
- return;
1720
- }
1721
- let c = n[r], l = (t, u) => {
1722
- if (a.step(), t >= c.occur.min && ht(e, u, n, r + 1, i, a, o, s), !(t >= c.occur.max || u >= e.length)) for (let n of gt(e, u, c, i, a, o)) n !== u && l(t + 1, n);
1723
- };
1724
- l(0, t);
1725
- }
1726
- function gt(e, t, n, r, i, a) {
1727
- return n.kind === "type" ? z(e[t], n.type, n.env, [...r, t], i, a) ? [t + 1] : [] : (i.checkDepth(a), [...mt(e, t, n.choices, r, i, a + 1)].sort((e, t) => t - e));
1728
- }
1729
- function _t(e, t, n, r, i, a) {
1730
- i.checkDepth(a);
1731
- let o = Array(e.entries.length).fill(!1);
1732
- for (let s of X(t, n)) if (o.fill(!1), Z(e, s.entries, 0, o, s.env, r, i, a, () => vt(e, o, r, i, t))) return !0;
1733
- return !1;
1734
- }
1735
- function vt(e, t, n, r, i) {
1736
- for (let a = 0; a < t.length; a++) {
1737
- if (t[a]) continue;
1738
- let [o, s] = e.entries[a];
1739
- if (!(o instanceof y)) return r.fail([...n, xt(o, a)], s, i, `entry ${St(o)} is not allowed by the group`), !1;
1740
- }
1741
- return !0;
1742
- }
1743
- function Z(e, t, n, r, i, a, o, s, c) {
1744
- if (n === t.length) return c();
1745
- let l = ft(t[n], i, o);
1746
- if (l.kind === "type") {
1747
- if (!l.memberKey) return o.fail(a, e, l.node, "group entry without a member key cannot match a map entry");
1748
- let u = 0;
1749
- for (let t = 0; t < e.entries.length && u < l.occur.max; t++) {
1750
- if (r[t]) continue;
1751
- let [n, i] = e.entries[t];
1752
- if (!yt(l.memberKey, n, l.env, a, o, s)) continue;
1753
- let c = [...a, xt(n, t)];
1754
- if (z(i, l.type, l.env, c, o, s + 1)) {
1755
- r[t] = !0, u++;
1756
- continue;
1757
- }
1758
- if (l.memberKey.cut) return o.fail(c, i, l.node, `value for ${bt(l.memberKey)} does not match`);
1759
- }
1760
- return u < l.occur.min ? o.fail(a, e, l.node, `missing required entry ${bt(l.memberKey)}`) : Z(e, t, n + 1, r, i, a, o, s, c);
1761
- }
1762
- o.checkDepth(s);
1763
- let u = () => {
1764
- let e = 0;
1765
- for (let t of r) t && e++;
1766
- return e;
1767
- }, d = (f) => {
1768
- if (o.step(), f >= l.occur.min && Z(e, t, n + 1, r, i, a, o, s, c)) return !0;
1769
- if (f >= l.occur.max) return !1;
1770
- let p = r.slice(), m = u();
1771
- for (let h of l.choices) {
1772
- if (Z(e, h.entries, 0, r, h.env, a, o, s + 1, () => u() === m ? Z(e, t, n + 1, r, i, a, o, s, c) : d(f + 1))) return !0;
1773
- for (let e = 0; e < r.length; e++) r[e] = p[e];
1774
- }
1775
- return !1;
1776
- };
1777
- return d(0);
1778
- }
1779
- function yt(e, t, n, r, i, a) {
1780
- if (t = Ye(t), t instanceof y) return !0;
1781
- switch (e.kind) {
1782
- case "bareword": return A(t) === e.key;
1783
- case "value": return U(t, e.key);
1784
- case "type1": return B(t, e.key, n, r, i, a + 1);
1785
- }
1786
- }
1787
- function bt(e) {
1788
- switch (e.kind) {
1789
- case "bareword": return `'${e.key}'`;
1790
- case "value": return e.key.raw;
1791
- case "type1": return J(e.key.target);
1792
- }
1793
- }
1794
- function xt(e, t) {
1795
- let n = A(e);
1796
- if (n !== void 0) return n;
1797
- if (e instanceof d || e instanceof l) {
1798
- let t = e.value;
1799
- if (t >= BigInt(-(2 ** 53 - 1)) && t <= BigInt(2 ** 53 - 1)) return Number(t);
1800
- }
1801
- return t;
1802
- }
1803
- function St(e) {
1804
- let t = A(e);
1805
- return t === void 0 ? e instanceof d || e instanceof l ? e.value.toString() : e.constructor.name.replace(/^Cbor/, "").toLowerCase() : JSON.stringify(t);
1806
- }
1807
- function Ct(e, t, n) {
1808
- return {
1809
- matchType2: (r, i, a) => V(r, i, e, a, t, n + 1),
1810
- matchEmbedded: (r, i, a) => {
1811
- t.embeddedDepth++;
1812
- try {
1813
- return V(r, i, e, a, t, n + 1);
1814
- } finally {
1815
- t.embeddedDepth--;
1816
- }
1817
- },
1818
- resolveValue: (n) => W(n, e, t),
1819
- existsIntGE: (n, r) => Xe(n, r, e, t),
1820
- matchesLiteral: U,
1821
- fail: (e, n, r, i) => t.fail(e, n, r, i),
1822
- warnOnce: (e, n) => t.warnOnce(e, n),
1823
- features: t.features,
1824
- uint: (e) => new d(e)
1825
- };
1826
- }
1827
- //#endregion
1828
- //#region src/cddl/schema.ts
1829
- var wt = class {
1830
- source;
1831
- comments;
1832
- ast;
1833
- rules;
1834
- root;
1835
- warnings;
1836
- constructor(e, t, n, r, i, a) {
1837
- this.source = e, this.comments = t, this.ast = n, this.rules = r, i && (this.root = i), a.length > 0 && (this.warnings = a);
1838
- }
1839
- format(e) {
1840
- return pe(this.ast, {
1841
- ...e,
1842
- source: this.source,
1843
- comments: this.comments
1844
- });
1845
- }
1846
- validate(e, t) {
1847
- let n = e instanceof o ? e : typeof e == "string" ? h(e) : v(e);
1848
- return Ge(this, n, t);
1849
- }
1850
- };
1851
- function Tt(e, t) {
1852
- let { rules: n, comments: r } = w(e), i = [], a = E();
1853
- n.length === 0 && i.push({
1854
- code: "no-rules",
1855
- message: "a CDDL data model needs at least one rule to provide the root of the definition"
1856
- });
1857
- let o = /* @__PURE__ */ new Map();
1858
- for (let e of n) {
1859
- let t = o.get(e.name);
1860
- if (!t) {
1861
- o.set(e.name, [e]);
1862
- continue;
1863
- }
1864
- e.assign === "=" && i.push({
1865
- code: "duplicate-rule",
1866
- message: `rule '${e.name}' is already defined; use /= or //= to extend it`,
1867
- start: e.start,
1868
- end: e.end
1869
- }), t.push(e);
1870
- }
1871
- let s = (e) => {
1872
- let t = o.get(e);
1873
- if (t) return t[0].generics?.length ?? 0;
1874
- let n = a.get(e);
1875
- if (n) return n.generics?.length ?? 0;
1876
- };
1877
- for (let e of n) {
1878
- let t = e.generics;
1879
- Dt(e.body, {
1880
- onRef(e) {
1881
- if (t?.includes(e.name) || e.name.startsWith("$")) return;
1882
- let n = s(e.name);
1883
- if (n === void 0) {
1884
- i.push({
1885
- code: "undefined-name",
1886
- message: `'${e.name}' is not defined (and is not a prelude name)`,
1887
- start: e.start,
1888
- end: e.end
1889
- });
1890
- return;
1891
- }
1892
- let r = e.genericArgs?.length ?? 0;
1893
- n !== r && i.push({
1894
- code: "generic-arity",
1895
- message: `'${e.name}' takes ${n} generic argument${n === 1 ? "" : "s"} but is used with ${r}`,
1896
- start: e.start,
1897
- end: e.end
1898
- });
1899
- },
1900
- onMajor(e, t, n) {
1901
- e > 7 && i.push({
1902
- code: "invalid-major",
1903
- message: `#${e} is not a CBOR major type (0–7)`,
1904
- start: t,
1905
- end: n
1906
- });
1907
- }
1908
- });
1909
- }
1910
- let c = n[0];
1911
- if (c && !Et(c.body) && i.push({
1912
- code: "invalid-root",
1913
- message: `the first rule '${c.name}' is the root of the data model and must define a type, not a group (RFC 8610 §2.2.4)`,
1914
- start: c.start,
1915
- end: c.end
1916
- }), (t?.strict ?? !0) && i.length > 0) throw new ie(i);
1917
- return new wt(e, r, n, o, c, i);
1918
- }
1919
- function Et(e) {
1920
- if (e.kind === "entry") return !e.occur && !e.memberKey;
1921
- if (e.occur || e.group.trailingComma) return !1;
1922
- let { choices: t } = e.group;
1923
- return t.length === 1 && t[0].length === 1 && Et(t[0][0]);
1924
- }
1925
- function Dt(e, t) {
1926
- if (e.kind === "entry-group") {
1927
- Ot(e.group, t);
1928
- return;
1929
- }
1930
- e.memberKey?.kind === "type1" && $(e.memberKey.key, t), Q(e.value, t);
1931
- }
1932
- function Ot(e, t) {
1933
- for (let n of e.choices) for (let e of n) Dt(e, t);
1934
- }
1935
- function Q(e, t) {
1936
- for (let n of e.alternatives) $(n, t);
1937
- }
1938
- function $(e, t) {
1939
- kt(e.target, t), e.controller && kt(e.controller, t);
1940
- }
1941
- function kt(e, t) {
1942
- switch (e.kind) {
1943
- case "value":
1944
- case "any": return;
1945
- case "ref":
1946
- t.onRef(e);
1947
- for (let n of e.genericArgs ?? []) $(n, t);
1948
- return;
1949
- case "paren":
1950
- Q(e.type, t);
1951
- return;
1952
- case "map":
1953
- case "array":
1954
- Ot(e.group, t);
1955
- return;
1956
- case "unwrap":
1957
- t.onRef(e.ref);
1958
- for (let n of e.ref.genericArgs ?? []) $(n, t);
1959
- return;
1960
- case "enum":
1961
- if (e.group.kind === "ref") {
1962
- t.onRef(e.group);
1963
- for (let n of e.group.genericArgs ?? []) $(n, t);
1964
- } else Ot(e.group, t);
1965
- return;
1966
- case "tagged":
1967
- typeof e.tag == "object" && Q(e.tag, t), Q(e.item, t);
1968
- return;
1969
- case "major":
1970
- t.onMajor(e.major, e.start, e.end), typeof e.ai == "object" && Q(e.ai, t);
1971
- return;
1972
- }
1973
- }
1974
- //#endregion
1975
- export { w as a, ie as c, E as i, b as l, Tt as n, se as o, de as r, re as s, wt as t };
1976
-
1977
- //# sourceMappingURL=schema-DN9inJny.js.map