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

57 lines
1.5 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), options(SymbolOption(0)) {}
2014-04-25 22:17:23 -07:00
Symbol::Symbol(int index, SymbolOption options)
: index(index), options(options) {}
bool Symbol::operator==(const Symbol &other) const {
return (other.index == index) && (other.options == options);
}
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<int16_t>()(options);
}
2014-04-25 22:17:23 -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 = (options & SymbolOptionAuxiliary) ? "aux_" : "";
name += (options & SymbolOptionToken) ? "token" : "sym";
return "(" + name + " " + std::to_string(index) + ")";
}
2014-04-25 22:17:23 -07:00
bool Symbol::operator<(const Symbol &other) const {
if (options < other.options)
return true;
if (options > other.options)
return false;
return (index < other.index);
}
2014-04-25 22:17:23 -07:00
bool Symbol::is_token() const { return options & SymbolOptionToken; }
2014-04-25 22:17:23 -07:00
bool Symbol::is_built_in() const { return index < 0; }
2014-04-25 22:17:23 -07:00
bool Symbol::is_auxiliary() const { return options & SymbolOptionAuxiliary; }
2014-04-25 22:17:23 -07:00
void Symbol::accept(Visitor *visitor) const { visitor->visit(this); }
2014-04-25 22:17:23 -07:00
} // namespace rules
} // namespace tree_sitter