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-12 18:37:02 -08:00
|
|
|
Item Item::at_beginning_of_rule(const std::string &rule_name, 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) {
|
|
|
|
|
return item_ptr(new Item(rule_name, to_rule, consumed_sym_count + 1));
|
|
|
|
|
});
|
|
|
|
|
};
|
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);
|
|
|
|
|
if (sym != nullptr) result.push_back(sym);
|
|
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Item::operator==(const Item &other) const {
|
|
|
|
|
return (
|
|
|
|
|
other.rule_name == rule_name &&
|
|
|
|
|
other.rule == rule &&
|
|
|
|
|
other.consumed_sym_count == consumed_sym_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|