Move shared rule pointer factories into individual rule files

This commit is contained in:
Max Brunsfeld 2013-11-14 12:55:02 -08:00
parent 8a0a442a24
commit 040ec86000
19 changed files with 102 additions and 76 deletions

View file

@ -1,41 +0,0 @@
#include "rules.h"
namespace tree_sitter {
namespace rules {
rule_ptr blank() {
return rule_ptr(new Blank());
}
rule_ptr sym(const std::string &name) {
return rule_ptr(new Symbol(name));
}
rule_ptr character(char value) {
return rule_ptr(new Char(value));
}
rule_ptr str(const std::string &value) {
return rule_ptr(new String(value));
}
rule_ptr pattern(const std::string &value) {
return rule_ptr(new Pattern(value));
}
template <typename RuleClass>
rule_ptr build_binary_tree(const std::initializer_list<rule_ptr> &rules) {
rule_ptr result(nullptr);
for (auto it = rules.end() - 1; it >= rules.begin(); --it)
result = result.get() ? rule_ptr(new RuleClass(*it, result)) : *it;
return result;
}
rule_ptr seq(const std::initializer_list<rule_ptr> &rules) {
return build_binary_tree<Seq>(rules);
}
rule_ptr choice(const std::initializer_list<rule_ptr> &rules) {
return build_binary_tree<Choice>(rules);
}
}
}

View file

@ -10,18 +10,4 @@
#include "pattern.h"
#include "char.h"
namespace tree_sitter {
namespace rules {
rule_ptr blank();
rule_ptr sym(const std::string &name);
rule_ptr character(char value);
rule_ptr str(const std::string &value);
rule_ptr pattern(const std::string &value);
rule_ptr seq(const std::initializer_list<rule_ptr> &rules);
rule_ptr choice(const std::initializer_list<rule_ptr> &rules);
typedef std::shared_ptr<const Symbol> sym_ptr;
}
}
#endif

View file

@ -4,7 +4,10 @@
namespace tree_sitter {
namespace rules {
Pattern::Pattern(const std::string &string) : value(string) {};
Pattern::Pattern(const char *string) : value(string) {};
pattern_ptr pattern(const std::string &value) {
return std::make_shared<Pattern>(value);
}
TransitionMap<Rule> Pattern::transitions() const {
return tree_sitter::TransitionMap<Rule>();

View file

@ -7,7 +7,6 @@ namespace tree_sitter {
namespace rules {
class Pattern : public Rule {
public:
Pattern(const char *string);
Pattern(const std::string &string);
TransitionMap<Rule> transitions() const;
bool operator==(const Rule& other) const;
@ -15,6 +14,9 @@ namespace tree_sitter {
private:
const std::string value;
};
typedef std::shared_ptr<const Pattern> pattern_ptr;
pattern_ptr pattern(const std::string &value);
}
}

View file

@ -5,6 +5,10 @@ namespace tree_sitter {
namespace rules {
Blank::Blank() {}
blank_ptr blank() {
return std::make_shared<Blank>();
}
TransitionMap<Rule> Blank::transitions() const {
return TransitionMap<Rule>();
}

View file

@ -12,6 +12,9 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
std::string to_string() const;
};
typedef std::shared_ptr<const Blank> blank_ptr;
blank_ptr blank();
}
}

View file

@ -7,9 +7,13 @@ using namespace std;
namespace tree_sitter {
namespace rules {
Char::Char(char value) : value(value) {};
char_ptr character(char value) {
return std::make_shared<Char>(value);
}
TransitionMap<Rule> Char::transitions() const {
return TransitionMap<Rule>({ rule_ptr(new Char(value)) }, { rule_ptr(new Blank()) });
return TransitionMap<Rule>({ character(value) }, { blank() });
}
bool Char::operator==(const Rule &rule) const {

View file

@ -14,6 +14,9 @@ namespace tree_sitter {
private:
const char value;
};
typedef std::shared_ptr<const Char> char_ptr;
char_ptr character(char value);
}
}

View file

@ -5,10 +5,14 @@ namespace tree_sitter {
namespace rules {
Choice::Choice(rule_ptr left, rule_ptr right) : left(left), right(right) {};
rule_ptr choice(const std::initializer_list<rule_ptr> &rules) {
return build_binary_rule_tree<Choice>(rules);
}
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 choice({ left, right });
});
return result;
}

View file

@ -15,6 +15,8 @@ namespace tree_sitter {
const rule_ptr left;
const rule_ptr right;
};
rule_ptr choice(const std::initializer_list<rule_ptr> &rules);
}
}

View file

@ -15,7 +15,16 @@ namespace tree_sitter {
};
typedef std::shared_ptr<const Rule> rule_ptr;
std::ostream& operator<<(std::ostream& stream, const Rule &rule);
template <typename RuleClass>
rule_ptr build_binary_rule_tree(const std::initializer_list<rule_ptr> &rules) {
rule_ptr result(nullptr);
for (auto it = rules.end() - 1; it >= rules.begin(); --it)
result = result.get() ? std::make_shared<RuleClass>(*it, result) : *it;
return result;
}
}
}

