foonathan/lexy

Compile-time parsing quoted lists does not work

jgopel opened this issue · 3 comments

I'm trying to build a compile-time parser for graphviz DOT. I'm working on parsing quoted node names into string views. I can parse without the quotes, but not with them.

Sample code: https://godbolt.org/z/xf91q66Mz

Note: I have tried this with latest main and I am getting the same failure.

When you have escape sequences, you cannot parse it into a std::string_view, you have to use std::string instead: https://godbolt.org/z/oqMsxr9Ed

(You also need to specify a Unicode input encoding due to -dsl::ascii::control, but that appears to be a bug in lexy. You should get that with the default as well, I think.)

That breaks its usability in compile-time contexts though. I know it's supposed to be supported, but neither gcc nor clang seems to have fully implemented it. Simply changing the assert to a static_assert causes failure in both compilers. https://godbolt.org/z/1dbG86Mb4

I've done all I can do on my end, but std::string isn't constexpr.

You can write your own constexpr_string though on top of std::vector; this is the interface required by lexy::as_string:

struct constexpr_string
{
    std::vector<char> impl;

    constexpr char operator[](std::size_t idx) const
    {
        return impl[idx];
    }

    constexpr void push_back(char c)
    {
        impl.push_back(c);
    }
    constexpr void append(constexpr_string&& string)
    {
        impl.insert(impl.end(), string.impl.begin(), string.impl.end());
    }
    template <typename iterator>
    constexpr void append(iterator begin, iterator end)
    {
        impl.insert(impl.end(), begin, end);
    }
};