ckdl icon indicating copy to clipboard operation
ckdl copied to clipboard

How to parse a kdl file with comment, change a value then write it back with comments?

Open Gitzzq opened this issue 10 months ago • 1 comments

I tried to change KDL_DEFAULTS to KDL_EMIT_COMMENTS then I got "Invalid event from kdl_parser" exception....

Document parse(std::u8string_view kdl_text)
{
    kdl_str text = {reinterpret_cast<char const*>(kdl_text.data()), kdl_text.size()};
    kdl_parser* parser = kdl_create_string_parser(text, KDL_EMIT_COMMENTS);
    //                                                  ~~~~~~~~~~~~~~~~~~
}

Thanks,

Gitzzq avatar Feb 26 '25 07:02 Gitzzq

As you've noticed, the kdlpp data model doesn't know or care about comments or formatting, it only cares about the data. Even the order of properties is lost by being stored in a std::map.

This is not a document-oriented parser. Any information about things like empty lines, indentation, and exact position of comments in the document, are lost. I imagine you'd want to change a value somewhere in a file and leave the rest of the document untouched. For this to work you'd probably need a parser that works very differently.

While you can get the C API to emit events for comments (which the C++ bindings don't know what to do with, the way the API is designed you'd never be able to distinguish

( /* comment */ type ) node

from

/* comment */ (type)node

or

(type) /* comment */ node

tjol avatar Feb 26 '25 08:02 tjol