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.
In the AST, a comment is represented by the following structure:
export type Comment = {
Type: 'comment'
Content: string
Leading: boolean
}
Where:
Type
: Always set to ‘comment’ to identify this node as a commentContent
: The text content of the comment (without the comment marker)Leading
: A boolean indicating if this is a leading comment (appears before the definition) or a trailing comment (appears after the definition)In CDDL, comments are denoted by a semicolon ;
and continue until the end of the line:
; This is a comment
person = {
name: tstr, ; This is another comment
age: uint
}
Comments can be associated with various elements in the AST:
Each of these elements has a Comments
field that contains an array of comment objects associated with that element.
; This is a comment
; foobar
; This is another
; very nice comment
person = {
; a good employer
employer: tstr
}
AST representation:
{
"Type": "group",
"Name": "person",
"IsChoiceAddition": false,
"Properties": [
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "employer",
"Type": ["tstr"],
"Comments": [
{
"Type": "comment",
"Content": "a good employer",
"Leading": false
}
]
}
],
"Comments": [
{
"Type": "comment",
"Content": "This is a comment",
"Leading": false
},
{
"Type": "comment",
"Content": "foobar",
"Leading": false
},
{
"Type": "comment",
"Content": "This is another",
"Leading": false
},
{
"Type": "comment",
"Content": "very nice comment",
"Leading": false
}
]
}
Geography = [
; a city
city: tstr,
; some coordinates
gpsCoordinates: GpsCoordinates
]
AST representation:
{
"Type": "array",
"Name": "Geography",
"Values": [
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "city",
"Type": ["tstr"],
"Comments": [
{
"Type": "comment",
"Content": "a city",
"Leading": false
}
]
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "gpsCoordinates",
"Type": [{
"Type": "group",
"Value": "GpsCoordinates",
"Unwrapped": false
}],
"Comments": [
{
"Type": "comment",
"Content": "some coordinates",
"Leading": false
}
]
}
],
"Comments": []
}
; unit: m/s
speed = number .ge 0
AST representation:
{
"Type": "variable",
"Name": "speed",
"IsChoiceAddition": false,
"PropertyType": [
{
"Type": {
"Type": "group",
"Value": "number",
"Unwrapped": false
},
"Operator": {
"Type": "ge",
"Value": {
"Type": "literal",
"Value": 0,
"Unwrapped": false
}
}
}
],
"Comments": [
{
"Type": "comment",
"Content": "unit: m/s",
"Leading": false
}
]
}
GpsCoordinates = {
; degrees, scaled by 10^7
longitude: uint,
; degreed, scaled by 10^7
latitude: uint
}
AST representation:
{
"Type": "group",
"Name": "GpsCoordinates",
"IsChoiceAddition": false,
"Properties": [
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "longitude",
"Type": ["uint"],
"Comments": [
{
"Type": "comment",
"Content": "degrees, scaled by 10^7",
"Leading": false
}
]
},
{
"HasCut": true,
"Occurrence": { "n": 1, "m": 1 },
"Name": "latitude",
"Type": ["uint"],
"Comments": [
{
"Type": "comment",
"Content": "degreed, scaled by 10^7",
"Leading": false
}
]
}
],
"Comments": []
}
Comments can be stacked together in the CDDL file:
; some comment
; another comment
foo = {
bar
}
AST representation:
{
"Type": "group",
"Name": "foo",
"IsChoiceAddition": false,
"Properties": [
{
"HasCut": false,
"Occurrence": { "n": 1, "m": 1 },
"Name": "",
"Type": [{
"Type": "group",
"Value": "bar",
"Unwrapped": false
}],
"Comments": []
}
],
"Comments": [
{
"Type": "comment",
"Content": "some comment",
"Leading": false
},
{
"Type": "comment",
"Content": "another comment",
"Leading": false
}
]
}
When processing the AST, you can use the comments to:
Comments provide valuable metadata about the CDDL definitions and should be preserved when transforming the AST to other formats.