From 9a737e23160cc9cf877cffbd9174d63753bce44e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 12 Feb 2014 08:30:50 -0800 Subject: [PATCH] Use map, not unordered_map in Grammar --- spec/compiler/prepare_grammar_spec.cpp | 4 +-- spec/fixtures/parsers/json.c | 28 +++++++++---------- spec/stream_methods.h | 15 ---------- src/compiler/grammar.cpp | 4 +-- src/compiler/grammar.h | 8 +++--- .../prepare_grammar/expand_repeats.cpp | 8 +++--- .../prepare_grammar/extract_tokens.cpp | 12 ++++---- 7 files changed, 32 insertions(+), 47 deletions(-) diff --git a/spec/compiler/prepare_grammar_spec.cpp b/spec/compiler/prepare_grammar_spec.cpp index eace36b3..e93d5d43 100644 --- a/spec/compiler/prepare_grammar_spec.cpp +++ b/spec/compiler/prepare_grammar_spec.cpp @@ -29,7 +29,7 @@ describe("preparing a grammar", []() { aux_sym("token1") }) } }))); - AssertThat(result.second, Equals(Grammar("", {}, { + AssertThat(result.second, Equals(Grammar("", map(), { { "token1", rules::seq({ rules::character('a'), rules::character('b') }) }, @@ -98,7 +98,7 @@ describe("preparing a grammar", []() { }) }, }))); - AssertThat(result.second, Equals(Grammar("", {}, { + AssertThat(result.second, Equals(Grammar("", map(), { { "token1", repeat(seq({ str("a"), str("b") })) }, }))); }); diff --git a/spec/fixtures/parsers/json.c b/spec/fixtures/parsers/json.c index 9b7e3eb0..7ac34365 100644 --- a/spec/fixtures/parsers/json.c +++ b/spec/fixtures/parsers/json.c @@ -232,7 +232,7 @@ static TSParseResult ts_parse(const char *input) { case 5: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper2: + case ts_aux_repeat_helper1: SHIFT(6); case ts_aux_token2: SHIFT(8); @@ -290,12 +290,12 @@ static TSParseResult ts_parse(const char *input) { case 10: SET_LEX_STATE(6); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper2: + case ts_aux_repeat_helper1: SHIFT(11); case ts_aux_token2: SHIFT(8); case ts_aux_token4: - REDUCE(ts_aux_repeat_helper2, 2, COLLAPSE({1, 0})); + REDUCE(ts_aux_repeat_helper1, 2, COLLAPSE({1, 0})); default: PARSE_PANIC(); } @@ -303,7 +303,7 @@ static TSParseResult ts_parse(const char *input) { SET_LEX_STATE(4); switch (LOOKAHEAD_SYM()) { case ts_aux_token4: - REDUCE(ts_aux_repeat_helper2, 3, COLLAPSE({1, 0, 1})); + REDUCE(ts_aux_repeat_helper1, 3, COLLAPSE({1, 0, 1})); default: PARSE_PANIC(); } @@ -330,7 +330,7 @@ static TSParseResult ts_parse(const char *input) { case 13: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper2: + case ts_aux_repeat_helper1: SHIFT(14); case ts_aux_token2: SHIFT(8); @@ -380,7 +380,7 @@ static TSParseResult ts_parse(const char *input) { case 17: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper2: + case ts_aux_repeat_helper1: SHIFT(18); case ts_aux_token2: SHIFT(8); @@ -446,7 +446,7 @@ static TSParseResult ts_parse(const char *input) { case 23: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper1: + case ts_aux_repeat_helper2: SHIFT(24); case ts_aux_token2: SHIFT(26); @@ -522,12 +522,12 @@ static TSParseResult ts_parse(const char *input) { case 30: SET_LEX_STATE(9); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper1: + case ts_aux_repeat_helper2: SHIFT(31); case ts_aux_token2: SHIFT(26); case ts_aux_token7: - REDUCE(ts_aux_repeat_helper1, 4, COLLAPSE({1, 0, 1, 0})); + REDUCE(ts_aux_repeat_helper2, 4, COLLAPSE({1, 0, 1, 0})); default: PARSE_PANIC(); } @@ -535,7 +535,7 @@ static TSParseResult ts_parse(const char *input) { SET_LEX_STATE(7); switch (LOOKAHEAD_SYM()) { case ts_aux_token7: - REDUCE(ts_aux_repeat_helper1, 5, COLLAPSE({1, 0, 1, 0, 1})); + REDUCE(ts_aux_repeat_helper2, 5, COLLAPSE({1, 0, 1, 0, 1})); default: PARSE_PANIC(); } @@ -562,7 +562,7 @@ static TSParseResult ts_parse(const char *input) { case 33: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper2: + case ts_aux_repeat_helper1: SHIFT(34); case ts_aux_token2: SHIFT(8); @@ -628,7 +628,7 @@ static TSParseResult ts_parse(const char *input) { case 39: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper1: + case ts_aux_repeat_helper2: SHIFT(40); case ts_aux_token2: SHIFT(26); @@ -694,7 +694,7 @@ static TSParseResult ts_parse(const char *input) { case 45: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper1: + case ts_aux_repeat_helper2: SHIFT(46); case ts_aux_token2: SHIFT(26); @@ -760,7 +760,7 @@ static TSParseResult ts_parse(const char *input) { case 51: SET_LEX_STATE(2); switch (LOOKAHEAD_SYM()) { - case ts_aux_repeat_helper1: + case ts_aux_repeat_helper2: SHIFT(52); case ts_aux_token2: SHIFT(26); diff --git a/spec/stream_methods.h b/spec/stream_methods.h index 18f41c16..0a90080e 100644 --- a/spec/stream_methods.h +++ b/spec/stream_methods.h @@ -3,7 +3,6 @@ #include #include -#include #include using std::cout; @@ -34,20 +33,6 @@ namespace std { return stream << ">"; } - template - inline std::ostream& operator<<(std::ostream &stream, const std::unordered_map &map) { - stream << std::string("# "); - stream << pair.second; - started = true; - } - return stream << ">"; - } - template inline std::ostream& operator<<(std::ostream &stream, const std::map &map) { stream << std::string("#first) {} - Grammar::Grammar(std::string start_rule_name, rule_map &rules) : + Grammar::Grammar(std::string start_rule_name, const rule_map &rules) : rules(rules), start_rule_name(start_rule_name) {} - Grammar::Grammar(std::string start_rule_name, rule_map &rules, rule_map &aux_rules) : + Grammar::Grammar(std::string start_rule_name, const rule_map &rules, const rule_map &aux_rules) : rules(rules), aux_rules(aux_rules), start_rule_name(start_rule_name) {} diff --git a/src/compiler/grammar.h b/src/compiler/grammar.h index 8d743ebd..f87da155 100644 --- a/src/compiler/grammar.h +++ b/src/compiler/grammar.h @@ -1,19 +1,19 @@ #ifndef __TreeSitter__grammar__ #define __TreeSitter__grammar__ -#include +#include #include #include "rules.h" namespace tree_sitter { class Grammar { typedef std::initializer_list> rule_map_init_list; - typedef const std::unordered_map rule_map; + typedef std::map rule_map; public: Grammar(const rule_map_init_list &rules); - Grammar(std::string start_rule_name, rule_map &rules); - Grammar(std::string start_rule_name, rule_map &rules, rule_map &aux_rules); + Grammar(std::string start_rule_name, const rule_map &rules); + Grammar(std::string start_rule_name, const rule_map &rules, const rule_map &aux_rules); const std::string start_rule_name; std::vector rule_names() const; diff --git a/src/compiler/prepare_grammar/expand_repeats.cpp b/src/compiler/prepare_grammar/expand_repeats.cpp index a837343d..9d5051af 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cpp +++ b/src/compiler/prepare_grammar/expand_repeats.cpp @@ -1,9 +1,9 @@ #include "expand_repeats.h" -#include +#include using std::string; using std::to_string; -using std::unordered_map; +using std::map; using namespace tree_sitter::rules; namespace tree_sitter { @@ -11,7 +11,7 @@ namespace tree_sitter { class RepeatExpander : rules::Visitor { public: rule_ptr value; - unordered_map aux_rules; + map aux_rules; rule_ptr apply(const rule_ptr &rule) { rule->accept(*this); @@ -46,7 +46,7 @@ namespace tree_sitter { }; Grammar expand_repeats(const Grammar &grammar) { - unordered_map result; + map result; RepeatExpander visitor; for (auto pair : grammar.rules) diff --git a/src/compiler/prepare_grammar/extract_tokens.cpp b/src/compiler/prepare_grammar/extract_tokens.cpp index ea9438b7..6d7b62b2 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cpp +++ b/src/compiler/prepare_grammar/extract_tokens.cpp @@ -1,11 +1,11 @@ #include "extract_tokens.h" #include "search_for_symbols.h" -#include +#include using std::pair; using std::string; using std::to_string; -using std::unordered_map; +using std::map; using namespace tree_sitter::rules; namespace tree_sitter { @@ -13,7 +13,7 @@ namespace tree_sitter { class TokenExtractor : Visitor { public: rule_ptr value; - unordered_map tokens; + map tokens; rule_ptr initial_apply(const rule_ptr rule) { if (!search_for_symbols(rule)) { @@ -61,9 +61,9 @@ namespace tree_sitter { pair extract_tokens(const Grammar &input_grammar) { TokenExtractor extractor; - unordered_map rules; - unordered_map aux_rules; - unordered_map tokens; + map rules; + map aux_rules; + map tokens; for (auto pair : input_grammar.rules) { string name = pair.first;