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"
|
|
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::hash;
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-22 23:38:26 -07:00
|
|
|
namespace rules {
|
2014-04-28 20:43:27 -07:00
|
|
|
Symbol::Symbol(int index) :
|
2014-04-22 23:38:26 -07:00
|
|
|
index(index),
|
|
|
|
|
options(SymbolOption(0)) {}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
Symbol::Symbol(int index, SymbolOption options) :
|
2014-04-22 23:38:26 -07:00
|
|
|
index(index),
|
|
|
|
|
options(options) {}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
bool Symbol::operator==(const Symbol &other) const {
|
2014-04-23 13:12:33 -07:00
|
|
|
return (other.index == index) && (other.options == options);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
bool Symbol::operator==(const Rule &rule) const {
|
|
|
|
|
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
|
2014-04-23 13:12:33 -07:00
|
|
|
return other && this->operator==(*other);
|
2014-04-22 23:38:26 -07:00
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
size_t Symbol::hash_code() const {
|
2014-04-23 08:32:11 -07:00
|
|
|
return hash<int>()(index) ^ hash<int16_t>()(options);
|
2014-04-22 23:38:26 -07:00
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
rule_ptr Symbol::copy() const {
|
|
|
|
|
return std::make_shared<Symbol>(*this);
|
2014-04-22 23:38:26 -07:00
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
string Symbol::to_string() const {
|
2014-04-22 23:38:26 -07:00
|
|
|
string name = (options & SymbolOptionAuxiliary) ? "aux_" : "";
|
|
|
|
|
name += (options & SymbolOptionToken) ? "token" : "sym";
|
2014-06-16 08:35:20 -07:00
|
|
|
return "#<" + name + " " + std::to_string(index) + ">";
|
2014-04-22 23:38:26 -07:00
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
bool Symbol::operator<(const Symbol &other) const {
|
2014-04-22 23:38:26 -07:00
|
|
|
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-04-28 20:43:27 -07:00
|
|
|
bool Symbol::is_token() const {
|
2014-04-22 23:38:26 -07:00
|
|
|
return options & SymbolOptionToken;
|
|
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
bool Symbol::is_built_in() const {
|
2014-04-22 23:38:26 -07:00
|
|
|
return index < 0;
|
|
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
bool Symbol::is_auxiliary() const {
|
2014-04-22 23:38:26 -07:00
|
|
|
return options & SymbolOptionAuxiliary;
|
|
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2014-04-28 20:43:27 -07:00
|
|
|
void Symbol::accept(Visitor *visitor) const {
|
2014-04-22 23:38:26 -07:00
|
|
|
visitor->visit(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|