foonathan / lexy

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

Surprising debug behavior when re-using `lexy::scan_result<>` #133

Closed nickelpro closed 1 year ago

nickelpro commented 1 year ago

I have no idea if this counts as a bug, it seems to be intended behavior but I don't understand why

Consider the following:

namespace dsl = lexy::dsl;

struct word {
  static constexpr auto rule = dsl::identifier(dsl::ascii::alpha);
  static constexpr auto value = lexy::as_string<std::string>;
};

int main() {

  auto in {lexy::zstring_input("abc def")};

  auto scanner {lexy::scan(in, lexy_ext::report_error)};

  lexy::scan_result<std::string> result;

  while(scanner.branch<word>(result)) {
    fmt::print("{}\n", result.value());
    scanner.parse(dsl::whitespace(dsl::ascii::blank));
  }
}

This builds and runs as expected under a release build, but in a debug build hits the precondition assert in lazy init.

This is maybe a documentation bug? Or I'm just dumb?

foonathan commented 1 year ago

It's a bug/oversight in the library, but fixed now.

nickelpro commented 1 year ago

Much appreciated :grinning: