lexy icon indicating copy to clipboard operation
lexy copied to clipboard

Passing a `constexpr std::string_view` to LEXY_KEYWORD

Open bruno-j-nicoletti opened this issue 1 year ago • 5 comments

This is very minor issue with the API.

For constants I use constexpr over #define wherever possible. I want to have the set of keywords used by my grammar defined via constexpr std::string_view and use them elsewhere in my code. Unfortunately the LEXY_KEYWORD macro relies on values passed to it to be literals, so I need to #define them.

Not a pressing issue, but a nice API change if it is simple enough to do.

bruno-j-nicoletti avatar May 22 '24 15:05 bruno-j-nicoletti

lexy needs to encode the strings in the type system (it internally creates a type_string<char, 'a', 'b', 'c'> type), once it's in a std::string_view LEXY_KEYWORD can't get them out of there easily.

If you use C++20, I could support dsl::keyword<std::string_view("abc")>(identifier) though. Would that help?

foonathan avatar May 22 '24 20:05 foonathan

Ah, perfect. I'm on C++23.

Thank you so much for getting back to me on everything. Very much appreciate the work you've put into the library and responding to me.

bruno-j-nicoletti avatar May 23 '24 07:05 bruno-j-nicoletti

I spoke too soon. That refuses to compile because you can't pass a string_view instance as an NTTP. I tried passing in a constexpr string class along the lines of the one below, and it gets further, but it still breaks compilation.

This is definitely a rabbit hole I've dragged you down. Appologies.

https://oleksandrkvl.github.io/2021/04/02/cpp-20-overview.html#nttp

bruno-j-nicoletti avatar May 23 '24 07:05 bruno-j-nicoletti

I was thinking of something like this, the problem is that I can't get the size where I have ???: https://godbolt.org/z/7zjbfWj96

foonathan avatar May 23 '24 16:05 foonathan

size is a member function so that can't work. I've spent a bit of time playing with approaches and I can almost but not quite get it to work via a helper function. string_view::size() is constexpr, but I cant figure out how to get the helper function to accept an string_view argument and remain constexpr

https://godbolt.org/z/4GbY7W4ae

Uncomment line 55 to see the issue.

bruno-j-nicoletti avatar May 24 '24 11:05 bruno-j-nicoletti