Having trouble finding the right sink / callback combo for a function call postfix operator #237
-
Hello, I am trying to make a parser for a toy programming language. I am trying to implement function calls as a postfix operator in a Lexy expression. I took example from the calculator example. This is the expression production: struct nested_expression: lexy::transparent_production {
static constexpr auto rule = dsl::recurse<struct expression>;
static constexpr auto value = lexy::forward<ExpressionSyntaxPtr>;
};
struct expression: lexy::expression_production {
static constexpr auto whitespace = dsl::ascii::blank;
static constexpr auto atom = dsl::parenthesized(dsl::p<nested_expression>) | dsl::p<integer_literal> | dsl::p<identifier>;
struct addition: dsl::infix_op_left {
static constexpr auto op = dsl::op<BinaryOperatorExpressionSyntax::PLUS>(LEXY_LIT("+")) / dsl::op<BinaryOperatorExpressionSyntax::MINUS>(LEXY_LIT("-"));
using operand = dsl::atom;
};
struct function_call: dsl::postfix_op {
static constexpr auto op = dsl::op<void>(dsl::parenthesized.opt_list(dsl::p<nested_expression>, dsl::trailing_sep(dsl::comma)));
using operand = addition;
};
using operation = function_call;
static constexpr auto value = lexy::callback<ExpressionSyntaxPtr>(
// Atoms
lexy::forward<ExpressionSyntaxPtr>,
lexy::new_<IntegerLiteralExpressionSyntax, ExpressionSyntaxPtr>,
lexy::new_<IdentifierExpressionSyntax, ExpressionSyntaxPtr>,
// Operators
lexy::new_<BinaryOperatorExpressionSyntax, ExpressionSyntaxPtr>,
// Function call
lexy::new_<FunctionCallExpressionSyntax, ExpressionSyntaxPtr>
);
};
using ExpressionSyntaxPtr = std::shared_ptr<struct ExpressionSyntax>;
struct ExpressionSyntax { /* ... */ };
struct FunctionCallExpressionSyntax: public ExpressionSyntax {
ExpressionSyntaxPtr called_expression;
std::vector<ExpressionSyntaxPtr> arguments;
explicit FunctionCallExpressionSyntax(ExpressionSyntaxPtr called_expression, std::vector<ExpressionSyntaxPtr> arguments): called_expression(called_expression), arguments(arguments) {}
// ...
} When trying to compile this, I have the error "missing value sink for production". I understand that this comes from the From what I understand from the documentation, I need to use a sink with the I think there is a right combination of sink / callback to use but I have a hard time figuring what it is. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I think the issue is that FYI, the static assert "missing value callback for production" shows you the callback that is missing in the error message in the types of the
This means "the callback for |
Beta Was this translation helpful? Give feedback.
I think the issue is that
opt_list
produceslexy::nullopt
when the list is not empty, not an empty sink: https://lexy.foonathan.net/reference/dsl/terminator/#_rule_opt_list So you need to add a constructor toFunctionCallExpressionSyntax
that acceptsstd::nullopt
, or change it to one that constructs astd::optional<std::vector<...>>
.FYI, the static assert "missing value callback for production" shows you the callback that is missing in the error message in the types of the
_detail::error
instantiation. For example: https://godbolt.org/z/6ojrMYWf5 fails with