cddl 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +1 -1
  2. package/package.json +5 -1
  3. package/.release-it.ts +0 -10
  4. package/docs/README.md +0 -47
  5. package/docs/arrays.md +0 -356
  6. package/docs/ast-structure.md +0 -166
  7. package/docs/basic-types.md +0 -243
  8. package/docs/cddl-cheatsheet.md +0 -231
  9. package/docs/comments.md +0 -307
  10. package/docs/groups.md +0 -351
  11. package/docs/operators.md +0 -466
  12. package/docs/properties.md +0 -334
  13. package/docs/ranges.md +0 -339
  14. package/docs/references.md +0 -340
  15. package/docs/variables.md +0 -335
  16. package/src/ast.ts +0 -180
  17. package/src/cli/commands/repl.ts +0 -33
  18. package/src/cli/commands/validate.ts +0 -44
  19. package/src/cli/constants.ts +0 -1
  20. package/src/cli/index.ts +0 -18
  21. package/src/constants.ts +0 -16
  22. package/src/index.ts +0 -12
  23. package/src/lexer.ts +0 -238
  24. package/src/parser.ts +0 -993
  25. package/src/tokens.ts +0 -50
  26. package/src/utils.ts +0 -96
  27. package/tests/__snapshots__/complex_types.test.ts.snap +0 -80
  28. package/tests/__snapshots__/examples.test.ts.snap +0 -1077
  29. package/tests/__snapshots__/group-choices.test.ts.snap +0 -403
  30. package/tests/__snapshots__/parser.test.ts.snap +0 -3045
  31. package/tests/__snapshots__/webdriver-local.test.ts.snap +0 -11192
  32. package/tests/__snapshots__/webdriver-remote.test.ts.snap +0 -16350
  33. package/tests/commands/repl.test.ts +0 -52
  34. package/tests/commands/validate.test.ts +0 -66
  35. package/tests/complex_types.test.ts +0 -18
  36. package/tests/examples.test.ts +0 -25
  37. package/tests/group-choices.test.ts +0 -132
  38. package/tests/lexer.test.ts +0 -63
  39. package/tests/module.test.ts +0 -21
  40. package/tests/parser.test.ts +0 -63
  41. package/tests/utils.test.ts +0 -211
  42. package/tests/webdriver-local.test.ts +0 -15
  43. package/tests/webdriver-remote.test.ts +0 -15
  44. package/tsconfig.json +0 -11
