This commit is contained in:
yhirose 2021-03-13 20:49:24 -05:00 committed by GitHub
parent 54b14aa4d9
commit 45fb3d4f3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -461,7 +461,7 @@ struct SemanticValues : protected std::vector<std::any> {
// Input text // Input text
const char *path = nullptr; const char *path = nullptr;
const char *ss = nullptr; const char *ss = nullptr;
const std::vector<size_t> *source_line_index = nullptr; std::function<const std::vector<size_t> &()> source_line_index;
// Matched string // Matched string
std::string_view sv() const { return sv_; } std::string_view sv() const { return sv_; }
@ -473,7 +473,7 @@ struct SemanticValues : protected std::vector<std::any> {
// Line number and column at which the matched string is // Line number and column at which the matched string is
std::pair<size_t, size_t> line_info() const { std::pair<size_t, size_t> line_info() const {
const auto &idx = *source_line_index; auto &idx = source_line_index();
auto cur = static_cast<size_t>(std::distance(ss, sv_.data())); auto cur = static_cast<size_t>(std::distance(ss, sv_.data()));
auto it = std::lower_bound( auto it = std::lower_bound(
@ -838,11 +838,6 @@ public:
cache_success(enablePackratParsing ? def_count * (l + 1) : 0), cache_success(enablePackratParsing ? def_count * (l + 1) : 0),
tracer_enter(tracer_enter), tracer_leave(tracer_leave), log(log) { tracer_enter(tracer_enter), tracer_leave(tracer_leave), log(log) {
for (size_t pos = 0; pos < l; pos++) {
if (s[pos] == '\n') { source_line_index.push_back(pos); }
}
source_line_index.push_back(l);
args_stack.resize(1); args_stack.resize(1);
push_capture_scope(); push_capture_scope();
@ -905,7 +900,16 @@ 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;
vs.source_line_index = &source_line_index; vs.source_line_index = [&]() -> const std::vector<size_t> & {
if (source_line_index.empty()) {
for (size_t pos = 0; pos < l; pos++) {
if (s[pos] == '\n') { source_line_index.push_back(pos); }
}
source_line_index.push_back(l);
}
return source_line_index;
};
return vs; return vs;
} }
@ -2430,25 +2434,30 @@ inline size_t parse_literal(const char *s, size_t n, SemanticValues &vs,
} }
// Word check // Word check
if (c.wordOpe) {
std::call_once(init_is_word, [&]() {
SemanticValues dummy_vs; SemanticValues dummy_vs;
Context dummy_c(nullptr, c.s, c.l, 0, nullptr, nullptr, false, nullptr, Context dummy_c(nullptr, c.s, c.l, 0, nullptr, nullptr, false, nullptr,
nullptr, nullptr); nullptr, nullptr);
std::any dummy_dt; std::any dummy_dt;
std::call_once(init_is_word, [&]() {
if (c.wordOpe) {
auto len = auto len =
c.wordOpe->parse(lit.data(), lit.size(), dummy_vs, dummy_c, dummy_dt); c.wordOpe->parse(lit.data(), lit.size(), dummy_vs, dummy_c, dummy_dt);
is_word = success(len); is_word = success(len);
}
}); });
if (is_word) { if (is_word) {
SemanticValues dummy_vs;
Context dummy_c(nullptr, c.s, c.l, 0, nullptr, nullptr, false, nullptr,
nullptr, nullptr);
std::any dummy_dt;
NotPredicate ope(c.wordOpe); NotPredicate ope(c.wordOpe);
auto len = ope.parse(s + i, n - i, dummy_vs, dummy_c, dummy_dt); auto len = ope.parse(s + i, n - i, dummy_vs, dummy_c, dummy_dt);
if (fail(len)) { return len; } if (fail(len)) { return len; }
i += len; i += len;
} }
}
// Skip whiltespace // Skip whiltespace
if (!c.in_token_boundary_count) { if (!c.in_token_boundary_count) {