2014-02-17 12:53:57 -08:00
|
|
|
#include "tree_sitter/compiler.h"
|
2014-02-19 13:36:38 -08:00
|
|
|
#include "rules/rule.h"
|
2013-11-10 14:24:25 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-02-12 23:06:26 -08:00
|
|
|
using std::string;
|
2014-02-16 22:13:08 -08:00
|
|
|
using std::map;
|
2014-02-12 23:06:26 -08:00
|
|
|
using std::ostream;
|
2014-02-16 22:13:08 -08:00
|
|
|
using rules::rule_ptr;
|
2014-02-12 23:06:26 -08:00
|
|
|
|
2014-02-19 13:36:38 -08:00
|
|
|
Grammar::Grammar(std::string start_rule_name, const map<const string, const rule_ptr> &rules) :
|
2014-02-18 09:07:00 -08:00
|
|
|
start_rule_name(start_rule_name),
|
|
|
|
|
rules(rules) {}
|
2014-01-03 01:02:24 -08:00
|
|
|
|
|
|
|
|
bool Grammar::operator==(const Grammar &other) const {
|
|
|
|
|
if (other.start_rule_name != start_rule_name) return false;
|
|
|
|
|
if (other.rules.size() != rules.size()) return false;
|
2014-01-28 13:27:30 -08:00
|
|
|
|
2014-01-03 01:02:24 -08:00
|
|
|
for (auto pair : rules) {
|
|
|
|
|
auto other_pair = other.rules.find(pair.first);
|
|
|
|
|
if (other_pair == other.rules.end()) return false;
|
|
|
|
|
if (!other_pair->second->operator==(*pair.second)) return false;
|
|
|
|
|
}
|
2014-01-28 13:27:30 -08:00
|
|
|
|
2014-01-03 01:02:24 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 15:30:05 -08:00
|
|
|
ostream& operator<<(ostream &stream, const Grammar &grammar) {
|
2014-01-28 13:27:30 -08:00
|
|
|
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;
|
|
|
|
|
}
|
2014-02-19 13:36:38 -08:00
|
|
|
return stream << string("}>");
|
2014-01-03 01:02:24 -08:00
|
|
|
}
|
2013-12-28 23:26:20 -08:00
|
|
|
}
|