-
I'm trying to parse SQL select statements and then generate code to iterate over a std::vector. The full example can be seen here: https://godbolt.org/z/P9nvPej9d
Then I could extract the person's age using:
or the whole person if I supplied the SQL asterisk meaning all columns:
My main problem is that my special variant class does not seem to be constexpr. I believe this is because the StringLiteral class is not fully constexpr. This seems to be because lexy calls push_back as it parses each char of a data member. From my understanding of lexy, it should be possible to use it in constexpr and consteval settings. Does anyone have any sugestions, as I have been going around in circles for about a week. Edited: Fixed a hold_alternative problem, but the errors still remain. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The errors generated are the following: <source>:134:18: error: constexpr variable 'result' must be initialized by a constant expression
134 | constexpr auto result = lexy::parse<grammar::selectCols>(input, lexy_ext::report_error);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:148:12: note: in instantiation of function template specialization 'extractData<StringLiteral<>{{42}, 1}>' requested here
148 | return extractData<"*">(child).age;
| ^
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/action/validate.hpp:260:85: note: non-constexpr function 'finish' cannot be used in a constant expression
260 | return Result(rule_parse_result, LEXY_MOV(_cb.sink->template get<sink_t>()).finish());
| ^
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/action/parse.hpp:125:23: note: in call to 'static_cast<std::remove_reference_t<decltype(this->_validate)> &&>(this->_validate).get_result<lexy::validate_result<lexy_ext::_report_error<lexy::stderr_output_iterator>>>(true)'
125 | return Result(LEXY_MOV(_validate).template get_result<validate_result>(rule_parse_result),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/_detail/config.hpp:29:23: note: expanded from macro 'LEXY_MOV'
29 | #define LEXY_MOV(...) static_cast<std::remove_reference_t<decltype(__VA_ARGS__)>&&>(__VA_ARGS__)
| ^
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/action/base.hpp:242:16: note: in call to 'static_cast<std::remove_reference_t<decltype(control_block.parse_handler)> &&>(control_block.parse_handler).get_result<lexy::parse_result<LexyVariant<ast::AllColumns, StringLiteral<>>, lexy_ext::_report_error<lexy::stderr_output_iterator>>, LexyVariant<ast::AllColumns, StringLiteral<>>>(true, context.value._lazy_init_storage_trivial::union (anonymous union at /opt/compiler-explorer/libs/lexy/trunk/include/lexy/_detail/lazy_init.hpp:17:5)._lazy_init_storage_trivial::_value)'
242 | return LEXY_MOV(control_block.parse_handler)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243 | .template get_result<Result<value_type>>(rule_result, LEXY_MOV(*context.value));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/_detail/config.hpp:29:23: note: expanded from macro 'LEXY_MOV'
29 | #define LEXY_MOV(...) static_cast<std::remove_reference_t<decltype(__VA_ARGS__)>&&>(__VA_ARGS__)
| ^
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/action/parse.hpp:164:16: note: in call to 'do_action<grammar::selectCols, lexy::parse_action<void, lexy::string_input<lexy::ascii_encoding>, lexy_ext::_report_error<lexy::stderr_output_iterator>>::result_type, lexy::_ph<lexy::_pr<lexy::ascii_encoding>>, void, lexy::_pr<lexy::ascii_encoding>>(handler(input_holder, sink), nullptr, reader)'
164 | return lexy::do_action<Production, result_type>(handler(input_holder, sink), _state,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165 | reader);
| ~~~~~~~
/opt/compiler-explorer/libs/lexy/trunk/include/lexy/action/parse.hpp:173:12: note: in call to 'parse_action<void, string_input<ascii_encoding>, _report_error<stderr_output_iterator>>(callback).operator()<grammar::selectCols>({}, input)'
173 | return parse_action<void, Input, ErrorCallback>(callback)(Production{}, input);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:134:27: note: in call to 'parse<grammar::selectCols, lexy::string_input<lexy::ascii_encoding>, lexy_ext::_report_error<lexy::stderr_output_iterator>>(input, report_error)'
134 | constexpr auto result = lexy::parse<grammar::selectCols>(input, lexy_ext::report_error);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/lexy/trunk/include/lexy_ext/report_error.hpp:344:21: note: declared here
344 | std::size_t finish() &&
| ^
<source>:135:17: error: constexpr if condition is not a constant expression
135 | if constexpr (!result.has_value()) {
| ^~~~~~~~~~~~~~~~~~~
<source>:135:25: note: initializer of 'result' is not a constant expression
135 | if constexpr (!result.has_value()) {
| ^
<source>:134:18: note: declared here
134 | constexpr auto result = lexy::parse<grammar::selectCols>(input, lexy_ext::report_error);
| ^
<source>:138:19: error: constexpr if condition is not a constant expression
138 | if constexpr (std::holds_alternative<ast::AllColumns>(result.value().value)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:138:66: note: initializer of 'result' is not a constant expression
138 | if constexpr (std::holds_alternative<ast::AllColumns>(result.value().value)) {
| ^
<source>:134:18: note: declared here
134 | constexpr auto result = lexy::parse<grammar::selectCols>(input, lexy_ext::report_error);
| ^
<source>:139:7: error: 'auto' in return type deduced as 'Person' here but deduced as 'void' in earlier return statement
139 | return p;
| ^
<source>:148:35: error: member reference base type 'void' is not a structure or union
148 | return extractData<"*">(child).age;
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~
5 errors generated. |
Beta Was this translation helpful? Give feedback.
lexy_ext::report_error
isn't constexpr. You need to write a constexpr error handler: https://lexy.foonathan.net/reference/action/validate/#error-callback