Add copy method to rules

This commit is contained in:
Max Brunsfeld 2014-01-02 13:04:41 -08:00
parent 257b8d7b68
commit f5a9fb67a4
18 changed files with 47 additions and 4 deletions

View file

@ -14,11 +14,11 @@ namespace tree_sitter {
}
void visit(const Character *rule) {
value = transition_map<Rule, Rule>({{ std::make_shared<Character>(*rule), blank() }});
value = transition_map<Rule, Rule>({{ rule->copy(), blank() }});
}
void visit(const Symbol *rule) {
value = transition_map<Rule, Rule>({{ std::make_shared<Symbol>(*rule), blank() }});
value = transition_map<Rule, Rule>({{ rule->copy(), blank() }});
}
void visit(const Choice *rule) {

View file

@ -13,6 +13,10 @@ namespace tree_sitter {
return typeid(this).hash_code();
}
rule_ptr Blank::copy() const {
return std::make_shared<Blank>();
}
std::string Blank::to_string() const {
return "#<blank>";
}

View file

@ -10,8 +10,9 @@ namespace tree_sitter {
Blank();
bool operator==(const Rule& other) const;
std::string to_string() const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;
};
}

View file

@ -19,6 +19,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ hash<string>()(CharMatchToString(value));
}
rule_ptr Character::copy() const {
return std::make_shared<Character>(*this);
}
string Character::to_string() const {
return string("#<char ") + CharMatchToString(value) + ">";
}

View file

@ -14,6 +14,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;

View file

@ -16,6 +16,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ left->hash_code() ^ right->hash_code();
}
rule_ptr Choice::copy() const {
return std::make_shared<Choice>(*this);
}
string Choice::to_string() const {
return string("#<choice ") + left->to_string() + " " + right->to_string() + ">";
}

View file

@ -11,6 +11,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;

View file

@ -113,6 +113,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ hash<string>()(value);
}
rule_ptr Pattern::copy() const {
return std::make_shared<Pattern>(*this);
}
string Pattern::to_string() const {
return string("#<pattern '") + value + "'>";
}

View file

@ -12,6 +12,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;

View file

@ -16,6 +16,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ content->hash_code();
}
rule_ptr Repeat::copy() const {
return std::make_shared<Repeat>(*this);
}
string Repeat::to_string() const {
return string("#<repeat ") + content->to_string() + ">";
}

View file

@ -11,6 +11,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;

View file

@ -6,16 +6,19 @@
namespace tree_sitter {
namespace rules {
class Visitor;
class Rule;
typedef std::shared_ptr<const Rule> rule_ptr;
class Rule {
public:
virtual bool operator==(const Rule& other) const = 0;
virtual size_t hash_code() const = 0;
virtual rule_ptr copy() const = 0;
virtual std::string to_string() const = 0;
virtual void accept(Visitor &visitor) const = 0;
};
typedef std::shared_ptr<const Rule> rule_ptr;
std::ostream& operator<<(std::ostream& stream, const Rule &rule);
std::ostream& operator<<(std::ostream& stream, const rule_ptr &rule);
}

View file

@ -16,6 +16,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ left->hash_code() ^ right->hash_code();
}
rule_ptr Seq::copy() const {
return std::make_shared<Seq>(*this);
}
string Seq::to_string() const {
return string("#<seq ") + left->to_string() + " " + right->to_string() + ">";
}

View file

@ -11,6 +11,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;

View file

@ -17,6 +17,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ hash<string>()(value);
}
rule_ptr String::copy() const {
return std::make_shared<String>(*this);
}
string String::to_string() const {
return string("#<string '") + value + "'>";
}

View file

@ -11,6 +11,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;

View file

@ -17,6 +17,10 @@ namespace tree_sitter {
return typeid(this).hash_code() ^ hash<string>()(name);
}
rule_ptr Symbol::copy() const {
return std::make_shared<Symbol>(*this);
}
string Symbol::to_string() const {
return string("#<sym '") + name + "'>";
}

View file

@ -11,6 +11,7 @@ namespace tree_sitter {
bool operator==(const Rule& other) const;
size_t hash_code() const;
rule_ptr copy() const;
std::string to_string() const;
void accept(Visitor &visitor) const;