Use map, not unordered_map in Grammar

This commit is contained in:
Max Brunsfeld 2014-02-12 08:30:50 -08:00
parent dd0feea8e6
commit 9a737e2316
7 changed files with 32 additions and 47 deletions

View file

@ -29,7 +29,7 @@ describe("preparing a grammar", []() {
aux_sym("token1") }) }
})));
AssertThat(result.second, Equals(Grammar("", {}, {
AssertThat(result.second, Equals(Grammar("", map<const string, const rule_ptr>(), {
{ "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<const string, const rule_ptr>(), {
{ "token1", repeat(seq({ str("a"), str("b") })) },
})));
});

View file

@ -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);

View file

@ -3,7 +3,6 @@
#include <iostream>
#include <set>
#include <unordered_map>
#include <vector>
using std::cout;
@ -34,20 +33,6 @@ namespace std {
return stream << ">";
}
template<typename TKey, typename TValue>
inline std::ostream& operator<<(std::ostream &stream, const std::unordered_map<TKey, TValue> &map) {
stream << std::string("#<map: ");
bool started = false;
for (auto pair : map) {
if (started) stream << std::string(", ");
stream << pair.first;
stream << std::string(" => ");
stream << pair.second;
started = true;
}
return stream << ">";
}
template<typename TKey, typename TValue>
inline std::ostream& operator<<(std::ostream &stream, const std::map<TKey, TValue> &map) {
stream << std::string("#<map: ");

View file

@ -11,11 +11,11 @@ namespace tree_sitter {
rules(rules),
start_rule_name(rules.begin()->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) {}

View file

@ -1,19 +1,19 @@
#ifndef __TreeSitter__grammar__
#define __TreeSitter__grammar__
#include <unordered_map>
#include <map>
#include <vector>
#include "rules.h"
namespace tree_sitter {
class Grammar {
typedef std::initializer_list<std::pair<const std::string, const rules::rule_ptr>> rule_map_init_list;
typedef const std::unordered_map<std::string, const rules::rule_ptr> rule_map;
typedef std::map<const std::string, const rules::rule_ptr> 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<std::string> rule_names() const;

View file

@ -1,9 +1,9 @@
#include "expand_repeats.h"
#include <unordered_map>
#include <map>
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<string, const rule_ptr> aux_rules;
map<const string, const rule_ptr> 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<string, const rule_ptr> result;
map<const string, const rule_ptr> result;
RepeatExpander visitor;
for (auto pair : grammar.rules)

View file

@ -1,11 +1,11 @@
#include "extract_tokens.h"
#include "search_for_symbols.h"
#include <unordered_map>
#include <map>
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<string, const rule_ptr> tokens;
map<const string, const rule_ptr> tokens;
rule_ptr initial_apply(const rule_ptr rule) {
if (!search_for_symbols(rule)) {
@ -61,9 +61,9 @@ namespace tree_sitter {
pair<Grammar, Grammar> extract_tokens(const Grammar &input_grammar) {
TokenExtractor extractor;
unordered_map<string, const rule_ptr> rules;
unordered_map<string, const rule_ptr> aux_rules;
unordered_map<string, const rule_ptr> tokens;
map<const string, const rule_ptr> rules;
map<const string, const rule_ptr> aux_rules;
map<const string, const rule_ptr> tokens;
for (auto pair : input_grammar.rules) {
string name = pair.first;