2014-02-17 12:53:57 -08:00
|
|
|
#include "tree_sitter/compiler.h"
|
2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/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;
|
|
|
|
|
using std::ostream;
|
2014-06-09 21:05:25 -07:00
|
|
|
using std::pair;
|
|
|
|
|
using std::vector;
|
2014-02-16 22:13:08 -08:00
|
|
|
using rules::rule_ptr;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-24 13:05:04 -07:00
|
|
|
Grammar::Grammar(const std::vector<std::pair<std::string, rules::rule_ptr>> &rules) :
|
2014-06-09 21:47:57 -07:00
|
|
|
rules_(rules),
|
|
|
|
|
ubiquitous_tokens_({}) {}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-03 01:02:24 -08:00
|
|
|
bool Grammar::operator==(const Grammar &other) const {
|
2014-06-09 21:05:25 -07:00
|
|
|
if (other.rules_.size() != rules_.size()) return false;
|
2014-01-28 13:27:30 -08:00
|
|
|
|
2014-06-09 21:05:25 -07:00
|
|
|
for (size_t i = 0; i < rules_.size(); i++) {
|
|
|
|
|
auto &pair = rules_[i];
|
|
|
|
|
auto &other_pair = other.rules_[i];
|
2014-03-24 13:05:04 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2014-03-24 19:18:06 -07:00
|
|
|
|
2014-03-24 13:05:04 -07:00
|
|
|
string Grammar::start_rule_name() const {
|
2014-06-09 21:05:25 -07:00
|
|
|
return rules_.front().first;
|
2014-03-24 13:05:04 -07:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
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;
|
2014-06-09 21:05:25 -07:00
|
|
|
for (auto pair : grammar.rules()) {
|
2014-01-03 01:02:24 -08:00
|
|
|
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
|
|
|
}
|
2014-05-01 23:28:40 -07:00
|
|
|
|
|
|
|
|
GrammarError::GrammarError(GrammarErrorType type, std::string message) :
|
|
|
|
|
type(type),
|
|
|
|
|
message(message) {}
|
2014-05-30 13:29:54 -07:00
|
|
|
|
2014-05-19 20:54:59 -07:00
|
|
|
bool GrammarError::operator==(const GrammarError &other) const {
|
|
|
|
|
return type == other.type && message == other.message;
|
|
|
|
|
}
|
2014-05-01 23:28:40 -07:00
|
|
|
|
|
|
|
|
ostream& operator<<(ostream &stream, const GrammarError *error) {
|
|
|
|
|
if (error)
|
|
|
|
|
return stream << (string("#<grammar-error '") + error->message + "'>");
|
|
|
|
|
else
|
|
|
|
|
return stream << string("#<null>");
|
|
|
|
|
}
|
2014-06-09 21:14:38 -07:00
|
|
|
|
2014-06-09 21:05:25 -07:00
|
|
|
const vector<string> & Grammar::ubiquitous_tokens() const {
|
|
|
|
|
return ubiquitous_tokens_;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 13:27:16 -07:00
|
|
|
Grammar & Grammar::ubiquitous_tokens(const vector<string> &ubiquitous_tokens) {
|
2014-06-09 21:05:25 -07:00
|
|
|
ubiquitous_tokens_ = ubiquitous_tokens;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2014-06-09 21:14:38 -07:00
|
|
|
|
2014-06-25 13:27:16 -07:00
|
|
|
const vector<char> & Grammar::separators() const {
|
|
|
|
|
return separators_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Grammar & Grammar::separators(const vector<char> &separators) {
|
|
|
|
|
separators_ = separators;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 21:05:25 -07:00
|
|
|
const vector<pair<string, rule_ptr>> & Grammar::rules() const {
|
|
|
|
|
return rules_;
|
|
|
|
|
}
|
2013-12-28 23:26:20 -08:00
|
|
|
}
|