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
|
|
|
|
2016-11-14 10:25:26 -08:00
|
|
|
Symbol::Symbol(Symbol::Index index) : index(index), is_token(false) {}
|
2014-09-07 22:16:45 -07:00
|
|
|
|
2016-11-14 10:25:26 -08:00
|
|
|
Symbol::Symbol(Symbol::Index index, bool is_token) : index(index), is_token(is_token) {}
|
2014-04-23 13:12:33 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
bool Symbol::operator==(const Symbol &other) const {
|
2015-09-05 17:05:37 -07:00
|
|
|
return (other.index == index) && (other.is_token == is_token);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Symbol::operator==(const Rule &rule) const {
|
2015-10-12 15:26:50 -07:00
|
|
|
auto other = rule.as<Symbol>();
|
2014-07-20 21:43:27 -07:00
|
|
|
return other && this->operator==(*other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Symbol::hash_code() const {
|
2016-11-14 10:25:26 -08:00
|
|
|
return hash<Symbol::Index>()(index) ^ hash<bool>()(is_token);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
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
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
string Symbol::to_string() const {
|
2015-09-05 17:05:37 -07:00
|
|
|
string name = is_token ? "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 {
|
2015-09-05 17:05:37 -07:00
|
|
|
if (is_token && !other.is_token)
|
2016-11-14 08:36:06 -08:00
|
|
|
return true;
|
|
|
|
|
if (!is_token && other.is_token)
|
2014-07-20 21:43:27 -07:00
|
|
|
return false;
|
|
|
|
|
return (index < other.index);
|
|
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2016-11-14 10:25:26 -08:00
|
|
|
bool Symbol::is_built_in(Symbol::Index index) {
|
2015-07-31 16:32:24 -07:00
|
|
|
return index < 0;
|
|
|
|
|
}
|
2014-04-25 22:17:23 -07:00
|
|
|
|
2016-11-14 10:25:26 -08:00
|
|
|
bool Symbol::is_built_in() const {
|
|
|
|
|
return is_built_in(index);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
} // namespace rules
|
|
|
|
|
} // namespace tree_sitter
|