Put rule transitions in LR namespace
This commit is contained in:
parent
656f6b0819
commit
30315a78d2
10 changed files with 177 additions and 144 deletions
|
|
@ -3,7 +3,7 @@
|
|||
using namespace std;
|
||||
|
||||
namespace tree_sitter {
|
||||
Grammar::Grammar(const rule_map_init_list &rules) :
|
||||
Grammar::Grammar(const std::initializer_list<std::pair<const std::string, rules::rule_ptr>> &rules) :
|
||||
rules(rules),
|
||||
start_rule_name(rules.begin()->first) {}
|
||||
|
||||
|
|
@ -21,5 +21,4 @@ namespace tree_sitter {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -7,15 +7,14 @@
|
|||
|
||||
namespace tree_sitter {
|
||||
class Grammar {
|
||||
typedef std::unordered_map<std::string, rules::rule_ptr> rule_map;
|
||||
typedef std::initializer_list<std::pair<const std::string, rules::rule_ptr>> rule_map_init_list;
|
||||
const rule_map rules;
|
||||
|
||||
public:
|
||||
Grammar(const rule_map_init_list &rules);
|
||||
const rules::rule_ptr rule(const std::string &) const;
|
||||
const std::string start_rule_name;
|
||||
std::vector<std::string> rule_names() const;
|
||||
|
||||
const std::unordered_map<std::string, rules::rule_ptr> rules;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -18,14 +18,14 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
transition_map<rules::Rule, Item> Item::transitions() const {
|
||||
return rules::transitions(rule).map<Item>([&](rules::rule_ptr to_rule) -> item_ptr {
|
||||
return lr::transitions(rule).map<Item>([&](rules::rule_ptr to_rule) -> item_ptr {
|
||||
return std::make_shared<Item>(rule_name, to_rule, consumed_sym_count + 1);
|
||||
});
|
||||
};
|
||||
|
||||
vector<rules::Symbol> Item::next_symbols() const {
|
||||
vector<rules::Symbol> result;
|
||||
for (auto pair : rules::transitions(rule)) {
|
||||
for (auto pair : lr::transitions(rule)) {
|
||||
shared_ptr<const rules::Symbol> sym = dynamic_pointer_cast<const rules::Symbol>(pair.first);
|
||||
if (sym) result.push_back(*sym);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
#include "transitions.h"
|
||||
#include "rules.h"
|
||||
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
class TransitionsVisitor : public Visitor {
|
||||
namespace lr {
|
||||
class TransitionsVisitor : public rules::Visitor {
|
||||
public:
|
||||
transition_map<Rule, Rule> value;
|
||||
|
||||
|
|
@ -16,7 +18,7 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
void visit(const Symbol *rule) {
|
||||
value = transition_map<Rule, Rule>({{ sym(rule->name), blank() }});
|
||||
value = transition_map<Rule, Rule>({{ std::make_shared<Symbol>(*rule), blank() }});
|
||||
}
|
||||
|
||||
void visit(const Choice *rule) {
|
||||
|
|
@ -5,8 +5,8 @@
|
|||
#include "transition_map.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
transition_map<Rule, Rule> transitions(const rule_ptr &rule);
|
||||
namespace lr {
|
||||
transition_map<rules::Rule, rules::Rule> transitions(const rules::rule_ptr &rule);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -11,10 +11,10 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
ostream& operator<<(ostream& stream, const rule_ptr &rule) {
|
||||
if (rule.get() == nullptr)
|
||||
stream << string("<NULL rule>");
|
||||
if (rule.get())
|
||||
stream << *rule;
|
||||
else
|
||||
stream << rule->to_string();
|
||||
stream << string("#<null rule>");
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue