tree-sitter/src/compiler/rules/metadata.cc

48 lines
1.4 KiB
C++
Raw Normal View History

#include "compiler/rules/metadata.h"
#include <string>
#include <map>
2014-04-14 08:38:11 -07:00
#include "compiler/rules/visitor.h"
namespace tree_sitter {
using std::hash;
using std::make_shared;
using std::map;
2014-04-04 13:10:55 -07:00
namespace rules {
Metadata::Metadata(rule_ptr rule, map<MetadataKey, int> values) : rule(rule), value(values) {}
2014-04-04 13:10:55 -07:00
bool Metadata::operator==(const Rule &rule) const {
auto other = dynamic_cast<const Metadata *>(&rule);
return other && other->value == value && other->rule->operator==(*this->rule);
}
2014-04-04 13:10:55 -07:00
size_t Metadata::hash_code() const {
size_t result = hash<size_t>()(value.size());
for (auto &pair : value) {
result ^= hash<int>()(pair.first);
result ^= hash<int>()(pair.second);
}
return result;
}
2014-04-04 13:10:55 -07:00
rule_ptr Metadata::copy() const {
return make_shared<Metadata>(rule->copy(), value);
}
int Metadata::value_for(MetadataKey key) const {
auto pair = value.find(key);
return (pair != value.end()) ?
pair->second :
0;
}
2014-04-04 13:10:55 -07:00
std::string Metadata::to_string() const {
return "#<metadata " + rule->to_string() + ">";
}
2014-04-04 13:10:55 -07:00
void Metadata::accept(Visitor *visitor) const {
visitor->visit(this);
}
}
2014-04-14 08:38:11 -07:00
}