From 41e891dc95b5d2de16985813b2bca531fdb21e3f Mon Sep 17 00:00:00 2001 From: Zach Laine Date: Mon, 13 Oct 2025 17:46:27 -0500 Subject: [PATCH] In opt_parser, don't forget to clear the attribute if the subparser fails. Fixes #279 --- include/boost/parser/parser.hpp | 2 ++ test/github_issues.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/boost/parser/parser.hpp b/include/boost/parser/parser.hpp index 6d1d8d29..ddfff576 100644 --- a/include/boost/parser/parser.hpp +++ b/include/boost/parser/parser.hpp @@ -3235,6 +3235,8 @@ namespace boost { namespace parser { //[ opt_parser_gen_attr_path parser_.call(first, last, context, skip, flags, success, retval); + if (!success) + retval = Attribute(); success = true; //] } diff --git a/test/github_issues.cpp b/test/github_issues.cpp index 1aab4af2..f64beccb 100644 --- a/test/github_issues.cpp +++ b/test/github_issues.cpp @@ -344,6 +344,23 @@ void github_issue_248() } } +void github_issue_279() +{ + namespace bp = boost::parser; + + constexpr auto condition_clause = bp::lit(U"while") > + bp::lit(U"someexpression") >> bp::attr(true); + + constexpr auto do_statement = + bp::lexeme[bp::lit(U"do") >> &bp::ws] > -condition_clause > bp::eol; + + auto const result = bp::parse( + U"do\n", do_statement, bp::blank, bp::trace::off); + BOOST_TEST(result); + std::optional const & condition = result.value(); + BOOST_TEST(!condition.has_value()); +} + int main() { @@ -356,5 +373,6 @@ int main() github_issue_209(); github_issue_223(); github_issue_248(); + github_issue_279(); return boost::report_errors(); }