Skip to content

Commit

Permalink
Fix #270
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Mar 15, 2023
1 parent d8ec599 commit d9dfc5b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
18 changes: 8 additions & 10 deletions peglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2756,9 +2756,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
auto ope_ptr = ope_.get();
{
auto tok_ptr = dynamic_cast<const peg::TokenBoundary *>(ope_ptr);
if (tok_ptr) {
ope_ptr = tok_ptr->ope_.get();
}
if (tok_ptr) { ope_ptr = tok_ptr->ope_.get(); }
}
if (!dynamic_cast<const peg::PrioritizedChoice *>(ope_ptr)) {
chvs.choice_count_ = 0;
Expand Down Expand Up @@ -4538,8 +4536,8 @@ class parser {
const char *path = nullptr) const {
if (grammar_ != nullptr) {
const auto &rule = (*grammar_)[start_];
return post_process(s, n,
rule.parse_and_get_value(s, n, dt, val, path, log_));
auto result = rule.parse_and_get_value(s, n, dt, val, path, log_);
return post_process(s, n, result);
}
return false;
}
Expand Down Expand Up @@ -4685,7 +4683,7 @@ class parser {
inline void enable_tracing(parser &parser, std::ostream &os) {
parser.enable_trace(
[&](auto &ope, auto s, auto, auto &, auto &c, auto &, auto &trace_data) {
auto prev_pos = std::any_cast<size_t>(trace_data);
auto prev_pos = std::any_cast<size_t>(trace_data);
auto pos = static_cast<size_t>(s - c.s);
auto backtrack = (pos < prev_pos ? "*" : "");
std::string indent;
Expand Down Expand Up @@ -4809,8 +4807,8 @@ inline void enable_profiling(parser &parser, std::ostream &os) {
"Total counters");
os << buff << std::endl;

snprintf(buff, BUFSIZ, "%4s %10s %5s %10.2f %10.2f %s", "",
"", "", total_success * 100.0 / grand_total,
snprintf(buff, BUFSIZ, "%4s %10s %5s %10.2f %10.2f %s", "", "",
"", total_success * 100.0 / grand_total,
total_fail * 100.0 / grand_total, "% success/fail");
os << buff << std::endl << std::endl;
;
Expand All @@ -4819,8 +4817,8 @@ inline void enable_profiling(parser &parser, std::ostream &os) {
for (auto &[name, success, fail] : stats.items) {
auto total = success + fail;
auto ratio = total * 100.0 / stats.total;
snprintf(buff, BUFSIZ, "%4zu %10zu %5.2f %10zu %10zu %s",
id, total, ratio, success, fail, name.c_str());
snprintf(buff, BUFSIZ, "%4zu %10zu %5.2f %10zu %10zu %s", id,
total, ratio, success, fail, name.c_str());
os << buff << std::endl;
id++;
}
Expand Down
17 changes: 16 additions & 1 deletion test/test1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ TEST(GeneralTest, ChoiceWithWhitespace) {
%whitespace <- ' '*
)");

parser["type"] = [](const SemanticValues& vs) {
parser["type"] = [](const SemanticValues &vs) {
auto n = vs.choice();
EXPECT_EQ(1, n);
};
Expand All @@ -1248,3 +1248,18 @@ TEST(GeneralTest, ChoiceWithWhitespace) {
EXPECT_TRUE(ret);
}

TEST(GeneralTest, PassingContextAndOutputParameter) {
parser parser(R"(
START <- TOKEN
TOKEN <- [0-9]+
)");

parser["TOKEN"] = [&](const peg::SemanticValues &vs, std::any & /*dt*/) {
return vs.token_to_number<int>();
};

int output = 0;
std::any dt = std::string{"context"};
parser.parse<int>("42", dt, output);
EXPECT_EQ(42, output);
}

0 comments on commit d9dfc5b

Please sign in to comment.