Make more things immutable
This commit is contained in:
parent
f7063ba1d8
commit
8a0a442a24
12 changed files with 40 additions and 60 deletions
|
|
@ -13,11 +13,11 @@ Describe(item_sets) {
|
|||
AssertThat(
|
||||
item_set,
|
||||
EqualsContainer(ItemSet({
|
||||
Item("term", grammar.rules["term"], 0),
|
||||
Item("factor", grammar.rules["factor"], 0),
|
||||
Item("variable", grammar.rules["variable"], 0),
|
||||
Item("number", grammar.rules["number"], 0),
|
||||
Item("left_paren", grammar.rules["left_paren"], 0),
|
||||
Item("term", grammar.rule("term"), 0),
|
||||
Item("factor", grammar.rule("factor"), 0),
|
||||
Item("variable", grammar.rule("variable"), 0),
|
||||
Item("number", grammar.rule("number"), 0),
|
||||
Item("left_paren", grammar.rule("left_paren"), 0),
|
||||
})));
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ Describe(items) {
|
|||
|
||||
It(finds_the_item_at_the_start_of_a_rule) {
|
||||
Item item = Item::at_beginning_of_rule("expression", grammar);
|
||||
AssertThat(item, Equals(Item("expression", grammar.rules["expression"], 0)));
|
||||
AssertThat(item, Equals(Item("expression", grammar.rule("expression"), 0)));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -7,41 +7,31 @@ using namespace tree_sitter::rules;
|
|||
namespace test_grammars {
|
||||
Grammar arithmetic() {
|
||||
return Grammar({
|
||||
"expression",
|
||||
"term",
|
||||
"factor",
|
||||
"number",
|
||||
"variable",
|
||||
"plus",
|
||||
"times",
|
||||
"left_paren",
|
||||
"right_paren"
|
||||
}, {
|
||||
choice({
|
||||
{ "expression", choice({
|
||||
seq({
|
||||
sym("term"),
|
||||
sym("plus"),
|
||||
sym("term") }),
|
||||
sym("term") }),
|
||||
choice({
|
||||
sym("term") }) },
|
||||
{ "term", choice({
|
||||
seq({
|
||||
sym("factor"),
|
||||
sym("times"),
|
||||
sym("factor") }),
|
||||
sym("factor") }),
|
||||
choice({
|
||||
sym("factor") }) },
|
||||
{ "factor", choice({
|
||||
sym("variable"),
|
||||
sym("number"),
|
||||
seq({
|
||||
sym("left_paren"),
|
||||
sym("expression"),
|
||||
sym("right_paren") }) }),
|
||||
pattern("\\d+"),
|
||||
pattern("\\w+"),
|
||||
str("+"),
|
||||
str("*"),
|
||||
str("("),
|
||||
str(")")
|
||||
sym("right_paren") }) }) },
|
||||
{ "number", pattern("\\d+") },
|
||||
{ "variable", pattern("\\w+") },
|
||||
{ "plus", str("+") },
|
||||
{ "times", str("*") },
|
||||
{ "left_paren", str("(") },
|
||||
{ "right_paren", str(")") }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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