@@ -1,243 +0,0 @@
1
- # Basic Types
2
-
3
- The CDDL AST parser defines several basic types that form the building blocks of the Abstract Syntax Tree. This document describes these core types.
4
-
5
- ## Primitive Types
6
-
7
- The parser supports the following primitive types as defined in the CDDL specification:
8
-
9
- ```typescript
10
- export enum Type {
11
- /**
12
- * any types
13
- */
14
- ANY = 'any',
15
-
16
- /**
17
- * boolean types
18
- */
19
- // Boolean value (major type 7, additional information 20 or 21).
20
- BOOL = 'bool',
21
-
22
- /**
23
- * numeric types
24
- */
25
- // An unsigned integer or a negative integer.
26
- INT = 'int',
27
- // An unsigned integer (major type 0).
28
- UINT = 'uint',
29
- // A negative integer (major type 1).
30
- NINT = 'nint',
31
- // One of float16, float32, or float64.
32
- FLOAT = 'float',
33
- // A number representable as an IEEE 754 half-precision float
34
- FLOAT16 = 'float16',
35
- // A number representable as an IEEE 754 single-precision
36
- FLOAT32 = 'float32',
37
- // A number representable as an IEEE 754 double-precision
38
- FLOAT64 = 'float64',
39
-
40
- /**
41
- * string types
42
- */
43
- // A byte string (major type 2).
44
- BSTR = 'bstr',
45
- // A byte string (major type 2).
46
- BYTES = 'bytes',
47
- // Text string (major type 3)
48
- TSTR = 'tstr',
49
- // Text string (major type 3)
50
- TEXT = 'text',
51
-
52
- /**
53
- * null types
54
- */
55
- NIL = 'nil',
56
- NULL = 'null'
57
- }
58
- ```
59
-
60
- These types correspond to the CBOR (Concise Binary Object Representation) types as specified in [RFC 7049](https://datatracker.ietf.org/doc/rfc7049/).
61
-
62
- ## Type References
63
-
64
- In the AST, a type reference can be represented in several ways:
65
-
66
- 1. As a string directly corresponding to one of the primitive types:
67
- ```typescript
68
- "Type": "tstr"
69
- ```
70
-
71
- 2. As a property reference to another named type:
72
- ```typescript
73
- "Type": {
74
- "Type": "group",
75
- "Value": "person",
76
- "Unwrapped": false
77
- }
78
- ```
79
-
80
- 3. As a literal value:
81
- ```typescript
82
- "Type": {
83
- "Type": "literal",
84
- "Value": "some string",
85
- "Unwrapped": false
86
- }
87
- ```
88
-
89
- 4. As a range:
90
- ```typescript
91
- "Type": {
92
- "Type": "range",
93
- "Value": {
94
- "Min": 0,
95
- "Max": 255,
96
- "Inclusive": true
97
- },
98
- "Unwrapped": false
99
- }
100
- ```
101
-
102
- 5. As a type with an operator:
103
- ```typescript
104
- {
105
- "Type": "uint",
106
- "Operator": {
107
- "Type": "ge",
108
- "Value": {
109
- "Type": "literal",
110
- "Value": 0,
111
- "Unwrapped": false
112
- }
113
- }
114
- }
115
- ```
116
-
117
- ## Type Combinations
118
-
119
- Types can be combined in various ways:
120
-
121
- 1. As an array of alternative types (choices):
122
- ```typescript
123
- "Type": [
124
- "tstr",
125
- "uint",
126
- {
127
- "Type": "literal",
128
- "Value": "default value",
129
- "Unwrapped": false
130
- }
131
- ]
132
- ```
133
-
134
- 2. As complex structures (groups and arrays):
135
- ```typescript
136
- "Type": {
137
- "Type": "group",
138
- "Name": "address",
139
- "IsChoiceAddition": false,
140
- "Properties": [
141
- // properties...
142
- ],
143
- "Comments": []
144
- }
145
- ```
146
-
147
- ## Usage in the AST
148
-
149
- These basic types are used throughout the AST to define:
150
-
151
- 1. The type of a property in a group
152
- 2. The possible values of a variable assignment
153
- 3. The elements in an array
154
- 4. The parameters for operators
155
-
156
- Understanding these basic types is essential for working with the AST and properly interpreting its structure.
157
-
158
- ## Example
159
-
160
- Here's an example showing how different types are represented in a CDDL file and its corresponding AST:
161
-
162
- ```cddl
163
- person = {
164
- name: tstr,
165
- age: uint,
166
- is-active: bool,
167
- tags: [* tstr],
168
- status: "active" / "inactive" / "suspended"
169
- }
170
- ```
171
-
172
- The AST representation for this would include the various types we've discussed:
173
-
174
- ```json
175
- {
176
- "Type": "group",
177
- "Name": "person",
178
- "IsChoiceAddition": false,
179
- "Properties": [
180
- {
181
- "HasCut": true,
182
- "Occurrence": { "n": 1, "m": 1 },
183
- "Name": "name",
184
- "Type": ["tstr"],
185
- "Comments": []
186
- },
187
- {
188
- "HasCut": true,
189
- "Occurrence": { "n": 1, "m": 1 },
190
- "Name": "age",
191
- "Type": ["uint"],
192
- "Comments": []
193
- },
194
- {
195
- "HasCut": true,
196
- "Occurrence": { "n": 1, "m": 1 },
197
- "Name": "is-active",
198
- "Type": ["bool"],
199
- "Comments": []
200
- },
201
- {
202
- "HasCut": true,
203
- "Occurrence": { "n": 1, "m": 1 },
204
- "Name": "tags",
205
- "Type": [{
206
- "Type": "array",
207
- "Values": [{
208
- "HasCut": false,
209
- "Occurrence": { "n": 0, "m": Infinity },
210
- "Name": "",
211
- "Type": ["tstr"],
212
- "Comments": []
213
- }]
214
- }],
215
- "Comments": []
216
- },
217
- {
218
- "HasCut": true,
219
- "Occurrence": { "n": 1, "m": 1 },
220
- "Name": "status",
221
- "Type": [
222
- {
223
- "Type": "literal",
224
- "Value": "active",
225
- "Unwrapped": false
226
- },
227
- {
228
- "Type": "literal",
229
- "Value": "inactive",
230
- "Unwrapped": false
231
- },
232
- {
233
- "Type": "literal",
234
- "Value": "suspended",
235
- "Unwrapped": false
236
- }
237
- ],
238
- "Comments": []
239
- }
240
- ],
241
- "Comments": []
242
- }
243
- ```
@@ -1,231 +0,0 @@
1
- # CDDL Core Concepts: Choices and Maps
2
-
3
- This guide explains three fundamental concepts in CDDL that are often confused: **Type Choices**, **Group Choices**, and **Maps**.
4
-
5
- ## 1. Type Choice (`/`)
6
-
7
- A **Type Choice** allows a value to be one of several distinct types. It is represented by the single forward slash `/`.
8
-
9
- Think of it as an "OR" operator for *values*.
10
-
11
- ### Syntax
12
- ```cddl
13
- type1 / type2 / type3
14
- ```
15
-
16
- ### Examples
17
-
18
- **Simple Value Choice:**
19
- ```cddl
20
- ; MyValue can be an integer OR a text string
21
- MyValue = int / tstr
22
- ```
23
-
24
- **Inside an Array:**
25
- ```cddl
26
- ; An array where the first element is either an int OR a float
27
- MyArray = [ int / float, tstr ]
28
- ```
29
-
30
- **Inside a Map Property:**
31
- ```cddl
32
- ; The "score" property can be an integer OR null
33
- MyMap = {
34
- "score" => int / null
35
- }
36
- ```
37
-
38
- ---
39
-
40
- ## 2. Group Choice (`//`)
41
-
42
- A **Group Choice** allows a structure (like a map or array) to match one of several *groups* of properties. It is represented by the double forward slash `//`.
43
-
44
- Think of it as an "OR" operator for *sets of fields/entries*.
45
-
46
- ### Syntax
47
- ```cddl
48
- (group1) // (group2) // (group3)
49
- ```
50
-
51
- ### Examples
52
-
53
- **Choice between sets of fields:**
54
- ```cddl
55
- ; A defined group that is either a "street address" OR a "po box"
56
- Address = (
57
- street: tstr,
58
- city: tstr
59
- ) // (
60
- po-box: int,
61
- city: tstr
62
- )
63
- ```
64
-
65
- **Usage in Maps:**
66
- When used inside a map, it means the map must contain *either* the keys from the first group *OR* the keys from the second group.
67
-
68
- ```cddl
69
- ; A user profile must have either a "username" OR an "email" (but not necessarily both, depending on other rules)
70
- UserProfile = {
71
- id: int,
72
- (
73
- username: tstr
74
- ) // (
75
- email: tstr
76
- )
77
- }
78
- ```
79
-
80
- **Key Difference from Type Choice:**
81
- - `key: int / tstr` -> The key exists, and its value is either int or string.
82
- - `(key1: int) // (key2: int)` -> Either the key `key1` exists OR the key `key2` exists.
83
-
84
- ---
85
-
86
- ## 3. Maps
87
-
88
- In CDDL, a **Map** is defined using curly braces `{}`. It corresponds to a JSON Object or a dictionary/hash map.
89
-
90
- Inside a Map, you define **Groups** of properties/pairs using `key => value` or `key: value` syntax.
91
-
92
- ### Syntax
93
- ```cddl
94
- MyMap = {
95
- key1 => value1,
96
- key2: value2,
97
- optional-key3?: value3
98
- }
99
- ```
100
-
101
- ### Deep Dive: Maps vs. Groups
102
-
103
- - A **Group** is just a sequence of fields (key-value pairs) inside a structure.
104
- - A **Map** `{ ... }` acts as a container for a Group.
105
-
106
- **Defining a reusable Group:**
107
- ```cddl
108
- ; "PersonFields" is just a list of fields, NOT a map yet.
109
- PersonFields = (
110
- name: tstr,
111
- age: int
112
- )
113
- ```
114
-
115
- **Using the Group in a Map:**
116
- ```cddl
117
- ; "Student" is a Map that contains the PersonFields
118
- Student = {
119
- PersonFields,
120
- school: tstr
121
- }
122
-
123
- ; "Teacher" is also a Map that contains the PersonFields
124
- Teacher = {
125
- PersonFields,
126
- subject: tstr
127
- }
128
- ```
129
-
130
- ### Mixing Choices in Maps
131
-
132
- You can combine Maps and Choices to create complex validation logic:
133
-
134
- ```cddl
135
- Response = {
136
- "status" => "ok",
137
- "data" => tstr
138
- } / {
139
- "status" => "error",
140
- "code" => int,
141
- "message" => tstr
142
- }
143
- ```
144
- *This definition means a Response object must look like EITHER the first block (status="ok", data=...) OR the second block (status="error", code=...).*
145
-
146
- ---
147
-
148
- ## 4. Arrays
149
-
150
- Arrays in CDDL are defined using square brackets `[]`. They correspond to JSON Arrays.
151
-
152
- ### Syntax
153
- ```cddl
154
- MyArray = [ type1, type2, ... ]
155
- ```
156
-
157
- ### Examples
158
-
159
- **Simple Array:**
160
- ```cddl
161
- ; An array of an integer, a string, and a float
162
- Point3D = [ x: float, y: float, z: float ]
163
- ```
164
-
165
- **Array with Group Choice:**
166
- You can use group choices inside arrays to allow different structures.
167
-
168
- ```cddl
169
- ; An item is either an integer OR a string
170
- Item = [ (int // tstr) ]
171
- ```
172
-
173
- ---
174
-
175
- ## 5. Literals
176
-
177
- CDDL supports various literal values which are useful for exact matching.
178
-
179
- ### Examples
180
-
181
- ```cddl
182
- LiteralTest = {
183
- ; Exact string match
184
- type: "widget",
185
-
186
- ; Exact integer match
187
- version: 1,
188
-
189
- ; Big Integers
190
- big_id: 9007199254740995,
191
-
192
- ; Null and Booleans
193
- is_active: true,
194
- deleted_at: null
195
- }
196
-
197
- ---
198
-
199
- ## 6. Groups as Maps
200
-
201
- Wrap a **Group** in curly braces `{}` to treat it as a **Map**.
202
-
203
- ```cddl
204
- ; A reusable Group of fields
205
- DateFields = ( year: int, month: int, day: int )
206
-
207
- ; 1. Mix it into a Map
208
- Appointment = {
209
- description: tstr,
210
- DateFields
211
- }
212
-
213
- ; 2. Standalone Map
214
- DateObject = { DateFields }
215
- ```
216
-
217
- ### Choices with Groups and Maps
218
-
219
- You can mix explicit Maps and Groups-as-Maps in type choices:
220
-
221
- ```cddl
222
- ArrayMap = { type: "array", values: [...] }
223
- DateGroup = ( type: "date", value: tstr )
224
-
225
- ; Matches either map structure
226
- MixedValue = (
227
- ArrayMap / ; Map defined elsewhere
228
- { DateGroup } ; Group wrapped in Map
229
- )
230
- ```
231
- ```