From f5a9fb67a42f597e336f9e8d18ee28aefd7022e6 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 2 Jan 2014 13:04:41 -0800 Subject: [PATCH] Add copy method to rules --- src/compiler/lr/transitions.cpp | 4 ++-- src/compiler/rules/blank.cpp | 4 ++++ src/compiler/rules/blank.h | 3 ++- src/compiler/rules/character.cpp | 4 ++++ src/compiler/rules/character.h | 1 + src/compiler/rules/choice.cpp | 4 ++++ src/compiler/rules/choice.h | 1 + src/compiler/rules/pattern.cpp | 4 ++++ src/compiler/rules/pattern.h | 1 + src/compiler/rules/repeat.cpp | 4 ++++ src/compiler/rules/repeat.h | 1 + src/compiler/rules/rule.h | 5 ++++- src/compiler/rules/seq.cpp | 4 ++++ src/compiler/rules/seq.h | 1 + src/compiler/rules/string.cpp | 4 ++++ src/compiler/rules/string.h | 1 + src/compiler/rules/symbol.cpp | 4 ++++ src/compiler/rules/symbol.h | 1 + 18 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/compiler/lr/transitions.cpp b/src/compiler/lr/transitions.cpp index c095fe13..0a41039c 100644 --- a/src/compiler/lr/transitions.cpp +++ b/src/compiler/lr/transitions.cpp @@ -14,11 +14,11 @@ namespace tree_sitter { } void visit(const Character *rule) { - value = transition_map({{ std::make_shared(*rule), blank() }}); + value = transition_map({{ rule->copy(), blank() }}); } void visit(const Symbol *rule) { - value = transition_map({{ std::make_shared(*rule), blank() }}); + value = transition_map({{ rule->copy(), blank() }}); } void visit(const Choice *rule) { diff --git a/src/compiler/rules/blank.cpp b/src/compiler/rules/blank.cpp index 80207631..cc72963c 100644 --- a/src/compiler/rules/blank.cpp +++ b/src/compiler/rules/blank.cpp @@ -13,6 +13,10 @@ namespace tree_sitter { return typeid(this).hash_code(); } + rule_ptr Blank::copy() const { + return std::make_shared(); + } + std::string Blank::to_string() const { return "#"; } diff --git a/src/compiler/rules/blank.h b/src/compiler/rules/blank.h index f58eb12c..b32e2c4b 100644 --- a/src/compiler/rules/blank.h +++ b/src/compiler/rules/blank.h @@ -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; }; } diff --git a/src/compiler/rules/character.cpp b/src/compiler/rules/character.cpp index f8a9bf87..545e76f8 100644 --- a/src/compiler/rules/character.cpp +++ b/src/compiler/rules/character.cpp @@ -19,6 +19,10 @@ namespace tree_sitter { return typeid(this).hash_code() ^ hash()(CharMatchToString(value)); } + rule_ptr Character::copy() const { + return std::make_shared(*this); + } + string Character::to_string() const { return string("#"; } diff --git a/src/compiler/rules/character.h b/src/compiler/rules/character.h index f8cd4343..deb8fc4f 100644 --- a/src/compiler/rules/character.h +++ b/src/compiler/rules/character.h @@ -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; diff --git a/src/compiler/rules/choice.cpp b/src/compiler/rules/choice.cpp index f631f3a4..16ed90ea 100644 --- a/src/compiler/rules/choice.cpp +++ b/src/compiler/rules/choice.cpp @@ -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(*this); + } + string Choice::to_string() const { return string("#to_string() + " " + right->to_string() + ">"; } diff --git a/src/compiler/rules/choice.h b/src/compiler/rules/choice.h index 4f28ef14..bca1f6d9 100644 --- a/src/compiler/rules/choice.h +++ b/src/compiler/rules/choice.h @@ -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; diff --git a/src/compiler/rules/pattern.cpp b/src/compiler/rules/pattern.cpp index 1a594799..3ae02a42 100644 --- a/src/compiler/rules/pattern.cpp +++ b/src/compiler/rules/pattern.cpp @@ -113,6 +113,10 @@ namespace tree_sitter { return typeid(this).hash_code() ^ hash()(value); } + rule_ptr Pattern::copy() const { + return std::make_shared(*this); + } + string Pattern::to_string() const { return string("#"; } diff --git a/src/compiler/rules/pattern.h b/src/compiler/rules/pattern.h index efec73d7..a19ec576 100644 --- a/src/compiler/rules/pattern.h +++ b/src/compiler/rules/pattern.h @@ -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; diff --git a/src/compiler/rules/repeat.cpp b/src/compiler/rules/repeat.cpp index 9fa62ccf..a8fb37f3 100644 --- a/src/compiler/rules/repeat.cpp +++ b/src/compiler/rules/repeat.cpp @@ -16,6 +16,10 @@ namespace tree_sitter { return typeid(this).hash_code() ^ content->hash_code(); } + rule_ptr Repeat::copy() const { + return std::make_shared(*this); + } + string Repeat::to_string() const { return string("#to_string() + ">"; } diff --git a/src/compiler/rules/repeat.h b/src/compiler/rules/repeat.h index 5ddb60a0..0031df31 100644 --- a/src/compiler/rules/repeat.h +++ b/src/compiler/rules/repeat.h @@ -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; diff --git a/src/compiler/rules/rule.h b/src/compiler/rules/rule.h index cb5d9d86..9e52b496 100644 --- a/src/compiler/rules/rule.h +++ b/src/compiler/rules/rule.h @@ -6,16 +6,19 @@ namespace tree_sitter { namespace rules { class Visitor; + class Rule; + + typedef std::shared_ptr 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 rule_ptr; std::ostream& operator<<(std::ostream& stream, const Rule &rule); std::ostream& operator<<(std::ostream& stream, const rule_ptr &rule); } diff --git a/src/compiler/rules/seq.cpp b/src/compiler/rules/seq.cpp index 852177b8..d8b20fa3 100644 --- a/src/compiler/rules/seq.cpp +++ b/src/compiler/rules/seq.cpp @@ -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(*this); + } + string Seq::to_string() const { return string("#to_string() + " " + right->to_string() + ">"; } diff --git a/src/compiler/rules/seq.h b/src/compiler/rules/seq.h index c7c64131..db77dda1 100644 --- a/src/compiler/rules/seq.h +++ b/src/compiler/rules/seq.h @@ -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; diff --git a/src/compiler/rules/string.cpp b/src/compiler/rules/string.cpp index 37a3f607..b29f466d 100644 --- a/src/compiler/rules/string.cpp +++ b/src/compiler/rules/string.cpp @@ -17,6 +17,10 @@ namespace tree_sitter { return typeid(this).hash_code() ^ hash()(value); } + rule_ptr String::copy() const { + return std::make_shared(*this); + } + string String::to_string() const { return string("#"; } diff --git a/src/compiler/rules/string.h b/src/compiler/rules/string.h index 30ab2cf8..982bc88e 100644 --- a/src/compiler/rules/string.h +++ b/src/compiler/rules/string.h @@ -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; diff --git a/src/compiler/rules/symbol.cpp b/src/compiler/rules/symbol.cpp index fbfad5d4..99ff25d1 100644 --- a/src/compiler/rules/symbol.cpp +++ b/src/compiler/rules/symbol.cpp @@ -17,6 +17,10 @@ namespace tree_sitter { return typeid(this).hash_code() ^ hash()(name); } + rule_ptr Symbol::copy() const { + return std::make_shared(*this); + } + string Symbol::to_string() const { return string("#"; } diff --git a/src/compiler/rules/symbol.h b/src/compiler/rules/symbol.h index 80f93cd9..d287e0ff 100644 --- a/src/compiler/rules/symbol.h +++ b/src/compiler/rules/symbol.h @@ -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;