View file

@ -6,12 +6,16 @@ namespace tree_sitter {
namespace rules {
Seq::Seq(rule_ptr left, rule_ptr right) : left(left), right(right) {};
rule_ptr seq(const std::initializer_list<rule_ptr> &rules) {
return build_binary_rule_tree<Seq>(rules);
}
TransitionMap<Rule> Seq::transitions() const {
return left->transitions().map<Rule>([&](rule_ptr left_rule) -> rule_ptr {
if (typeid(*left_rule) == typeid(Blank))
return right;
else
return rule_ptr(new Seq(left_rule, right));
return seq({ left_rule, right });
});
}

View file

@ -15,6 +15,8 @@ namespace tree_sitter {
const rule_ptr left;
const rule_ptr right;
};
rule_ptr seq(const std::initializer_list<rule_ptr> &rules);
}
}

View file

@ -7,10 +7,14 @@ namespace tree_sitter {
namespace rules {
String::String(std::string value) : value(value) {};
string_ptr str(const std::string &value) {
return std::make_shared<String>(value);
}
TransitionMap<Rule> String::transitions() const {
auto result = rule_ptr(new Char(value[0]));
rule_ptr result = character(value[0]);
for (int i = 1; i < value.length(); i++)
result = rule_ptr(new Seq(result, rule_ptr(new Char(value[i]))));
result = seq({ result, character(value[i]) });
return result->transitions();
}
@ -18,13 +22,10 @@ namespace tree_sitter {
const String *other = dynamic_cast<const String *>(&rule);
return (other != NULL) && (other->value == value);
}
String * String::copy() const {
return new String(value);
}
std::string String::to_string() const {
return std::string("(string '") + value + "')";
}
}
}

View file

@ -9,12 +9,14 @@ namespace tree_sitter {
public:
String(std::string value);
TransitionMap<Rule> transitions() const;
String * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
private:
const std::string value;
};
typedef std::shared_ptr<const String> string_ptr;
string_ptr str(const std::string &value);
}
}

View file

@ -5,10 +5,13 @@
namespace tree_sitter {
namespace rules {
Symbol::Symbol(const std::string &name) : name(name) {};
Symbol::Symbol(const char *name) : name(name) {};
sym_ptr sym(const std::string &name) {
return std::make_shared<Symbol>(name);
}
TransitionMap<Rule> Symbol::transitions() const {
return TransitionMap<Rule>({ rule_ptr(new Symbol(name)) }, { rule_ptr(new Blank()) });
return TransitionMap<Rule>({ sym(name) }, { blank() });
}
bool Symbol::operator==(const Rule &rule) const {

View file

@ -8,13 +8,14 @@ namespace tree_sitter {
class Symbol : public Rule {
public:
Symbol(const std::string &name);
Symbol(const char *name);
TransitionMap<Rule> transitions() const;
Symbol * copy() const;
bool operator==(const Rule& other) const;
std::string to_string() const;
const std::string name;
};
typedef std::shared_ptr<const Symbol> sym_ptr;
sym_ptr sym(const std::string &name);
}
}