@cbortech/cbor 0.26.5 → 0.26.7

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 (52) hide show
  1. package/README.ja.md +239 -34
  2. package/README.md +242 -38
  3. package/dist/ast/CborByteString.d.ts +14 -0
  4. package/dist/ast/CborEllipsis.d.ts +94 -2
  5. package/dist/ast/CborFloat.d.ts +10 -0
  6. package/dist/ast/CborItem.d.ts +93 -4
  7. package/dist/ast/CborNint.d.ts +9 -0
  8. package/dist/ast/CborSimple.d.ts +11 -2
  9. package/dist/ast/CborTag.d.ts +8 -0
  10. package/dist/ast/CborTextString.d.ts +20 -0
  11. package/dist/ast/CborUint.d.ts +8 -0
  12. package/dist/ast/index.cjs +1 -1
  13. package/dist/ast/index.js +2 -2
  14. package/dist/cbor.d.ts +8 -0
  15. package/dist/cddl/ast.d.ts +196 -0
  16. package/dist/cddl/controls.d.ts +28 -0
  17. package/dist/cddl/equal.d.ts +19 -0
  18. package/dist/cddl/errors.d.ts +91 -0
  19. package/dist/cddl/index.cjs +3 -0
  20. package/dist/cddl/index.cjs.map +1 -0
  21. package/dist/cddl/index.d.ts +52 -0
  22. package/dist/cddl/index.js +67 -0
  23. package/dist/cddl/index.js.map +1 -0
  24. package/dist/cddl/parser.d.ts +13 -0
  25. package/dist/cddl/position.d.ts +13 -0
  26. package/dist/cddl/prelude.d.ts +5 -0
  27. package/dist/cddl/schema.d.ts +90 -0
  28. package/dist/cddl/tokenizer.d.ts +138 -0
  29. package/dist/cddl/validator.d.ts +30 -0
  30. package/dist/cddl/writer.d.ts +23 -0
  31. package/dist/cdn/serialize-utils.d.ts +186 -8
  32. package/dist/extensions/dt.d.ts +3 -0
  33. package/dist/extensions/types.d.ts +31 -9
  34. package/dist/index.cjs +4 -4
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.ts +2 -0
  37. package/dist/index.js +97 -107
  38. package/dist/index.js.map +1 -1
  39. package/dist/mapEntries-D2NyeCX3.cjs +17 -0
  40. package/dist/mapEntries-D2NyeCX3.cjs.map +1 -0
  41. package/dist/{mapEntries-Clr-oNtQ.js → mapEntries-DxrDre2P.js} +1498 -991
  42. package/dist/mapEntries-DxrDre2P.js.map +1 -0
  43. package/dist/schema-Bofmsptw.js +1977 -0
  44. package/dist/schema-Bofmsptw.js.map +1 -0
  45. package/dist/schema-t_bdPk8_.cjs +63 -0
  46. package/dist/schema-t_bdPk8_.cjs.map +1 -0
  47. package/dist/types.d.ts +258 -7
  48. package/dist/utils/base64.d.ts +12 -0
  49. package/package.json +24 -7
  50. package/dist/mapEntries-6hy7UgeN.cjs +0 -15
  51. package/dist/mapEntries-6hy7UgeN.cjs.map +0 -1
  52. package/dist/mapEntries-Clr-oNtQ.js.map +0 -1
@@ -1,11 +1,14 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
3
  import { CborWriter, EncodingWidth } from '../cbor/encode';
4
+ import { ByteCommentSyntax } from '../cdn/serialize-utils';
4
5
  /** One part of a byte string parsed from a CDN `+` concatenation chain. */
5
6
  export interface CborByteStringPart {
6
7
  bytes: Uint8Array;
7
8
  /** Original literal source text, when the part came from a byte string token. */
8
9
  source?: string;
10
+ /** Which comment syntax `source` recognizes, if any — see `ednCommentSyntax`. */
11
+ commentSyntax?: ByteCommentSyntax;
9
12
  }
10
13
  /** CBOR Major Type 2 — definite-length byte string. */
