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

83 lines
2.9 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::to_string;
2014-01-04 15:30:05 -08:00
using std::ostream;
using tree_sitter::rules::Symbol;
2013-11-12 08:17:19 -08:00
namespace tree_sitter {
2014-01-11 15:14:17 -08:00
namespace build_tables {
Item::Item(const Symbol &lhs, const rules::rule_ptr rule) :
lhs(lhs),
rule(rule) {};
bool Item::is_done() const {
return rule_can_be_blank(rule);
}
2013-11-12 08:17:19 -08:00
ostream& operator<<(ostream &stream, const LexItem &item) {
return stream <<
string("#<item '") <<
item.lhs <<
string("' ") <<
*item.rule <<
string(">");
2013-11-12 08:17:19 -08:00
}
ostream& operator<<(ostream &stream, const ParseItem &item) {
return stream <<
string("#<item '") <<
item.lhs <<
string("' ") <<
*item.rule <<
string(" ") <<
to_string(item.consumed_sym_count) <<
string(" ") <<
item.lookahead_sym <<
string(">");
2014-01-13 18:47:57 -08:00
}
bool LexItem::operator<(const LexItem &other) const {
if (lhs < other.lhs) return true;
if (other.lhs < lhs) return false;
if (rule->to_string() < other.rule->to_string()) return true;
return false;
}
bool ParseItem::operator<(const ParseItem &other) const {
if (lhs < other.lhs) return true;
if (other.lhs < lhs) 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 (consumed_sym_count > other.consumed_sym_count) return false;
if (lookahead_sym < other.lookahead_sym) return true;
return false;
}
LexItem::LexItem(const Symbol &lhs, const rules::rule_ptr rule) : Item(lhs, rule) {}
bool LexItem::operator==(const LexItem &other) const {
bool lhs_eq = other.lhs == lhs;
bool rules_eq = (*other.rule == *rule);
return lhs_eq && rules_eq;
2013-12-15 14:41:51 -08:00
}
ParseItem::ParseItem(const Symbol &lhs, const rules::rule_ptr rule, int consumed_sym_count, const rules::Symbol &lookahead_sym) :
Item(lhs, rule),
consumed_sym_count(consumed_sym_count),
lookahead_sym(lookahead_sym) {}
bool ParseItem::operator==(const ParseItem &other) const {
bool lhs_eq = other.lhs == lhs;
bool rules_eq = (*other.rule == *rule);
bool consumed_sym_counts_eq = (other.consumed_sym_count == consumed_sym_count);
bool lookaheads_eq = other.lookahead_sym == lookahead_sym;
return lhs_eq && rules_eq && consumed_sym_counts_eq && lookaheads_eq;
2013-11-12 08:17:19 -08:00
}
}
2013-11-12 08:17:19 -08:00
}