From 8a0a442a24633a5d5ae3f0a00fbce33ff4d516e5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 13 Nov 2013 20:22:06 -0800 Subject: [PATCH] Make more things immutable --- spec/lr/item_set_spec.cpp | 10 ++++----- spec/lr/item_spec.cpp | 2 +- spec/test_grammars/arithmetic.cpp | 34 +++++++++++-------------------- src/grammar.cpp | 26 +++++++---------------- src/grammar.h | 11 ++++++---- src/lr/item.cpp | 2 +- src/lr/item_set.cpp | 1 - src/rules/Pattern.h | 2 +- src/rules/char.h | 2 +- src/rules/choice.h | 4 ++-- src/rules/seq.h | 4 ++-- src/rules/string.h | 2 +- 12 files changed, 40 insertions(+), 60 deletions(-) diff --git a/spec/lr/item_set_spec.cpp b/spec/lr/item_set_spec.cpp index b6e0f79c..bce8fb8b 100644 --- a/spec/lr/item_set_spec.cpp +++ b/spec/lr/item_set_spec.cpp @@ -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), }))); } }; diff --git a/spec/lr/item_spec.cpp b/spec/lr/item_spec.cpp index 43af20f0..c74b72e3 100644 --- a/spec/lr/item_spec.cpp +++ b/spec/lr/item_spec.cpp @@ -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))); } }; }; diff --git a/spec/test_grammars/arithmetic.cpp b/spec/test_grammars/arithmetic.cpp index 78330a3a..837e647f 100644 --- a/spec/test_grammars/arithmetic.cpp +++ b/spec/test_grammars/arithmetic.cpp @@ -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(")") } }); } } diff --git a/src/grammar.cpp b/src/grammar.cpp index 7948834b..6e4440e9 100644 --- a/src/grammar.cpp +++ b/src/grammar.cpp @@ -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 build_rule_map(const initializer_list &rule_names, - const initializer_list &rule_vals) { - std::unordered_map 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 &names, const initializer_list &values) : - rules(build_rule_map(names, values)), - start_rule_name(*names.begin()) {} } \ No newline at end of file diff --git a/src/grammar.h b/src/grammar.h index 8b28f093..b9ffbe95 100644 --- a/src/grammar.h +++ b/src/grammar.h @@ -7,12 +7,15 @@ namespace tree_sitter { class Grammar { typedef std::unordered_map rule_map; + typedef std::initializer_list> rule_map_init_list; + public: - Grammar(const rule_map &rules, const std::string &start_rule_name); - Grammar(const std::initializer_list &rule_names, - const std::initializer_list &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; }; } diff --git a/src/lr/item.cpp b/src/lr/item.cpp index 99d5414c..997221bb 100644 --- a/src/lr/item.cpp +++ b/src/lr/item.cpp @@ -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::transitions() const { diff --git a/src/lr/item_set.cpp b/src/lr/item_set.cpp index 4b4412da..f5f806d1 100644 --- a/src/lr/item_set.cpp +++ b/src/lr/item_set.cpp @@ -57,6 +57,5 @@ namespace tree_sitter { stream << string(")"); return stream; } - } } diff --git a/src/rules/Pattern.h b/src/rules/Pattern.h index 90c5bccd..81a33780 100644 --- a/src/rules/Pattern.h +++ b/src/rules/Pattern.h @@ -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; }; } } diff --git a/src/rules/char.h b/src/rules/char.h index 9c83ac8c..efb0e8e3 100644 --- a/src/rules/char.h +++ b/src/rules/char.h @@ -12,7 +12,7 @@ namespace tree_sitter { bool operator==(const Rule& other) const; std::string to_string() const; private: - char value; + const char value; }; } } diff --git a/src/rules/choice.h b/src/rules/choice.h index e69ed716..ca121c93 100644 --- a/src/rules/choice.h +++ b/src/rules/choice.h @@ -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; }; } } diff --git a/src/rules/seq.h b/src/rules/seq.h index bafbad2d..bb7f8da5 100644 --- a/src/rules/seq.h +++ b/src/rules/seq.h @@ -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; }; } } diff --git a/src/rules/string.h b/src/rules/string.h index 0e1f4b4b..e32fe616 100644 --- a/src/rules/string.h +++ b/src/rules/string.h @@ -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; }; } }