tree-sitter/src/compiler/rules/symbol.cpp

45 lines
1.4 KiB
C++
Raw Normal View History

2013-12-18 20:58:05 -08:00
#include "rules.h"
#include <unordered_map>
2013-11-07 13:24:01 -08:00
using std::string;
2013-12-30 23:52:38 -08:00
using std::hash;
2013-11-07 13:24:01 -08:00
namespace tree_sitter {
namespace rules {
Symbol::Symbol(const std::string &name) : name(name), is_auxiliary(false) {};
Symbol::Symbol(const std::string &name, bool is_auxiliary) : name(name), is_auxiliary(is_auxiliary) {};
2013-11-07 13:24:01 -08:00
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return other && this->operator==(*other);
2013-11-07 13:24:01 -08:00
}
bool Symbol::operator==(const Symbol &other) const {
return (other.name == name) && (other.is_auxiliary == is_auxiliary);
}
2013-12-30 23:52:38 -08:00
size_t Symbol::hash_code() const {
return typeid(this).hash_code() ^ hash<string>()(name) ^ hash<bool>()(is_auxiliary);
2013-12-30 23:52:38 -08:00
}
2014-01-02 13:04:41 -08:00
rule_ptr Symbol::copy() const {
return std::make_shared<Symbol>(*this);
}
string Symbol::to_string() const {
return is_auxiliary ?
string("#<aux_sym '") + name + "'>" :
string("#<sym '") + name + "'>";
}
2013-12-18 20:58:05 -08:00
bool Symbol::operator<(const Symbol &other) const {
if (name < other.name) return true;
if (other.name < name) return false;
return is_auxiliary < other.is_auxiliary;
}
2013-12-19 23:16:13 -08:00
void Symbol::accept(Visitor &visitor) const {
2013-12-18 20:58:05 -08:00
visitor.visit(this);
}
2013-11-07 13:24:01 -08:00
}
}