2013-11-10 14:24:25 -08:00
|
|
|
#include "item.h"
|
2013-11-12 18:37:02 -08:00
|
|
|
#include "grammar.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
#include "rule_transitions.h"
|
2013-11-10 14:24:25 -08:00
|
|
|
|
2014-01-04 15:30:05 -08:00
|
|
|
using std::string;
|
|
|
|
|
using std::ostream;
|
2013-11-12 08:17:19 -08:00
|
|
|
|
2013-11-10 14:24:25 -08:00
|
|
|
namespace tree_sitter {
|
2014-01-11 15:14:17 -08:00
|
|
|
namespace build_tables {
|
2014-01-11 16:48:40 -08:00
|
|
|
const int NO_SYMBOLS = -1;
|
|
|
|
|
|
2014-01-04 15:30:05 -08:00
|
|
|
Item::Item(const 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) {};
|
|
|
|
|
|
2014-01-04 15:30:05 -08:00
|
|
|
Item Item::at_beginning_of_rule(const 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
|
|
|
}
|
2014-01-03 22:42:05 -08:00
|
|
|
|
2014-01-04 15:30:05 -08:00
|
|
|
Item Item::at_beginning_of_token(const string &rule_name, const Grammar &grammar) {
|
2014-01-11 16:48:40 -08:00
|
|
|
return Item(rule_name, grammar.rule(rule_name), NO_SYMBOLS);
|
2014-01-03 22:42:05 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
|
2014-01-11 16:48:40 -08:00
|
|
|
int Item::next_sym_count() const {
|
|
|
|
|
return (consumed_sym_count == NO_SYMBOLS) ? NO_SYMBOLS : (consumed_sym_count + 1);
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
2014-01-11 16:48:40 -08:00
|
|
|
|
2013-11-12 08:17:19 -08:00
|
|
|
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);
|
2014-01-04 15:01:06 -08:00
|
|
|
bool consumed_sym_counts_eq = (other.consumed_sym_count == consumed_sym_count);
|
|
|
|
|
return rule_names_eq && rules_eq && consumed_sym_counts_eq;
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 14:41:51 -08:00
|
|
|
bool Item::is_done() const {
|
2014-01-11 16:48:40 -08:00
|
|
|
for (auto pair : rule_transitions(rule))
|
2014-01-04 15:01:06 -08:00
|
|
|
if (*pair.first == rules::Blank())
|
|
|
|
|
return true;
|
2014-01-03 22:42:05 -08:00
|
|
|
return false;
|
2013-12-15 14:41:51 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-04 15:30:05 -08:00
|
|
|
ostream& operator<<(ostream &stream, const Item &item) {
|
2014-01-04 15:01:06 -08:00
|
|
|
return stream <<
|
2013-12-30 23:12:19 -08:00
|
|
|
string("#<item '") <<
|
2013-11-12 08:17:19 -08:00
|
|
|
item.rule_name <<
|
|
|
|
|
string("' ") <<
|
|
|
|
|
*item.rule <<
|
2013-12-30 23:12:19 -08:00
|
|
|
string(">");
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|