How to parse a kdl file with comment, change a value then write it back with 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,
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