Store rule metadata as a map, not a single number

Need to store more than just boolean values
This commit is contained in:
Max Brunsfeld 2014-04-07 08:50:00 -07:00
parent 610a8e4a29
commit 1da9f1fdfd
12 changed files with 120 additions and 88 deletions

View file

@ -6,9 +6,10 @@
namespace tree_sitter {
using std::hash;
using std::make_shared;
using std::map;
namespace rules {
Metadata::Metadata(rule_ptr rule, MetadataValue value) : rule(rule), value(value) {}
Metadata::Metadata(rule_ptr rule, map<MetadataKey, int> values) : rule(rule), value(values) {}
bool Metadata::operator==(const Rule &rule) const {
auto other = dynamic_cast<const Metadata *>(&rule);
@ -16,7 +17,12 @@ namespace tree_sitter {
}
size_t Metadata::hash_code() const {
return hash<int>()(value);
size_t result = hash<size_t>()(value.size());
for (auto &pair : value) {
result ^= hash<int>()(pair.first);
result ^= hash<int>()(pair.second);
}
return result;
}
rule_ptr Metadata::copy() const {

View file

@ -2,18 +2,19 @@
#define COMPILER_RULES_METADATA_H_
#include <string>
#include <map>
#include "compiler/rules/rule.h"
namespace tree_sitter {
namespace rules {
typedef enum {
NONE = 0,
START_TOKEN = 1,
} MetadataValue;
START_TOKEN,
PRECEDENCE
} MetadataKey;
class Metadata : public Rule {
public:
Metadata(rule_ptr rule, MetadataValue value);
Metadata(rule_ptr rule, std::map<MetadataKey, int> value);
bool operator==(const Rule& other) const;
size_t hash_code() const;
@ -22,7 +23,7 @@ namespace tree_sitter {
void accept(Visitor *visitor) const;
const rule_ptr rule;
const MetadataValue value;
const std::map<MetadataKey, int> value;
};
}
}