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;
|
2014-01-21 23:38:23 -08:00
|
|
|
using std::to_string;
|
2014-01-04 15:30:05 -08:00
|
|
|
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-18 09:47:26 -08:00
|
|
|
Item::Item(const string &rule_name, const rules::rule_ptr rule) :
|
2013-11-10 14:24:25 -08:00
|
|
|
rule_name(rule_name),
|
2014-01-18 09:47:26 -08:00
|
|
|
rule(rule) {};
|
2013-11-10 14:24:25 -08:00
|
|
|
|
2014-01-18 09:47:26 -08:00
|
|
|
bool Item::is_done() const {
|
2014-01-19 01:49:56 -08:00
|
|
|
return rule_can_be_blank(rule);
|
2014-01-03 22:42:05 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
|
2014-01-21 23:38:23 -08:00
|
|
|
ostream& operator<<(ostream &stream, const LexItem &item) {
|
2014-01-18 09:47:26 -08:00
|
|
|
return stream <<
|
|
|
|
|
string("#<item '") <<
|
|
|
|
|
item.rule_name <<
|
|
|
|
|
string("' ") <<
|
|
|
|
|
*item.rule <<
|
|
|
|
|
string(">");
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-21 23:38:23 -08:00
|
|
|
ostream& operator<<(ostream &stream, const ParseItem &item) {
|
|
|
|
|
return stream <<
|
|
|
|
|
string("#<item '") <<
|
|
|
|
|
item.rule_name <<
|
|
|
|
|
string("' ") <<
|
|
|
|
|
*item.rule <<
|
|
|
|
|
string(" ") <<
|
|
|
|
|
to_string(item.consumed_sym_count) <<
|
|
|
|
|
string(" ") <<
|
|
|
|
|
item.lookahead_sym_name <<
|
|
|
|
|
string(">");
|
2014-01-13 18:47:57 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-21 23:38:23 -08:00
|
|
|
bool LexItem::operator<(const LexItem &other) const {
|
|
|
|
|
if (rule_name < other.rule_name) return true;
|
|
|
|
|
if (rule->to_string() < other.rule->to_string()) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseItem::operator<(const ParseItem &other) const {
|
|
|
|
|
if (rule_name < other.rule_name) return true;
|
|
|
|
|
if (rule_name > other.rule_name) return false;
|
|
|
|
|
if (rule->to_string() < other.rule->to_string()) return true;
|
|
|
|
|
if (rule->to_string() > other.rule->to_string()) return false;
|
|
|
|
|
if (consumed_sym_count < other.consumed_sym_count) return true;
|
|
|
|
|
if (lookahead_sym_name < other.lookahead_sym_name) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-18 09:47:26 -08:00
|
|
|
LexItem::LexItem(const std::string &rule_name, const rules::rule_ptr rule) : Item(rule_name, rule) {}
|
|
|
|
|
|
|
|
|
|
bool LexItem::operator==(const LexItem &other) const {
|
|
|
|
|
bool rule_names_eq = other.rule_name == rule_name;
|
|
|
|
|
bool rules_eq = (*other.rule == *rule);
|
|
|
|
|
return rule_names_eq && rules_eq;
|
2013-12-15 14:41:51 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-19 01:49:56 -08:00
|
|
|
ParseItem::ParseItem(const std::string &rule_name, const rules::rule_ptr rule, int consumed_sym_count, const std::string &lookahead_sym_name) :
|
2014-01-18 09:47:26 -08:00
|
|
|
Item(rule_name, rule),
|
|
|
|
|
consumed_sym_count(consumed_sym_count),
|
2014-01-19 01:49:56 -08:00
|
|
|
lookahead_sym_name(lookahead_sym_name) {}
|
2014-01-18 09:47:26 -08:00
|
|
|
|
|
|
|
|
bool ParseItem::operator==(const ParseItem &other) const {
|
|
|
|
|
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);
|
2014-01-21 23:38:23 -08:00
|
|
|
bool lookaheads_eq = other.lookahead_sym_name == lookahead_sym_name;
|
|
|
|
|
return rule_names_eq && rules_eq && consumed_sym_counts_eq && lookaheads_eq;
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|