yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
879 stars 112 forks source link

Compilation error when trying to pass both `any& dt` and an output param to peg::parser::parse() #270

Closed curldivergence closed 1 year ago

curldivergence commented 1 year ago

Hi! First of all, thank you for such an outstanding library :) But unfortunately, I faced a compilation problem today: as mentioned in the title, I've tried to use the overload of peg::parser::parse() that accepts both an std::any& dt and an output parameter T& (my use case is such that in dt I'm trying to pass my symbol table to the semantic actions, and in the output parameter I'm passing the root of my AST). The compilation error consists in the inability to bind a temporary to a non-const reference:

In file included from /Users/andrew/Dev/cpp-peglib/test/test1.cc:2:
/Users/andrew/Dev/cpp-peglib/test/../peglib.h:4542:27: error: non-const lvalue reference to type 'Definition::Result' cannot bind to a temporary of type 'peg::Definition::Result'
                          rule.parse_and_get_value(s, n, dt, val, path, log_));
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/andrew/Dev/cpp-peglib/test/../peglib.h:4564:12: note: in instantiation of function template specialization 'peg::parser::parse_n<int>' requested here
    return parse_n(sv.data(), sv.size(), dt, val, path);
           ^
/Users/andrew/Dev/cpp-peglib/test/test1.cc:223:10: note: in instantiation of function template specialization 'peg::parser::parse<int>' requested here
  parser.parse("42", dt, output);
         ^
/Users/andrew/Dev/cpp-peglib/test/../peglib.h:4662:66: note: passing argument to parameter 'r' here
  bool post_process(const char *s, size_t n, Definition::Result &r) const {

I've put together a minimal test and committed it here, so may I ask you to kindly have a look and give a bit advice regarding dealing with this? :) Maybe I'm doing something wrong on my side?

Thank you!

yhirose commented 1 year ago

@curldivergence thank you for the report! I fixed it in peglib.h.

curldivergence commented 1 year ago

@yhirose Thank you very much for such a quick fix!