2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/rules/symbol.h"
|
2014-02-19 13:05:54 -08:00
|
|
|
#include <map>
|
2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/rules/visitor.h"
|
2013-12-19 23:05:54 -08:00
|
|
|
|
2013-11-07 13:24:01 -08:00
|
|
|
namespace tree_sitter {
|
2014-03-09 21:37:21 -07:00
|
|
|
using std::string;
|
|
|
|
|
using std::hash;
|
|
|
|
|
|
2013-11-07 13:24:01 -08:00
|
|
|
namespace rules {
|
2014-02-19 13:05:54 -08:00
|
|
|
Symbol::Symbol(const std::string &name) : name(name), type(SymbolTypeNormal) {};
|
|
|
|
|
Symbol::Symbol(const std::string &name, SymbolType type) : name(name), type(type) {};
|
2013-11-14 12:55:02 -08:00
|
|
|
|
2013-11-07 13:24:01 -08:00
|
|
|
bool Symbol::operator==(const Rule &rule) const {
|
|
|
|
|
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
|
2014-01-31 00:13:05 -08:00
|
|
|
return other && this->operator==(*other);
|
2013-11-07 13:24:01 -08:00
|
|
|
}
|
2014-01-31 00:13:05 -08:00
|
|
|
|
|
|
|
|
bool Symbol::operator==(const Symbol &other) const {
|
2014-02-19 13:05:54 -08:00
|
|
|
return (other.name == name) && (other.type == type);
|
2014-01-31 00:13:05 -08:00
|
|
|
}
|
|
|
|
|
|
2013-12-30 23:52:38 -08:00
|
|
|
size_t Symbol::hash_code() const {
|
2014-02-19 13:05:54 -08:00
|
|
|
return hash<string>()(name) ^ hash<short int>()(type);
|
2013-12-30 23:52:38 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-02 13:04:41 -08:00
|
|
|
rule_ptr Symbol::copy() const {
|
|
|
|
|
return std::make_shared<Symbol>(*this);
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2013-12-19 23:05:54 -08:00
|
|
|
string Symbol::to_string() const {
|
2014-02-19 13:05:54 -08:00
|
|
|
switch (type) {
|
|
|
|
|
case SymbolTypeNormal:
|
|
|
|
|
return string("#<sym '") + name + "'>";
|
|
|
|
|
case SymbolTypeHidden:
|
|
|
|
|
return string("#<hidden_sym '") + name + "'>";
|
|
|
|
|
case SymbolTypeAuxiliary:
|
|
|
|
|
return string("#<aux_sym '") + name + "'>";
|
2014-02-26 19:03:43 -08:00
|
|
|
case SymbolTypeBuiltIn:
|
|
|
|
|
return string("#<builtin_sym '") + name + "'>";
|
2014-02-19 13:05:54 -08:00
|
|
|
}
|
2013-11-10 14:24:25 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-04 15:01:06 -08:00
|
|
|
bool Symbol::operator<(const Symbol &other) const {
|
2014-02-19 13:05:54 -08:00
|
|
|
if (type < other.type) return true;
|
|
|
|
|
if (type > other.type) return false;
|
2014-02-10 18:38:01 -08:00
|
|
|
return (name < other.name);
|
2014-01-04 15:01:06 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-26 19:03:43 -08:00
|
|
|
bool Symbol::is_built_in() const {
|
|
|
|
|
return type == SymbolTypeBuiltIn;
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-19 13:05:54 -08:00
|
|
|
bool Symbol::is_auxiliary() const {
|
|
|
|
|
return type == SymbolTypeAuxiliary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Symbol::is_hidden() const {
|
|
|
|
|
return (type == SymbolTypeHidden || type == SymbolTypeAuxiliary);
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2013-12-19 23:16:13 -08:00
|
|
|
void Symbol::accept(Visitor &visitor) const {
|
2013-12-18 20:58:05 -08:00
|
|
|
visitor.visit(this);
|
|
|
|
|
}
|
2013-11-07 13:24:01 -08:00
|
|
|
}
|
|
|
|
|
}
|