Make more things immutable
This commit is contained in:
parent
f7063ba1d8
commit
8a0a442a24
12 changed files with 40 additions and 60 deletions
|
|
@ -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()) {}
|
||||
}
|
||||
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -57,6 +57,5 @@ namespace tree_sitter {
|
|||
stream << string(")");
|
||||
return stream;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace tree_sitter {
|
|||
bool operator==(const Rule& other) const;
|
||||
std::string to_string() const;
|
||||
private:
|
||||
char value;
|
||||
const char value;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue