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

57 lines
1.3 KiB
C++
Raw Normal View History

#include "compiler/rules/symbol.h"
#include <string>
2014-04-28 21:46:43 -07:00
#include <map>
#include "compiler/rules/visitor.h"
namespace tree_sitter {
namespace rules {
2014-04-25 22:17:23 -07:00
using std::string;
using std::to_string;
using std::hash;
2014-04-25 22:17:23 -07:00
Symbol::Symbol(int index) : index(index), is_token(false) {}
Symbol::Symbol(int index, bool is_token) : index(index), is_token(is_token) {}
bool Symbol::operator==(const Symbol &other) const {
return (other.index == index) && (other.is_token == is_token);
}
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return other && this->operator==(*other);
}
size_t Symbol::hash_code() const {
return hash<int>()(index) ^ hash<bool>()(is_token);
}
2014-04-25 22:17:23 -07:00
2015-07-31 16:32:24 -07:00
rule_ptr Symbol::copy() const {
return std::make_shared<Symbol>(*this);
}
2014-04-25 22:17:23 -07:00
string Symbol::to_string() const {
string name = is_token ? "token" : "sym";
return "(" + name + " " + std::to_string(index) + ")";
}
2014-04-25 22:17:23 -07:00
bool Symbol::operator<(const Symbol &other) const {
if (!is_token && other.is_token)
return true;
if (is_token && !other.is_token)
return false;
return (index < other.index);
}
2014-04-25 22:17:23 -07:00
2015-07-31 16:32:24 -07:00
bool Symbol::is_built_in() const {
return index < 0;
}
2014-04-25 22:17:23 -07:00
2015-07-31 16:32:24 -07:00
void Symbol::accept(Visitor *visitor) const {
visitor->visit(this);
}
2014-04-25 22:17:23 -07:00
} // namespace rules
} // namespace tree_sitter