Parser caching: transformers would need to accept const lvalue references.

15 views
Skip to first unread message

ku...@mareimbrium.org

unread,
Jul 8, 2019, 1:08:00 PM7/8/19
to Cap'n Proto
I'm experimenting with caching lexer output, and have devised a `cache()` combinator
that takes a parser outputting `Foo` and returns a parser outputting `const Foo &`. The subparser
is queried only once for a given position in the input.

This works a treat until you get to transforms: the functors all take rvalue references. The quick
fix is to let the cache copy results. Another is to make the functors take `auto` arguments.
Yet another would be to change the transformer argument types to take const lvalue references
where the lower level parsing is cached.

Is there anything more generic or appropriate that you'd suggest to address this problem?

The cache might be an unnecessary addition, but at least it can be benchmarked, and the hit
ratio looked at, and then removed should it be redundant. I've ported our crusty in-house DSL
compiler to kj framework and the cache really helps there, because of how the grammar is
written. Of course, the grammar could be fixed, but the cache is a decent stop-gap.

An aside: the parsing framework is a joy to work with, as long as one doesn't need to decode
error messages. I'll have to eventually look at more obvious places to add compile-time error
catching to. Usually, it's some low level function applicator in the framework that bombs out,
with the real problem far away. Visual Studio's approach to error reporting doesn't help either.

Cheers, Kuba

Kenton Varda

unread,
Jul 8, 2019, 1:13:39 PM7/8/19
to ku...@mareimbrium.org, Cap'n Proto
Hi Kuba,

As a quick fix, you could always pass a pointer instead of a reference. Or you could wrap the reference in a struct.

I agree that lvalue references "should" work, but it might be tricky to design the parser code such that it doesn't accidentally decide to use a reference where inappropriate, and end up with a dangling reference. Or maybe it would be easy... I haven't looked much at that code in about 5 years, TBH, so I don't quite remember.

-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.
Visit this group at https://groups.google.com/group/capnproto.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/1de280d3-e818-4c4b-af5c-126916267116%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages