foonathan / lexy

C++ parsing DSL
https://lexy.foonathan.net
Boost Software License 1.0
1.01k stars 67 forks source link

Passing a `constexpr std::string_view` to LEXY_KEYWORD #203

Open bruno-j-nicoletti opened 4 months ago

bruno-j-nicoletti commented 4 months ago

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.

foonathan commented 4 months ago

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?

bruno-j-nicoletti commented 4 months ago

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 commented 4 months ago

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

foonathan commented 4 months ago

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

bruno-j-nicoletti commented 4 months ago

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.