From a2c9c907e96fd0dbf6506f387562c487f0bc2816 Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Mon, 4 Aug 2025 13:14:57 +0200 Subject: [PATCH] Add repeat_directive::operator() to allow repeat with delimiter --- include/boost/parser/parser.hpp | 27 +++++++++++++++++++++++++++ test/github_issues.cpp | 19 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/include/boost/parser/parser.hpp b/include/boost/parser/parser.hpp index c701ca49..164fe363 100644 --- a/include/boost/parser/parser.hpp +++ b/include/boost/parser/parser.hpp @@ -3087,6 +3087,23 @@ namespace boost { namespace parser { for (int64_t end = detail::resolve(context, min_); count != end; ++count) { + if constexpr (!detail::is_nope_v) { + if (count != 0) { + detail::skip(first, last, skip, flags); + delimiter_parser_.call( + first, + last, + context, + skip, + detail::disable_attrs(flags), + success); + if (!success) { + detail::assign(retval, Attribute()); + return; + } + } + } + detail::skip(first, last, skip, flags); attr_t attr{}; parser_.call( @@ -6405,6 +6422,16 @@ namespace boost { namespace parser { repeat_parser_type{rhs.parser_, min_, max_}}; } + + template + constexpr auto operator()(parser_interface rhs, parser_interface delim) const noexcept + { + using repeat_parser_type = + repeat_parser; + return parser_interface{ + repeat_parser_type{rhs.parser_, min_, max_, delim.parser_}}; + } + MinType min_; MaxType max_; }; diff --git a/test/github_issues.cpp b/test/github_issues.cpp index 1aab4af2..6f08749e 100644 --- a/test/github_issues.cpp +++ b/test/github_issues.cpp @@ -287,6 +287,24 @@ void github_issue_223() } } +void github_issue_231() +{ + namespace bp = boost::parser; + + constexpr auto parser = bp::repeat(3,5)(+bp::char_('a', 'z'), bp::lit(',')); + + auto result = bp::parse("hello, world, ", parser, bp::ws); + BOOST_TEST(!result.has_value()); + result = bp::parse("hello, world, there", parser, bp::ws); + BOOST_TEST(result.has_value()); + BOOST_TEST(result->size() == 3); + result = bp::parse("hello, world, there, foo, nix", parser, bp::ws); + BOOST_TEST(result.has_value()); + BOOST_TEST(result->size() == 5); + result = bp::parse("hello, world, there, foo, nix, bar", parser, bp::ws); + BOOST_TEST(!result.has_value()); +} + namespace github_issue_248_ { namespace bp = boost::parser; @@ -355,6 +373,7 @@ int main() github_issue_125(); github_issue_209(); github_issue_223(); + github_issue_231(); github_issue_248(); return boost::report_errors(); }