Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to parse & lines #312

Open
alistair23 opened this issue Mar 23, 2023 · 3 comments
Open

Unable to parse & lines #312

alistair23 opened this issue Mar 23, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@alistair23
Copy link

zcbor is unable to parse the & (choice) operator

For example when running

zcbor code --cddl ./test.cddl -t spdm-toc --encode --output-c out.c --output-h out.h

on:

$version-scheme /= decimal

version-map = {
  &(version: 0) => text
  ? &(version-scheme: 1) => $version-scheme
}

The following exception occurs

Parsing files: ./test.cddl, .local/lib/python3.10/site-packages/zcbor/cddl/prelude.cddl
Traceback (most recent call last):
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 847, in get_value
    handler(match_str)
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 724, in <lambda>
    lambda map_str: self.type_and_value("MAP", lambda: self.parse(map_str))),
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 490, in type_and_value
    self.set_value(value_generator)
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 493, in set_value
    value = value_generator()
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 724, in <lambda>
    lambda map_str: self.type_and_value("MAP", lambda: self.parse(map_str))),
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 934, in parse
    instr = value.get_value(instr)
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 858, in get_value
    raise TypeError("Could not parse this: '%s'" % instr)
TypeError: Could not parse this: '&(version: 0) => text   ? &(version-scheme: 1) => $version-scheme'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".local/bin/zcbor", line 8, in <module>
    sys.exit(main())
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 3081, in main
    args.process(args)
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 2940, in process_code
    cddl_res[mode] = CodeGenerator.from_cddl(
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 1699, in from_cddl
    cddl_res = super(CodeGenerator, cddl_class).from_cddl(*args, **kwargs)
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 269, in from_cddl
    parsed.get_value(cddl_string.replace("\n", " ").lstrip("&"))
  File ".local/lib/python3.10/site-packages/zcbor/zcbor.py", line 849, in get_value
    raise Exception("Failed while parsing this: '%s'" % match_str) from e
Exception: Failed while parsing this: '   &(version: 0) => text   ? &(version-scheme: 1) => $version-scheme '
@oyvindronningstad
Copy link
Collaborator

Thanks for the report. This is not supported, and there are currently no plans to support it, since AFAIU you can acheive the same thing quite easily with '/'. I will track this internally though.

@nslowell
Copy link

nslowell commented Aug 7, 2023

As the RFC hints at in 2.2.2.2., there is value in this feature in the situation where many values are given semantic names in a group but then someone has a desire to create a choice. It would be much more tedious to accumulate the names into a choice using '/' than just simply using the group that already exists and using '&'.

@oyvindronningstad oyvindronningstad added the enhancement New feature or request label Aug 15, 2024
@sam-golioth
Copy link

This tripped me up today as well. I find that the & operator makes the CDDL more readable and maintainable, and would very much appreciate support being added into zcbor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants