Skip to content

Commit d9dfc5b

Browse files
committed
Fix #270
1 parent d8ec599 commit d9dfc5b

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

peglib.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -2756,9 +2756,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
27562756
auto ope_ptr = ope_.get();
27572757
{
27582758
auto tok_ptr = dynamic_cast<const peg::TokenBoundary *>(ope_ptr);
2759-
if (tok_ptr) {
2760-
ope_ptr = tok_ptr->ope_.get();
2761-
}
2759+
if (tok_ptr) { ope_ptr = tok_ptr->ope_.get(); }
27622760
}
27632761
if (!dynamic_cast<const peg::PrioritizedChoice *>(ope_ptr)) {
27642762
chvs.choice_count_ = 0;
@@ -4538,8 +4536,8 @@ class parser {
45384536
const char *path = nullptr) const {
45394537
if (grammar_ != nullptr) {
45404538
const auto &rule = (*grammar_)[start_];
4541-
return post_process(s, n,
4542-
rule.parse_and_get_value(s, n, dt, val, path, log_));
4539+
auto result = rule.parse_and_get_value(s, n, dt, val, path, log_);
4540+
return post_process(s, n, result);
45434541
}
45444542
return false;
45454543
}
@@ -4685,7 +4683,7 @@ class parser {
46854683
inline void enable_tracing(parser &parser, std::ostream &os) {
46864684
parser.enable_trace(
46874685
[&](auto &ope, auto s, auto, auto &, auto &c, auto &, auto &trace_data) {
4688-
auto prev_pos = std::any_cast<size_t>(trace_data);
4686+
auto prev_pos = std::any_cast<size_t>(trace_data);
46894687
auto pos = static_cast<size_t>(s - c.s);
46904688
auto backtrack = (pos < prev_pos ? "*" : "");
46914689
std::string indent;
@@ -4809,8 +4807,8 @@ inline void enable_profiling(parser &parser, std::ostream &os) {
48094807
"Total counters");
48104808
os << buff << std::endl;
48114809

4812-
snprintf(buff, BUFSIZ, "%4s %10s %5s %10.2f %10.2f %s", "",
4813-
"", "", total_success * 100.0 / grand_total,
4810+
snprintf(buff, BUFSIZ, "%4s %10s %5s %10.2f %10.2f %s", "", "",
4811+
"", total_success * 100.0 / grand_total,
48144812
total_fail * 100.0 / grand_total, "% success/fail");
48154813
os << buff << std::endl << std::endl;
48164814
;
@@ -4819,8 +4817,8 @@ inline void enable_profiling(parser &parser, std::ostream &os) {
48194817
for (auto &[name, success, fail] : stats.items) {
48204818
auto total = success + fail;
48214819
auto ratio = total * 100.0 / stats.total;
4822-
snprintf(buff, BUFSIZ, "%4zu %10zu %5.2f %10zu %10zu %s",
4823-
id, total, ratio, success, fail, name.c_str());
4820+
snprintf(buff, BUFSIZ, "%4zu %10zu %5.2f %10zu %10zu %s", id,
4821+
total, ratio, success, fail, name.c_str());
48244822
os << buff << std::endl;
48254823
id++;
48264824
}

test/test1.cc

+16-1
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ TEST(GeneralTest, ChoiceWithWhitespace) {
12391239
%whitespace <- ' '*
12401240
)");
12411241

1242-
parser["type"] = [](const SemanticValues& vs) {
1242+
parser["type"] = [](const SemanticValues &vs) {
12431243
auto n = vs.choice();
12441244
EXPECT_EQ(1, n);
12451245
};
@@ -1248,3 +1248,18 @@ TEST(GeneralTest, ChoiceWithWhitespace) {
12481248
EXPECT_TRUE(ret);
12491249
}
12501250

1251+
TEST(GeneralTest, PassingContextAndOutputParameter) {
1252+
parser parser(R"(
1253+
START <- TOKEN
1254+
TOKEN <- [0-9]+
1255+
)");
1256+
1257+
parser["TOKEN"] = [&](const peg::SemanticValues &vs, std::any & /*dt*/) {
1258+
return vs.token_to_number<int>();
1259+
};
1260+
1261+
int output = 0;
1262+
std::any dt = std::string{"context"};
1263+
parser.parse<int>("42", dt, output);
1264+
EXPECT_EQ(42, output);
1265+
}

0 commit comments

Comments
 (0)