Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence + optional rule yields unexpected results #125

Closed
anarthal opened this issue Feb 27, 2024 · 1 comment
Closed

Sequence + optional rule yields unexpected results #125

anarthal opened this issue Feb 27, 2024 · 1 comment

Comments

@anarthal
Copy link

The following program

#include <boost/parser/parser.hpp>

#include <optional>

namespace bp = boost::parser;

constexpr auto print_repl_field = [](auto& ctx) {
    const std::optional<unsigned short>& val = bp::_attr(ctx);
    if (val)
    {
        std::cout << "Found integer: " << *val << std::endl;
    }
    else
    {
        std::cout << "No integer found\n";
    }
};

constexpr auto replacement_field_def = bp::lit('{') >> -bp::ushort_;

constexpr bp::rule<struct replacement_field_rule, std::optional<unsigned short>>
    replacement_field = "replacement_field";

BOOST_PARSER_DEFINE_RULES(replacement_field)

int main(int argc, char** argv) { bp::parse(argv[1], replacement_field[print_repl_field]); }

Yields the following

./build/main '{9'
Found integer: 9 // correct

./build/main '{'
Found integer: 0 // incorrect

Doesn't happen if I remove the rule and make it a regular parser.

Compiler: gcc-12, debug, under Linux.

@tzlaine
Copy link
Owner

tzlaine commented Feb 27, 2024

Interesting. Thanks for reporting this! I'll investigate.

tzlaine added a commit that referenced this issue Mar 7, 2024
… was

that the changes to support recursive rules made all rules use the out-arg
overload of parse_rule.  This is wrong.  Now, rules use the
attribute-returning overload unless asked explicitly to use the out-arg one,
*or* if the rule is being invoked recursively, and we are in an iteration
after the 0th.

Fixes #125.
tzlaine added a commit that referenced this issue Mar 7, 2024
… was

that the changes to support recursive rules made all rules use the out-arg
overload of parse_rule.  This is wrong.  Now, rules use the
attribute-returning overload unless asked explicitly to use the out-arg one,
*or* if the rule is being invoked recursively, and we are in an iteration
after the 0th.

Fixes #125.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants