Refactor parse table builder
This commit is contained in:
parent
c49913a6b1
commit
73d8cb3722
2 changed files with 27 additions and 17 deletions
|
|
@ -17,7 +17,8 @@ namespace tree_sitter {
|
|||
ParseTableBuilder::ParseTableBuilder(const Grammar &grammar) :
|
||||
grammar(grammar),
|
||||
table(ParseTable(grammar.rule_names())),
|
||||
state_indices(unordered_map<const ItemSet, 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);
|
||||
|
|
@ -31,24 +32,31 @@ namespace tree_sitter {
|
|||
state_index = table.add_state();
|
||||
state_indices[item_set] = state_index;
|
||||
|
||||
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);
|
||||
table.add_action(state_index, symbol->name, ParseAction::Shift(new_state_index));
|
||||
}
|
||||
|
||||
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());
|
||||
} else {
|
||||
for (string rule_name : table.symbol_names)
|
||||
table.add_action(state_index, rule_name, ParseAction::Reduce(item.rule_name, item.consumed_sym_count));
|
||||
}
|
||||
add_shift_actions(item_set, state_index);
|
||||
add_reduce_actions(item_set, state_index);
|
||||
}
|
||||
return state_index;
|
||||
}
|
||||
|
||||
void ParseTableBuilder::add_shift_actions(const ItemSet &item_set, size_t state_index) {
|
||||
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);
|
||||
table.add_action(state_index, symbol->name, ParseAction::Shift(new_state_index));
|
||||
}
|
||||
}
|
||||
|
||||
void ParseTableBuilder::add_reduce_actions(const ItemSet &item_set, size_t state_index) {
|
||||
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());
|
||||
} else {
|
||||
for (string rule_name : table.symbol_names)
|
||||
table.add_action(state_index, rule_name, ParseAction::Reduce(item.rule_name, item.consumed_sym_count));
|
||||
}
|
||||
}
|
||||
}
|
||||
return state_index;
|
||||
}
|
||||
|
||||
long ParseTableBuilder::state_index_for_item_set(const ItemSet &item_set) const {
|
||||
|
|
|
|||
|
|
@ -13,8 +13,10 @@ namespace tree_sitter {
|
|||
ParseTable table;
|
||||
std::unordered_map<const ItemSet, size_t> state_indices;
|
||||
|
||||
size_t add_item_set(const ItemSet &item_set);
|
||||
long state_index_for_item_set(const ItemSet &item_set) const;
|
||||
void add_shift_actions(const ItemSet &item_set, size_t state_index);
|
||||
void add_reduce_actions(const ItemSet &item_set, size_t state_index);
|
||||
size_t add_item_set(const ItemSet &item_set);
|
||||
public:
|
||||
ParseTableBuilder(const Grammar &grammar);
|
||||
static ParseTable build_table(const Grammar &grammar);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue