Skip to content

Commit 4e853af

Browse files
fix(cddl): choice member operators in parser
1 parent 0aeacd2 commit 4e853af

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

packages/cddl/src/parser.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export default class Parser {
117117
Type: 'variable',
118118
Name: groupName,
119119
IsChoiceAddition: isChoiceAddition,
120-
PropertyType: this.parsePropertyTypes(),
120+
PropertyType: this.parsePropertyTypes(true),
121121
Comments: []
122122
}
123123

@@ -148,7 +148,7 @@ export default class Parser {
148148
) {
149149
const propertyType: PropertyType[] = []
150150
while (!closingTokens.includes(this.curToken.Type)) {
151-
propertyType.push(...this.parsePropertyTypes())
151+
propertyType.push(...this.parsePropertyTypes(true))
152152
if (closingTokens.includes(this.curToken.Type)) {
153153
this.nextToken()
154154
break
@@ -820,7 +820,7 @@ export default class Parser {
820820
return this.curToken.Literal === Tokens.DOT && OPERATORS.includes(this.peekToken.Literal as OperatorType)
821821
}
822822

823-
private parsePropertyTypes (): PropertyType[] {
823+
private parsePropertyTypes (attachChoiceOperators = false): PropertyType[] {
824824
const propertyTypes: PropertyType[] = []
825825

826826
let prop: PropertyType = this.parsePropertyType()
@@ -870,14 +870,22 @@ export default class Parser {
870870
while ([Tokens.COMMENT].includes(this.curToken.Type)) {
871871
this.parseComment()
872872
}
873-
propertyTypes.push(this.parsePropertyType())
874-
if (!this.isOperator() && this.curToken.Type !== Tokens.SLASH) {
873+
let nextProp: PropertyType = this.parsePropertyType()
874+
if (this.isOperator()) {
875+
if (attachChoiceOperators) {
876+
nextProp = {
877+
Type: nextProp,
878+
Operator: this.parseOperator()
879+
} as NativeTypeWithOperator
880+
}
881+
} else if (this.curToken.Type !== Tokens.SLASH) {
875882
/**
876883
* If we are not parsing an operator, we need to eat the next token;
877884
* otherwise, the operator will be parsed by the caller
878885
*/
879886
this.nextToken()
880887
}
888+
propertyTypes.push(nextProp)
881889

882890
while ([Tokens.COMMENT].includes(this.curToken.Type) && this.peekToken.Type === Tokens.SLASH) {
883891
this.parseComment()

packages/cddl/tests/parser.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,33 @@ describe('parser', () => {
8585

8686
vi.restoreAllMocks()
8787
})
88+
89+
it('parses type choices with regexp operators on later members', () => {
90+
vi.spyOn(fs, 'readFileSync').mockReturnValue('channel = "values" / tstr .regexp "custom:.+"\n')
91+
const p = new Parser('foo.cddl')
92+
93+
expect(p.parse()).toEqual([{
94+
Type: 'variable',
95+
Name: 'channel',
96+
IsChoiceAddition: false,
97+
PropertyType: [{
98+
Type: 'literal',
99+
Value: 'values',
100+
Unwrapped: false
101+
}, {
102+
Type: 'tstr',
103+
Operator: {
104+
Type: 'regexp',
105+
Value: {
106+
Type: 'literal',
107+
Value: 'custom:.+',
108+
Unwrapped: false
109+
}
110+
}
111+
}],
112+
Comments: []
113+
}])
114+
115+
vi.restoreAllMocks()
116+
})
88117
})

0 commit comments

Comments
 (0)