Code cleanup

This commit is contained in:
yhirose 2022-07-03 19:23:35 -04:00
parent 2e0b941b7e
commit eab2ea05cd

203
peglib.h
View File

@ -563,6 +563,7 @@ private:
friend class Context; friend class Context;
friend class Sequence; friend class Sequence;
friend class PrioritizedChoice; friend class PrioritizedChoice;
friend class Repetition;
friend class Holder; friend class Holder;
friend class PrecedenceClimbing; friend class PrecedenceClimbing;
@ -892,7 +893,7 @@ public:
} }
} }
SemanticValues &push() { SemanticValues &push_semantic_values_scope() {
assert(value_stack_size <= value_stack.size()); assert(value_stack_size <= value_stack.size());
if (value_stack_size == value_stack.size()) { if (value_stack_size == value_stack.size()) {
value_stack.emplace_back(std::make_shared<SemanticValues>(this)); value_stack.emplace_back(std::make_shared<SemanticValues>(this));
@ -911,11 +912,10 @@ public:
auto &vs = *value_stack[value_stack_size++]; auto &vs = *value_stack[value_stack_size++];
vs.path = path; vs.path = path;
vs.ss = s; vs.ss = s;
return vs; return vs;
} }
void pop() { value_stack_size--; } void pop_semantic_values_scope() { value_stack_size--; }
void push_args(std::vector<std::shared_ptr<Ope>> &&args) { void push_args(std::vector<std::shared_ptr<Ope>> &&args) {
args_stack.emplace_back(args); args_stack.emplace_back(args);
@ -988,30 +988,23 @@ public:
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c,
std::any &dt) const override { std::any &dt) const override {
auto &chldvs = c.push(); auto &chvs = c.push_semantic_values_scope();
auto pop_se = scope_exit([&]() { c.pop(); }); auto se = scope_exit([&]() { c.pop_semantic_values_scope(); });
size_t i = 0; size_t i = 0;
for (const auto &ope : opes_) { for (const auto &ope : opes_) {
const auto &rule = *ope; auto len = ope->parse(s + i, n - i, chvs, c, dt);
auto len = rule.parse(s + i, n - i, chldvs, c, dt);
if (fail(len)) { return len; } if (fail(len)) { return len; }
i += len; i += len;
} }
if (!chldvs.empty()) { vs.sv_ = chvs.sv_;
for (size_t j = 0; j < chldvs.size(); j++) { for (auto &v : chvs) {
vs.emplace_back(std::move(chldvs[j])); vs.emplace_back(std::move(v));
}
} }
if (!chldvs.tags.empty()) { for (auto &tag : chvs.tags) {
for (size_t j = 0; j < chldvs.tags.size(); j++) { vs.tags.emplace_back(std::move(tag));
vs.tags.emplace_back(std::move(chldvs.tags[j]));
}
} }
vs.sv_ = chldvs.sv_; for (auto &tok : chvs.tokens) {
if (!chldvs.tokens.empty()) { vs.tokens.emplace_back(std::move(tok));
for (size_t j = 0; j < chldvs.tokens.size(); j++) {
vs.tokens.emplace_back(std::move(chldvs.tokens[j]));
}
} }
return i; return i;
} }
@ -1041,36 +1034,29 @@ public:
for (const auto &ope : opes_) { for (const auto &ope : opes_) {
if (!c.cut_stack.empty()) { c.cut_stack.back() = false; } if (!c.cut_stack.empty()) { c.cut_stack.back() = false; }
auto &chldvs = c.push(); auto &chvs = c.push_semantic_values_scope();
c.push_capture_scope(); c.push_capture_scope();
c.error_info.keep_previous_token = id > 0; c.error_info.keep_previous_token = id > 0;
auto se = scope_exit([&]() { auto se = scope_exit([&]() {
c.pop(); c.pop_semantic_values_scope();
c.pop_capture_scope(); c.pop_capture_scope();
c.error_info.keep_previous_token = false; c.error_info.keep_previous_token = false;
}); });
len = ope->parse(s, n, chldvs, c, dt); len = ope->parse(s, n, chvs, c, dt);
if (success(len)) { if (success(len)) {
if (!chldvs.empty()) { vs.sv_ = chvs.sv_;
for (size_t i = 0; i < chldvs.size(); i++) {
vs.emplace_back(std::move(chldvs[i]));
}
}
if (!chldvs.tags.empty()) {
for (size_t i = 0; i < chldvs.tags.size(); i++) {
vs.tags.emplace_back(std::move(chldvs.tags[i]));
}
}
vs.sv_ = chldvs.sv_;
vs.choice_count_ = opes_.size(); vs.choice_count_ = opes_.size();
vs.choice_ = id; vs.choice_ = id;
if (!chldvs.tokens.empty()) { for (auto &v : chvs) {
for (size_t i = 0; i < chldvs.tokens.size(); i++) { vs.emplace_back(std::move(v));
vs.tokens.emplace_back(std::move(chldvs.tokens[i])); }
} for (auto &tag : chvs.tags) {
vs.tags.emplace_back(std::move(tag));
}
for (auto &tok : chvs.tokens) {
vs.tokens.emplace_back(std::move(tok));
} }
c.shift_capture_values(); c.shift_capture_values();
break; break;
@ -1104,11 +1090,26 @@ public:
size_t count = 0; size_t count = 0;
size_t i = 0; size_t i = 0;
while (count < min_) { while (count < min_) {
auto &chvs = c.push_semantic_values_scope();
c.push_capture_scope(); c.push_capture_scope();
auto se = scope_exit([&]() { c.pop_capture_scope(); }); auto se = scope_exit([&]() {
const auto &rule = *ope_; c.pop_semantic_values_scope();
auto len = rule.parse(s + i, n - i, vs, c, dt); c.pop_capture_scope();
});
auto len = ope_->parse(s + i, n - i, chvs, c, dt);
if (success(len)) { if (success(len)) {
vs.sv_ = chvs.sv_;
for (auto &v : chvs) {
vs.emplace_back(std::move(v));
}
for (auto &tag : chvs.tags) {
vs.tags.emplace_back(std::move(tag));
}
for (auto &tok : chvs.tokens) {
vs.tokens.emplace_back(std::move(tok));
}
c.shift_capture_values(); c.shift_capture_values();
} else { } else {
return len; return len;
@ -1118,25 +1119,28 @@ public:
} }
while (count < max_) { while (count < max_) {
auto &chvs = c.push_semantic_values_scope();
c.push_capture_scope(); c.push_capture_scope();
auto se = scope_exit([&]() { c.pop_capture_scope(); }); auto se = scope_exit([&]() {
auto save_sv_size = vs.size(); c.pop_semantic_values_scope();
auto save_tok_size = vs.tokens.size(); c.pop_capture_scope();
});
auto len = ope_->parse(s + i, n - i, chvs, c, dt);
const auto &rule = *ope_;
auto len = rule.parse(s + i, n - i, vs, c, dt);
if (success(len)) { if (success(len)) {
vs.sv_ = chvs.sv_;
for (auto &v : chvs) {
vs.emplace_back(std::move(v));
}
for (auto &tag : chvs.tags) {
vs.tags.emplace_back(std::move(tag));
}
for (auto &tok : chvs.tokens) {
vs.tokens.emplace_back(std::move(tok));
}
c.shift_capture_values(); c.shift_capture_values();
} else { } else {
if (vs.size() != save_sv_size) {
vs.erase(vs.begin() + static_cast<std::ptrdiff_t>(save_sv_size));
vs.tags.erase(vs.tags.begin() +
static_cast<std::ptrdiff_t>(save_sv_size));
}
if (vs.tokens.size() != save_tok_size) {
vs.tokens.erase(vs.tokens.begin() +
static_cast<std::ptrdiff_t>(save_tok_size));
}
break; break;
} }
i += len; i += len;
@ -1176,14 +1180,15 @@ public:
size_t parse_core(const char *s, size_t n, SemanticValues & /*vs*/, size_t parse_core(const char *s, size_t n, SemanticValues & /*vs*/,
Context &c, std::any &dt) const override { Context &c, std::any &dt) const override {
auto &chldvs = c.push(); auto &chvs = c.push_semantic_values_scope();
c.push_capture_scope(); c.push_capture_scope();
auto se = scope_exit([&]() { auto se = scope_exit([&]() {
c.pop(); c.pop_semantic_values_scope();
c.pop_capture_scope(); c.pop_capture_scope();
}); });
const auto &rule = *ope_;
auto len = rule.parse(s, n, chldvs, c, dt); auto len = ope_->parse(s, n, chvs, c, dt);
if (success(len)) { if (success(len)) {
return 0; return 0;
} else { } else {
@ -1202,13 +1207,13 @@ public:
size_t parse_core(const char *s, size_t n, SemanticValues & /*vs*/, size_t parse_core(const char *s, size_t n, SemanticValues & /*vs*/,
Context &c, std::any &dt) const override { Context &c, std::any &dt) const override {
auto &chldvs = c.push(); auto &chvs = c.push_semantic_values_scope();
c.push_capture_scope(); c.push_capture_scope();
auto se = scope_exit([&]() { auto se = scope_exit([&]() {
c.pop(); c.pop_semantic_values_scope();
c.pop_capture_scope(); c.pop_capture_scope();
}); });
auto len = ope_->parse(s, n, chldvs, c, dt); auto len = ope_->parse(s, n, chvs, c, dt);
if (success(len)) { if (success(len)) {
c.set_error_pos(s); c.set_error_pos(s);
return static_cast<size_t>(-1); return static_cast<size_t>(-1);
@ -1371,9 +1376,7 @@ public:
std::any &dt) const override { std::any &dt) const override {
c.push_capture_scope(); c.push_capture_scope();
auto se = scope_exit([&]() { c.pop_capture_scope(); }); auto se = scope_exit([&]() { c.pop_capture_scope(); });
const auto &rule = *ope_; return ope_->parse(s, n, vs, c, dt);
auto len = rule.parse(s, n, vs, c, dt);
return len;
} }
void accept(Visitor &v) override; void accept(Visitor &v) override;
@ -1390,8 +1393,7 @@ public:
size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c, size_t parse_core(const char *s, size_t n, SemanticValues &vs, Context &c,
std::any &dt) const override { std::any &dt) const override {
const auto &rule = *ope_; auto len = ope_->parse(s, n, vs, c, dt);
auto len = rule.parse(s, n, vs, c, dt);
if (success(len) && match_action_) { match_action_(s, len, c); } if (success(len) && match_action_) { match_action_(s, len, c); }
return len; return len;
} }
@ -1420,10 +1422,9 @@ public:
size_t parse_core(const char *s, size_t n, SemanticValues & /*vs*/, size_t parse_core(const char *s, size_t n, SemanticValues & /*vs*/,
Context &c, std::any &dt) const override { Context &c, std::any &dt) const override {
const auto &rule = *ope_; auto &chvs = c.push_semantic_values_scope();
auto &chldvs = c.push(); auto se = scope_exit([&]() { c.pop_semantic_values_scope(); });
auto se = scope_exit([&]() { c.pop(); }); return ope_->parse(s, n, chvs, c, dt);
return rule.parse(s, n, chldvs, c, dt);
} }
void accept(Visitor &v) override; void accept(Visitor &v) override;
@ -1456,8 +1457,7 @@ public:
std::any &dt) const override { std::any &dt) const override {
auto ope = weak_.lock(); auto ope = weak_.lock();
assert(ope); assert(ope);
const auto &rule = *ope; return ope->parse(s, n, vs, c, dt);
return rule.parse(s, n, vs, c, dt);
} }
void accept(Visitor &v) override; void accept(Visitor &v) override;
@ -1522,8 +1522,7 @@ public:
if (c.in_whitespace) { return 0; } if (c.in_whitespace) { return 0; }
c.in_whitespace = true; c.in_whitespace = true;
auto se = scope_exit([&]() { c.in_whitespace = false; }); auto se = scope_exit([&]() { c.in_whitespace = false; });
const auto &rule = *ope_; return ope_->parse(s, n, vs, c, dt);
return rule.parse(s, n, vs, c, dt);
} }
void accept(Visitor &v) override; void accept(Visitor &v) override;
@ -2703,30 +2702,28 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
c.packrat(s, outer_->id, len, val, [&](std::any &a_val) { c.packrat(s, outer_->id, len, val, [&](std::any &a_val) {
if (outer_->enter) { outer_->enter(s, n, dt); } if (outer_->enter) { outer_->enter(s, n, dt); }
auto &chvs = c.push_semantic_values_scope();
auto se2 = scope_exit([&]() { auto se = scope_exit([&]() {
c.pop(); c.pop_semantic_values_scope();
if (outer_->leave) { outer_->leave(s, n, len, a_val, dt); } if (outer_->leave) { outer_->leave(s, n, len, a_val, dt); }
}); });
auto &chldvs = c.push();
c.rule_stack.push_back(outer_); c.rule_stack.push_back(outer_);
len = ope_->parse(s, n, chldvs, c, dt); len = ope_->parse(s, n, chvs, c, dt);
c.rule_stack.pop_back(); c.rule_stack.pop_back();
// Invoke action // Invoke action
if (success(len)) { if (success(len)) {
chldvs.sv_ = std::string_view(s, len); chvs.sv_ = std::string_view(s, len);
chldvs.name_ = outer_->name; chvs.name_ = outer_->name;
if (!dynamic_cast<const peg::PrioritizedChoice *>(ope_.get())) { if (!dynamic_cast<const peg::PrioritizedChoice *>(ope_.get())) {
chldvs.choice_count_ = 0; chvs.choice_count_ = 0;
chldvs.choice_ = 0; chvs.choice_ = 0;
} }
std::string msg; std::string msg;
if (outer_->predicate && !outer_->predicate(chldvs, dt, msg)) { if (outer_->predicate && !outer_->predicate(chvs, dt, msg)) {
if (c.log && !msg.empty() && c.error_info.message_pos < s) { if (c.log && !msg.empty() && c.error_info.message_pos < s) {
c.error_info.message_pos = s; c.error_info.message_pos = s;
c.error_info.message = msg; c.error_info.message = msg;
@ -2734,7 +2731,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
len = static_cast<size_t>(-1); len = static_cast<size_t>(-1);
} else if (outer_->declare_symbol) { } else if (outer_->declare_symbol) {
assert(outer_->is_token()); assert(outer_->is_token());
auto symbol = chldvs.token_to_string(); auto symbol = chvs.token_to_string();
auto &table = c.symbol_tables[outer_->symbol_table_name]; auto &table = c.symbol_tables[outer_->symbol_table_name];
if (table.find(symbol) != table.end()) { if (table.find(symbol) != table.end()) {
msg = "'" + symbol + "' already exists."; msg = "'" + symbol + "' already exists.";
@ -2744,7 +2741,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
} }
} else if (outer_->check_symbol) { } else if (outer_->check_symbol) {
assert(outer_->is_token()); assert(outer_->is_token());
auto symbol = chldvs.token_to_string(); auto symbol = chvs.token_to_string();
auto &table = c.symbol_tables[outer_->symbol_table_name]; auto &table = c.symbol_tables[outer_->symbol_table_name];
if (table.find(symbol) == table.end()) { if (table.find(symbol) == table.end()) {
msg = "'" + symbol + "' doesn't exist."; msg = "'" + symbol + "' doesn't exist.";
@ -2753,7 +2750,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
} }
if (success(len)) { if (success(len)) {
a_val = reduce(chldvs, dt); a_val = reduce(chvs, dt);
} else { } else {
if (c.log && !msg.empty() && c.error_info.message_pos < s) { if (c.log && !msg.empty() && c.error_info.message_pos < s) {
c.error_info.message_pos = s; c.error_info.message_pos = s;
@ -2894,11 +2891,11 @@ inline size_t PrecedenceClimbing::parse_expression(const char *s, size_t n,
std::vector<std::any> save_values(vs.begin(), vs.end()); std::vector<std::any> save_values(vs.begin(), vs.end());
auto save_tokens = vs.tokens; auto save_tokens = vs.tokens;
auto chv = c.push(); auto chvs = c.push_semantic_values_scope();
auto chl = binop_->parse(s + i, n - i, chv, c, dt); auto chlen = binop_->parse(s + i, n - i, chvs, c, dt);
c.pop(); c.pop_semantic_values_scope();
if (fail(chl)) { break; } if (fail(chlen)) { break; }
auto it = info_.find(tok); auto it = info_.find(tok);
if (it == info_.end()) { break; } if (it == info_.end()) { break; }
@ -2908,25 +2905,25 @@ inline size_t PrecedenceClimbing::parse_expression(const char *s, size_t n,
if (level < min_prec) { break; } if (level < min_prec) { break; }
vs.emplace_back(std::move(chv[0])); vs.emplace_back(std::move(chvs[0]));
i += chl; i += chlen;
auto next_min_prec = level; auto next_min_prec = level;
if (assoc == 'L') { next_min_prec = level + 1; } if (assoc == 'L') { next_min_prec = level + 1; }
chv = c.push(); chvs = c.push_semantic_values_scope();
chl = parse_expression(s + i, n - i, chv, c, dt, next_min_prec); chlen = parse_expression(s + i, n - i, chvs, c, dt, next_min_prec);
c.pop(); c.pop_semantic_values_scope();
if (fail(chl)) { if (fail(chlen)) {
vs.assign(save_values.begin(), save_values.end()); vs.assign(save_values.begin(), save_values.end());
vs.tokens = save_tokens; vs.tokens = save_tokens;
i = chl; i = chlen;
break; break;
} }
vs.emplace_back(std::move(chv[0])); vs.emplace_back(std::move(chvs[0]));
i += chl; i += chlen;
std::any val; std::any val;
if (rule_.action) { if (rule_.action) {