diff --git a/peglib.h b/peglib.h index bb18307..558db81 100644 --- a/peglib.h +++ b/peglib.h @@ -2753,7 +2753,14 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs, chvs.sv_ = std::string_view(s, len); chvs.name_ = outer_->name; - if (!dynamic_cast(ope_.get())) { + auto ope_ptr = ope_.get(); + { + auto tok_ptr = dynamic_cast(ope_ptr); + if (tok_ptr) { + ope_ptr = tok_ptr->ope_.get(); + } + } + if (!dynamic_cast(ope_ptr)) { chvs.choice_count_ = 0; chvs.choice_ = 0; } diff --git a/test/test1.cc b/test/test1.cc index 1bf0947..5c3f1fa 100644 --- a/test/test1.cc +++ b/test/test1.cc @@ -1233,3 +1233,18 @@ LINE_END <- '\r\n' / '\r' / '\n' / !. } } +TEST(GeneralTest, ChoiceWithWhitespace) { + auto parser = peg::parser(R"( + type <- 'string' / 'int' / 'double' + %whitespace <- ' '* + )"); + + parser["type"] = [](const SemanticValues& vs) { + auto n = vs.choice(); + EXPECT_EQ(1, n); + }; + + auto ret = parser.parse("int"); + EXPECT_TRUE(ret); +} +