foonathan / lexy

C++ parsing DSL
https://lexy.foonathan.net
Boost Software License 1.0
991 stars 66 forks source link

How to signal a parse error from a value callback? #148

Closed Korsar13 closed 1 year ago

Korsar13 commented 1 year ago

Discussed in https://github.com/foonathan/lexy/discussions/147

Originally posted by Korsar13 June 15, 2023

std::set<std::string> block_names;

static constexpr auto ident = dsl::identifier( dsl::ascii::alpha, dsl::ascii::alpha_digit_underscore );

struct block_name
{
    static constexpr auto rule  = ident;
    static constexpr auto value = lexy::callback<std::string>(
        []( const auto& lx ){
            auto s = std::string{ lx.begin(), lx.end() };
            if ( !block_names.insert( s ).second )
            {
                // error "redefinition" ?
            }
            return std::move(s);
        }
    );
};