Skip to content

Commit 5d6d2f7

Browse files
committed
Add missing special case for parsing a sequence of optional<T>s, writing the
results into a sequence container of Ts. Fixes #223.
1 parent fd6c56d commit 5d6d2f7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

include/boost/parser/parser.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,8 @@ namespace boost { namespace parser {
28092809
{
28102810
if constexpr (is_nope_v<ParserAttr>) {
28112811
return nope{};
2812+
} else if constexpr (is_optional_v<ParserAttr>) {
2813+
return ParserAttr{};
28122814
} else {
28132815
using value_type = range_value_t<GivenContainerAttr>;
28142816
return std::conditional_t<

test/github_issues.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,41 @@ void github_issue_209()
258258
std::end(bp::detail::char_set<detail::upper_case_chars>::chars)));
259259
}
260260

261+
void github_issue_223()
262+
{
263+
namespace bp = boost::parser;
264+
265+
// failing case
266+
{
267+
std::vector<char> v;
268+
const auto parser = *('x' | bp::char_('y'));
269+
bp::parse("xy", parser, bp::ws, v);
270+
271+
BOOST_TEST(v.size() == 1);
272+
BOOST_TEST(v == std::vector<char>({'y'}));
273+
274+
std::cout << "v.size()=" << v.size() << "\n";
275+
for (auto c : v) {
276+
std::cout << std::hex << (int)c << ' ';
277+
}
278+
std::cout << "\n";
279+
280+
// the assert fails since there are two elements in the vector: '\0'
281+
// and 'y'. Seems pretty surprising to me
282+
}
283+
284+
// working case
285+
{
286+
const auto parser = *('x' | bp::char_('y'));
287+
const auto result = bp::parse("xy", parser, bp::ws);
288+
289+
BOOST_TEST(result->size() == 1);
290+
BOOST_TEST(*result == std::vector<std::optional<char>>({'y'}));
291+
292+
// success, the vector has only one 'y' element
293+
}
294+
}
295+
261296

262297
int main()
263298
{
@@ -268,5 +303,6 @@ int main()
268303
github_issue_90();
269304
github_issue_125();
270305
github_issue_209();
306+
github_issue_223();
271307
return boost::report_errors();
272308
}

0 commit comments

Comments
 (0)