tree-sitter/src/compiler/build_tables/parse_item.cc
Max Brunsfeld 97bb7a26cf Fix precedence calculations when building parse table
* Recurse into choice rules
* Compute reduction precedence differently than shift precedence
2015-09-02 13:05:54 -07:00

39 lines
1.2 KiB
C++

#include "compiler/build_tables/parse_item.h"
#include "tree_sitter/compiler.h"
namespace tree_sitter {
namespace build_tables {
using std::string;
using std::vector;
using std::ostream;
ParseItem::ParseItem(const rules::Symbol &lhs, const rules::rule_ptr rule,
const vector<rules::Symbol> &consumed_symbols)
: Item(lhs, rule), consumed_symbols(consumed_symbols) {}
bool ParseItem::operator==(const ParseItem &other) const {
return (lhs == other.lhs) &&
(consumed_symbols.size() == other.consumed_symbols.size()) &&
(rule == other.rule || rule->operator==(*other.rule));
}
bool ParseItem::operator<(const ParseItem &other) const {
if (lhs < other.lhs)
return true;
if (other.lhs < lhs)
return false;
if (consumed_symbols.size() < other.consumed_symbols.size())
return true;
if (other.consumed_symbols.size() < consumed_symbols.size())
return false;
return rule < other.rule;
}
ostream &operator<<(ostream &stream, const ParseItem &item) {
return stream << string("(item ") << item.lhs << string(" ") << *item.rule
<< string(")");
}
} // namespace build_tables
} // namespace tree_sitter