11
14
  export declare class CborByteString extends CborItem {
@@ -15,12 +18,23 @@ export declare class CborByteString extends CborItem {
15
18
  readonly ednEncoding: 'hex' | 'base64' | 'base64url' | 'base32' | 'base32hex';
16
19
  encodingWidth: EncodingWidth | undefined;
17
20
  readonly ednSource: string | undefined;
21
+ /**
22
+ * Which comment syntax `ednSource` recognizes, if any — set once at parse
23
+ * time by whoever actually knows the literal's real origin (see
24
+ * `ByteCommentSyntax`), never re-derived later from its prefix string:
25
+ * a user extension can register under any prefix, including one a
26
+ * built-in (`b32`/`h32`) also uses, so the prefix string alone can't say
27
+ * which comment rules (if any) actually apply. `undefined` when
28
+ * `ednSource` has no comment syntax, or its extension's isn't known.
29
+ */
30
+ readonly ednCommentSyntax: ByteCommentSyntax | undefined;
18
31
  /** Part boundaries of the original `+` concatenation chain, if any. */
19
32
  readonly ednParts: readonly CborByteStringPart[] | undefined;
20
33
  constructor(value: Uint8Array, options?: {
21
34
  ednEncoding?: 'hex' | 'base64' | 'base64url' | 'base32' | 'base32hex';
22
35
  encodingWidth?: EncodingWidth;
23
36
  ednSource?: string;
37
+ ednCommentSyntax?: ByteCommentSyntax;
24
38
  ednParts?: readonly CborByteStringPart[];
25
39
  });
26
40
  _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
@@ -3,9 +3,101 @@ import { CborTag } from './CborTag';
3
3
  import { CborItem } from './CborItem';
4
4
  export declare const CPA888_TAG = 888n;
5
5
  export declare class CborEllipsis extends CborTag {
6
+ /**
7
+ * For the array (string/bytes elision) form: `realBoundary[i]` is `true`
8
+ * when a genuine `+` from the source precedes `items[i]` — as opposed to
9
+ * `items[i]` sitting *inside* a single `h'xx...yy'` literal's own `...`
10
+ * notation (index 0's value is never consulted — there is nothing before
11
+ * the first item). `preserveConcatenation` uses this to show only the
12
+ * real boundaries and fuse everything else, exactly as each source
13
+ * literal was spelled.
14
+ *
15
+ * `undefined` means no boundary information is available at all (e.g.
16
+ * reconstructed from raw CBOR bytes, which carry no notion of "was there
17
+ * a `+` here" to begin with) — `preserveConcatenation` then has no effect,
18
+ * the same as for a value that didn't originate from CDN source.
19
+ */
20
+ readonly realBoundary: readonly boolean[] | undefined;
21
+ /**
22
+ * For a subtree-elision placeholder (`888(null)`, i.e. `content
23
+ * instanceof CborSimple`) that sits *inside* another `CborEllipsis`'s
24
+ * items: `true` when it came from a `h'xx...yy'`-family literal's own
25
+ * `...` notation — even a fully-elided `h'...'` with no hex digits at all
26
+ * — as opposed to a bare standalone `...` token. Only consulted when this
27
+ * placeholder ends up isolated as its own preserved fragment (nothing to
28
+ * fuse it with on either side), to pick the right spelling: `h'...'` vs
29
+ * plain `...`.
30
+ */
31
+ readonly fromByteLiteral: boolean;
32
+ /**
33
+ * When `fromByteLiteral` is `true`: that `h'xx...yy'`-family literal's own
34
+ * raw source text (e.g. `h'AB...CD'` verbatim — case, interior whitespace,
35
+ * and any `/ ... /`/`# ...` comments included), for `preserveByteString`
36
+ * to round-trip instead of re-emitting a freshly lower-cased, comment-free
37
+ * `h'...'` literal. `undefined` when `fromByteLiteral` is `false`.
38
+ */
39
+ readonly literalSource: string | undefined;
6
40
  /** Subtree elision: 888(null) */
7
- constructor();
41
+ constructor(fromByteLiteral?: boolean, literalSource?: string);
8
42
  /** String/bytes elision: 888([items...]) */
9
- constructor(items: CborItem[]);
43
+ constructor(items: CborItem[], realBoundary?: readonly boolean[]);
10
44
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
45
+ /**
46
+ * `true` when this bytes elision has at least one real `+` boundary
47
+ * somewhere — as opposed to being a single `h'xx...yy'` literal's own
48
+ * `...` notation, which is not "produced by + concatenation" (see
49
+ * `preserveByteString`'s own docs) and so has its spelling preserved by
50
+ * `preserveByteString` alone, the same as a non-elided `h'...'` literal.
51
+ *
52
+ * A real boundary can hide two ways: as `realBoundary[i]` (`i > 0`) on the
53
+ * items array itself, or *inside* a merged `CborByteString` whose
54
+ * `ednParts.length > 1` — two `+`-joined literals that sat next to each
55
+ * other with no ellipsis between them (e.g. `h'AB' + h'CD...EF'`) merge
56
+ * into one item during parsing, so their boundary doesn't show up in
57
+ * `realBoundary` at that item's own index.
58
+ */
59
+ private _hasRealConcatenation;
60
+ /**
61
+ * Render one elision fragment as it should appear under
62
+ * `preserveConcatenation`: a merged multi-part `CborTextString` (see the
63
+ * parser's `currentParts` consolidation) is expanded back into its
64
+ * original `+`-joined literals, single-line, honoring `preserveRawString`
65
+ * per part. Anything else (a single-part fragment, or a nested
66
+ * `CborEllipsis`) renders normally.
67
+ *
68
+ * Only reached for text elision (or anything not shaped like a pure bytes
69
+ * elision, or a bytes elision with no `realBoundary` information) —
70
+ * `_renderPreservedBytesElision` handles the bytes case that has that
71
+ * information, since it needs to see all the fragments together to know
72
+ * which `...`s are real `+`-joined ellipses and which are internal to one
73
+ * `h'...'` literal.
74
+ */
75
+ private _renderFragment;
76
+ /**
77
+ * Re-emit a `888([...])` bytes elision as a single `h'xx...yy'` literal
78
+ * when every item is either a plain `CborByteString` fragment or a
79
+ * subtree-elision placeholder (`888(null)`) — i.e. exactly what
80
+ * `h'xx...yy'` parses into. Returns `undefined` when the items don't
81
+ * match that shape (e.g. text-string elision, or a fragment that isn't a
82
+ * plain byte string), so the caller falls back to the `frag + ... + frag`
83
+ * form.
84
+ */
85
+ private _compactHexElided;
86
+ /**
87
+ * `preserveConcatenation` rendering for a bytes elision. Groups the
88
+ * fragments at every *real* boundary (`realBoundary[i]`) and renders each
89
+ * group as one unit — fusing together whatever sits between real
90
+ * boundaries, including any `h'xx...yy'` literal's own internal `...`
91
+ * (wherever it's positioned — leading, trailing, or in the middle) and
92
+ * the fragments on either side of it, exactly as that literal was
93
+ * written. Within a group, a `CborByteString` that itself merged several
94
+ * `+`-joined literals (`ednParts.length > 1`, always a real boundary
95
+ * internally — see the parser) is further split at each of those parts.
96
+ *
97
+ * Returns `undefined` when there's no `realBoundary` to work from, or an
98
+ * item isn't a plain `CborByteString` or subtree-elision placeholder
99
+ * (e.g. text elision), so the caller falls back to the compact literal or
100
+ * the simpler per-fragment `frag + ... + frag` rendering.
101
+ */
102
+ private _renderPreservedBytesElision;
11
103
  }
@@ -25,6 +25,15 @@ export declare class CborFloat extends CborItem {
25
25
  * to round-trip the literal when `appStrings` is not false.
26
26
  */
27
27
  ednSource?: string;
28
+ /**
29
+ * Original CDN literal source text (e.g. `1.50`, `1.5_1`, `0x1.8p+0_1`),
30
+ * set by the parser when this float came from a plain CDN float literal
31
+ * (as opposed to a `float'...'` app-string, which uses `ednSource`
32
+ * above). Used by `_toCDN()` to round-trip the literal's exact spelling,
33
+ * including its encoding-indicator suffix, when `preserveNumberFormat`
34
+ * is set.
35
+ */
36
+ literalSource?: string;
28
37
  /**
29
38
  * Original encoded payload bytes (big-endian, without the initial byte),
30
39
  * set by the decoder when the value is NaN so that NaN payloads survive a
@@ -36,6 +45,7 @@ export declare class CborFloat extends CborItem {
36
45
  constructor(value: number, options?: {
37
46
  precision?: FloatPrecision;
38
47
  rawBits?: Uint8Array;
48
+ literalSource?: string;
39
49
  });
40
50
  _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
41
51
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
@@ -1,4 +1,4 @@
1
- import { CBOROptions, ToCDNOptions, ToJSOptions, ToHexDumpOptions, ToCBOROptions, CborComments, DecodeWarning, ParseWarning } from '../types';
1
+ import { CBOROptions, ToCDNOptions, ToJSOptions, ToHexDumpOptions, ToCBOROptions, CborComment, CborComments, DecodeWarning, ParseWarning } from '../types';
2
2
  import { CborWriter } from '../cbor/encode';
3
3
  /** @internal One line of an annotated hex dump. */
4
4
  export interface AnnotatedLine {
@@ -6,6 +6,27 @@ export interface AnnotatedLine {
6
6
  hex: string;
7
7
  comment: string;
8
8
  }
9
+ export interface AppSeqEncodingEdit {
10
+ /** Start/end offsets within appSeqSource of an existing indicator. */
11
+ start: number;
12
+ end: number;
13
+ /** Replacement used by encodingIndicators: 'always'. */
14
+ always: string;
15
+ /** Replacement used by encodingIndicators: 'never'. */
16
+ never: string;
17
+ }
18
+ /**
19
+ * Original literal features used by the sole item inside a preserved
20
+ * `prefix<<item>>` source. They let serialization honour an explicitly
21
+ * disabled sibling `preserve*` option instead of replaying that literal
22
+ * verbatim through `preserveAppSequence`.
23
+ */
24
+ export interface AppSeqSourceFeatures {
25
+ byteString?: boolean;
26
+ textString?: boolean;
27
+ rawString?: boolean;
28
+ concatenation?: boolean;
29
+ }
9
30
  /**
10
31
  * Abstract base class for all CBOR AST nodes.
11
32
  *
@@ -31,6 +52,71 @@ export declare abstract class CborItem {
31
52
  * They do not affect CBOR bytes or JS conversion.
32
53
  */
33
54
  comments?: CborComments;
55
+ /**
56
+ * `true` when this node is an array/map entry (or indefinite-length
57
+ * string chunk) immediately preceded by a blank line in the parsed CDN
58
+ * source — set unconditionally by the parser, regardless of any
59
+ * `preserve*` option, mirroring `start`/`end`. Only consulted by
60
+ * `toCDN()` when `ToCDNOptions.preserveBlankLines` is set; otherwise
61
+ * ignored. Left `undefined` for nodes not parsed as a container entry, or
62
+ * with no blank line before them.
63
+ */
64
+ blankLineBefore?: boolean;
65
+ /**
66
+ * Original application-string/-sequence source text — `prefix'...'`,
67
+ * `` prefix`...` ``, or `prefix<<...>>` — set by the parser when the
68
+ * resolving extension declares `preserveAppSeqSource: 'optional'`. A
69
+ * subclass's own `_toCDN()` override may check this (gated behind
70
+ * `ToCDNOptions.preserveAppSequence`) to round-trip the exact original
71
+ * spelling instead of always regenerating `prefix'...'` notation from the
72
+ * resolved value. Left `undefined` for nodes not parsed from one of these
73
+ * forms.
74
+ */
75
+ appSeqSource?: string;
76
+ /**
77
+ * Comments contained within `appSeqSource`, with `start`/`end` offsets
78
+ * relative to that string. These spans allow comment markers to be
79
+ * converted (or comments to be removed) without regenerating and thereby
80
+ * losing the original application-string/-sequence notation.
81
+ */
82
+ appSeqComments?: CborComment[];
83
+ /**
84
+ * Source edits for encoding indicators contained in a raw-tag
85
+ * `appSeqSource`. Includes zero-width edits where an indicator was absent
86
+ * so `encodingIndicators: 'always'` can insert one without regenerating
87
+ * the surrounding source.
88
+ */
89
+ appSeqEncodingEdits?: AppSeqEncodingEdit[];
90
+ /**
91
+ * `false` when `appSeqEncodingEdits` does not cover every encoding
92
+ * indicator nested inside a raw-tag `appSeqSource` — i.e. its content
93
+ * contains a node type `collectContentEncodingEdits` doesn't know how to
94
+ * edit (e.g. a `CborMap`, `CborTag`, or indefinite-length string inside an
95
+ * `ip` array). Left `undefined` (treated as complete) when coverage is
96
+ * exhaustive, which holds for every tag content type `dt` accepts and for
97
+ * most content `ip` accepts. When `false`, `decideTaggedAppSeqRendering`
98
+ * must not choose the `'source'` decision under `encodingIndicators !==
99
+ * 'auto'`, since surgical span edits would silently leave the uncovered
100
+ * node's indicator unchanged; it falls back to `'structural'` instead.
101
+ */
102
+ appSeqEncodingEditsComplete?: boolean;
103
+ /**
104
+ * For an `appSeqSource` parsed from `prefix<<item>>` notation: the offset
105
+ * within `appSeqSource`, relative to its own start, where the sole inner
106
+ * item's own consumption ends — i.e. right after its own encoding
107
+ * indicator, if it had one. Lets `adjustAppSeqIndicator` locate and strip
108
+ * that inner indicator exactly, regardless of what (whitespace, a
109
+ * trailing comma, a comment) separates it from the closing `>>`, rather
110
+ * than pattern-matching text near `>>`. `undefined` when `appSeqSource`
111
+ * isn't `<<...>>` notation, or wasn't captured with a single inner item.
112
+ */
113
+ appSeqInnerEnd?: number;
114
+ /**
115
+ * Literal-preservation features present in the sole item of a captured
116
+ * `prefix<<item>>` source. Used to resolve explicitly disabled
117
+ * `preserve*` options without treating unrelated options as conflicts.
118
+ */
119
+ appSeqSourceFeatures?: AppSeqSourceFeatures;
34
120
  /**
35
121
  * Validity violations detected while decoding or parsing this node.
36
122
  * Populated when `strict: false` is set in `FromCBOROptions` or
@@ -46,9 +132,12 @@ export declare abstract class CborItem {
46
132
  /**
47
133
  * @internal
48
134
  * True when this node is, or contains through wrapper nodes (tags,
49
- * embedded CBOR, app-sequence results), an array or map.
50
- * `inlineLeafContainers` never inlines a container whose entries contain
51
- * another container, even one that renders on a single line.
135
+ * app-sequence results), an array or map. `inlineLeafContainers` never
136
+ * inlines a container whose entries contain another container, even one
137
+ * that renders on a single line. `CborEmbeddedCBOR` (`<<...>>`) is the one
138
+ * exception: it inlines its own entries based purely on whether they
139
+ * render without a line break, regardless of this flag — see its
140
+ * `_toCDN()`, which omits `entryIsLeaf` for that reason.
52
141
  */
53
142
  get _containsCdnContainer(): boolean;
54
143
  /** Serialize this node to CBOR binary. */
@@ -16,8 +16,17 @@ export declare class CborNint extends CborItem {
16
16
  /** CBOR raw argument n, where actual value = −1 − n. */
17
17
  readonly argument: bigint;
18
18
  encodingWidth: EncodingWidth | undefined;
19
+ /**
20
+ * Original CDN digit spelling (sign + base + digits, without the
21
+ * encoding-indicator suffix), set by the parser when this value came
22
+ * from CDN text. Used by `_toCDN()` to round-trip the literal's base
23
+ * (`-0xff`, `-0o377`, `-0b101`, decimal) when `preserveNumberFormat` is
24
+ * set.
25
+ */
26
+ readonly ednSource?: string;
19
27
  constructor(value: number | bigint, options?: {
20
28
  encodingWidth?: EncodingWidth;
29
+ ednSource?: string;
21
30
  });
22
31
  /** The actual decoded negative value (−1 − argument). */
23
32
  get value(): bigint;
@@ -12,12 +12,21 @@ import { CborWriter } from '../cbor/encode';
12
12
  */
13
13
  export declare class CborSimple extends CborItem {
14
14
  readonly value: number;
15
- constructor(value: number);
15
+ /**
16
+ * Original CDN digit spelling of the argument to `simple(...)` (base +
17
+ * digits), set by the parser when this value came from CDN text. Used by
18
+ * `_toCDN()` to round-trip the argument's base (`0x10`, decimal, …) when
19
+ * `preserveNumberFormat` is set.
20
+ */
21
+ readonly ednSource?: string;
22
+ constructor(value: number, options?: {
23
+ ednSource?: string;
24
+ });
16
25
  static readonly FALSE: CborSimple;
17
26
  static readonly TRUE: CborSimple;
18
27
  static readonly NULL: CborSimple;
19
28
  static readonly UNDEFINED: CborSimple;
20
29
  _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
21
- _toCDN(_options: ToCDNOptions | undefined, _depth: number): string;
30
+ _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
22
31
  _toJS(_options?: ToJSOptions): unknown;
23
32
  }
@@ -6,8 +6,16 @@ export declare class CborTag extends CborItem {
6
6
  readonly tag: bigint;
7
7
  readonly content: CborItem;
8
8
  encodingWidth: EncodingWidth | undefined;
9
+ /**
10
+ * Original CDN digit spelling of the tag number (base + digits, without
11
+ * the encoding-indicator suffix), set by the parser when this tag came
12
+ * from CDN text. Used by `_toCDN()` to round-trip the tag number's base
13
+ * (`0x3e7`, decimal, …) when `preserveNumberFormat` is set.
14
+ */
15
+ ednSource?: string;
9
16
  constructor(tag: number | bigint, content: CborItem, options?: {
10
17
  encodingWidth?: EncodingWidth;
18
+ ednSource?: string;
11
19
  });
12
20
  get _containsCdnContainer(): boolean;
13
21
  _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
@@ -10,16 +10,36 @@ export declare class CborTextString extends CborItem {
10
10
  readonly ednParts: readonly string[] | undefined;
11
11
  /** Original raw-string source text, when parsed from a single backtick literal. */
12
12
  readonly ednSource: string | undefined;
13
+ /**
14
+ * Original double-quoted source text (including its escape sequences),
15
+ * when parsed from a single non-concatenated `"..."` literal. Used by
16
+ * `_toCDN()` to round-trip the literal's exact spelling when
17
+ * `preserveTextString` is set.
18
+ */
19
+ readonly quotedEdnSource: string | undefined;
13
20
  /**
14
21
  * Original source text per `ednParts` entry, aligned by index; `undefined`
15
22
  * for parts that were not raw backtick literals.
16
23
  */
17
24
  readonly ednPartSources: readonly (string | undefined)[] | undefined;
25
+ /**
26
+ * `true` at index `i`, aligned with `ednParts`, when that part came from a
27
+ * byte-string literal on the right of a text-leading `+` concatenation
28
+ * (decoded as UTF-8 and merged in per §5.1) rather than a double-quoted
29
+ * `"..."` literal. Both cases leave `ednPartSources[i]` `undefined` (byte
30
+ * strings have no preserved raw source here, same as an unpreserved
31
+ * double-quoted literal), so this is what lets `appSeqSourceFeatures`
32
+ * attribute the part to `byteString` instead of the unpreservable
33
+ * `textString`.
34
+ */
35
+ readonly ednPartIsByteString: readonly boolean[] | undefined;
18
36
  constructor(value: string, options?: {
19
37
  encodingWidth?: EncodingWidth;
20
38
  ednParts?: readonly string[];
21
39
  ednSource?: string;
40
+ quotedEdnSource?: string;
22
41
  ednPartSources?: readonly (string | undefined)[];
42
+ ednPartIsByteString?: readonly boolean[];
23
43
  });
24
44
  _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
25
45
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
@@ -5,8 +5,16 @@ import { CborWriter, EncodingWidth } from '../cbor/encode';
5
5
  export declare class CborUint extends CborItem {
6
6
  readonly value: bigint;
7
7
  encodingWidth: EncodingWidth | undefined;
8
+ /**
9
+ * Original CDN digit spelling (base + digits, without the encoding-
10
+ * indicator suffix), set by the parser when this value came from CDN
11
+ * text. Used by `_toCDN()` to round-trip the literal's base (`0xff`,
12
+ * `0o377`, `0b101`, decimal) when `preserveNumberFormat` is set.
13
+ */
14
+ readonly ednSource?: string;
8
15
  constructor(value: number | bigint, options?: {
9
16
  encodingWidth?: EncodingWidth;
17
+ ednSource?: string;
10
18
  });
11
19
  _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
12
20
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-6hy7UgeN.cjs");exports.CborArray=e.C,exports.CborBigNint=e.v,exports.CborBigUint=e.y,exports.CborByteString=e.E,exports.CborEmbeddedCBOR=e.b,exports.CborFloat=e.O,exports.CborIndefiniteByteString=e.T,exports.CborIndefiniteTextString=e.w,exports.CborItem=e.j,exports.CborMap=e.S,exports.CborNint=e.k,exports.CborSimple=e.x,exports.CborTag=e.D,exports.CborTextString=e.g,exports.CborUint=e.A;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-D2NyeCX3.cjs");exports.CborArray=e.D,exports.CborBigNint=e._,exports.CborBigUint=e.v,exports.CborByteString=e.A,exports.CborEmbeddedCBOR=e.w,exports.CborFloat=e.M,exports.CborIndefiniteByteString=e.k,exports.CborIndefiniteTextString=e.O,exports.CborItem=e.I,exports.CborMap=e.E,exports.CborNint=e.P,exports.CborSimple=e.T,exports.CborTag=e.j,exports.CborTextString=e.h,exports.CborUint=e.F;
package/dist/ast/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { A as e, C as t, D as n, E as r, O as i, S as a, T as o, b as s, g as c, j as l, k as u, v as d, w as f, x as p, y as m } from "../mapEntries-Clr-oNtQ.js";
2
- export { t as CborArray, d as CborBigNint, m as CborBigUint, r as CborByteString, s as CborEmbeddedCBOR, i as CborFloat, o as CborIndefiniteByteString, f as CborIndefiniteTextString, l as CborItem, a as CborMap, u as CborNint, p as CborSimple, n as CborTag, c as CborTextString, e as CborUint };
1
+ import { A as e, D as t, E as n, F as r, I as i, M as a, O as o, P as s, T as c, _ as l, h as u, j as d, k as f, v as p, w as m } from "../mapEntries-DxrDre2P.js";
2
+ export { t as CborArray, l as CborBigNint, p as CborBigUint, e as CborByteString, m as CborEmbeddedCBOR, a as CborFloat, f as CborIndefiniteByteString, o as CborIndefiniteTextString, i as CborItem, n as CborMap, s as CborNint, c as CborSimple, d as CborTag, u as CborTextString, r as CborUint };
package/dist/cbor.d.ts CHANGED
@@ -177,6 +177,14 @@ export declare class CBOR {
177
177
  * @example
178
178
  * // CDN text input
179
179
  * CBOR.validate('{"a": 1}', { type: 'cdn' });
180
+ *
181
+ * @example
182
+ * // Schema validation with a compiled CDDL schema
183
+ * import { CDDL } from '@cbortech/cbor/cddl';
184
+ * const schema = CDDL.compile('person = { name: tstr, ? age: uint }');
185
+ * const result = CBOR.validate('{"name": "kudo"}', { type: 'cdn', cddl: schema });
186
+ * result.valid; // true
187
+ * result.cddlErrors; // []
180
188
  */
181
189
  static validate(input: ArrayBufferView | ArrayBufferLike | string, options?: ValidateOptions): ValidateResult;
182
190
  /**
@@ -0,0 +1,196 @@
1
+ /**
2
+ * CDDL AST node definitions.
3
+ *
4
+ * Unlike the CBOR side (class-based CborItem nodes), the CDDL AST is a plain
5
+ * discriminated-union structure: nodes are produced by the parser, consumed
6
+ * by the compiler/writer (and, in a later phase, the validator), and never
7
+ * carry behavior of their own.
8
+ *
9
+ * All nodes carry `start`/`end` character offsets into the parsed source so
10
+ * diagnostics and editor tooling can point at exact ranges.
11
+ */
12
+ export interface CddlNodeBase {
13
+ /** Character offset of the first character of this node in the source. */
14
+ start: number;
15
+ /** Character offset just past the last character of this node. */
16
+ end: number;
17
+ }
18
+ /**
19
+ * rule = typename [genericparm] S assignt S type
20
+ * / groupname [genericparm] S assigng S grpent
21
+ *
22
+ * Both forms are parsed into the same shape: `body` is a group entry, which
23
+ * subsumes a plain type (an entry with no occurrence and no member key).
24
+ * Whether a rule is used as a type or as a group is resolved semantically at
25
+ * validation time, not at parse time.
26
+ */
27
+ export interface CddlRule extends CddlNodeBase {
28
+ kind: 'rule';
29
+ name: string;
30
+ /** Generic parameter names from `name<A, B> = …`, if any. */
31
+ generics?: string[];
32
+ /** '=' defines; '/=' extends a type choice; '//=' extends a group choice. */
33
+ assign: '=' | '/=' | '//=';
34
+ body: CddlGroupEntry;
35
+ }
36
+ /** type = type1 *(S "/" S type1) — always wrapped, even for one alternative. */
37
+ export interface CddlType extends CddlNodeBase {
38
+ kind: 'type';
39
+ alternatives: CddlType1[];
40
+ }
41
+ /** type1 = type2 [S (rangeop / ctlop) S type2] */
42
+ export interface CddlType1 extends CddlNodeBase {
43
+ kind: 'type1';
44
+ target: CddlType2;
45
+ /** Present when a range or control operator follows the target. */
46
+ op?: {
47
+ kind: 'range';
48
+ inclusive: boolean;
49
+ } | {
50
+ kind: 'ctl';
51
+ name: string;
52
+ };
53
+ /** The right-hand operand; present exactly when `op` is present. */
54
+ controller?: CddlType2;
55
+ }
56
+ export type CddlType2 = CddlValue | CddlRef | CddlParenType | CddlMapType | CddlArrayType | CddlUnwrap | CddlEnum | CddlTagged | CddlMajor | CddlAny;
57
+ /** A literal value: number, text string, or byte string. */
58
+ export type CddlValue = CddlNodeBase & {
59
+ kind: 'value';
60
+ raw: string;
61
+ } & ({
62
+ type: 'int';
63
+ value: number | bigint;
64
+ } | {
65
+ type: 'float';
66
+ value: number;
67
+ } | {
68
+ type: 'text';
69
+ value: string;
70
+ } | {
71
+ type: 'bytes';
72
+ value: Uint8Array;
73
+ qualifier: '' | 'h' | 'b64';
74
+ });
75
+ /** typename [genericarg] — also used for groupname references. */
76
+ export interface CddlRef extends CddlNodeBase {
77
+ kind: 'ref';
78
+ name: string;
79
+ genericArgs?: CddlType1[];
80
+ }
81
+ /** "(" S type S ")" */
82
+ export interface CddlParenType extends CddlNodeBase {
83
+ kind: 'paren';
84
+ type: CddlType;
85
+ }
86
+ /** "{" S group S "}" */
87
+ export interface CddlMapType extends CddlNodeBase {
88
+ kind: 'map';
89
+ group: CddlGroup;
90
+ }
91
+ /** "[" S group S "]" */
92
+ export interface CddlArrayType extends CddlNodeBase {
93
+ kind: 'array';
94
+ group: CddlGroup;
95
+ }
96
+ /** "~" S typename [genericarg] */
97
+ export interface CddlUnwrap extends CddlNodeBase {
98
+ kind: 'unwrap';
99
+ ref: CddlRef;
100
+ }
101
+ /** "&" S "(" S group S ")" / "&" S groupname [genericarg] */
102
+ export interface CddlEnum extends CddlNodeBase {
103
+ kind: 'enum';
104
+ group: CddlGroup | CddlRef;
105
+ }
106
+ /**
107
+ * "#" "6" ["." head-number] "(" S type S ")" — a tagged item.
108
+ * `tag` is a literal tag number, a `<type>` head-number expression
109
+ * (RFC 9682 §3.2), or absent for `#6(…)` (any tag number).
110
+ */
111
+ export interface CddlTagged extends CddlNodeBase {
112
+ kind: 'tagged';
113
+ tag?: bigint | CddlType;
114
+ item: CddlType;
115
+ /**
116
+ * Source text of the '#6[.head]' part (e.g. '#6.0x10') when the tag number
117
+ * is literal or absent; lets the formatter preserve the number base.
118
+ */
119
+ raw?: string;
120
+ }
121
+ /**
122
+ * "#" DIGIT ["." uint] and "#" "7" ["." head-number] — a major type,
123
+ * optionally constrained by additional information (or, for major 7, the
124
+ * simple value / float head-number, which may be a `<type>` expression).
125
+ */
126
+ export interface CddlMajor extends CddlNodeBase {
127
+ kind: 'major';
128
+ major: number;
129
+ ai?: bigint | CddlType;
130
+ /**
131
+ * Source text of the '#N[.ai]' expression (e.g. '#7.0b11001') when the
132
+ * head-number is literal or absent; lets the formatter preserve the
133
+ * number base.
134
+ */
135
+ raw?: string;
136
+ }
137
+ /** "#" — any data item. */
138
+ export interface CddlAny extends CddlNodeBase {
139
+ kind: 'any';
140
+ }
141
+ /** group = grpchoice *(S "//" S grpchoice); each choice is an entry list. */
142
+ export interface CddlGroup extends CddlNodeBase {
143
+ kind: 'group';
144
+ choices: CddlGroupEntry[][];
145
+ /**
146
+ * True when the final entry is followed by a comma (optcom). Commas
147
+ * between entries are cosmetic and not recorded, but the trailing comma is
148
+ * syntactically significant: `(int,)` is a group, never a parenthesized
149
+ * type, so e.g. it cannot be the root of a data model.
150
+ */
151
+ trailingComma?: boolean;
152
+ }
153
+ export type CddlGroupEntry = CddlEntryValue | CddlEntryGroup;
154
+ /** grpent = [occur S] [memberkey S] type — also covers bare group references. */
155
+ export interface CddlEntryValue extends CddlNodeBase {
156
+ kind: 'entry';
157
+ occur?: CddlOccur;
158
+ memberKey?: CddlMemberKey;
159
+ value: CddlType;
160
+ }
161
+ /** grpent = [occur S] "(" S group S ")" — an inline parenthesized group. */
162
+ export interface CddlEntryGroup extends CddlNodeBase {
163
+ kind: 'entry-group';
164
+ occur?: CddlOccur;
165
+ group: CddlGroup;
166
+ }
167
+ /**
168
+ * occur = [uint] "*" [uint] / "+" / "?"
169
+ * marker '*' covers `*`, `n*`, `*m`, and `n*m` via min/max.
170
+ */
171
+ export interface CddlOccur extends CddlNodeBase {
172
+ kind: 'occur';
173
+ marker: '?' | '+' | '*';
174
+ min?: number;
175
+ max?: number;
176
+ }
177
+ /**
178
+ * memberkey = type1 S ["^" S] "=>"
179
+ * / bareword S ":"
180
+ * / value S ":"
181
+ *
182
+ * `cut` is true for the ':' forms (implicit cut, RFC 8610 §3.5.4) and for
183
+ * the explicit `^ =>` form.
184
+ */
185
+ export type CddlMemberKey = CddlNodeBase & {
186
+ cut: boolean;
187
+ } & ({
188
+ kind: 'type1';
189
+ key: CddlType1;
190
+ } | {
191
+ kind: 'bareword';
192
+ key: string;
193
+ } | {
194
+ kind: 'value';
195
+ key: CddlValue;
196
+ });
@@ -0,0 +1,28 @@
1
+ import { CborItem } from '../ast/CborItem';
2
+ import { CddlNodeBase, CddlType2, CddlValue } from './ast';
3
+ type Path = readonly (string | number)[];
4
+ /** Matching primitives provided by the validator (closed over env/ctx). */
5
+ export interface ControlDeps {
6
+ matchType2(item: CborItem, t2: CddlType2, path: Path): boolean;
7
+ /** Like matchType2, but suppresses instance offsets in recorded errors —
8
+ * the item was decoded out of an embedded byte string, so its offsets
9
+ * are relative to the embedded bytes, not the outer document. */
10
+ matchEmbedded(item: CborItem, t2: CddlType2, path: Path): boolean;
11
+ resolveValue(t2: CddlType2): CddlValue | undefined;
12
+ /** Whether some integer ≥ min matches the type; undefined = unanalyzable. */
13
+ existsIntGE(t2: CddlType2, min: bigint): boolean | undefined;
14
+ matchesLiteral(item: CborItem, v: CddlValue): boolean;
15
+ fail(path: Path, item: CborItem | undefined, node: CddlNodeBase | undefined, message: string): false;
16
+ warnOnce(message: string, node?: CddlNodeBase): void;
17
+ features: ReadonlySet<string>;
18
+ uint(n: number | bigint): CborItem;
19
+ }
20
+ export type ControlHandler = (deps: ControlDeps, item: CborItem, target: CddlType2, controller: CddlType2, path: Path, node: CddlNodeBase) => boolean;
21
+ /**
22
+ * The .feature controller is a feature name, or an array whose first
23
+ * element is the feature name and whose rest is detail (RFC 9165 §5) —
24
+ * either form may be parenthesized, e.g. `.feature (["x", "detail"])`.
25
+ */
26
+ export declare const featureName: (deps: ControlDeps, controller: CddlType2) => string | undefined;
27
+ export declare function getControl(name: string): ControlHandler | undefined;
28
+ export {};