Arrays are a core component of CDDL that define ordered collections of elements. This document explains how arrays are represented in the AST.
In the AST, an array is represented by the following structure:
export type Array = {
Type: 'array'
Name: string
Values: (Property|Property[])[]
Comments: Comment[]
}
Where:
Type
: Always set to ‘array’ to identify this node as an arrayName
: The name of the arrayValues
: An array of properties or property choice arrays that define the elements of the arrayComments
: An array of comments associated with the arrayThe elements of an array are defined by the Values
field, which contains property definitions. Each property can represent:
[field1: int, field2: text]
[int, text]
[* int]
[(int // text)]
basic-header = [
field1: int,
field2: text
]
AST representation:
{
"Type": "array",
"Name": "basic-header",
"Values": [
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "field1",
"Type": ["int"],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "field2",
"Type": ["text"],
"Comments": []
}
],
"Comments": []
}
CDDL allows specifying how many times an element can occur using occurrence indicators. The AST represents this using the Occurrence
field in the Property object.
unlimited-people = [* person]
at-least-one-person = [+ person]
zero-or-one-person = [? person]
one-or-two-people = [1*2 person]
zero-or-two-people = [0*2 person]
two-or-infinity-people = [2* person]
AST representation for “unlimited-people”:
{
"Type": "array",
"Name": "unlimited-people",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 0, "m": Infinity },
"Name": "",
"Type": [
{
"Type": "group",
"Value": "person",
"Unwrapped": false
}
],
"Comments": []
}
],
"Comments": []
}
AST representation for “one-or-two-people”:
{
"Type": "array",
"Name": "one-or-two-people",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 2 },
"Name": "",
"Type": [
{
"Type": "group",
"Value": "person",
"Unwrapped": false
}
],
"Comments": []
}
],
"Comments": []
}
CDDL allows for unwrapping arrays to include their elements directly in another array. This is represented in the AST with the Unwrapped
flag set to true
in the type reference.
basic-header = [
field1: int,
field2: text
]
advanced-header = [
~basic-header,
field3: bytes,
field4: ~time
]
AST representation for “advanced-header”:
{
"Type": "array",
"Name": "advanced-header",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 1 },
"Name": "",
"Type": [
{
"Type": "group",
"Value": "basic-header",
"Unwrapped": true
}
],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "field3",
"Type": ["bytes"],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "field4",
"Type": [
{
"Type": "group",
"Value": "time",
"Unwrapped": true
}
],
"Comments": []
}
],
"Comments": []
}
Arrays can be nested to represent more complex structures:
script.MappingRemoteValue = [
* [
script.RemoteValue / text,
script.RemoteValue
]
]
AST representation:
{
"Type": "array",
"Name": "script.MappingRemoteValue",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 0, "m": Infinity },
"Name": "",
"Type": {
"Type": "array",
"Name": "",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 1 },
"Name": "",
"Type": [
{
"Type": "group",
"Value": "script.RemoteValue",
"Unwrapped": false
},
"text"
],
"Comments": []
},
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 1 },
"Name": "",
"Type": [
{
"Type": "group",
"Value": "script.RemoteValue",
"Unwrapped": false
}
],
"Comments": []
}
],
"Comments": []
},
"Comments": []
}
],
"Comments": []
}
CDDL allows for choices between different types of elements in an array. In the AST, this is represented by an array of types in the Type
field of a property.
points = [* (int / float)]
AST representation:
{
"Type": "array",
"Name": "points",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 0, "m": Infinity },
"Name": "",
"Type": [
"int",
"float"
],
"Comments": []
}
],
"Comments": []
}
Arrays can also be used as types in property definitions:
located-samples = {
sample-point: int,
samples: [+ float]
}
AST representation:
{
"Type": "group",
"Name": "located-samples",
"IsChoiceAddition": false,
"Properties": [
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 1 },
"Name": "sample-point",
"Type": ["int"],
"Comments": []
},
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 1 },
"Name": "samples",
"Type": [
{
"Type": "array",
"Name": "",
"Values": [
{
"HasCut": false,
"Occurrence": { "n": 1, "m": Infinity },
"Name": "",
"Type": "float",
"Comments": []
}
],
"Comments": []
}
],
"Comments": []
}
],
"Comments": []
}
By understanding these different array representations in the AST, you can properly interpret and process CDDL array definitions for further transformation or analysis.