CapnProto C++ as library for parsing textual schemas

37 views
Skip to first unread message

John Demme

unread,
Jun 18, 2020, 7:15:22 PM6/18/20
to Cap'n Proto
Hello all-

I've spent a lot of time digging around the CapnProto C++ code base, but I can't figure how to point a function to a textual capnp schema and get out the CodeGenRequest. Parsing to a capnp::Schema is not sufficient as that class only represents the root node with (AFAICT) no way to access the other nodes and other information in the CodeGenRequest. I saw there was infrastructure to do this in the compiler, but those header files aren't distributed with the library code.

~John

Kenton Varda

unread,
Jun 18, 2020, 7:59:51 PM6/18/20
to John Demme, Cap'n Proto
Hi John,

`capnp::SchemaParser` will parse capnp files and give you a `capnp::ParsedSchema`, which is like `capnp::Schema` but also has the `getNested()` method which allows you to traverse the whole tree of child nodes. So it's not just the root node.

That said, it's true that it's not super-convenient to construct a `CodeGeneratorRequest` from this. You would need to traverse the tree and flatten it into a list, which is easy enough, but filling in `requestedFiles` (especially `imports`) might be tricky.

Out of curiosity, why do you need to construct a `CodeGeneratorRequest`?

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/d23c4480-ab9a-4b77-9a5d-02535698bc01o%40googlegroups.com.

John Demme

unread,
Jun 18, 2020, 9:25:46 PM6/18/20
to Kenton Varda, Cap'n Proto
Hi Kenton-

I only see:
  ParsedSchema getNested(kj::StringPtr name) const;
  // Gets the nested node with the given name, or throws an exception if there is no such nested
  // declaration.

If there were a getNested() which returned a list of ParsedSchemas that'd probably be sufficient. Knowing what file housed a particular node would be nice, but not strictly necessary.

I'm writing software which takes in a capnp schema along with a bunch of other stuff. It's basically a message passing compiler which can #include capnp schemas to use for message schemas. Only there's no language associated with it, so a source-to-source translator (codegen) doesn't apply.

~John

Kenton Varda

unread,
Jun 18, 2020, 9:40:13 PM6/18/20
to m...@teqdruid.com, Cap'n Proto
`schema.getProto().getNestedNodes()` returns a list of `capnp::schema::Node::NestedNode`, which has `name` and `id` fields. So that gives you the names of the nested declarations, which you can get plug into `getNested()`.

It's a little ugly, but in general, the C++ schema classes only have methods for things that aren't already easy to get from the underlying capnp struct.

That said, I could be convinced that a `getAllNested()` that returns some sort of iterable would be worthwhile here.

-Kenton

John Demme

unread,
Jun 18, 2020, 10:54:02 PM6/18/20
to Kenton Varda, Cap'n Proto
Yeah, I thought of that but it was too ugly. Also, I had some C# code which worked on a CodeGenRequest lying around from an abandoned version of this. There was another reason as well, though I don't remember it now...

If that method existed, I'd use it ;)

~John

John Demme

unread,
Jun 19, 2020, 7:21:09 PM6/19/20
to Cap'n Proto
To unsubscribe from this group and stop receiving emails from it, send an email to capn...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capn...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages