cddl 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/parser.d.ts.map +1 -1
- package/build/parser.js +26 -2
- package/package.json +1 -1
- package/src/parser.ts +33 -7
- package/tests/__snapshots__/group-choices.test.ts.snap +125 -0
- package/tests/group-choices.test.ts +29 -0
- package/tests/parser.test.ts +19 -0
package/build/parser.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,YAAY,CAAA;AAE9B,OAAO,EAAE,KAAK,EAAU,MAAM,aAAa,CAAC;AAG5C,OAAO,EAE2C,UAAU,EAE3D,MAAM,UAAU,CAAA;AAoBjB,MAAM,CAAC,OAAO,OAAO,MAAM;;IAEvB,CAAC,EAAE,KAAK,CAAC;IAET,QAAQ,EAAE,KAAK,CAAa;IAC5B,SAAS,EAAE,KAAK,CAAa;IAC7B,cAAc,EAAE,KAAK,CAAa;gBAErB,QAAQ,EAAE,MAAM;IAS7B,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,gBAAgB;
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,YAAY,CAAA;AAE9B,OAAO,EAAE,KAAK,EAAU,MAAM,aAAa,CAAC;AAG5C,OAAO,EAE2C,UAAU,EAE3D,MAAM,UAAU,CAAA;AAoBjB,MAAM,CAAC,OAAO,OAAO,MAAM;;IAEvB,CAAC,EAAE,KAAK,CAAC;IAET,QAAQ,EAAE,KAAK,CAAa;IAC5B,SAAS,EAAE,KAAK,CAAa;IAC7B,cAAc,EAAE,KAAK,CAAa;gBAErB,QAAQ,EAAE,MAAM;IAS7B,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,gBAAgB;IA8CxB,OAAO,CAAC,oBAAoB;IAue5B,OAAO,CAAC,wBAAwB;IAahC;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAenB,OAAO,CAAC,iBAAiB;IAazB,OAAO,CAAC,iBAAiB;IAqKzB,OAAO,CAAC,aAAa;IAgBrB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,kBAAkB;IA8E1B,OAAO,CAAC,gBAAgB;IA4DxB;;OAEG;IACH,OAAO,CAAC,YAAY;IAcpB,KAAK;IAaL,OAAO,CAAC,WAAW;CAKtB"}
|
package/build/parser.js
CHANGED
|
@@ -43,7 +43,10 @@ export default class Parser {
|
|
|
43
43
|
parseAssignments() {
|
|
44
44
|
const comments = [];
|
|
45
45
|
while (this.curToken.Type === Tokens.COMMENT) {
|
|
46
|
-
|
|
46
|
+
const comment = this.parseComment();
|
|
47
|
+
if (comment) {
|
|
48
|
+
comments.push(comment);
|
|
49
|
+
}
|
|
47
50
|
}
|
|
48
51
|
/**
|
|
49
52
|
* expect group identifier, e.g.
|
|
@@ -588,7 +591,7 @@ export default class Parser {
|
|
|
588
591
|
Unwrapped: isUnwrapped
|
|
589
592
|
};
|
|
590
593
|
}
|
|
591
|
-
else if (this.curToken.Literal === Tokens.LBRACE) {
|
|
594
|
+
else if (this.curToken.Literal === Tokens.LBRACE || this.curToken.Literal === Tokens.LBRACK) {
|
|
592
595
|
const val = this.parseAssignmentValue();
|
|
593
596
|
if (Array.isArray(val)) {
|
|
594
597
|
throw new Error('Unexpected array in property type parsing');
|
|
@@ -739,6 +742,21 @@ export default class Parser {
|
|
|
739
742
|
this.nextToken(); // eat Property if not already consumed (e.g. by Group parsing)
|
|
740
743
|
}
|
|
741
744
|
propertyTypes.push(prop);
|
|
745
|
+
/**
|
|
746
|
+
* ignore comments between type choice members, e.g.
|
|
747
|
+
* ```
|
|
748
|
+
* Foo = int ; comment
|
|
749
|
+
* / text
|
|
750
|
+
* ```
|
|
751
|
+
* or
|
|
752
|
+
* ```
|
|
753
|
+
* Foo = int / ; comment
|
|
754
|
+
* text
|
|
755
|
+
* ```
|
|
756
|
+
*/
|
|
757
|
+
while (this.curToken.Type === Tokens.COMMENT && this.peekToken.Type === Tokens.SLASH) {
|
|
758
|
+
this.parseComment();
|
|
759
|
+
}
|
|
742
760
|
/**
|
|
743
761
|
* ensure we don't go into the next choice, e.g.:
|
|
744
762
|
* ```
|
|
@@ -754,6 +772,9 @@ export default class Parser {
|
|
|
754
772
|
*/
|
|
755
773
|
while (this.curToken.Type === Tokens.SLASH) {
|
|
756
774
|
this.nextToken(); // eat `/`
|
|
775
|
+
while ([Tokens.COMMENT].includes(this.curToken.Type)) {
|
|
776
|
+
this.parseComment();
|
|
777
|
+
}
|
|
757
778
|
propertyTypes.push(this.parsePropertyType());
|
|
758
779
|
if (!this.isOperator() && this.curToken.Type !== Tokens.SLASH) {
|
|
759
780
|
/**
|
|
@@ -762,6 +783,9 @@ export default class Parser {
|
|
|
762
783
|
*/
|
|
763
784
|
this.nextToken();
|
|
764
785
|
}
|
|
786
|
+
while ([Tokens.COMMENT].includes(this.curToken.Type) && this.peekToken.Type === Tokens.SLASH) {
|
|
787
|
+
this.parseComment();
|
|
788
|
+
}
|
|
765
789
|
/**
|
|
766
790
|
* ensure we don't go into the next choice, e.g.:
|
|
767
791
|
* ```
|
package/package.json
CHANGED
package/src/parser.ts
CHANGED
|
@@ -56,7 +56,10 @@ export default class Parser {
|
|
|
56
56
|
private parseAssignments (): Assignment {
|
|
57
57
|
const comments: Comment[] = []
|
|
58
58
|
while (this.curToken.Type === Tokens.COMMENT) {
|
|
59
|
-
|
|
59
|
+
const comment = this.parseComment()
|
|
60
|
+
if (comment) {
|
|
61
|
+
comments.push(comment)
|
|
62
|
+
}
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
/**
|
|
@@ -667,12 +670,12 @@ export default class Parser {
|
|
|
667
670
|
Value: this.curToken.Literal === 'true',
|
|
668
671
|
Unwrapped: isUnwrapped
|
|
669
672
|
}
|
|
670
|
-
} else if (this.curToken.Literal === Tokens.LBRACE) {
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
673
|
+
} else if (this.curToken.Literal === Tokens.LBRACE || this.curToken.Literal === Tokens.LBRACK) {
|
|
674
|
+
const val = this.parseAssignmentValue()
|
|
675
|
+
if (Array.isArray(val)) {
|
|
676
|
+
throw new Error('Unexpected array in property type parsing')
|
|
677
|
+
}
|
|
678
|
+
type = val
|
|
676
679
|
} else if (this.curToken.Type === Tokens.IDENT) {
|
|
677
680
|
type = {
|
|
678
681
|
Type: 'group' as PropertyReferenceType,
|
|
@@ -829,6 +832,22 @@ export default class Parser {
|
|
|
829
832
|
|
|
830
833
|
propertyTypes.push(prop)
|
|
831
834
|
|
|
835
|
+
/**
|
|
836
|
+
* ignore comments between type choice members, e.g.
|
|
837
|
+
* ```
|
|
838
|
+
* Foo = int ; comment
|
|
839
|
+
* / text
|
|
840
|
+
* ```
|
|
841
|
+
* or
|
|
842
|
+
* ```
|
|
843
|
+
* Foo = int / ; comment
|
|
844
|
+
* text
|
|
845
|
+
* ```
|
|
846
|
+
*/
|
|
847
|
+
while (this.curToken.Type === Tokens.COMMENT && this.peekToken.Type === Tokens.SLASH) {
|
|
848
|
+
this.parseComment()
|
|
849
|
+
}
|
|
850
|
+
|
|
832
851
|
/**
|
|
833
852
|
* ensure we don't go into the next choice, e.g.:
|
|
834
853
|
* ```
|
|
@@ -845,6 +864,9 @@ export default class Parser {
|
|
|
845
864
|
*/
|
|
846
865
|
while (this.curToken.Type === Tokens.SLASH) {
|
|
847
866
|
this.nextToken() // eat `/`
|
|
867
|
+
while ([Tokens.COMMENT].includes(this.curToken.Type)) {
|
|
868
|
+
this.parseComment()
|
|
869
|
+
}
|
|
848
870
|
propertyTypes.push(this.parsePropertyType())
|
|
849
871
|
if (!this.isOperator() && this.curToken.Type !== Tokens.SLASH) {
|
|
850
872
|
/**
|
|
@@ -854,6 +876,10 @@ export default class Parser {
|
|
|
854
876
|
this.nextToken()
|
|
855
877
|
}
|
|
856
878
|
|
|
879
|
+
while ([Tokens.COMMENT].includes(this.curToken.Type) && this.peekToken.Type === Tokens.SLASH) {
|
|
880
|
+
this.parseComment()
|
|
881
|
+
}
|
|
882
|
+
|
|
857
883
|
/**
|
|
858
884
|
* ensure we don't go into the next choice, e.g.:
|
|
859
885
|
* ```
|
|
@@ -262,6 +262,131 @@ exports[`Group Choice Parsing > Type Choice (/) > should correctly handle slash
|
|
|
262
262
|
]
|
|
263
263
|
`;
|
|
264
264
|
|
|
265
|
+
exports[`Group Choice Parsing > Type Choice (/) > should parse inline array alternatives inside map properties 1`] = `
|
|
266
|
+
[
|
|
267
|
+
{
|
|
268
|
+
"Comments": [],
|
|
269
|
+
"IsChoiceAddition": false,
|
|
270
|
+
"Name": "StorePutParams",
|
|
271
|
+
"Properties": [
|
|
272
|
+
{
|
|
273
|
+
"Comments": [],
|
|
274
|
+
"HasCut": true,
|
|
275
|
+
"Name": "storeNamespace",
|
|
276
|
+
"Occurrence": {
|
|
277
|
+
"m": 1,
|
|
278
|
+
"n": 1,
|
|
279
|
+
},
|
|
280
|
+
"Type": [
|
|
281
|
+
{
|
|
282
|
+
"Comments": [],
|
|
283
|
+
"Name": "",
|
|
284
|
+
"Type": "array",
|
|
285
|
+
"Values": [
|
|
286
|
+
{
|
|
287
|
+
"Comments": [],
|
|
288
|
+
"HasCut": false,
|
|
289
|
+
"Name": "",
|
|
290
|
+
"Occurrence": {
|
|
291
|
+
"m": Infinity,
|
|
292
|
+
"n": 0,
|
|
293
|
+
},
|
|
294
|
+
"Type": "text",
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
"Comments": [],
|
|
302
|
+
"HasCut": true,
|
|
303
|
+
"Name": "key",
|
|
304
|
+
"Occurrence": {
|
|
305
|
+
"m": 1,
|
|
306
|
+
"n": 1,
|
|
307
|
+
},
|
|
308
|
+
"Type": [
|
|
309
|
+
"text",
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
"Comments": [],
|
|
314
|
+
"HasCut": true,
|
|
315
|
+
"Name": "value",
|
|
316
|
+
"Occurrence": {
|
|
317
|
+
"m": 1,
|
|
318
|
+
"n": 1,
|
|
319
|
+
},
|
|
320
|
+
"Type": [
|
|
321
|
+
{
|
|
322
|
+
"Comments": [],
|
|
323
|
+
"IsChoiceAddition": false,
|
|
324
|
+
"Name": "",
|
|
325
|
+
"Properties": [
|
|
326
|
+
{
|
|
327
|
+
"Comments": [],
|
|
328
|
+
"HasCut": false,
|
|
329
|
+
"Name": "text",
|
|
330
|
+
"Occurrence": {
|
|
331
|
+
"m": Infinity,
|
|
332
|
+
"n": 0,
|
|
333
|
+
},
|
|
334
|
+
"Type": [
|
|
335
|
+
"any",
|
|
336
|
+
],
|
|
337
|
+
},
|
|
338
|
+
],
|
|
339
|
+
"Type": "group",
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
"Comments": [],
|
|
345
|
+
"HasCut": true,
|
|
346
|
+
"Name": "index",
|
|
347
|
+
"Occurrence": {
|
|
348
|
+
"m": Infinity,
|
|
349
|
+
"n": 0,
|
|
350
|
+
},
|
|
351
|
+
"Type": [
|
|
352
|
+
"bool",
|
|
353
|
+
{
|
|
354
|
+
"Comments": [],
|
|
355
|
+
"Name": "",
|
|
356
|
+
"Type": "array",
|
|
357
|
+
"Values": [
|
|
358
|
+
{
|
|
359
|
+
"Comments": [],
|
|
360
|
+
"HasCut": false,
|
|
361
|
+
"Name": "",
|
|
362
|
+
"Occurrence": {
|
|
363
|
+
"m": Infinity,
|
|
364
|
+
"n": 0,
|
|
365
|
+
},
|
|
366
|
+
"Type": "text",
|
|
367
|
+
},
|
|
368
|
+
],
|
|
369
|
+
},
|
|
370
|
+
],
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
"Comments": [],
|
|
374
|
+
"HasCut": true,
|
|
375
|
+
"Name": "ttl",
|
|
376
|
+
"Occurrence": {
|
|
377
|
+
"m": Infinity,
|
|
378
|
+
"n": 0,
|
|
379
|
+
},
|
|
380
|
+
"Type": [
|
|
381
|
+
"uint",
|
|
382
|
+
],
|
|
383
|
+
},
|
|
384
|
+
],
|
|
385
|
+
"Type": "group",
|
|
386
|
+
},
|
|
387
|
+
]
|
|
388
|
+
`;
|
|
389
|
+
|
|
265
390
|
exports[`Group Choice Parsing > Type Choice (/) > should parse type choice inside group 1`] = `
|
|
266
391
|
[
|
|
267
392
|
{
|
|
@@ -70,6 +70,21 @@ describe('Group Choice Parsing', () => {
|
|
|
70
70
|
expect(ast).toMatchSnapshot()
|
|
71
71
|
})
|
|
72
72
|
|
|
73
|
+
it('should treat comments around type choice separators the same', () => {
|
|
74
|
+
const slashAfterComment = `
|
|
75
|
+
FlowStrategy = "drop-oldest" ; Discard oldest when buffer is full
|
|
76
|
+
/ "pause-producer" ; Apply backpressure to slow production
|
|
77
|
+
/ "sample" ; Deliver every Nth event under pressure
|
|
78
|
+
`
|
|
79
|
+
const slashBeforeComment = `
|
|
80
|
+
FlowStrategy = "drop-oldest" / ; Discard oldest when buffer is full
|
|
81
|
+
"pause-producer" / ; Apply backpressure to slow production
|
|
82
|
+
"sample" ; Deliver every Nth event under pressure
|
|
83
|
+
`
|
|
84
|
+
|
|
85
|
+
expect(parse(slashAfterComment)).toEqual(parse(slashBeforeComment))
|
|
86
|
+
})
|
|
87
|
+
|
|
73
88
|
// This tests the change: closingTokens.includes(Tokens.RPAREN) && this.peekToken.Type === Tokens.SLASH
|
|
74
89
|
it('should correctly handle slash in mixed context', () => {
|
|
75
90
|
const cddl = `
|
|
@@ -78,6 +93,20 @@ describe('Group Choice Parsing', () => {
|
|
|
78
93
|
const ast = parse(cddl)
|
|
79
94
|
expect(ast).toMatchSnapshot()
|
|
80
95
|
})
|
|
96
|
+
|
|
97
|
+
it('should parse inline array alternatives inside map properties', () => {
|
|
98
|
+
const cddl = `
|
|
99
|
+
StorePutParams = {
|
|
100
|
+
storeNamespace: [* text],
|
|
101
|
+
key: text,
|
|
102
|
+
value: {* text => any},
|
|
103
|
+
? index: bool / [* text],
|
|
104
|
+
? ttl: uint,
|
|
105
|
+
}
|
|
106
|
+
`
|
|
107
|
+
const ast = parse(cddl)
|
|
108
|
+
expect(ast).toMatchSnapshot()
|
|
109
|
+
})
|
|
81
110
|
})
|
|
82
111
|
|
|
83
112
|
describe('Blocks with Braces', () => {
|
package/tests/parser.test.ts
CHANGED
|
@@ -41,4 +41,23 @@ describe('parser', () => {
|
|
|
41
41
|
expect(() => p.parse()).toThrow('group identifier expected')
|
|
42
42
|
vi.restoreAllMocks()
|
|
43
43
|
})
|
|
44
|
+
|
|
45
|
+
it('skips blank comment lines in assignment comments', () => {
|
|
46
|
+
vi.spyOn(fs, 'readFileSync').mockReturnValue('; heading\n;\nfoo = int\n')
|
|
47
|
+
const p = new Parser('foo.cddl')
|
|
48
|
+
|
|
49
|
+
expect(p.parse()).toEqual([{
|
|
50
|
+
Type: 'variable',
|
|
51
|
+
Name: 'foo',
|
|
52
|
+
IsChoiceAddition: false,
|
|
53
|
+
PropertyType: ['int'],
|
|
54
|
+
Comments: [{
|
|
55
|
+
Type: 'comment',
|
|
56
|
+
Content: 'heading',
|
|
57
|
+
Leading: false
|
|
58
|
+
}]
|
|
59
|
+
}])
|
|
60
|
+
|
|
61
|
+
vi.restoreAllMocks()
|
|
62
|
+
})
|
|
44
63
|
})
|