tree-sitter/src/compiler/grammar.cc

43 lines
1.2 KiB
C++
Raw Normal View History

#include "tree_sitter/compiler.h"
2014-03-09 21:37:21 -07:00
#include "compiler/rules/rule.h"
namespace tree_sitter {
2014-02-12 23:06:26 -08:00
using std::string;
using std::ostream;
using rules::rule_ptr;
2014-03-09 19:49:35 -07:00
Grammar::Grammar(const std::vector<std::pair<std::string, rules::rule_ptr>> &rules) :
2014-02-18 09:07:00 -08:00
rules(rules) {}
2014-03-09 19:49:35 -07:00
2014-01-03 01:02:24 -08:00
bool Grammar::operator==(const Grammar &other) const {
if (other.rules.size() != rules.size()) return false;
for (size_t i = 0; i < rules.size(); i++) {
auto &pair = rules[i];
auto &other_pair = other.rules[i];
if (other_pair.first != pair.first) return false;
if (!other_pair.second->operator==(*pair.second)) return false;
2014-01-03 01:02:24 -08:00
}
2014-03-09 19:49:35 -07:00
2014-01-03 01:02:24 -08:00
return true;
}
string Grammar::start_rule_name() const {
return rules.front().first;
}
2014-03-09 19:49:35 -07:00
2014-01-04 15:30:05 -08:00
ostream& operator<<(ostream &stream, const Grammar &grammar) {
stream << string("#<grammar");
stream << string(" rules: {");
2014-01-03 01:02:24 -08:00
bool started = false;
for (auto pair : grammar.rules) {
if (started) stream << string(", ");
stream << pair.first;
stream << string(" => ");
stream << pair.second;
started = true;
}
return stream << string("}>");
2014-01-03 01:02:24 -08:00
}
2013-12-28 23:26:20 -08:00
}