The CDDL AST parser defines several basic types that form the building blocks of the Abstract Syntax Tree. This document describes these core types.
The parser supports the following primitive types as defined in the CDDL specification:
export enum Type {
/**
* any types
*/
ANY = 'any',
/**
* boolean types
*/
// Boolean value (major type 7, additional information 20 or 21).
BOOL = 'bool',
/**
* numeric types
*/
// An unsigned integer or a negative integer.
INT = 'int',
// An unsigned integer (major type 0).
UINT = 'uint',
// A negative integer (major type 1).
NINT = 'nint',
// One of float16, float32, or float64.
FLOAT = 'float',
// A number representable as an IEEE 754 half-precision float
FLOAT16 = 'float16',
// A number representable as an IEEE 754 single-precision
FLOAT32 = 'float32',
// A number representable as an IEEE 754 double-precision
FLOAT64 = 'float64',
/**
* string types
*/
// A byte string (major type 2).
BSTR = 'bstr',
// A byte string (major type 2).
BYTES = 'bytes',
// Text string (major type 3)
TSTR = 'tstr',
// Text string (major type 3)
TEXT = 'text',
/**
* null types
*/
NIL = 'nil',
NULL = 'null'
}
These types correspond to the CBOR (Concise Binary Object Representation) types as specified in RFC 7049.
In the AST, a type reference can be represented in several ways:
"Type": "tstr"
"Type": {
"Type": "group",
"Value": "person",
"Unwrapped": false
}
"Type": {
"Type": "literal",
"Value": "some string",
"Unwrapped": false
}
"Type": {
"Type": "range",
"Value": {
"Min": 0,
"Max": 255,
"Inclusive": true
},
"Unwrapped": false
}
{
"Type": "uint",
"Operator": {
"Type": "ge",
"Value": {
"Type": "literal",
"Value": 0,
"Unwrapped": false
}
}
}
Types can be combined in various ways:
"Type": [
"tstr",
"uint",
{
"Type": "literal",
"Value": "default value",
"Unwrapped": false
}
]
"Type": {
"Type": "group",
"Name": "address",
"IsChoiceAddition": false,
"Properties": [
// properties...
],
"Comments": []
}
These basic types are used throughout the AST to define:
Understanding these basic types is essential for working with the AST and properly interpreting its structure.
Here’s an example showing how different types are represented in a CDDL file and its corresponding AST:
person = {
name: tstr,
age: uint,
is-active: bool,
tags: [* tstr],
status: "active" / "inactive" / "suspended"
}
The AST representation for this would include the various types we’ve discussed:
{
"Type": "group",
"Name": "person",
"IsChoiceAddition": false,
"Properties": [
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "name",
"Type": ["tstr"],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "age",
"Type": ["uint"],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "is-active",
"Type": ["bool"],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "tags",
"Type": [{
"Type": "array",
"Values": [{
"HasCut": false,
"Occurrence": { "n": 0, "m": Infinity },
"Name": "",
"Type": ["tstr"],
"Comments": []
}]
}],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "status",
"Type": [
{
"Type": "literal",
"Value": "active",
"Unwrapped": false
},
{
"Type": "literal",
"Value": "inactive",
"Unwrapped": false
},
{
"Type": "literal",
"Value": "suspended",
"Unwrapped": false
}
],
"Comments": []
}
],
"Comments": []
}