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

53 lines
1.6 KiB
C++
Raw Normal View History

2014-03-09 21:37:21 -07:00
#include "compiler/rules/choice.h"
2014-03-09 22:45:33 -07:00
#include <string>
#include <set>
2014-03-09 21:37:21 -07:00
#include "compiler/rules/visitor.h"
2013-11-07 13:24:01 -08:00
namespace tree_sitter {
using std::string;
using std::make_shared;
using std::vector;
using std::set;
using std::dynamic_pointer_cast;
2014-03-09 19:49:35 -07:00
2013-11-07 13:24:01 -08:00
namespace rules {
Choice::Choice(const vector<rule_ptr> &elements) : elements(elements) {}
2014-03-09 19:49:35 -07:00
rule_ptr Choice::Build(const vector<rule_ptr> &elements) {
return make_shared<Choice>(elements);
}
2014-03-09 19:49:35 -07:00
2013-11-07 13:24:01 -08:00
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
if (!other) return false;
size_t size = elements.size();
if (size != other->elements.size()) return false;
for (size_t i = 0; i < size; i++)
if (!elements[i]->operator==(*other->elements[i])) return false;
return true;
2013-11-07 13:24:01 -08:00
}
2013-12-30 23:52:38 -08:00
size_t Choice::hash_code() const {
size_t result = std::hash<size_t>()(elements.size());
for (const auto &element : elements)
result ^= element->hash_code();
return result;
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 Choice::copy() const {
return std::make_shared<Choice>(*this);
}
string Choice::to_string() const {
string result = "#<choice";
for (const auto &element : elements)
result += " " + element->to_string();
return result + ">";
2013-11-07 13:24:01 -08:00
}
2014-03-09 19:49:35 -07:00
void Choice::accept(Visitor *visitor) const {
visitor->visit(this);
2013-12-18 20:58:05 -08:00
}
2013-11-07 13:24:01 -08:00
}
2014-04-14 08:38:11 -07:00
}