tree-sitter/src/compiler/build_tables/item.cpp

54 lines
1.7 KiB
C++
Raw Normal View History

#include "item.h"
#include "grammar.h"
2014-01-11 15:14:17 -08:00
#include "rule_transitions.h"
2014-01-04 15:30:05 -08:00
using std::string;
using std::ostream;
2013-11-12 08:17:19 -08:00
namespace tree_sitter {
2014-01-11 15:14:17 -08:00
namespace build_tables {
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) :
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);
}
2014-01-04 15:30:05 -08:00
Item Item::at_beginning_of_token(const string &rule_name, const Grammar &grammar) {
return Item(rule_name, grammar.rule(rule_name), NO_SYMBOLS);
}
2013-11-12 08:17:19 -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
}
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);
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 {
for (auto pair : rule_transitions(rule))
if (*pair.first == rules::Blank())
return true;
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) {
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-12 08:17:19 -08:00
}