cddl 0.8.4 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/build/cli/commands/repl.d.ts.map +1 -1
- package/build/cli/commands/validate.d.ts.map +1 -1
- package/build/cli/commands/validate.js +1 -1
- package/build/lexer.d.ts.map +1 -1
- package/build/lexer.js +1 -3
- package/build/parser.d.ts.map +1 -1
- package/build/parser.js +26 -0
- package/docs/README.md +47 -0
- package/docs/arrays.md +356 -0
- package/docs/ast-structure.md +166 -0
- package/docs/basic-types.md +243 -0
- package/docs/comments.md +307 -0
- package/docs/groups.md +351 -0
- package/docs/operators.md +466 -0
- package/docs/properties.md +334 -0
- package/docs/ranges.md +339 -0
- package/docs/references.md +340 -0
- package/docs/variables.md +335 -0
- package/package.json +8 -8
|
@@ -0,0 +1,243 @@
|
|
|
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
|
+
```
|
package/docs/comments.md
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
# Comments
|
|
2
|
+
|
|
3
|
+
Comments are an important part of CDDL files that provide additional context and information about the definitions. This document explains how comments are represented in the AST.
|
|
4
|
+
|
|
5
|
+
## Comment Definition
|
|
6
|
+
|
|
7
|
+
In the AST, a comment is represented by the following structure:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
export type Comment = {
|
|
11
|
+
Type: 'comment'
|
|
12
|
+
Content: string
|
|
13
|
+
Leading: boolean
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Where:
|
|
18
|
+
- `Type`: Always set to 'comment' to identify this node as a comment
|
|
19
|
+
- `Content`: The text content of the comment (without the comment marker)
|
|
20
|
+
- `Leading`: A boolean indicating if this is a leading comment (appears before the definition) or a trailing comment (appears after the definition)
|
|
21
|
+
|
|
22
|
+
## Comment Syntax
|
|
23
|
+
|
|
24
|
+
In CDDL, comments are denoted by a semicolon `;` and continue until the end of the line:
|
|
25
|
+
|
|
26
|
+
```cddl
|
|
27
|
+
; This is a comment
|
|
28
|
+
person = {
|
|
29
|
+
name: tstr, ; This is another comment
|
|
30
|
+
age: uint
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Comment Location in the AST
|
|
35
|
+
|
|
36
|
+
Comments can be associated with various elements in the AST:
|
|
37
|
+
|
|
38
|
+
1. Groups
|
|
39
|
+
2. Arrays
|
|
40
|
+
3. Variables
|
|
41
|
+
4. Properties
|
|
42
|
+
|
|
43
|
+
Each of these elements has a `Comments` field that contains an array of comment objects associated with that element.
|
|
44
|
+
|
|
45
|
+
## Examples
|
|
46
|
+
|
|
47
|
+
### Comments on Group Definitions
|
|
48
|
+
|
|
49
|
+
```cddl
|
|
50
|
+
; This is a comment
|
|
51
|
+
; foobar
|
|
52
|
+
; This is another
|
|
53
|
+
; very nice comment
|
|
54
|
+
person = {
|
|
55
|
+
; a good employer
|
|
56
|
+
employer: tstr
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
AST representation:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"Type": "group",
|
|
65
|
+
"Name": "person",
|
|
66
|
+
"IsChoiceAddition": false,
|
|
67
|
+
"Properties": [
|
|
68
|
+
{
|
|
69
|
+
"HasCut": true,
|
|
70
|
+
"Occurrence": { "n": 1, "m": 1 },
|
|
71
|
+
"Name": "employer",
|
|
72
|
+
"Type": ["tstr"],
|
|
73
|
+
"Comments": [
|
|
74
|
+
{
|
|
75
|
+
"Type": "comment",
|
|
76
|
+
"Content": "a good employer",
|
|
77
|
+
"Leading": false
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
"Comments": [
|
|
83
|
+
{
|
|
84
|
+
"Type": "comment",
|
|
85
|
+
"Content": "This is a comment",
|
|
86
|
+
"Leading": false
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"Type": "comment",
|
|
90
|
+
"Content": "foobar",
|
|
91
|
+
"Leading": false
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"Type": "comment",
|
|
95
|
+
"Content": "This is another",
|
|
96
|
+
"Leading": false
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
"Type": "comment",
|
|
100
|
+
"Content": "very nice comment",
|
|
101
|
+
"Leading": false
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Comments on Array Elements
|
|
108
|
+
|
|
109
|
+
```cddl
|
|
110
|
+
Geography = [
|
|
111
|
+
; a city
|
|
112
|
+
city: tstr,
|
|
113
|
+
; some coordinates
|
|
114
|
+
gpsCoordinates: GpsCoordinates
|
|
115
|
+
]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
AST representation:
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"Type": "array",
|
|
123
|
+
"Name": "Geography",
|
|
124
|
+
"Values": [
|
|
125
|
+
{
|
|
126
|
+
"HasCut": true,
|
|
127
|
+
"Occurrence": { "n": 1, "m": 1 },
|
|
128
|
+
"Name": "city",
|
|
129
|
+
"Type": ["tstr"],
|
|
130
|
+
"Comments": [
|
|
131
|
+
{
|
|
132
|
+
"Type": "comment",
|
|
133
|
+
"Content": "a city",
|
|
134
|
+
"Leading": false
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"HasCut": true,
|
|
140
|
+
"Occurrence": { "n": 1, "m": 1 },
|
|
141
|
+
"Name": "gpsCoordinates",
|
|
142
|
+
"Type": [{
|
|
143
|
+
"Type": "group",
|
|
144
|
+
"Value": "GpsCoordinates",
|
|
145
|
+
"Unwrapped": false
|
|
146
|
+
}],
|
|
147
|
+
"Comments": [
|
|
148
|
+
{
|
|
149
|
+
"Type": "comment",
|
|
150
|
+
"Content": "some coordinates",
|
|
151
|
+
"Leading": false
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
}
|
|
155
|
+
],
|
|
156
|
+
"Comments": []
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Comments on Variable Definitions
|
|
161
|
+
|
|
162
|
+
```cddl
|
|
163
|
+
; unit: m/s
|
|
164
|
+
speed = number .ge 0
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
AST representation:
|
|
168
|
+
|
|
169
|
+
```json
|
|
170
|
+
{
|
|
171
|
+
"Type": "variable",
|
|
172
|
+
"Name": "speed",
|
|
173
|
+
"IsChoiceAddition": false,
|
|
174
|
+
"PropertyType": [
|
|
175
|
+
{
|
|
176
|
+
"Type": {
|
|
177
|
+
"Type": "group",
|
|
178
|
+
"Value": "number",
|
|
179
|
+
"Unwrapped": false
|
|
180
|
+
},
|
|
181
|
+
"Operator": {
|
|
182
|
+
"Type": "ge",
|
|
183
|
+
"Value": {
|
|
184
|
+
"Type": "literal",
|
|
185
|
+
"Value": 0,
|
|
186
|
+
"Unwrapped": false
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
],
|
|
191
|
+
"Comments": [
|
|
192
|
+
{
|
|
193
|
+
"Type": "comment",
|
|
194
|
+
"Content": "unit: m/s",
|
|
195
|
+
"Leading": false
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Comments on Properties in Groups
|
|
202
|
+
|
|
203
|
+
```cddl
|
|
204
|
+
GpsCoordinates = {
|
|
205
|
+
; degrees, scaled by 10^7
|
|
206
|
+
longitude: uint,
|
|
207
|
+
; degreed, scaled by 10^7
|
|
208
|
+
latitude: uint
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
AST representation:
|
|
213
|
+
|
|
214
|
+
```json
|
|
215
|
+
{
|
|
216
|
+
"Type": "group",
|
|
217
|
+
"Name": "GpsCoordinates",
|
|
218
|
+
"IsChoiceAddition": false,
|
|
219
|
+
"Properties": [
|
|
220
|
+
{
|
|
221
|
+
"HasCut": true,
|
|
222
|
+
"Occurrence": { "n": 1, "m": 1 },
|
|
223
|
+
"Name": "longitude",
|
|
224
|
+
"Type": ["uint"],
|
|
225
|
+
"Comments": [
|
|
226
|
+
{
|
|
227
|
+
"Type": "comment",
|
|
228
|
+
"Content": "degrees, scaled by 10^7",
|
|
229
|
+
"Leading": false
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
"HasCut": true,
|
|
235
|
+
"Occurrence": { "n": 1, "m": 1 },
|
|
236
|
+
"Name": "latitude",
|
|
237
|
+
"Type": ["uint"],
|
|
238
|
+
"Comments": [
|
|
239
|
+
{
|
|
240
|
+
"Type": "comment",
|
|
241
|
+
"Content": "degreed, scaled by 10^7",
|
|
242
|
+
"Leading": false
|
|
243
|
+
}
|
|
244
|
+
]
|
|
245
|
+
}
|
|
246
|
+
],
|
|
247
|
+
"Comments": []
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Multiple Comments on an Element
|
|
252
|
+
|
|
253
|
+
Comments can be stacked together in the CDDL file:
|
|
254
|
+
|
|
255
|
+
```cddl
|
|
256
|
+
; some comment
|
|
257
|
+
; another comment
|
|
258
|
+
foo = {
|
|
259
|
+
bar
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
AST representation:
|
|
264
|
+
|
|
265
|
+
```json
|
|
266
|
+
{
|
|
267
|
+
"Type": "group",
|
|
268
|
+
"Name": "foo",
|
|
269
|
+
"IsChoiceAddition": false,
|
|
270
|
+
"Properties": [
|
|
271
|
+
{
|
|
272
|
+
"HasCut": false,
|
|
273
|
+
"Occurrence": { "n": 1, "m": 1 },
|
|
274
|
+
"Name": "",
|
|
275
|
+
"Type": [{
|
|
276
|
+
"Type": "group",
|
|
277
|
+
"Value": "bar",
|
|
278
|
+
"Unwrapped": false
|
|
279
|
+
}],
|
|
280
|
+
"Comments": []
|
|
281
|
+
}
|
|
282
|
+
],
|
|
283
|
+
"Comments": [
|
|
284
|
+
{
|
|
285
|
+
"Type": "comment",
|
|
286
|
+
"Content": "some comment",
|
|
287
|
+
"Leading": false
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
"Type": "comment",
|
|
291
|
+
"Content": "another comment",
|
|
292
|
+
"Leading": false
|
|
293
|
+
}
|
|
294
|
+
]
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## Working with Comments
|
|
299
|
+
|
|
300
|
+
When processing the AST, you can use the comments to:
|
|
301
|
+
|
|
302
|
+
1. Generate documentation from the CDDL file
|
|
303
|
+
2. Add explanatory comments to generated code
|
|
304
|
+
3. Provide additional context or constraints not directly expressible in CDDL
|
|
305
|
+
4. Preserve the original author's intentions and notes
|
|
306
|
+
|
|
307
|
+
Comments provide valuable metadata about the CDDL definitions and should be preserved when transforming the AST to other formats.
|