Simplify item_set -> state index map data in parse table builder
This commit is contained in:
parent
2f74c1de10
commit
c49913a6b1
4 changed files with 14 additions and 22 deletions
|
|
@ -56,9 +56,9 @@ Describe(item_sets) {
|
|||
It(can_be_hashed) {
|
||||
ItemSet set1 = ItemSet(Item::at_beginning_of_rule("factor", grammar), grammar);
|
||||
ItemSet set2 = ItemSet(Item::at_beginning_of_rule("factor", grammar), grammar);
|
||||
AssertThat(std::hash<ItemSet>()(set1), Equals(std::hash<ItemSet>()(set2)));
|
||||
AssertThat(std::hash<const ItemSet>()(set1), Equals(std::hash<const ItemSet>()(set2)));
|
||||
|
||||
ItemSet set3 = ItemSet(Item::at_beginning_of_rule("term", grammar), grammar);
|
||||
AssertThat(std::hash<ItemSet>()(set1), !Equals(std::hash<ItemSet>()(set3)));
|
||||
AssertThat(std::hash<const ItemSet>()(set1), !Equals(std::hash<const ItemSet>()(set3)));
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ namespace tree_sitter {
|
|||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<tree_sitter::lr::ItemSet> {
|
||||
struct hash<const tree_sitter::lr::ItemSet> {
|
||||
size_t operator()(const tree_sitter::lr::ItemSet &item_set) const {
|
||||
size_t result = hash<size_t>()(item_set.size());
|
||||
for (auto item : item_set)
|
||||
|
|
|
|||
|
|
@ -17,27 +17,27 @@ namespace tree_sitter {
|
|||
ParseTableBuilder::ParseTableBuilder(const Grammar &grammar) :
|
||||
grammar(grammar),
|
||||
table(ParseTable(grammar.rule_names())),
|
||||
state_indices(unordered_map<size_t, size_t>()) {};
|
||||
state_indices(unordered_map<const ItemSet, size_t>()) {};
|
||||
|
||||
void ParseTableBuilder::build() {
|
||||
auto item = Item(ParseTable::START, rules::sym(grammar.start_rule_name), 0);
|
||||
auto item_set = std::make_shared<ItemSet>(item, grammar);
|
||||
auto item_set = ItemSet(item, grammar);
|
||||
add_item_set(item_set);
|
||||
}
|
||||
|
||||
size_t ParseTableBuilder::add_item_set(const shared_ptr<const ItemSet> item_set) {
|
||||
auto state_index = state_index_for_item_set(*item_set);
|
||||
size_t ParseTableBuilder::add_item_set(const ItemSet &item_set) {
|
||||
auto state_index = state_index_for_item_set(item_set);
|
||||
if (state_index == NOT_FOUND) {
|
||||
state_index = table.add_state();
|
||||
state_indices[std::hash<ItemSet>()(*item_set)] = state_index;
|
||||
state_indices[item_set] = state_index;
|
||||
|
||||
for (auto transition : item_set->sym_transitions(grammar)) {
|
||||
for (auto transition : item_set.sym_transitions(grammar)) {
|
||||
rules::sym_ptr symbol = static_pointer_cast<const rules::Symbol>(transition.first);
|
||||
size_t new_state_index = add_item_set(transition.second);
|
||||
size_t new_state_index = add_item_set(*transition.second);
|
||||
table.add_action(state_index, symbol->name, ParseAction::Shift(new_state_index));
|
||||
}
|
||||
|
||||
for (Item item : *item_set) {
|
||||
for (Item item : item_set) {
|
||||
if (item.is_done()) {
|
||||
if (item.rule_name == ParseTable::START) {
|
||||
table.add_action(state_index, ParseTable::END_OF_INPUT, ParseAction::Accept());
|
||||
|
|
@ -52,7 +52,7 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
long ParseTableBuilder::state_index_for_item_set(const ItemSet &item_set) const {
|
||||
auto entry = state_indices.find(std::hash<ItemSet>()(item_set));
|
||||
auto entry = state_indices.find(item_set);
|
||||
return (entry == state_indices.end()) ? NOT_FOUND : entry->second;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,20 +8,12 @@
|
|||
|
||||
namespace tree_sitter {
|
||||
namespace lr {
|
||||
class ItemSet;
|
||||
|
||||
struct ItemSetPointerHasher {
|
||||
std::size_t operator()(const std::shared_ptr<const ItemSet> &item_set) const {
|
||||
return std::hash<ItemSet>()(*item_set);
|
||||
}
|
||||
};
|
||||
|
||||
class ParseTableBuilder {
|
||||
const Grammar grammar;
|
||||
ParseTable table;
|
||||
std::unordered_map<size_t, size_t> state_indices;
|
||||
std::unordered_map<const ItemSet, size_t> state_indices;
|
||||
|
||||
size_t add_item_set(const std::shared_ptr<const ItemSet> item_set);
|
||||
size_t add_item_set(const ItemSet &item_set);
|
||||
long state_index_for_item_set(const ItemSet &item_set) const;
|
||||
public:
|
||||
ParseTableBuilder(const Grammar &grammar);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue