Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion examples/commons/comments.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ person = {
foo = { bar } ; some comment
; another comment

som = { g } ; some g
som = { g } ; some g

sport = {
; Some sports involve kicking a ball,
; this field is self-explanatory but it is
; good to test multiline comments
involves-kicking: bool

? uses-ball: bool

; a simple leading comment
? involves-throwing: bool ; an extra comment
}
11 changes: 7 additions & 4 deletions packages/cddl/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ export default class Parser {
}

while (!closingTokens.includes(this.curToken.Type)) {
const comments: Comment[] = []
let leadingComment = this.parseComment(true)
while (leadingComment) {
comments.push(leadingComment)
leadingComment = this.parseComment(true)
}

/**
* check if we have a group choice instead of an assignment
*/
Expand All @@ -230,14 +237,10 @@ export default class Parser {
}

const propertyType: PropertyType[] = []
const comments: Comment[] = []
let isUnwrapped = false
let hasCut = false
let propertyName = ''

const leadingComment = this.parseComment(true)
leadingComment && comments.push(leadingComment)

const occurrence = this.parseOccurrences()

/**
Expand Down
72 changes: 72 additions & 0 deletions packages/cddl/tests/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,78 @@ exports[`parser > can parse comments 1`] = `
],
"Type": "group",
},
{
"Comments": [],
"IsChoiceAddition": false,
"Name": "sport",
"Properties": [
{
"Comments": [
{
"Content": "Some sports involve kicking a ball,",
"Leading": true,
"Type": "comment",
},
{
"Content": "this field is self-explanatory but it is",
"Leading": true,
"Type": "comment",
},
{
"Content": "good to test multiline comments",
"Leading": true,
"Type": "comment",
},
],
"HasCut": true,
"Name": "involves-kicking",
"Occurrence": {
"m": 1,
"n": 1,
},
"Type": [
"bool",
],
},
{
"Comments": [
{
"Content": "a simple leading comment",
"Leading": false,
"Type": "comment",
},
],
"HasCut": true,
"Name": "uses-ball",
"Occurrence": {
"m": Infinity,
"n": 0,
},
"Type": [
"bool",
],
},
{
"Comments": [
{
"Content": "an extra comment",
"Leading": false,
"Type": "comment",
},
],
"HasCut": true,
"Name": "involves-throwing",
"Occurrence": {
"m": Infinity,
"n": 0,
},
"Type": [
"bool",
],
},
],
"Type": "group",
},
]
`;

Expand Down