Initial commit

This commit is contained in:
Max Brunsfeld 2013-11-05 22:15:19 -08:00
commit 84c5bceb81
12 changed files with 968 additions and 0 deletions

95
src/rules/rule.cpp Normal file
View file

@ -0,0 +1,95 @@
#include "rule.h"
#include "spec_helper.h"
#include "transition_map.h"
using namespace std;
namespace tree_sitter {
namespace rules {
// Constructors
Blank::Blank() {}
Symbol::Symbol(int id) : id(id) {};
Seq::Seq(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Seq::Seq(const Rule *left, const Rule *right) : left(left), right(right) {};
Seq::Seq(shared_ptr<const Rule> left, shared_ptr<const Rule> right) : left(left), right(right) {};
Choice::Choice(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Choice::Choice(const Rule *left, const Rule *right) : left(left), right(right) {};
Choice::Choice(shared_ptr<const Rule> left, shared_ptr<const Rule> right) : left(left), right(right) {};
// Transitions
TransitionMap<Rule> Blank::transitions() const {
return TransitionMap<Rule>();
}
TransitionMap<Rule> Symbol::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
TransitionMap<Rule> Choice::transitions() const {
auto result = left->transitions();
result.merge(right->transitions(), [&](const Rule &left, const Rule &right) {
return new Choice(left, right);
});
return result;
}
TransitionMap<Rule> Seq::transitions() const {
return TransitionMap<Rule>({ left->copy() }, { right->copy() });
}
// Equality
bool Blank::operator==(const Rule &rule) const {
return dynamic_cast<const Blank *>(&rule) != NULL;
}
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return (other != NULL) && (other->id == id);
}
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
bool Seq::operator==(const Rule &rule) const {
const Seq *other = dynamic_cast<const Seq *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
// Copying
Blank * Blank::copy() const {
return new Blank();
}
Symbol * Symbol::copy() const {
return new Symbol(id);
}
Choice * Choice::copy() const {
return new Choice(left, right);
}
Seq * Seq::copy() const {
return new Seq(left, right);
}
// Description
string Blank::to_string() const {
return "blank";
}
string Symbol::to_string() const {
return string(std::to_string(id));
}
string Choice::to_string() const {
return string("(choice ") + left->to_string() + " " + right->to_string() + ")";
}
string Seq::to_string() const {
return string("(seq ") + left->to_string() + " " + right->to_string() + ")";
}
}
}

68
src/rules/rule.h Normal file
View file

@ -0,0 +1,68 @@
#ifndef __TreeSitter__Rule__
#define __TreeSitter__Rule__
#include <string>
namespace tree_sitter {
namespace rules {
template<class value> class TransitionMap;
class Rule {
public:
virtual TransitionMap<Rule> transitions() const = 0;
virtual Rule * copy() const = 0;
virtual bool operator==(const Rule& other) const = 0;
virtual std::string to_string() const = 0;
};
class Blank : public Rule {
public:
Blank();
TransitionMap<Rule> transitions() const;
Blank * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
};
class Symbol : public Rule {
public:
Symbol(int id);
TransitionMap<Rule> transitions() const;
Symbol * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
int id;
};
class Choice : public Rule {
public:
Choice(const Rule &left, const Rule &right);
Choice(const Rule *left, const Rule *right);
Choice(std::shared_ptr<const Rule> left, std::shared_ptr<const Rule> right);
TransitionMap<Rule> transitions() const;
Choice * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
std::shared_ptr<const Rule> left;
std::shared_ptr<const Rule> right;
};
class Seq : public Rule {
public:
Seq(const Rule &left, const Rule &right);
Seq(const Rule *left, const Rule *right);
Seq(std::shared_ptr<const Rule> left, std::shared_ptr<const Rule> right);
TransitionMap<Rule> transitions() const;
Seq * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
std::shared_ptr<const Rule> left;
std::shared_ptr<const Rule> right;
};
}
}
#endif

View file

@ -0,0 +1,94 @@
#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