Store character rules’ matches in a set, not a vector

This commit is contained in:
Max Brunsfeld 2014-01-31 18:56:02 -08:00
parent 5ed5ae7514
commit 716a4a4259
9 changed files with 218 additions and 218 deletions

View file

@ -27,7 +27,7 @@ namespace tree_sitter {
item_transitions.add(rule, make_shared<LexItemSet>(new_item_set));
}
}
result.merge(item_transitions, [](shared_ptr<const LexItemSet> left, shared_ptr<const LexItemSet> right) -> shared_ptr<const LexItemSet> {
return make_shared<LexItemSet>(merge_sets(*left, *right));
});

View file

@ -4,7 +4,6 @@ using std::string;
using std::to_string;
using std::unordered_map;
using std::unordered_set;
using std::vector;
using tree_sitter::rules::Symbol;
namespace tree_sitter {

View file

@ -12,10 +12,6 @@ namespace tree_sitter {
value.range.max_character = bounds.second;
}
Character::Character(char character) : matches({ CharacterMatch(character) }), sign(true) {}
Character::Character(CharacterClass char_class) : matches({ CharacterMatch(char_class) }), sign(true) {}
Character::Character(const std::vector<CharacterMatch> &matches, bool sign) : matches(matches), sign(sign) {}
bool CharacterMatch::operator==(const CharacterMatch &right) const {
if (type != right.type)
return false;
@ -50,6 +46,10 @@ namespace tree_sitter {
}
}
Character::Character(char character) : matches({ CharacterMatch(character) }), sign(true) {}
Character::Character(CharacterClass char_class) : matches({ CharacterMatch(char_class) }), sign(true) {}
Character::Character(const std::unordered_set<CharacterMatch> &matches, bool sign) : matches(matches), sign(sign) {}
bool Character::operator==(const Rule &rule) const {
const Character *other = dynamic_cast<const Character *>(&rule);
return other && this->operator==(*other);
@ -57,10 +57,7 @@ namespace tree_sitter {
bool Character::operator==(const Character &other) const {
if (other.sign != sign) return false;
auto size = matches.size();
if (other.matches.size() != size) return false;
for (int i = 0; i < size; i++)
if (!(matches[i] == other.matches[i])) return false;
if (other.matches != matches) return false;
return true;
}

View file

@ -1,8 +1,7 @@
#ifndef __tree_sitter__char__
#define __tree_sitter__char__
#ifndef __tree_sitter__character__
#define __tree_sitter__character__
#include "rule.h"
#include <vector>
#include <unordered_set>
namespace tree_sitter {
@ -35,24 +34,6 @@ namespace tree_sitter {
bool operator==(const CharacterMatch &) const;
std::string to_string() const;
};
class Character : public Rule {
public:
Character(char character);
Character(CharacterClass character_class);
Character(char min_character, char max_character);
Character(const std::vector<CharacterMatch> &matches, bool sign);
bool operator==(const Rule& other) const;
bool operator==(const Character& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;
std::vector<CharacterMatch> matches;
bool sign;
};
}
}
@ -76,6 +57,29 @@ namespace std {
};
}
namespace tree_sitter {
namespace rules {
class Character : public Rule {
public:
Character(char character);
Character(CharacterClass character_class);
Character(char min_character, char max_character);
Character(const std::unordered_set<CharacterMatch> &matches, bool sign);
bool operator==(const Rule& other) const;
bool operator==(const Character& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;
std::unordered_set<CharacterMatch> matches;
bool sign;
};
}
}
namespace std {
template<>
struct hash<tree_sitter::rules::Character> : hash<tree_sitter::rules::Rule> {};

View file

@ -44,9 +44,9 @@ namespace tree_sitter {
next();
is_affirmative = false;
}
std::vector<CharacterMatch> matches;
std::unordered_set<CharacterMatch> matches;
while (has_more_input() && (peek() != ']'))
matches.push_back(single_char());
matches.insert(single_char());
return character(matches, is_affirmative);
}

View file

@ -18,7 +18,7 @@ namespace tree_sitter {
return make_shared<Character>(value);
}
rule_ptr character(const std::vector<CharacterMatch> &matches, bool is_affirmative) {
rule_ptr character(const std::unordered_set<CharacterMatch> &matches, bool is_affirmative) {
return make_shared<Character>(matches, is_affirmative);
}

View file

@ -17,8 +17,8 @@ namespace tree_sitter {
rule_ptr blank();
rule_ptr character(char value);
rule_ptr character(CharacterClass value);
rule_ptr character(const std::vector<CharacterMatch> &matches);
rule_ptr character(const std::vector<CharacterMatch> &matches, bool);
rule_ptr character(const std::unordered_set<CharacterMatch> &matches);
rule_ptr character(const std::unordered_set<CharacterMatch> &matches, bool);
rule_ptr choice(const std::initializer_list<rule_ptr> &rules);
rule_ptr pattern(const std::string &value);