Skip to content

Commit fcb6c7b

Browse files
authored
Handle multiline leading comments (#44)
1 parent 544749e commit fcb6c7b

3 files changed

Lines changed: 92 additions & 5 deletions

File tree

examples/commons/comments.cddl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@ person = {
99
foo = { bar } ; some comment
1010
; another comment
1111

12-
som = { g } ; some g
12+
som = { g } ; some g
13+
14+
sport = {
15+
; Some sports involve kicking a ball,
16+
; this field is self-explanatory but it is
17+
; good to test multiline comments
18+
involves-kicking: bool
19+
20+
? uses-ball: bool
21+
22+
; a simple leading comment
23+
? involves-throwing: bool ; an extra comment
24+
}

packages/cddl/src/parser.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ export default class Parser {
211211
}
212212

213213
while (!closingTokens.includes(this.curToken.Type)) {
214+
const comments: Comment[] = []
215+
let leadingComment = this.parseComment(true)
216+
while (leadingComment) {
217+
comments.push(leadingComment)
218+
leadingComment = this.parseComment(true)
219+
}
220+
214221
/**
215222
* check if we have a group choice instead of an assignment
216223
*/
@@ -230,14 +237,10 @@ export default class Parser {
230237
}
231238

232239
const propertyType: PropertyType[] = []
233-
const comments: Comment[] = []
234240
let isUnwrapped = false
235241
let hasCut = false
236242
let propertyName = ''
237243

238-
const leadingComment = this.parseComment(true)
239-
leadingComment && comments.push(leadingComment)
240-
241244
const occurrence = this.parseOccurrences()
242245

243246
/**

packages/cddl/tests/__snapshots__/parser.test.ts.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,78 @@ exports[`parser > can parse comments 1`] = `
778778
],
779779
"Type": "group",
780780
},
781+
{
782+
"Comments": [],
783+
"IsChoiceAddition": false,
784+
"Name": "sport",
785+
"Properties": [
786+
{
787+
"Comments": [
788+
{
789+
"Content": "Some sports involve kicking a ball,",
790+
"Leading": true,
791+
"Type": "comment",
792+
},
793+
{
794+
"Content": "this field is self-explanatory but it is",
795+
"Leading": true,
796+
"Type": "comment",
797+
},
798+
{
799+
"Content": "good to test multiline comments",
800+
"Leading": true,
801+
"Type": "comment",
802+
},
803+
],
804+
"HasCut": true,
805+
"Name": "involves-kicking",
806+
"Occurrence": {
807+
"m": 1,
808+
"n": 1,
809+
},
810+
"Type": [
811+
"bool",
812+
],
813+
},
814+
{
815+
"Comments": [
816+
{
817+
"Content": "a simple leading comment",
818+
"Leading": false,
819+
"Type": "comment",
820+
},
821+
],
822+
"HasCut": true,
823+
"Name": "uses-ball",
824+
"Occurrence": {
825+
"m": Infinity,
826+
"n": 0,
827+
},
828+
"Type": [
829+
"bool",
830+
],
831+
},
832+
{
833+
"Comments": [
834+
{
835+
"Content": "an extra comment",
836+
"Leading": false,
837+
"Type": "comment",
838+
},
839+
],
840+
"HasCut": true,
841+
"Name": "involves-throwing",
842+
"Occurrence": {
843+
"m": Infinity,
844+
"n": 0,
845+
},
846+
"Type": [
847+
"bool",
848+
],
849+
},
850+
],
851+
"Type": "group",
852+
},
781853
]
782854
`;
783855

0 commit comments

Comments
 (0)