Put rule classes in their own files

This commit is contained in:
Max Brunsfeld 2013-11-07 13:24:01 -08:00
parent 849f2ee195
commit d830c7c255
17 changed files with 326 additions and 215 deletions

View file

@ -1,118 +0,0 @@
#include "rules.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) {};
Char::Char(char value) : value(value) {};
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> Char::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
TransitionMap<Rule> Choice::transitions() const {
auto result = left->transitions();
result.merge(right->transitions(), [&](rule_ptr left, rule_ptr right) -> rule_ptr {
return rule_ptr(new Choice(left, right));
});
return result;
}
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map([&](rule_ptr left_rule) -> rule_ptr {
if (typeid(*left_rule) == typeid(Blank))
return right;
else
return rule_ptr(new Seq(left_rule, right));
});
}
// 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 Char::operator==(const Rule &rule) const {
const Char *other = dynamic_cast<const Char *>(&rule);
return (other != NULL) && (other->value == value);
}
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);
}
Char * Char::copy() const {
return new Char(value);
}
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 std::to_string(id);
}
string Char::to_string() const {
return std::to_string(value);
}
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() + ")";
}
}
}

View file

@ -1,81 +1,11 @@
#ifndef __TreeSitter__Rule__
#define __TreeSitter__Rule__
#ifndef __TreeSitter__rules__
#define __TreeSitter__rules__
#include <string>
namespace tree_sitter {
template<class value> class TransitionMap;
namespace rules {
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;
};
typedef std::shared_ptr<const Rule> rule_ptr;
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 Char : public Rule {
public:
Char(char value);
TransitionMap<Rule> transitions() const;
Char * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
char value;
};
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:
rule_ptr left;
rule_ptr 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:
rule_ptr left;
rule_ptr right;
};
}
}
#include "rules/rule.h"
#include "rules/blank.h"
#include "rules/char.h"
#include "rules/symbol.h"
#include "rules/choice.h"
#include "rules/seq.h"
#endif

24
src/rules/blank.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "blank.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Blank::Blank() {}
TransitionMap<Rule> Blank::transitions() const {
return TransitionMap<Rule>();
}
bool Blank::operator==(const Rule &rule) const {
return dynamic_cast<const Blank *>(&rule) != NULL;
}
Blank * Blank::copy() const {
return new Blank();
}
std::string Blank::to_string() const {
return "blank";
}
}
}

19
src/rules/blank.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef __tree_sitter__blank__
#define __tree_sitter__blank__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Blank : public Rule {
public:
Blank();
TransitionMap<Rule> transitions() const;
Blank * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
};
}
}
#endif

27
src/rules/char.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "char.h"
#include "transition_map.h"
using namespace std;
namespace tree_sitter {
namespace rules {
Char::Char(char value) : value(value) {};
TransitionMap<Rule> Char::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
bool Char::operator==(const Rule &rule) const {
const Char *other = dynamic_cast<const Char *>(&rule);
return (other != NULL) && (other->value == value);
}
Char * Char::copy() const {
return new Char(value);
}
string Char::to_string() const {
return std::to_string(value);
}
}
}

21
src/rules/char.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef __tree_sitter__char__
#define __tree_sitter__char__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Char : public Rule {
public:
Char(char value);
TransitionMap<Rule> transitions() const;
Char * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
char value;
};
}
}
#endif

30
src/rules/choice.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "choice.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Choice::Choice(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Choice::Choice(rule_ptr left, rule_ptr right) : left(left), right(right) {};
TransitionMap<Rule> Choice::transitions() const {
auto result = left->transitions();
result.merge(right->transitions(), [&](rule_ptr left, rule_ptr right) -> rule_ptr {
return rule_ptr(new Choice(left, right));
});
return result;
}
bool Choice::operator==(const Rule &rule) const {
const Choice *other = dynamic_cast<const Choice *>(&rule);
return (other != NULL) && (*other->left == *left) && (*other->right == *right);
}
Choice * Choice::copy() const {
return new Choice(left, right);
}
std::string Choice::to_string() const {
return std::string("(choice ") + left->to_string() + " " + right->to_string() + ")";
}
}
}

23
src/rules/choice.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef __tree_sitter__choice__
#define __tree_sitter__choice__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Choice : public Rule {
public:
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:
rule_ptr left;
rule_ptr right;
};
}
}
#endif

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

@ -0,0 +1,22 @@
#ifndef __TreeSitter__rule__
#define __TreeSitter__rule__
#include <string>
namespace tree_sitter {
template<class value> class TransitionMap;
namespace rules {
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;
};
typedef std::shared_ptr<const Rule> rule_ptr;
}
}
#endif

32
src/rules/seq.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "seq.h"
#include "blank.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Seq::Seq(const Rule &left, const Rule &right) : left(left.copy()), right(right.copy()) {};
Seq::Seq(rule_ptr left, rule_ptr right) : left(left), right(right) {};
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map([&](rule_ptr left_rule) -> rule_ptr {
if (typeid(*left_rule) == typeid(Blank))
return right;
else
return rule_ptr(new Seq(left_rule, 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);
}
Seq * Seq::copy() const {
return new Seq(left, right);
}
std::string Seq::to_string() const {
return std::string("(seq ") + left->to_string() + " " + right->to_string() + ")";
}
}
}

23
src/rules/seq.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef __tree_sitter__seq__
#define __tree_sitter__seq__
#include "rule.h"
namespace tree_sitter {
namespace rules {
class Seq : public Rule {
public:
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:
rule_ptr left;
rule_ptr right;
};
}
}
#endif

25
src/rules/symbol.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "symbol.h"
#include "transition_map.h"
namespace tree_sitter {
namespace rules {
Symbol::Symbol(int id) : id(id) {};
TransitionMap<Rule> Symbol::transitions() const {
return TransitionMap<Rule>({ copy() }, { new Blank() });
}
bool Symbol::operator==(const Rule &rule) const {
const Symbol *other = dynamic_cast<const Symbol *>(&rule);
return (other != NULL) && (other->id == id);
}
Symbol * Symbol::copy() const {
return new Symbol(id);
}
std::string Symbol::to_string() const {
return std::to_string(id);
}
}
}

21
src/rules/symbol.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef __tree_sitter__sym__
#define __tree_sitter__sym__
#include "rule.h"
namespace tree_sitter {
namespace rules {
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;
};
}
}
#endif

View file

@ -6,8 +6,6 @@
#include "rules.h"
namespace tree_sitter {
class rules::Rule;
template<typename MappedType>
class TransitionMap {
public: