Move TransitionMap out of rules namespace
This commit is contained in:
parent
84c5bceb81
commit
95d955e779
7 changed files with 106 additions and 115 deletions
|
|
@ -9,7 +9,7 @@
|
|||
/* Begin PBXBuildFile section */
|
||||
1214930E181E200B008E9BDA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492E9181E200B008E9BDA /* main.cpp */; };
|
||||
1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 121492EA181E200B008E9BDA /* rules_spec.cpp */; };
|
||||
12E71853181D081C0051A649 /* rule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E71851181D081C0051A649 /* rule.cpp */; };
|
||||
12E71853181D081C0051A649 /* rules.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E71851181D081C0051A649 /* rules.cpp */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
|
|
@ -101,8 +101,8 @@
|
|||
121492EA181E200B008E9BDA /* rules_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rules_spec.cpp; path = spec/rules_spec.cpp; sourceTree = SOURCE_ROOT; };
|
||||
12C344421822F27700B07BE3 /* transition_map.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = transition_map.h; sourceTree = "<group>"; };
|
||||
12E71794181D02A80051A649 /* specs */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = specs; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
12E71851181D081C0051A649 /* rule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule.cpp; sourceTree = "<group>"; };
|
||||
12E71852181D081C0051A649 /* rule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule.h; sourceTree = "<group>"; };
|
||||
12E71851181D081C0051A649 /* rules.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rules.cpp; sourceTree = "<group>"; };
|
||||
12E71852181D081C0051A649 /* rules.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rules.h; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
|
@ -327,7 +327,9 @@
|
|||
12E71701181D01890051A649 /* src */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
12E71850181D07EA0051A649 /* rules */,
|
||||
12E71851181D081C0051A649 /* rules.cpp */,
|
||||
12E71852181D081C0051A649 /* rules.h */,
|
||||
12C344421822F27700B07BE3 /* transition_map.h */,
|
||||
);
|
||||
path = src;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -352,16 +354,6 @@
|
|||
path = Specs;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
12E71850181D07EA0051A649 /* rules */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
12E71851181D081C0051A649 /* rule.cpp */,
|
||||
12E71852181D081C0051A649 /* rule.h */,
|
||||
12C344421822F27700B07BE3 /* transition_map.h */,
|
||||
);
|
||||
path = rules;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
|
|
@ -412,7 +404,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
12E71853181D081C0051A649 /* rule.cpp in Sources */,
|
||||
12E71853181D081C0051A649 /* rules.cpp in Sources */,
|
||||
1214930F181E200B008E9BDA /* rules_spec.cpp in Sources */,
|
||||
1214930E181E200B008E9BDA /* main.cpp in Sources */,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#include "rule.h"
|
||||
#include "rules.h"
|
||||
#include "transition_map.h"
|
||||
#include <igloo/igloo_alt.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace igloo;
|
||||
using namespace tree_sitter;
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
Describe(Rules) {
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@
|
|||
|
||||
#include "igloo/igloo_alt.h"
|
||||
#include "transition_map.h"
|
||||
#include "rule.h"
|
||||
#include "rules.h"
|
||||
|
||||
namespace snowhouse {
|
||||
template<>
|
||||
std::string Stringize(const tree_sitter::rules::TransitionMap<tree_sitter::rules::Rule> &map) {
|
||||
std::string Stringize(const tree_sitter::TransitionMap<tree_sitter::rules::Rule> &map) {
|
||||
std::string result("[");
|
||||
bool started = false;
|
||||
for (auto pair : map) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include "rule.h"
|
||||
#include "rules.h"
|
||||
#include "spec_helper.h"
|
||||
#include "transition_map.h"
|
||||
|
||||
|
|
@ -4,9 +4,9 @@
|
|||
#include <string>
|
||||
|
||||
namespace tree_sitter {
|
||||
template<class value> class TransitionMap;
|
||||
|
||||
namespace rules {
|
||||
template<class value> class TransitionMap;
|
||||
|
||||
class Rule {
|
||||
public:
|
||||
virtual TransitionMap<Rule> transitions() const = 0;
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
#ifndef __TreeSitter__TransitionSet__
|
||||
#define __TreeSitter__TransitionSet__
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "rule.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace rules {
|
||||
class Rule;
|
||||
|
||||
template<typename MappedType>
|
||||
class TransitionMap {
|
||||
public:
|
||||
typedef std::shared_ptr<const Rule> rule_ptr;
|
||||
typedef std::shared_ptr<const MappedType> mapped_ptr;
|
||||
typedef std::pair<rule_ptr, mapped_ptr> pair_type;
|
||||
typedef std::vector<pair_type> contents_type;
|
||||
|
||||
static bool elements_equal(const pair_type &left, const pair_type &right) {
|
||||
return (*left.first == *right.first) && (*left.second == *right.second);
|
||||
}
|
||||
|
||||
TransitionMap() : contents(contents_type()) {};
|
||||
|
||||
TransitionMap(std::initializer_list<Rule *> keys, std::initializer_list<MappedType *> values) : TransitionMap() {
|
||||
auto value_iter(values.begin());
|
||||
for (auto key_iter(keys.begin()); key_iter != keys.end(); ++key_iter) {
|
||||
add(*key_iter, *value_iter);
|
||||
++value_iter;
|
||||
}
|
||||
}
|
||||
|
||||
typedef typename contents_type::const_iterator const_iterator;
|
||||
typedef typename contents_type::iterator iterator;
|
||||
|
||||
iterator begin() { return contents.begin(); }
|
||||
iterator end() { return contents.end(); }
|
||||
const_iterator begin() const { return contents.begin(); }
|
||||
const_iterator end() const { return contents.end(); }
|
||||
|
||||
void add(const Rule *on_rule, const MappedType *to_value) {
|
||||
rule_ptr key(on_rule);
|
||||
mapped_ptr value(to_value);
|
||||
contents.push_back(pair_type(key, value));
|
||||
}
|
||||
|
||||
void add(rule_ptr on_rule, mapped_ptr to_value) {
|
||||
rule_ptr key(on_rule);
|
||||
mapped_ptr value(to_value);
|
||||
contents.push_back(pair_type(key, value));
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return contents.size();
|
||||
}
|
||||
|
||||
mapped_ptr operator[](Rule const &on_rule) const {
|
||||
pair_type *pair = pair_for_key(on_rule);
|
||||
if (pair)
|
||||
return pair->second;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mapped_ptr operator[](int i) const {
|
||||
return contents[i].second;
|
||||
}
|
||||
|
||||
void merge(const TransitionMap<MappedType> &other, std::function<const MappedType *(const MappedType &, const MappedType &)> merge_fn) {
|
||||
for (pair_type other_pair : other) {
|
||||
pair_type *current_pair = pair_for_key(*other_pair.first);
|
||||
if (current_pair)
|
||||
current_pair->second = mapped_ptr(merge_fn(*current_pair->second, *other_pair.second));
|
||||
else
|
||||
add(other_pair.first, other_pair.second);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
pair_type * pair_for_key(Rule const &key) {
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
pair_type *pair = &contents[i];
|
||||
if (*pair->first == key) return pair;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
contents_type contents;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
92
src/transition_map.h
Normal file
92
src/transition_map.h
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#ifndef __TreeSitter__TransitionSet__
|
||||
#define __TreeSitter__TransitionSet__
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "rules.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
class rules::Rule;
|
||||
|
||||
template<typename MappedType>
|
||||
class TransitionMap {
|
||||
public:
|
||||
typedef std::shared_ptr<const rules::Rule> rule_ptr;
|
||||
typedef std::shared_ptr<const MappedType> mapped_ptr;
|
||||
typedef std::pair<rule_ptr, mapped_ptr> pair_type;
|
||||
typedef std::vector<pair_type> contents_type;
|
||||
|
||||
static bool elements_equal(const pair_type &left, const pair_type &right) {
|
||||
return (*left.first == *right.first) && (*left.second == *right.second);
|
||||
}
|
||||
|
||||
TransitionMap() : contents(contents_type()) {};
|
||||
|
||||
TransitionMap(std::initializer_list<rules::Rule *> keys, std::initializer_list<MappedType *> values) : TransitionMap() {
|
||||
auto value_iter(values.begin());
|
||||
for (auto key_iter(keys.begin()); key_iter != keys.end(); ++key_iter) {
|
||||
add(*key_iter, *value_iter);
|
||||
++value_iter;
|
||||
}
|
||||
}
|
||||
|
||||
typedef typename contents_type::const_iterator const_iterator;
|
||||
typedef typename contents_type::iterator iterator;
|
||||
|
||||
iterator begin() { return contents.begin(); }
|
||||
iterator end() { return contents.end(); }
|
||||
const_iterator begin() const { return contents.begin(); }
|
||||
const_iterator end() const { return contents.end(); }
|
||||
|
||||
void add(const rules::Rule *on_rule, const MappedType *to_value) {
|
||||
rule_ptr key(on_rule);
|
||||
mapped_ptr value(to_value);
|
||||
contents.push_back(pair_type(key, value));
|
||||
}
|
||||
|
||||
void add(rule_ptr on_rule, mapped_ptr to_value) {
|
||||
rule_ptr key(on_rule);
|
||||
mapped_ptr value(to_value);
|
||||
contents.push_back(pair_type(key, value));
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return contents.size();
|
||||
}
|
||||
|
||||
mapped_ptr operator[](rules::Rule const &on_rule) const {
|
||||
pair_type *pair = pair_for_key(on_rule);
|
||||
if (pair)
|
||||
return pair->second;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mapped_ptr operator[](int i) const {
|
||||
return contents[i].second;
|
||||
}
|
||||
|
||||
void merge(const TransitionMap<MappedType> &other, std::function<const MappedType *(const MappedType &, const MappedType &)> merge_fn) {
|
||||
for (pair_type other_pair : other) {
|
||||
pair_type *current_pair = pair_for_key(*other_pair.first);
|
||||
if (current_pair)
|
||||
current_pair->second = mapped_ptr(merge_fn(*current_pair->second, *other_pair.second));
|
||||
else
|
||||
add(other_pair.first, other_pair.second);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
pair_type * pair_for_key(rules::Rule const &key) {
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
pair_type *pair = &contents[i];
|
||||
if (*pair->first == key) return pair;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
contents_type contents;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue