The CDDL parser generates an Abstract Syntax Tree (AST) that represents the structure and semantics of a CDDL document. This document provides an overview of the AST structure.
The root of the AST is an array of Assignment
nodes, which can be one of:
Group
- Defines a group of propertiesArray
- Defines an array of valuesVariable
- Defines a variable assignment// The type hierarchy of the AST root
export type Assignment = Group | Array | Variable
The AST is composed of several core types that represent different CDDL constructs:
Assignments are the top-level nodes in the AST. They represent named definitions in CDDL.
// Example AST for a group assignment:
{
"Type": "group",
"Name": "person",
"IsChoiceAddition": false,
"Properties": [
// Properties...
],
"Comments": []
}
Properties define members of groups or elements of arrays.
export type Property = {
HasCut: boolean // Whether the property has a "cut" marker (/)
Occurrence: Occurrence // How many times the property can occur
Name: PropertyName // The name of the property (string)
Type: PropertyType | PropertyType[] // The type(s) of the property
Comments: Comment[] // Comments attached to the property
Operator?: Operator // Optional operator modifying the property
}
Types define the allowed values for properties and variables. They can be:
export type PropertyType = Assignment | Array | PropertyReference | string | NativeTypeWithOperator
The AST forms a tree structure where:
This hierarchical structure allows the AST to represent complex CDDL documents with nested definitions, references, and type relationships.
Here’s a simplified example of an AST for a basic CDDL definition:
person = {
name: tstr,
age: uint,
addresses: [* address],
}
address = {
street: tstr,
city: tstr,
}
Would generate an AST like:
[
{
"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": "addresses",
"Type": [{
"Type": "array",
"Values": [{
"HasCut": false,
"Occurrence": { "n": 0, "m": Infinity },
"Name": "",
"Type": [{
"Type": "group",
"Value": "address",
"Unwrapped": false
}],
"Comments": []
}]
}],
"Comments": []
}
],
"Comments": []
},
{
"Type": "group",
"Name": "address",
"IsChoiceAddition": false,
"Properties": [
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "street",
"Type": ["tstr"],
"Comments": []
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "city",
"Type": ["tstr"],
"Comments": []
}
],
"Comments": []
}
]
See the other documentation files for details on each component of the AST.