Allow users to put their own auxiliary rules in grammars
This commit is contained in:
parent
812f27f43a
commit
a77ca1ee08
3 changed files with 138 additions and 109 deletions
|
|
@ -46,13 +46,16 @@ namespace tree_sitter {
|
|||
};
|
||||
|
||||
Grammar expand_repeats(const Grammar &grammar) {
|
||||
map<const string, const rule_ptr> result;
|
||||
map<const string, const rule_ptr> rules;
|
||||
map<const string, const rule_ptr> aux_rules(grammar.aux_rules);
|
||||
RepeatExpander visitor;
|
||||
|
||||
for (auto pair : grammar.rules)
|
||||
result.insert({ pair.first, visitor.apply(pair.second) });
|
||||
rules.insert({ pair.first, visitor.apply(pair.second) });
|
||||
|
||||
aux_rules.insert(visitor.aux_rules.begin(), visitor.aux_rules.end());
|
||||
|
||||
return Grammar(grammar.start_rule_name, result, visitor.aux_rules);
|
||||
return Grammar(grammar.start_rule_name, rules, aux_rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -62,13 +62,14 @@ namespace tree_sitter {
|
|||
pair<Grammar, Grammar> extract_tokens(const Grammar &input_grammar) {
|
||||
TokenExtractor extractor;
|
||||
map<const string, const rule_ptr> rules;
|
||||
map<const string, const rule_ptr> aux_rules;
|
||||
map<const string, const rule_ptr> tokens;
|
||||
map<const string, const rule_ptr> aux_rules;
|
||||
map<const string, const rule_ptr> aux_tokens;
|
||||
|
||||
for (auto pair : input_grammar.rules) {
|
||||
string name = pair.first;
|
||||
rule_ptr rule = pair.second;
|
||||
auto new_rule = extractor.initial_apply(rule);
|
||||
rule_ptr new_rule = extractor.initial_apply(rule);
|
||||
if (new_rule.get())
|
||||
rules.insert({ name, new_rule });
|
||||
else
|
||||
|
|
@ -78,16 +79,18 @@ namespace tree_sitter {
|
|||
for (auto pair : input_grammar.aux_rules) {
|
||||
string name = pair.first;
|
||||
rule_ptr rule = pair.second;
|
||||
auto new_rule = extractor.initial_apply(rule);
|
||||
rule_ptr new_rule = extractor.initial_apply(rule);
|
||||
if (new_rule.get())
|
||||
aux_rules.insert({ name, new_rule });
|
||||
else
|
||||
tokens.insert({ name, rule });
|
||||
aux_tokens.insert({ name, rule });
|
||||
}
|
||||
|
||||
|
||||
aux_tokens.insert(extractor.tokens.begin(), extractor.tokens.end());
|
||||
|
||||
return {
|
||||
Grammar(input_grammar.start_rule_name, rules),
|
||||
Grammar("", tokens, extractor.tokens)
|
||||
Grammar(input_grammar.start_rule_name, rules, aux_rules),
|
||||
Grammar("", tokens, aux_tokens)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue