diff --git a/src/compiler/build_tables/rule_transitions.cc b/src/compiler/build_tables/rule_transitions.cc index 200ab508..98623a59 100644 --- a/src/compiler/build_tables/rule_transitions.cc +++ b/src/compiler/build_tables/rule_transitions.cc @@ -41,12 +41,9 @@ namespace tree_sitter { } template - map - map_transitions(const map &initial, std::function map_fn) { - map result; - for (auto &pair : initial) - result.insert({ pair.first, map_fn(pair.second) }); - return result; + void transform_transitions(map &transitions, std::function fn) { + for (auto &pair : transitions) + pair.second = fn(pair.second); } template @@ -74,7 +71,8 @@ namespace tree_sitter { } map apply_to(const rules::Seq *rule) { - auto result = map_transitions(this->apply(rule->left), [&](const rule_ptr left_rule) { + auto result = this->apply(rule->left); + transform_transitions(result, [&](const rule_ptr &left_rule) { return rules::Seq::Build({ left_rule, rule->right }); }); if (rule_can_be_blank(rule->left)) { @@ -84,15 +82,19 @@ namespace tree_sitter { } map apply_to(const rules::Repeat *rule) { - return map_transitions(this->apply(rule->content), [&](const rule_ptr &value) { + auto result = this->apply(rule->content); + transform_transitions(result, [&](const rule_ptr &value) { return rules::Seq::Build({ value, rule->copy() }); }); + return result; } map apply_to(const rules::Metadata *rule) { - return map_transitions(this->apply(rule->rule), [&](const rule_ptr &to_rule) { + auto result = this->apply(rule->rule); + transform_transitions(result, [&](const rule_ptr &to_rule) { return make_shared(to_rule, rule->value); }); + return result; } map apply_to(const rules::String *rule) {