2014-04-28 20:43:27 -07:00
|
|
|
#include "compiler/rules/symbol.h"
|
2014-04-22 23:38:26 -07:00
|
|
|
#include <string>
|
2014-04-28 21:46:43 -07:00
|
|
|
#include <map>
|
2014-04-22 23:38:26 -07:00
|
|
|
#include "compiler/rules/visitor.h"
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
namespace tree_sitter {
|
|
|
|
|
namespace rules {
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
using std::string;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::hash;
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
Symbol::Symbol(int index) : index(index), options(SymbolOption(0)) {}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
Symbol::Symbol(int index, SymbolOption options)
|
|
|
|
|
: index(index), options(options) {}
|
2014-04-23 13:12:33 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
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
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
rule_ptr Symbol::copy() const { return std::make_shared<Symbol>(*this); }
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
string Symbol::to_string() const {
|
|
|
|
|
string name = (options & SymbolOptionAuxiliary) ? "aux_" : "";
|
|
|
|
|
name += (options & SymbolOptionToken) ? "token" : "sym";
|
2014-08-23 13:52:00 -07:00
|
|
|
return "(" + name + " " + std::to_string(index) + ")";
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -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
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
bool Symbol::is_token() const { return options & SymbolOptionToken; }
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
bool Symbol::is_built_in() const { return index < 0; }
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
bool Symbol::is_auxiliary() const { return options & SymbolOptionAuxiliary; }
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
void Symbol::accept(Visitor *visitor) const { visitor->visit(this); }
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
} // namespace rules
|
|
|
|
|
} // namespace tree_sitter
|