Make more things immutable

This commit is contained in:
Max Brunsfeld 2013-11-13 20:22:06 -08:00
parent f7063ba1d8
commit 8a0a442a24
12 changed files with 40 additions and 60 deletions

View file

@ -1,26 +1,14 @@
#include "grammar.h"
using namespace std;
namespace tree_sitter {
Grammar::Grammar(const rule_map &rules, const std::string &start_rule_name) :
Grammar::Grammar(const rule_map_init_list &rules) :
rules(rules),
start_rule_name(start_rule_name) {};
start_rule_name(rules.begin()->first) {}
std::unordered_map<std::string, rules::rule_ptr> build_rule_map(const initializer_list<string> &rule_names,
const initializer_list<rules::rule_ptr> &rule_vals) {
std::unordered_map<std::string, rules::rule_ptr> result;
auto rule_name_i = rule_names.begin();
auto rule_i = rule_vals.begin();
while (rule_i != rule_vals.end()) {
result[*rule_name_i] = *rule_i;
rule_i++;
rule_name_i++;
}
return result;
const rules::rule_ptr Grammar::rule(const std::string &name) {
auto iter = rules.find(name);
return (iter == rules.end()) ?
rules::rule_ptr(nullptr) :
iter->second;
}
Grammar::Grammar(const initializer_list<string> &names, const initializer_list<rules::rule_ptr> &values) :
rules(build_rule_map(names, values)),
start_rule_name(*names.begin()) {}
}

View file

@ -7,12 +7,15 @@
namespace tree_sitter {
class Grammar {
typedef std::unordered_map<std::string, rules::rule_ptr> rule_map;
typedef std::initializer_list<std::pair<const std::string, rules::rule_ptr>> rule_map_init_list;
public:
Grammar(const rule_map &rules, const std::string &start_rule_name);
Grammar(const std::initializer_list<std::string> &rule_names,
const std::initializer_list<rules::rule_ptr> &rules);
rule_map rules;
Grammar(const rule_map_init_list &rules);
const rules::rule_ptr rule(const std::string &);
const std::string start_rule_name;
private:
const rule_map rules;
};
}

View file

@ -11,7 +11,7 @@ namespace tree_sitter {
consumed_sym_count(consumed_sym_count) {};
Item Item::at_beginning_of_rule(const std::string &rule_name, Grammar &grammar) {
return Item(rule_name, grammar.rules[rule_name], 0);
return Item(rule_name, grammar.rule(rule_name), 0);
}
TransitionMap<Item> Item::transitions() const {

View file

@ -57,6 +57,5 @@ namespace tree_sitter {
stream << string(")");
return stream;
}
}
}

View file

@ -13,7 +13,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
std::string value;
const std::string value;
};
}
}

View file

@ -12,7 +12,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
char value;
const char value;
};
}
}

View file

@ -12,8 +12,8 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
rule_ptr left;
rule_ptr right;
const rule_ptr left;
const rule_ptr right;
};
}
}

View file

@ -12,8 +12,8 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
rule_ptr left;
rule_ptr right;
const rule_ptr left;
const rule_ptr right;
};
}
}

View file

@ -13,7 +13,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
std::string value;
const std::string value;
};
}
}