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

65 lines
2 KiB
C++
Raw Normal View History

#include "symbol.h"
#include "visitor.h"
#include <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), type(SymbolTypeNormal) {};
Symbol::Symbol(const std::string &name, SymbolType type) : name(name), type(type) {};
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.type == type);
}
2013-12-30 23:52:38 -08:00
size_t Symbol::hash_code() const {
return hash<string>()(name) ^ hash<short int>()(type);
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 {
switch (type) {
case SymbolTypeNormal:
return string("#<sym '") + name + "'>";
case SymbolTypeHidden:
return string("#<hidden_sym '") + name + "'>";
case SymbolTypeAuxiliary:
return string("#<aux_sym '") + name + "'>";
2014-02-26 19:03:43 -08:00
case SymbolTypeBuiltIn:
return string("#<builtin_sym '") + name + "'>";
}
}
2013-12-18 20:58:05 -08:00
bool Symbol::operator<(const Symbol &other) const {
if (type < other.type) return true;
if (type > other.type) return false;
return (name < other.name);
}
2014-02-26 19:03:43 -08:00
bool Symbol::is_built_in() const {
return type == SymbolTypeBuiltIn;
}
bool Symbol::is_auxiliary() const {
return type == SymbolTypeAuxiliary;
}
bool Symbol::is_hidden() const {
return (type == SymbolTypeHidden || type == SymbolTypeAuxiliary);
}
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
}
}