Skip to content

CDDL parser v2 - #41

Open
nitely wants to merge 1 commit into
mainfrom
cddl_dump
Open

nitely wants to merge 1 commit into
mainfrom
cddl_dump

Conversation

@nitely

@nitely nitely commented Aug 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #37

Changes:

  • Deprecate cbor_serialization/cddl/parser
  • Add cbor_serialization/cddl/parser2
  • Add CddlNode node based AST
  • Add proc parseCddl*(source: string, ignoreComments = false): CddlSchema, convert a CDDL into an AST
  • Add proc toCddl*(n: CddlNode, pretty = false, verify = true): string which converts an AST to CDDL. It also parses the resulting CDDL and checks the AST is the same as the original.
  • Add proc dumpTree*(n: CddlNode): string, similar to Nim macro dumpTree, it returns the AST structure.
  • Add func isValid*(n: CddlNode): bool =, checks an AST structure is valid.
  • Add roundtrip tests. The toCddl cannot keep the original format, but it should produce the same CDDL schema; so the roundtrip test is done on a pre-formatted schema that matches toCddl output.
  • Add cbor_serialization/cddl/parser2_schema.md which contains a cddl of the parser Node. It can be used to generate a matching DS. The type_generator is not able to parse it yet (requires union support).

@nitely
nitely requested a review from arnetheduck August 4, 2026 22:24
@nitely
nitely marked this pull request as ready for review August 4, 2026 22:24
@nitely

nitely commented Aug 5, 2026 •

Copy link
Copy Markdown
Collaborator Author

@arnetheduck is cbor_serialization/cddl/parser2_schema.md what you meant by schema of the schema? it can be used to encode a schema into CBOR for example.

For the toCddl (printer) output, you can check tests/test_cddl_parser_36_dump_pretty.txt to see what it produces. It also checks the resulting AST matches the original one (similar to what nph does I think); so it cannot construct a different/invalid CDDL; there is a param to disable it because it can be slow (it needs to parse the output).

No need to review the code I think. It's mostly tests, though. One thing to notice is the CddlNode has the =copy disallowed in Nim +2.2 with ORC to avoid unintended AST copies; an alternative would be making the CddlNode a ref, but then you cannot store it in a const (although, it's still not clear to me what's the use case).

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CDDL schema printer

1 participant