mirror of
https://github.com/textmate/textmate.git
synced 2026-04-28 03:00:34 -04:00
Make injection grammars part of grammar_t
This commit is contained in:
@@ -52,7 +52,7 @@ namespace parse
|
||||
.map("whileCaptures", &rule_t::while_captures, &convert_dictionary)
|
||||
.map("endCaptures", &rule_t::end_captures, &convert_dictionary)
|
||||
.map("repository", &rule_t::repository, &convert_dictionary)
|
||||
.map("injections", &rule_t::injections, &convert_dictionary)
|
||||
.map("injections", &rule_t::injection_rules, &convert_dictionary)
|
||||
;
|
||||
|
||||
if(plist::dictionary_t const* plist = boost::get<plist::dictionary_t>(&any))
|
||||
@@ -126,7 +126,7 @@ namespace parse
|
||||
for(rule_ptr child : rule->children)
|
||||
compile_patterns(child.get());
|
||||
|
||||
repository_ptr maps[] = { rule->repository, rule->injections, rule->captures, rule->begin_captures, rule->while_captures, rule->end_captures };
|
||||
repository_ptr maps[] = { rule->repository, rule->injection_rules, rule->captures, rule->begin_captures, rule->while_captures, rule->end_captures };
|
||||
for(auto const& map : maps)
|
||||
{
|
||||
if(!map)
|
||||
@@ -135,6 +135,10 @@ namespace parse
|
||||
for(auto const& pair : *map)
|
||||
compile_patterns(pair.second.get());
|
||||
}
|
||||
|
||||
if(rule->injection_rules)
|
||||
std::copy(rule->injection_rules->begin(), rule->injection_rules->end(), back_inserter(rule->injections));
|
||||
rule->injection_rules.reset();
|
||||
}
|
||||
|
||||
void grammar_t::setup_includes (rule_ptr const& rule, rule_ptr const& base, rule_ptr const& self, grammar_t::rule_stack_t const& stack)
|
||||
@@ -188,7 +192,7 @@ namespace parse
|
||||
for(rule_ptr child : rule->children)
|
||||
setup_includes(child, base, self, rule_stack_t(rule.get(), &stack));
|
||||
|
||||
repository_ptr maps[] = { rule->repository, rule->injections, rule->captures, rule->begin_captures, rule->while_captures, rule->end_captures };
|
||||
repository_ptr maps[] = { rule->repository, rule->injection_rules, rule->captures, rule->begin_captures, rule->while_captures, rule->end_captures };
|
||||
for(auto const& map : maps)
|
||||
{
|
||||
if(!map)
|
||||
@@ -210,6 +214,25 @@ namespace parse
|
||||
return rule_ptr();
|
||||
}
|
||||
|
||||
std::vector<std::pair<scope::selector_t, rule_ptr>> grammar_t::injection_grammars ()
|
||||
{
|
||||
std::vector<std::pair<scope::selector_t, rule_ptr>> res;
|
||||
for(auto item : bundles::query(bundles::kFieldAny, NULL_STR, scope::wildcard, bundles::kItemTypeGrammar))
|
||||
{
|
||||
std::string injectionSelector = item->value_for_field(bundles::kFieldGrammarInjectionSelector);
|
||||
if(injectionSelector != NULL_STR)
|
||||
{
|
||||
if(rule_ptr grammar = convert_plist(item->plist()))
|
||||
{
|
||||
setup_includes(grammar, grammar, grammar, rule_stack_t(grammar.get()));
|
||||
compile_patterns(grammar.get());
|
||||
res.emplace_back(injectionSelector, grammar);
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
rule_ptr grammar_t::add_grammar (std::string const& scope, plist::any_t const& plist, rule_ptr const& base)
|
||||
{
|
||||
rule_ptr grammar = convert_plist(plist);
|
||||
@@ -248,6 +271,9 @@ namespace parse
|
||||
_rule.reset(new rule_t);
|
||||
}
|
||||
|
||||
auto const grammars = injection_grammars();
|
||||
_rule->injections.insert(_rule->injections.end(), grammars.begin(), grammars.end());
|
||||
|
||||
_callbacks(&callback_t::grammar_did_change);
|
||||
}
|
||||
|
||||
@@ -276,7 +302,6 @@ namespace parse
|
||||
grammar_ptr parse_grammar (bundles::item_ptr const& grammarItem)
|
||||
{
|
||||
ASSERT(grammarItem);
|
||||
injected_grammars(); // ensure these are loaded in the main thread
|
||||
return grammar_ptr(new grammar_t(grammarItem));
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ namespace parse
|
||||
void setup_includes (rule_ptr const& rule, rule_ptr const& base, rule_ptr const& self, rule_stack_t const& stack);
|
||||
rule_ptr find_grammar (std::string const& scope, rule_ptr const& base);
|
||||
rule_ptr add_grammar (std::string const& scope, plist::any_t const& plist, rule_ptr const& base = rule_ptr());
|
||||
std::vector<std::pair<scope::selector_t, rule_ptr>> injection_grammars ();
|
||||
|
||||
void bundles_did_change ();
|
||||
|
||||
|
||||
@@ -10,26 +10,6 @@ OAK_DEBUG_VAR(Parser_Flow);
|
||||
|
||||
namespace parse
|
||||
{
|
||||
std::vector< std::pair<scope::selector_t, rule_ptr> >& injected_grammars ()
|
||||
{
|
||||
static std::vector< std::pair<scope::selector_t, rule_ptr> > res;
|
||||
static bool needs_setup = true;
|
||||
if(needs_setup)
|
||||
{
|
||||
needs_setup = false;
|
||||
citerate(item, bundles::query(bundles::kFieldAny, NULL_STR, scope::wildcard, bundles::kItemTypeGrammar))
|
||||
{
|
||||
std::string injectionSelector = (*item)->value_for_field(bundles::kFieldGrammarInjectionSelector);
|
||||
if(injectionSelector != NULL_STR)
|
||||
{
|
||||
if(grammar_ptr grammar = parse_grammar(*item))
|
||||
res.push_back(std::make_pair(injectionSelector, grammar->seed()->rule));
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t rule_t::rule_id_counter = 0;
|
||||
|
||||
bool equal (stack_ptr lhs, stack_ptr rhs)
|
||||
@@ -218,21 +198,12 @@ namespace parse
|
||||
{
|
||||
for(stack_ptr node = stack; node; node = node->parent)
|
||||
{
|
||||
if(!node->rule->injections)
|
||||
continue;
|
||||
|
||||
for(auto const& pair : *node->rule->injections)
|
||||
for(auto const& pair : node->rule->injections)
|
||||
{
|
||||
if(scope::selector_t(pair.first).does_match(scope))
|
||||
if(pair.first.does_match(scope))
|
||||
collect_rule(pair.second, res);
|
||||
}
|
||||
}
|
||||
|
||||
for(auto const& pair : injected_grammars())
|
||||
{
|
||||
if(pair.first.does_match(scope))
|
||||
collect_children(pair.second->children, res);
|
||||
}
|
||||
}
|
||||
|
||||
static void collect_rules (char const* first, char const* last, size_t i, bool firstLine, stack_ptr const& stack, std::set<ranked_match_t>& res, std::map<size_t, regexp::match_t>& match_cache)
|
||||
|
||||
@@ -44,7 +44,8 @@ namespace parse
|
||||
repository_ptr while_captures;
|
||||
repository_ptr end_captures;
|
||||
repository_ptr repository;
|
||||
repository_ptr injections;
|
||||
repository_ptr injection_rules;
|
||||
std::vector<std::pair<scope::selector_t, rule_ptr>> injections;
|
||||
|
||||
// =======================
|
||||
// = Pre-parsed versions =
|
||||
@@ -85,8 +86,6 @@ namespace parse
|
||||
bool operator!= (stack_t const& rhs) const;
|
||||
};
|
||||
|
||||
std::vector< std::pair<scope::selector_t, rule_ptr> >& injected_grammars ();
|
||||
|
||||
} /* parse */
|
||||
|
||||
#endif /* end of include guard: PARSE_PRIVATE_H_XR3VJA2H */
|
||||
|
||||
Reference in New Issue
Block a user