2013-11-10 14:24:25 -08:00
|
|
|
#include "item.h"
|
2013-11-12 18:37:02 -08:00
|
|
|
#include "grammar.h"
|
2013-11-10 14:24:25 -08:00
|
|
|
|
2013-11-12 08:17:19 -08:00
|
|
|
using namespace std;
|
|
|
|
|
|
2013-11-10 14:24:25 -08:00
|
|
|
namespace tree_sitter {
|
|
|
|
|
namespace lr {
|
2013-11-12 08:17:19 -08:00
|
|
|
Item::Item(const std::string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) :
|
2013-11-10 14:24:25 -08:00
|
|
|
rule_name(rule_name),
|
|
|
|
|
rule(rule),
|
|
|
|
|
consumed_sym_count(consumed_sym_count) {};
|
|
|
|
|
|
2013-11-20 19:00:20 -08:00
|
|
|
Item Item::at_beginning_of_rule(const std::string &rule_name, const Grammar &grammar) {
|
2013-11-13 20:22:06 -08:00
|
|
|
return Item(rule_name, grammar.rule(rule_name), 0);
|
2013-11-12 18:37:02 -08:00
|
|
|
}
|
|
|
|
|
|
2013-11-10 14:24:25 -08:00
|
|
|
TransitionMap<Item> Item::transitions() const {
|
|
|
|
|
return rule->transitions().map<Item>([&](rules::rule_ptr to_rule) {
|
2013-11-20 19:00:20 -08:00
|
|
|
return std::make_shared<Item>(rule_name, to_rule, consumed_sym_count + 1);
|
2013-11-10 14:24:25 -08:00
|
|
|
});
|
|
|
|
|
};
|
2013-11-12 08:17:19 -08:00
|
|
|
|
2013-11-12 18:37:02 -08:00
|
|
|
vector<rules::sym_ptr> Item::next_symbols() const {
|
|
|
|
|
vector<rules::sym_ptr> result;
|
|
|
|
|
for (auto pair : rule->transitions()) {
|
|
|
|
|
shared_ptr<const rules::Symbol> sym = dynamic_pointer_cast<const rules::Symbol>(pair.first);
|
2013-11-20 19:00:20 -08:00
|
|
|
if (sym) result.push_back(sym);
|
2013-11-12 18:37:02 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Item::operator==(const Item &other) const {
|
2013-11-20 19:00:20 -08:00
|
|
|
bool rule_names_eq = other.rule_name == rule_name;
|
|
|
|
|
bool rules_eq = (*other.rule == *rule);
|
|
|
|
|
return rule_names_eq && rules_eq;
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<(ostream &stream, const Item &item) {
|
|
|
|
|
stream <<
|
|
|
|
|
string("(item '") <<
|
|
|
|
|
item.rule_name <<
|
|
|
|
|
string("' ") <<
|
|
|
|
|
*item.rule <<
|
|
|
|
|
string(")");
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|