2013-11-10 14:24:25 -08:00
|
|
|
#include "item.h"
|
2013-11-12 18:37:02 -08:00
|
|
|
#include "grammar.h"
|
2014-02-11 13:21:45 -08:00
|
|
|
#include "rule_can_be_blank.h"
|
2013-11-10 14:24:25 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-02-12 22:56:44 -08:00
|
|
|
using std::string;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using rules::Symbol;
|
2014-02-12 23:06:26 -08:00
|
|
|
using rules::rule_ptr;
|
2014-02-12 22:56:44 -08:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
namespace build_tables {
|
2014-02-12 23:06:26 -08:00
|
|
|
Item::Item(const Symbol &lhs, const rule_ptr rule) :
|
2014-01-27 12:40:06 -08:00
|
|
|
lhs(lhs),
|
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 <<
|
2014-02-13 13:12:00 -08:00
|
|
|
string("#<item ") <<
|
2014-01-27 12:40:06 -08:00
|
|
|
item.lhs <<
|
2014-02-13 13:12:00 -08:00
|
|
|
string(" ") <<
|
2014-01-18 09:47:26 -08:00
|
|
|
*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 <<
|
2014-02-13 13:12:00 -08:00
|
|
|
string("#<item ") <<
|
2014-01-27 12:40:06 -08:00
|
|
|
item.lhs <<
|
2014-02-13 13:12:00 -08:00
|
|
|
string(" ") <<
|
2014-01-21 23:38:23 -08:00
|
|
|
*item.rule <<
|
|
|
|
|
string(" ") <<
|
2014-01-28 22:09:37 -08:00
|
|
|
to_string(item.consumed_symbols.size()) <<
|
2014-01-21 23:38:23 -08:00
|
|
|
string(" ") <<
|
2014-01-27 12:40:06 -08:00
|
|
|
item.lookahead_sym <<
|
2014-01-21 23:38:23 -08:00
|
|
|
string(">");
|
2014-01-13 18:47:57 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-21 23:38:23 -08:00
|
|
|
bool LexItem::operator<(const LexItem &other) const {
|
2014-01-27 12:40:06 -08:00
|
|
|
if (lhs < other.lhs) return true;
|
|
|
|
|
if (other.lhs < lhs) return false;
|
2014-01-21 23:38:23 -08:00
|
|
|
if (rule->to_string() < other.rule->to_string()) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseItem::operator<(const ParseItem &other) const {
|
2014-01-27 12:40:06 -08:00
|
|
|
if (lhs < other.lhs) return true;
|
|
|
|
|
if (other.lhs < lhs) return false;
|
2014-01-21 23:38:23 -08:00
|
|
|
if (rule->to_string() < other.rule->to_string()) return true;
|
|
|
|
|
if (rule->to_string() > other.rule->to_string()) return false;
|
2014-01-28 22:09:37 -08:00
|
|
|
if (consumed_symbols < other.consumed_symbols) return true;
|
|
|
|
|
if (consumed_symbols > other.consumed_symbols) return false;
|
2014-01-27 12:40:06 -08:00
|
|
|
if (lookahead_sym < other.lookahead_sym) return true;
|
2014-01-21 23:38:23 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-12 23:06:26 -08:00
|
|
|
LexItem::LexItem(const Symbol &lhs, const rule_ptr rule) : Item(lhs, rule) {}
|
2014-01-18 09:47:26 -08:00
|
|
|
|
|
|
|
|
bool LexItem::operator==(const LexItem &other) const {
|
2014-01-27 12:40:06 -08:00
|
|
|
bool lhs_eq = other.lhs == lhs;
|
2014-01-18 09:47:26 -08:00
|
|
|
bool rules_eq = (*other.rule == *rule);
|
2014-01-27 12:40:06 -08:00
|
|
|
return lhs_eq && rules_eq;
|
2013-12-15 14:41:51 -08:00
|
|
|
}
|
|
|
|
|
|
2014-02-12 23:06:26 -08:00
|
|
|
ParseItem::ParseItem(const Symbol &lhs, const rule_ptr rule, const vector<bool> &consumed_symbols, const Symbol &lookahead_sym) :
|
2014-01-27 12:40:06 -08:00
|
|
|
Item(lhs, rule),
|
2014-01-28 22:09:37 -08:00
|
|
|
consumed_symbols(consumed_symbols),
|
2014-01-27 12:40:06 -08:00
|
|
|
lookahead_sym(lookahead_sym) {}
|
2014-01-18 09:47:26 -08:00
|
|
|
|
|
|
|
|
bool ParseItem::operator==(const ParseItem &other) const {
|
2014-01-27 12:40:06 -08:00
|
|
|
bool lhs_eq = other.lhs == lhs;
|
2014-01-18 09:47:26 -08:00
|
|
|
bool rules_eq = (*other.rule == *rule);
|
2014-01-28 22:09:37 -08:00
|
|
|
bool consumed_sym_counts_eq = (other.consumed_symbols == consumed_symbols);
|
2014-01-27 12:40:06 -08:00
|
|
|
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-10 14:24:25 -08:00
|
|
|
}
|
2013-11-12 08:17:19 -08:00
|
|
|
}
|
|
|
|
|
|