Skip to content

? occurrence indicator parses with an unbounded upper bound instead of 1 #90

Description

@Mearman

parseOccurrences() in packages/cddl/src/parser.ts treats `?`, `*`, and `+` as sharing the same default upper bound (Infinity):

```ts
if (this.curToken.Type === Tokens.QUEST || this.curToken.Type === Tokens.ASTERISK || this.curToken.Type === Tokens.PLUS) {
const n = this.curToken.Type === Tokens.PLUS ? 1 : 0
let m = Infinity
...
```

Per RFC 8610 §3.5.1, `?` means zero-or-one (`{n:0, m:1}`), while `` means zero-or-more (`{n:0, m:Infinity}`). Only `` (and the bare `n*` numbered form) should default `m` to unbounded; `?` should default `m` to 1.

Repro:

```ts
import Parser from 'cddl/parser.js'
// mock readFileSync to return:
// foo = { ? bar: tstr }
const p = new Parser('foo.cddl')
console.log(p.parse()[0].Properties[0].Occurrence)
// actual: { n: 0, m: Infinity }
// expected: { n: 0, m: 1 }
```

This silently merges "optional single value" and "repeated value" into the same parsed shape, which breaks any downstream consumer (a codegen tool, a schema validator) that needs to distinguish the two rather than just checking `n === 0`.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions